breakout/settings.c
2018-01-22 20:07:14 +01:00

100 lines
3.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include "settings.h"
#define Slider_height 100
#define Scalar_width 20
#define Bar_width 400
#define round(x) ((int) ((x) + .5))
SDL_Texture* Settings_Texture;
SDL_Texture* Settings_Ball_Texture;
SDL_Rect Settings_rect;
SDL_Rect Settings_Ball_rect;
Slider BV;
Slider BS;
Slider BT;
bool Settings_IsInit=false;
void Settings_Initialize (SDL_Renderer* renderer) {
Initialize_Slider(400,300,Scalar_width,Bar_width,Slider_height,1,2,&BV);
Initialize_Slider(400,500,Scalar_width,Bar_width,Slider_height,10.0f,20.0f,&BS);
Initialize_Slider(400,700,Scalar_width,Bar_width,Slider_height,0,1,&BT);
Settings_Texture = IMG_LoadTexture(renderer, "assets/images/settings_title.png");
Settings_rect = (SDL_Rect){.x = 800, .y = 180, .w=313, .h=100};
Settings_Ball_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png");
Settings_Ball_rect = (SDL_Rect){.x = 1200, .y = 300, .w=100, .h=100};
Settings_IsInit = true;
}
void Settings_Draw (SDL_Renderer* renderer,Scenery* scenery) {
double x;
SDL_RenderCopy(renderer, Settings_Texture, NULL, &Settings_rect);
SDL_RenderCopy(renderer, Settings_Ball_Texture, NULL, &Settings_Ball_rect);
Draw_Slider(renderer,&BV);
Draw_Slider(renderer,&BS);
Draw_Slider(renderer,&BT);
Draw_Ballstate(renderer,scenery);
mapping(&x,&BS);
scenery->ball.Speed=x;
mapping(&x,&BT);
x=round(x);
scenery->ball.TextureIndex=x;
}
void Settings_Deinitialize(){
if(Settings_IsInit){
SDL_DestroyTexture(Settings_Texture);
SDL_DestroyTexture(Settings_Ball_Texture);
Settings_IsInit=false;
}
}
void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderDrawRect(renderer,&beta->Bar_rect);
int x,y;
Uint32 Mousestate=SDL_GetMouseState(&x,&y);
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))){
SDL_RenderFillRect(renderer,&beta->Scalar_rect);
SDL_RenderDrawRect(renderer,&beta->Scalar_rect);
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);
}
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;
}
else{
beta->Scalar_rect.x=x-(beta->Scalar_rect.w/2);
beta->Slider_value=x;
}
}
else{
SDL_RenderDrawRect(renderer,&beta->Scalar_rect);
}
}
void Draw_Ballstate(SDL_Renderer* renderer,Scenery* scenery){
BALL_Draw(renderer, &(scenery->ball));
}
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;
}
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta){
beta->Scalar_rect = (SDL_Rect){.x=x,.y=y,.w=sw,.h=h};
beta->Bar_rect = (SDL_Rect){.x=x,.y=y,.w=bw,.h=h};
beta->max=max;
beta->min=min;
beta->Slider_value=x+beta->Scalar_rect.w/2;
}