2018-01-18 15:44:34 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <SDL2/SDL_image.h>
|
2018-01-18 15:59:24 +01:00
|
|
|
#include <stdbool.h>
|
2018-01-18 15:44:34 +01:00
|
|
|
|
2018-01-18 22:01:09 +01:00
|
|
|
#include "settings.h"
|
|
|
|
|
2018-01-20 20:06:50 +01:00
|
|
|
#define Slider_height 100
|
2018-01-19 18:03:18 +01:00
|
|
|
#define Scalar_width 20
|
|
|
|
#define Bar_width 400
|
2018-01-22 20:07:14 +01:00
|
|
|
#define round(x) ((int) ((x) + .5))
|
2018-01-25 14:51:33 +01:00
|
|
|
#define distance(x1,y1,x2,y2) ((int)(sqrt(pow(x2-x1,2)+pow(y2-y1,2))))
|
2018-01-19 18:03:18 +01:00
|
|
|
|
2018-01-20 20:06:50 +01:00
|
|
|
SDL_Texture* Settings_Texture;
|
2018-01-25 14:51:33 +01:00
|
|
|
SDL_Texture* Settings_Return_Button_Texture;
|
2018-01-18 15:44:34 +01:00
|
|
|
|
|
|
|
SDL_Rect Settings_rect;
|
2018-01-25 14:51:33 +01:00
|
|
|
SDL_Rect Settings_Return_Button_rect;
|
2018-01-18 22:01:09 +01:00
|
|
|
Slider BV;
|
|
|
|
Slider BS;
|
|
|
|
Slider BT;
|
2018-01-25 14:51:33 +01:00
|
|
|
Uint32 Mousestate;
|
2018-01-18 15:59:24 +01:00
|
|
|
bool Settings_IsInit=false;
|
2018-01-18 15:44:34 +01:00
|
|
|
|
2018-01-25 14:51:33 +01:00
|
|
|
void Settings_Initialize (SDL_Renderer* renderer,Scenery* scenery) {
|
|
|
|
Initialize_Slider(400,300,Scalar_width,Bar_width,Slider_height,1,2,&BV,1.5);
|
|
|
|
Initialize_Slider(400,500,Scalar_width,Bar_width,Slider_height,30,100,&BS,scenery->ball.TargetRect.w);
|
|
|
|
Initialize_Slider(400,700,Scalar_width,Bar_width,Slider_height,0,8,&BT,scenery->ball.TextureIndex);
|
2018-01-20 20:06:50 +01:00
|
|
|
Settings_Texture = IMG_LoadTexture(renderer, "assets/images/settings_title.png");
|
2018-01-18 15:44:34 +01:00
|
|
|
Settings_rect = (SDL_Rect){.x = 800, .y = 180, .w=313, .h=100};
|
2018-01-25 14:51:33 +01:00
|
|
|
Settings_Return_Button_Texture = IMG_LoadTexture(renderer, "assets/images/return_button.png");
|
|
|
|
Settings_Return_Button_rect = (SDL_Rect){.x = 200, .y = 200, .w=75, .h=75};
|
2018-01-18 15:59:24 +01:00
|
|
|
Settings_IsInit = true;
|
2018-01-18 15:44:34 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 20:07:14 +01:00
|
|
|
void Settings_Draw (SDL_Renderer* renderer,Scenery* scenery) {
|
2018-01-20 20:06:50 +01:00
|
|
|
double x;
|
2018-01-25 14:51:33 +01:00
|
|
|
scenery->ball.TargetRect.x=900;
|
|
|
|
scenery->ball.TargetRect.y=700;
|
2018-01-20 20:06:50 +01:00
|
|
|
SDL_RenderCopy(renderer, Settings_Texture, NULL, &Settings_rect);
|
2018-01-25 14:51:33 +01:00
|
|
|
SDL_RenderCopy(renderer, Settings_Return_Button_Texture, NULL, &Settings_Return_Button_rect);
|
2018-01-18 22:01:09 +01:00
|
|
|
Draw_Slider(renderer,&BV);
|
|
|
|
Draw_Slider(renderer,&BS);
|
|
|
|
Draw_Slider(renderer,&BT);
|
2018-01-22 20:07:14 +01:00
|
|
|
Draw_Ballstate(renderer,scenery);
|
2018-01-20 20:06:50 +01:00
|
|
|
mapping(&x,&BT);
|
2018-01-25 14:51:33 +01:00
|
|
|
scenery->ball.TextureIndex=round(x);
|
|
|
|
mapping(&x,&BS);
|
|
|
|
scenery->ball.TargetRect.w=x;
|
|
|
|
scenery->ball.TargetRect.h=x;
|
|
|
|
Settings_Return();
|
2018-01-18 22:01:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Settings_Deinitialize(){
|
2018-01-25 15:12:53 +01:00
|
|
|
if(Settings_IsInit){
|
|
|
|
SDL_DestroyTexture(Settings_Texture);
|
|
|
|
SDL_DestroyTexture(Settings_Ball_Texture);
|
|
|
|
Settings_IsInit=false;
|
|
|
|
}
|
2018-01-18 22:01:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
|
2018-01-19 18:03:18 +01:00
|
|
|
SDL_SetRenderDrawColor(renderer,255,255,255,255);
|
2018-01-18 22:01:09 +01:00
|
|
|
SDL_RenderDrawRect(renderer,&beta->Bar_rect);
|
2018-01-18 15:44:34 +01:00
|
|
|
int x,y;
|
2018-01-25 14:51:33 +01:00
|
|
|
Mousestate=SDL_GetMouseState(&x,&y);
|
2018-01-22 20:07:14 +01:00
|
|
|
if(y<=((beta->Bar_rect.y)+(beta->Bar_rect.h))&&y>=(beta->Bar_rect.y)&&x<=(beta->Bar_rect.w+beta->Bar_rect.x)&&x>=(beta->Bar_rect.x)&&(Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT))){
|
2018-01-18 22:01:09 +01:00
|
|
|
SDL_RenderFillRect(renderer,&beta->Scalar_rect);
|
|
|
|
SDL_RenderDrawRect(renderer,&beta->Scalar_rect);
|
2018-01-22 20:07:14 +01:00
|
|
|
if(x>(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w)/2)){
|
|
|
|
beta->Scalar_rect.x=(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w));
|
|
|
|
beta->Slider_value=(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w)/2);
|
2018-01-18 15:44:34 +01:00
|
|
|
}
|
2018-01-22 20:07:14 +01:00
|
|
|
else if(x<beta->Bar_rect.x+(beta->Scalar_rect.w)/2){
|
|
|
|
beta->Scalar_rect.x=beta->Bar_rect.x;
|
|
|
|
beta->Slider_value=beta->Bar_rect.x+(beta->Scalar_rect.w)/2;
|
2018-01-18 16:11:43 +01:00
|
|
|
}
|
2018-01-25 15:12:53 +01:00
|
|
|
else{
|
|
|
|
beta->Scalar_rect.x=x-(beta->Scalar_rect.w/2);
|
|
|
|
beta->Slider_value=x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
SDL_RenderDrawRect(renderer,&beta->Scalar_rect);
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 18:03:18 +01:00
|
|
|
|
2018-01-25 15:12:53 +01:00
|
|
|
void Draw_Ballstate(SDL_Renderer* renderer,Scenery* scenery){
|
2018-01-22 20:07:14 +01:00
|
|
|
BALL_Draw(renderer, &(scenery->ball));
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:12:53 +01:00
|
|
|
void mapping(double *x,Slider* beta){
|
|
|
|
*x=((beta->max-beta->min)/(beta->Bar_rect.w-(beta->Scalar_rect.w)))*(beta->Slider_value-beta->Bar_rect.x-beta->Scalar_rect.w/2)+beta->min;
|
2018-01-19 18:03:18 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 14:51:33 +01:00
|
|
|
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta,double defaultvalue){
|
|
|
|
beta->Scalar_rect = (SDL_Rect){.x=(defaultvalue-min)/(max-min)*(bw-sw/2)+x,.y=y,.w=sw,.h=h};
|
2018-01-19 18:03:18 +01:00
|
|
|
beta->Bar_rect = (SDL_Rect){.x=x,.y=y,.w=bw,.h=h};
|
2018-01-20 20:06:50 +01:00
|
|
|
beta->max=max;
|
|
|
|
beta->min=min;
|
2018-01-25 14:51:33 +01:00
|
|
|
beta->Slider_value=(defaultvalue-min)/(max-min)*(bw-sw/2)+x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings_Return(Scenery* scenery){
|
|
|
|
int x,y;
|
|
|
|
Mousestate=SDL_GetMouseState(&x,&y);
|
|
|
|
if((distance(x,y,237,237)<=37)&&(Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT)))
|
|
|
|
GAME_ChangeState(MainMenu);
|
2018-01-19 18:03:18 +01:00
|
|
|
}
|