breakout/settings.c

171 lines
6.7 KiB
C
Raw Normal View History

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-25 16:49:10 +01:00
#include <math.h>
2018-01-18 15:44:34 +01:00
2018-01-18 22:01:09 +01:00
#include "settings.h"
2018-01-25 16:49:10 +01:00
extern float XScale, YScale;
2018-01-20 20:06:50 +01:00
#define Slider_height 100
2018-01-24 14:53:42 +01:00
#define Scalar_width 20
#define Bar_width 400
#define round(x) ((int)((x) + .5))
#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-24 14:53:42 +01:00
SDL_Texture * Settings_Texture;
SDL_Texture * Settings_Ball_Texture;
SDL_Texture * Settings_Skins_Texture;
SDL_Texture * Return_Button_Texture;
2018-01-25 21:45:48 +01:00
SDL_Texture * Bar_Texture;
SDL_Texture * Scalar_Button_Texture0;
SDL_Texture * Scalar_Button_Texture1;
SDL_Texture * Circle_Texture;
2018-01-18 15:44:34 +01:00
SDL_Rect Settings_rect;
2018-01-20 20:06:50 +01:00
SDL_Rect Settings_Ball_rect;
SDL_Rect Settings_Skins_rect;
SDL_Rect Return_Button_rect;
SDL_Rect Circle_rect;
2018-02-01 13:22:27 +01:00
Slider BG;
2018-01-18 22:01:09 +01:00
Slider BS;
Slider BT;
2018-01-18 15:44:34 +01:00
2018-01-25 16:13:17 +01:00
Uint32 Mousestate;
2018-01-24 14:53:42 +01:00
bool Settings_IsInit = false;
bool IsLock = false;
2018-01-18 15:44:34 +01:00
void Settings_Initialize(SDL_Renderer * renderer, Scenery * scenery) {
Initialize_Slider(400, 300, Scalar_width, Bar_width, Slider_height, 0, 3, &BG, 0);
Initialize_Slider(400, 500, Scalar_width, Bar_width, Slider_height, 10, 100, &BS, scenery->ball.TargetRect.w);
Initialize_Slider(400, 700, Scalar_width, Bar_width, Slider_height, 0, 8, &BT, scenery->ball.TextureIndex);
// Initialize_Slider(400,300,Scalar_width,Bar_width,Slider_height,0,8,&BT,scenery->paddle.TextureIndex);
Settings_Texture = IMG_LoadTexture(renderer, "assets/images/settings_title.png");
Settings_rect = (SDL_Rect) {.x = 647, .y = 50, .w = 626, .h = 200 };
Settings_Ball_rect = (SDL_Rect) {.x = 1200, .y = 700, .w = 90, .h = 90 };
Settings_Skins_Texture = IMG_LoadTexture(renderer, "assets/images/skins_button.png");
Settings_Skins_rect = (SDL_Rect) {.x = 50, .y = 710, .w = 315, .h = 70 };
Return_Button_Texture = IMG_LoadTexture(renderer, "assets/images/return_button.png");
Return_Button_rect = (SDL_Rect) {.x = 200, .y = 200, .w = 75, .h = 75 };
Bar_Texture = IMG_LoadTexture(renderer, "assets/images/bar_texture.png");
Scalar_Button_Texture0 = IMG_LoadTexture(renderer, "assets/images/scalar_button_unpressed.png");
Scalar_Button_Texture1 = IMG_LoadTexture(renderer, "assets/images/scalar_button_pressed.png");
2018-01-25 21:45:48 +01:00
Circle_Texture = IMG_LoadTexture(renderer, "assets/images/circle.png");
Circle_rect = (SDL_Rect) {.x = 1250, .y = 500, .w = 75, .h = 75 };
Settings_IsInit = true;
} /* Settings_Initialize */
2018-01-18 15:44:34 +01:00
void Settings_Draw(SDL_Renderer * renderer, Scenery * scenery, int * backgroundcount) {
2018-01-24 14:53:42 +01:00
double x;
if (!(Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT))) {
IsLock = BG.IsLock = BS.IsLock = BT.IsLock = false;
} else {
IsLock = true;
2018-01-25 21:45:48 +01:00
}
BACKGROUND_Draw(renderer, *backgroundcount);
2018-02-01 13:22:27 +01:00
Draw_Slider(renderer, &BG);
2018-01-25 21:45:48 +01:00
Draw_Slider(renderer, &BS);
Draw_Slider(renderer, &BT);
2018-01-24 14:53:42 +01:00
SDL_RenderCopy(renderer, Settings_Texture, NULL, &Settings_rect);
SDL_RenderCopy(renderer, Return_Button_Texture, NULL, &Return_Button_rect);
2018-01-25 16:13:17 +01:00
SDL_RenderCopy(renderer, Settings_Skins_Texture, NULL, &Settings_Skins_rect);
SDL_RenderCopy(renderer, Circle_Texture, NULL, &Circle_rect);
2018-01-25 16:13:17 +01:00
BALL_DrawTexture(renderer, &Settings_Ball_rect, scenery->ball.TextureIndex);
Settings_Return();
2018-01-24 14:53:42 +01:00
mapping(&x, &BS);
2018-01-25 16:13:17 +01:00
scenery->ball.TargetRect.w = x;
scenery->ball.TargetRect.h = x;
scenery->ball.Size = ((double)x / 2.0f);
int posx = 1250, posy = 500;
Circle_rect.x = posx - 0.5 * Circle_rect.w;
Circle_rect.y = posy + 0.5 * (100 - Circle_rect.w);
Circle_rect.w = x;
Circle_rect.h = x;
2018-01-24 14:53:42 +01:00
mapping(&x, &BT);
x = round(x);
scenery->ball.TextureIndex = x;
mapping(&x, &BG);
x = round(x);
*backgroundcount = x;
} /* Settings_Draw */
2018-01-18 22:01:09 +01:00
void Settings_Deinitialize(){
2018-01-24 14:53:42 +01:00
if (Settings_IsInit) {
SDL_DestroyTexture(Settings_Texture);
SDL_DestroyTexture(Settings_Ball_Texture);
2018-01-25 21:45:48 +01:00
SDL_DestroyTexture(Bar_Texture);
SDL_DestroyTexture(Return_Button_Texture);
SDL_DestroyTexture(Settings_Skins_Texture);
SDL_DestroyTexture(Scalar_Button_Texture0);
SDL_DestroyTexture(Scalar_Button_Texture1);
SDL_DestroyTexture(Circle_Texture);
2018-01-24 14:53:42 +01:00
Settings_IsInit = false;
}
2018-01-18 22:01:09 +01:00
}
2018-01-24 14:53:42 +01:00
void Draw_Slider(SDL_Renderer * renderer, Slider * beta){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
2018-01-25 21:45:48 +01:00
SDL_RenderCopy(renderer, Bar_Texture, NULL, &beta->Bar_rect);
2018-01-24 14:53:42 +01:00
int x, y;
2018-01-25 16:13:17 +01:00
Mousestate = SDL_GetMouseState(&x, &y);
2018-01-25 16:49:10 +01:00
x = round((float)x / XScale);
y = round((float)y / YScale);
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))) || beta->IsLock) {
if (!IsLock) {
beta->IsLock = true;
IsLock = true;
}
if (beta->IsLock) {
SDL_RenderCopy(renderer, Scalar_Button_Texture1, NULL, &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;
}
2018-01-24 14:53:42 +01:00
} else {
SDL_RenderCopy(renderer, Scalar_Button_Texture0, NULL, &beta->Scalar_rect);
2018-01-24 14:53:42 +01:00
}
} else {
SDL_RenderCopy(renderer, Scalar_Button_Texture0, NULL, &beta->Scalar_rect);
2018-01-18 16:11:43 +01:00
}
2018-01-24 14:53:42 +01:00
} /* Draw_Slider */
2018-01-19 18:03:18 +01:00
2018-01-24 14:53:42 +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
}
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-24 14:53:42 +01:00
beta->Bar_rect = (SDL_Rect) {.x = x, .y = y, .w = bw, .h = h };
beta->max = max;
beta->min = min;
beta->Slider_value = (defaultvalue - min) / (max - min) * (bw - sw / 2) + x;
2018-01-25 16:13:17 +01:00
}
void Settings_Return(){
if (!IsLock) {
int x, y;
Mousestate = SDL_GetMouseState(&x, &y);
x = round((float)x / XScale);
y = round((float)y / YScale);
if ((distance(x, y, 237, 237) <= 37) && (Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT)))
GAME_ChangeState(MainMenu);
}
2018-01-19 18:03:18 +01:00
}