Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
b69736027d | |||
c577dcc2d7 | |||
45221cb72b | |||
d7e5845e4e | |||
3b2a0c8741 | |||
9e1738c05d | |||
affda45ac5 | |||
9088047c68 | |||
fc805e5c31 | |||
ca643ec62f | |||
2ebac3c167 |
1
.gitignore
vendored
@ -8,6 +8,7 @@ sdl2-config
|
||||
*.o
|
||||
*.psd
|
||||
*.exe
|
||||
*.json
|
||||
!bhi.exe
|
||||
.tags*
|
||||
*.txt
|
||||
|
Before Width: | Height: | Size: 155 KiB After Width: | Height: | Size: 2.5 MiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 47 KiB |
BIN
bin/assets/images/gameover.png
Normal file
After Width: | Height: | Size: 140 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 8.8 KiB After Width: | Height: | Size: 35 KiB |
BIN
bin/assets/images/numbers.png
Normal file
After Width: | Height: | Size: 219 KiB |
BIN
bin/assets/images/paused.png
Normal file
After Width: | Height: | Size: 208 KiB |
Before Width: | Height: | Size: 9.3 KiB After Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 8.5 KiB After Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 8.1 KiB After Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 7.1 KiB After Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 9.1 KiB After Width: | Height: | Size: 35 KiB |
BIN
bin/assets/images/text.png
Normal file
After Width: | Height: | Size: 184 KiB |
BIN
bin/assets/images/yourscore.png
Normal file
After Width: | Height: | Size: 222 KiB |
266
breakout.c
@ -9,23 +9,43 @@
|
||||
|
||||
#include "breakout.h"
|
||||
#include "vector.h"
|
||||
#include "gamestate.h"
|
||||
#include "gameover.h"
|
||||
#include "main.h"
|
||||
|
||||
extern float XScale, YScale;
|
||||
extern int width, height;
|
||||
|
||||
#define BALL_TexturePath "assets/images/ball.png"
|
||||
#define PADDLE_TexturePath "assets/images/paddle.png"
|
||||
#define BLOCK_TexturePath "assets/images/spritesheet.png"
|
||||
#define BALL_TexturePath "assets/images/ball.png"
|
||||
#define PADDLE_TexturePath "assets/images/paddle.png"
|
||||
#define BLOCK_TexturePath "assets/images/spritesheet.png"
|
||||
#define BRAEKOUT_CountdownTexturePath "assets/images/text.png"
|
||||
#define BRAEKOUT_PausedTexturePath "assets/images/paused.png"
|
||||
#define BALL_MinSpeed 8.0f
|
||||
#define BALL_MaxSpeed 25.0f
|
||||
#define BALL_AccelerationTime 10000
|
||||
#define PADDLE_MaxSize 300
|
||||
#define PADDLE_MinSize 50
|
||||
#define PADDLE_AccelerationTime 18000
|
||||
#define BREAKOUT_LiveHUDSize 35
|
||||
#define BREAKOUT_LiveHUDMargin 8
|
||||
|
||||
#ifndef __nullptr__
|
||||
#define Nullptr(type) (type *)0
|
||||
#endif // __nullptr__
|
||||
|
||||
float PADDLE_SmoothFactor = 0.1f;
|
||||
int BLOCK_TextureCount = 24;
|
||||
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
|
||||
int BALL_TextureCount = 9;
|
||||
int BREAKOUT_CountdownTextureCount = 4;
|
||||
int PADDLE_TextureCount = 9;
|
||||
SDL_Texture * BALL_Texture;
|
||||
SDL_Texture * BREAKOUT_CountdownTexture;
|
||||
SDL_Texture * PADDLE_Texture;
|
||||
SDL_Texture * BLOCK_Texture;
|
||||
SDL_Texture * BREAKOUT_PausedTexture;
|
||||
SDL_Rect * BALL_SourceRects;
|
||||
SDL_Rect * BREAKOUT_CountdownSourceRects;
|
||||
SDL_Rect * PADDLE_SourceRects;
|
||||
SDL_Rect * BLOCK_SourceRects;
|
||||
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
||||
@ -34,20 +54,41 @@ bool BALL_IsInit = false;
|
||||
bool PADDLE_IsInit = false;
|
||||
bool BLOCK_IsInit = false;
|
||||
|
||||
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
||||
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer){
|
||||
if (!BREAKOUT_IsInit) {
|
||||
printf("Initializing Game...\n");
|
||||
srand(time(NULL));
|
||||
BREAKOUT_BoxWidth = width;
|
||||
BREAKOUT_BoxHeight = height;
|
||||
BALL_Initialize(renderer);
|
||||
PADDLE_Initialize(renderer);
|
||||
BLOCK_Initialize(renderer);
|
||||
BREAKOUT_CountdownTexture = IMG_LoadTexture(renderer, BRAEKOUT_CountdownTexturePath);
|
||||
if (!BREAKOUT_CountdownTexture) printf("Countdown texture failed to load!\n");
|
||||
BREAKOUT_PausedTexture = IMG_LoadTexture(renderer, BRAEKOUT_PausedTexturePath);
|
||||
if (!BREAKOUT_PausedTexture) printf("Paused texture failed to load!\n");
|
||||
BREAKOUT_CountdownTextureCount = 4;
|
||||
BREAKOUT_CountdownSourceRects = (SDL_Rect *)malloc(BREAKOUT_CountdownTextureCount * sizeof(SDL_Rect));
|
||||
if (!BREAKOUT_CountdownSourceRects) printf("FATAL! Memory allocation failed!\n");
|
||||
BREAKOUT_CountdownSourceRects[0] = (SDL_Rect) {.x = 1, .y = 668, .w = 1000, .h = 732 };
|
||||
BREAKOUT_CountdownSourceRects[1] = (SDL_Rect) {.x = 1, .y = 1, .w = 242, .h = 665 };
|
||||
BREAKOUT_CountdownSourceRects[2] = (SDL_Rect) {.x = 245, .y = 1, .w = 443, .h = 665 };
|
||||
BREAKOUT_CountdownSourceRects[3] = (SDL_Rect) {.x = 690, .y = 1, .w = 443, .h = 665 };
|
||||
printf("Game initialized!\n");
|
||||
BREAKOUT_IsInit = true;
|
||||
} else printf("Game is already initialized!\n");
|
||||
} /* BREAKOUT_INITIALIZE */
|
||||
|
||||
void BREAKOUT_TogglePause(Scenery * scenery){
|
||||
(scenery->IsPaused) = !(scenery->IsPaused);
|
||||
printf("Game was %s!\n", ( (scenery->IsPaused) ? "paused" : "unpaused"));
|
||||
}
|
||||
|
||||
void BREAKOUT_KeyPressed(Scenery * scenery, SDL_KeyboardEvent * b){
|
||||
if ((b->keysym).scancode == SDL_SCANCODE_ESCAPE) {
|
||||
printf("Escape was pressed ingame! Toggle Pause...\n");
|
||||
BREAKOUT_TogglePause(scenery);
|
||||
}
|
||||
}
|
||||
|
||||
Scenery BREAKOUT_CreateDefault(){
|
||||
Scenery scenery;
|
||||
|
||||
@ -57,9 +98,12 @@ Scenery BREAKOUT_CreateDefault(){
|
||||
scenery.ball = BALL_CreateDefault();
|
||||
scenery.paddle = PADDLE_CreateDefault();
|
||||
scenery.blocks = malloc(scenery.BlockCount * sizeof(Block));
|
||||
scenery.Frames = 0;
|
||||
scenery.Score = 0;
|
||||
if (!(scenery.blocks)) printf("FATAL! Memory allocation failed!\n");
|
||||
scenery.IsPaused = false;
|
||||
scenery.Lives = 3;
|
||||
scenery.DestroyedBlocks = 0;
|
||||
int index;
|
||||
for (int y = 0; y < 9; y++) {
|
||||
index = 15 * y;
|
||||
@ -72,33 +116,49 @@ Scenery BREAKOUT_CreateDefault(){
|
||||
return scenery;
|
||||
} /* BREAKOUT_CreateDefault */
|
||||
|
||||
// This Function is obsolete! Do not use it!
|
||||
void BREAKOUT_ChangeSize(int width, int height){
|
||||
BREAKOUT_BoxWidth = width;
|
||||
BREAKOUT_BoxHeight = height;
|
||||
void BREAKOUT_IncreaseScoreBy(Scenery * scenery, int scoreInc){
|
||||
(scenery->Score) += scoreInc;
|
||||
}
|
||||
|
||||
void TEXTURE_RenderCenteredSpriteSheet(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, float Scale){
|
||||
SDL_Rect target;
|
||||
|
||||
target.w = (int)roundf(((float)(srcRect->w)) * Scale);
|
||||
target.h = (int)roundf(((float)(srcRect->h)) * Scale);
|
||||
target.x = ((width - (target.w)) / 2);
|
||||
target.y = ((height - (target.h)) / 2);
|
||||
SDL_RenderCopy(renderer, texture, srcRect, &target);
|
||||
}
|
||||
|
||||
void TEXTURE_RenderCentered(SDL_Renderer * renderer, SDL_Texture * texture, float Scale){
|
||||
int w, h;
|
||||
SDL_Rect target;
|
||||
|
||||
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
|
||||
target.w = (int)roundf(((float)w) * Scale);
|
||||
target.h = (int)roundf(((float)h) * Scale);
|
||||
target.x = ((width - (target.w)) / 2);
|
||||
target.y = ((height - (target.h)) / 2);
|
||||
SDL_RenderCopy(renderer, texture, NULL, &target);
|
||||
}
|
||||
|
||||
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate){
|
||||
if (scenery->IsPaused) {
|
||||
// Render "Paused"
|
||||
return;
|
||||
}
|
||||
if ((scenery->StartCountdown)-- > 0) {
|
||||
// Render "Countdown"
|
||||
return;
|
||||
}
|
||||
if (scenery->IsPaused) return;
|
||||
if ((scenery->StartCountdown)-- > 0) return;
|
||||
(scenery->Frames)++;
|
||||
if (scenery->IsGameOver) {
|
||||
BALL_ResetPosition(&(scenery->ball));
|
||||
PADDLE_ResetPosition(&(scenery->paddle));
|
||||
scenery->StartCountdown = 240;
|
||||
scenery->IsGameOver = false;
|
||||
scenery->Frames = 0;
|
||||
if (--(scenery->Lives) <= 0)
|
||||
printf("Game over, no lives left!\n");
|
||||
GAME_ChangeState(GameOver);
|
||||
else
|
||||
printf("Oh oh, only %d lives left!\n", scenery->Lives);
|
||||
return;
|
||||
}
|
||||
PADDLE_Update(&(scenery->paddle), keystate); // Update paddle before ball because paddle is not static!
|
||||
PADDLE_Update(&(scenery->paddle), scenery, keystate); // Update paddle before ball because paddle is not static!
|
||||
BALL_Update(&(scenery->ball), scenery);
|
||||
for (int i = 0; i < (scenery->BlockCount); i++) {
|
||||
BLOCK_Update((scenery->blocks) + i);
|
||||
@ -111,16 +171,40 @@ void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer){
|
||||
}
|
||||
BALL_Draw(renderer, &(scenery->ball));
|
||||
PADDLE_Draw(renderer, &(scenery->paddle));
|
||||
SCORE_DrawHUD(renderer, scenery);
|
||||
BREAKOUT_DrawLivesHUD(renderer, scenery);
|
||||
if (scenery->IsPaused) {
|
||||
TEXTURE_RenderCentered(renderer, BREAKOUT_PausedTexture, 0.5f);
|
||||
} else if ((scenery->StartCountdown) > 0) { // ! Render Z-Layer !
|
||||
TEXTURE_RenderCenteredSpriteSheet(renderer, BREAKOUT_CountdownTexture, (BREAKOUT_CountdownSourceRects + (((scenery->StartCountdown) - 1) / 60)), 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void BREAKOUT_DrawLivesHUD(SDL_Renderer * renderer, Scenery * scenery){
|
||||
SDL_Rect tmpRect;
|
||||
|
||||
tmpRect.y = BREAKOUT_LiveHUDMargin;
|
||||
tmpRect.w = BREAKOUT_LiveHUDSize;
|
||||
tmpRect.h = BREAKOUT_LiveHUDSize;
|
||||
tmpRect.x = width - BREAKOUT_LiveHUDMargin;
|
||||
|
||||
for (int i = 0; i < (scenery->Lives); i++) {
|
||||
tmpRect.x -= (BREAKOUT_LiveHUDSize + BREAKOUT_LiveHUDMargin);
|
||||
BALL_DrawTexture(renderer, &tmpRect, (scenery->ball).TextureIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void BREAKOUT_DEINITIALIZE(){
|
||||
if (BREAKOUT_IsInit) {
|
||||
printf("De-initializing Game...\n");
|
||||
SDL_DestroyTexture(BREAKOUT_CountdownTexture);
|
||||
SDL_DestroyTexture(BREAKOUT_PausedTexture);
|
||||
free(PADDLE_MoveLeftKeys);
|
||||
free(PADDLE_MoveRightKeys);
|
||||
free(BALL_SourceRects);
|
||||
free(PADDLE_SourceRects);
|
||||
free(BLOCK_SourceRects);
|
||||
free(BREAKOUT_CountdownSourceRects);
|
||||
BALL_Deinitialize();
|
||||
PADDLE_Deinitialize();
|
||||
BLOCK_Deinitialize();
|
||||
@ -143,33 +227,49 @@ void BALL_Initialize(SDL_Renderer * renderer){
|
||||
printf("Initializing Ball...\n");
|
||||
BALL_Texture = IMG_LoadTexture(renderer, BALL_TexturePath);
|
||||
if (!BALL_Texture) printf("Ball texture failed to load!\n");
|
||||
BALL_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect));
|
||||
BALL_TextureCount = 9;
|
||||
BALL_SourceRects = (SDL_Rect *)malloc(BALL_TextureCount * sizeof(SDL_Rect));
|
||||
if (!BALL_SourceRects) printf("FATAL! Memory allocation failed!\n");
|
||||
BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[0] = (SDL_Rect) {.x = 1029, .y = 1029, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[1] = (SDL_Rect) {.x = 1, .y = 1, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[2] = (SDL_Rect) {.x = 1, .y = 515, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[3] = (SDL_Rect) {.x = 1, .y = 1029, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[4] = (SDL_Rect) {.x = 515, .y = 1, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[5] = (SDL_Rect) {.x = 1029, .y = 1, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[6] = (SDL_Rect) {.x = 515, .y = 515, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[7] = (SDL_Rect) {.x = 515, .y = 1029, .w = 512, .h = 512 };
|
||||
BALL_SourceRects[8] = (SDL_Rect) {.x = 1029, .y = 515, .w = 512, .h = 512 };
|
||||
printf("Ball initialized!\n");
|
||||
BALL_IsInit = true;
|
||||
} else printf("Ball is already initialized!\n");
|
||||
}
|
||||
} /* BALL_Initialize */
|
||||
|
||||
Ball BALL_CreateDefault(){
|
||||
double rotation = (double)(rand() % 360);
|
||||
|
||||
return (Ball) {
|
||||
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 130 },
|
||||
.Momentum = (Vector) {.x = 0.0f, .y = 15.0f },
|
||||
.TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 130, .w = 30, .h = 30 },
|
||||
.Location = (Vector) {.x = (width / 2) - 15, .y = height - 132 },
|
||||
.Momentum = (Vector) {.x = 0.0f, .y = BALL_MinSpeed },
|
||||
.TargetRect = (SDL_Rect) {.x = width / 2 - 15, .y = height - 130, .w = 30, .h = 30 },
|
||||
.Size = 15.0f,
|
||||
.Rotation = rotation,
|
||||
.RotationValue = 2,
|
||||
.Rotation = 0,
|
||||
.RotationValue = 9,
|
||||
.TextureIndex = 0,
|
||||
.Speed = 15.0f
|
||||
}; // Objekt für die Eigenschaften des Balls
|
||||
}
|
||||
|
||||
void BALL_ResetPosition(Ball * obj){
|
||||
(obj->Location).x = BREAKOUT_BoxWidth / 2 - 15;
|
||||
(obj->Location).y = BREAKOUT_BoxHeight - 130;
|
||||
(obj->Location).x = width / 2 - 15;
|
||||
(obj->Location).y = height - 130;
|
||||
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
|
||||
(obj->Momentum) = VECTOR_GetScaledDirectionalUnitVector(0.0f, (obj->Speed));
|
||||
}
|
||||
|
||||
void BALL_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index){
|
||||
if (index > BALL_TextureCount || index < 0) {
|
||||
printf("Ball with unkown texture index %d aus [0,...,%d]\n", index, (BALL_TextureCount - 1));
|
||||
return;
|
||||
}
|
||||
SDL_RenderCopy(renderer, BALL_Texture, BALL_SourceRects + index, dstRect);
|
||||
}
|
||||
|
||||
void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
|
||||
@ -257,7 +357,7 @@ SDL_Point BALL_GetCenter(Ball * obj){
|
||||
void BALL_CollideWithBorders(Ball * obj){
|
||||
if ((obj->Location).y < 0.0f)
|
||||
(obj->Momentum).y = -(obj->Momentum).y;
|
||||
if ((obj->Location).x < 0.0f || (obj->Location).x > BREAKOUT_BoxWidth - (2 * (obj->Size)))
|
||||
if ((obj->Location).x < 0.0f || (obj->Location).x > width - (2 * (obj->Size)))
|
||||
(obj->Momentum).x = -(obj->Momentum).x;
|
||||
}
|
||||
|
||||
@ -266,7 +366,7 @@ void BALL_MoveAwayFromBoundaries(Ball * obj){
|
||||
((obj->Location).y)++;
|
||||
while (((obj->Location).x) < 0)
|
||||
((obj->Location).x)++;
|
||||
while ((((obj->Location).x) + ((obj->Size) * 2)) > BREAKOUT_BoxWidth)
|
||||
while ((((obj->Location).x) + ((obj->Size) * 2)) > width)
|
||||
((obj->Location).x)--;
|
||||
}
|
||||
|
||||
@ -277,7 +377,7 @@ bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle){
|
||||
BALL_CollideWithRect(obj, &(paddle->TargetRect));
|
||||
else
|
||||
BALL_SteerMomentum(obj, paddle); // Sets it to unit vector!
|
||||
// Following assumes that the paddle position was udated before the ball was updated
|
||||
// Following assumes that the paddle position was udated before the ball was updated
|
||||
while (RECT_Collide(&(obj->TargetRect), &(paddle->TargetRect))) { // Move away from rect in small steps
|
||||
(obj->Location) = VECTOR_Add((obj->Location), (obj->Momentum));
|
||||
BALL_MoveAwayFromBoundaries(obj);
|
||||
@ -289,7 +389,16 @@ bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle){
|
||||
return false;
|
||||
} /* BALL_CollideWithPaddle */
|
||||
|
||||
void BALL_AdaptSpeedGradient(Ball * obj, int FrameCount){
|
||||
if (FrameCount > BALL_AccelerationTime)
|
||||
obj->Speed = BALL_MaxSpeed;
|
||||
else
|
||||
obj->Speed = BALL_MinSpeed + (((double)FrameCount / (double)BALL_AccelerationTime) * (BALL_MaxSpeed - BALL_MinSpeed));
|
||||
}
|
||||
|
||||
void BALL_Update(Ball * obj, Scenery * scenery){
|
||||
BALL_AdaptSpeedGradient(obj, (scenery->Frames));
|
||||
(obj->Momentum) = VECTOR_ChangeScaleTo((obj->Momentum), (obj->Speed));
|
||||
Block * blocks = (scenery->blocks);
|
||||
Paddle * paddle = &(scenery->paddle);
|
||||
int BlockCount = scenery->BlockCount;
|
||||
@ -307,14 +416,19 @@ void BALL_Update(Ball * obj, Scenery * scenery){
|
||||
oldLocation = obj->Location;
|
||||
if (BALL_CollideWithRect(obj, &(blocks[i].TargetRect))) {
|
||||
BLOCK_DealDamage(blocks + i, 1);
|
||||
if (blocks[i].HP <= 0) {
|
||||
(scenery->DestroyedBlocks)++;
|
||||
BREAKOUT_IncreaseScoreBy(scenery, (int)round((((scenery->ball).Speed) * ((scenery->ball).Speed) / 5.0f)));
|
||||
}
|
||||
(obj->Location) = VECTOR_Add(oldLocation, (obj->Momentum));
|
||||
BALL_MoveAwayFromBoundaries(obj);
|
||||
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
|
||||
}
|
||||
}
|
||||
if ((obj->Location).y > BREAKOUT_BoxHeight) // Collide with box boundaries
|
||||
if ((obj->Location).y > height) { // Collide with box boundaries
|
||||
scenery->IsGameOver = true;
|
||||
else BALL_CollideWithBorders(obj);
|
||||
printf("Ball called game_over!\n");
|
||||
} else BALL_CollideWithBorders(obj);
|
||||
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
|
||||
} /* BALL_Update */
|
||||
|
||||
@ -354,27 +468,31 @@ void PADDLE_Initialize(SDL_Renderer * renderer){
|
||||
} /* PADDLE_Initialize */
|
||||
|
||||
Paddle PADDLE_CreateDefault(){
|
||||
int defaultpaddlewidth = 300;
|
||||
|
||||
return (Paddle) {
|
||||
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 },
|
||||
.TargetRect = (SDL_Rect) {.x = (width - PADDLE_MaxSize) / 2, .y = height - 100, .w = PADDLE_MaxSize, .h = 30 },
|
||||
.TextureIndex = 0,
|
||||
.Speed = 10,
|
||||
.SteeringAngle = 40.0f,
|
||||
.Mode = KeyboardControl
|
||||
.Mode = MouseControl
|
||||
}; // Objekt für die Eigenschaften des Balls
|
||||
}
|
||||
|
||||
void PADDLE_ResetPosition(Paddle * obj){
|
||||
(obj->TargetRect).x = (BREAKOUT_BoxWidth - ((obj->TargetRect).w)) / 2;
|
||||
(obj->TargetRect).y = BREAKOUT_BoxHeight - 100;
|
||||
(obj->TargetRect).x = (width - ((obj->TargetRect).w)) / 2;
|
||||
(obj->TargetRect).y = height - 100;
|
||||
}
|
||||
|
||||
void PADDLE_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index){
|
||||
if (index > PADDLE_TextureCount || index < 0) {
|
||||
printf("Paddle with unkown texture index %d aus [0,...,%d]\n", index, (PADDLE_TextureCount - 1));
|
||||
return;
|
||||
}
|
||||
SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + index, dstRect);
|
||||
}
|
||||
|
||||
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){
|
||||
// printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
|
||||
SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect));
|
||||
// SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
// SDL_RenderDrawRect(renderer, &(obj->TargetRect));
|
||||
}
|
||||
|
||||
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray){
|
||||
@ -398,37 +516,45 @@ void DOUBLE_Constrain(double * variable, double min, double max){
|
||||
*variable = min;
|
||||
}
|
||||
|
||||
void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
|
||||
bool leftKeyPressed = false, rightKeyPressed = false;
|
||||
int paddleXMid = (obj->TargetRect).x + ((obj->TargetRect).w / 2);
|
||||
int mouseX;
|
||||
void PADDLE_MoveSmooth(Paddle * obj){
|
||||
int mouseX, paddleXMid, halfPaddle = ((obj->TargetRect).w / 2);
|
||||
|
||||
SDL_GetMouseState(&mouseX, NULL);
|
||||
mouseX = (int)roundf((float)mouseX / XScale);
|
||||
paddleXMid = halfPaddle + (obj->TargetRect.x); // Current State
|
||||
(obj->TargetRect).x = paddleXMid - (int)roundf((float)(paddleXMid - mouseX) * PADDLE_SmoothFactor) - halfPaddle;
|
||||
}
|
||||
|
||||
void PADDLE_AdaptSpeedGradient(Paddle * obj, int FrameCount){
|
||||
if (FrameCount > PADDLE_AccelerationTime)
|
||||
return;
|
||||
(obj->TargetRect).w = PADDLE_MaxSize - (((double)FrameCount / (double)PADDLE_AccelerationTime) * (PADDLE_MaxSize - PADDLE_MinSize));
|
||||
|
||||
}
|
||||
|
||||
void PADDLE_Update(Paddle * obj, Scenery * scenery, const Uint8 * keystate){
|
||||
PADDLE_AdaptSpeedGradient(obj, (scenery->Frames));
|
||||
bool leftKeyPressed, rightKeyPressed;
|
||||
|
||||
switch (obj->Mode) {
|
||||
case MouseControl:
|
||||
SDL_GetMouseState(&mouseX, NULL);
|
||||
mouseX = (int)roundf((float)mouseX / XScale);
|
||||
if (abs(mouseX - paddleXMid) > (obj->Speed)) {
|
||||
if (mouseX > paddleXMid)
|
||||
rightKeyPressed = true;
|
||||
else
|
||||
leftKeyPressed = true;
|
||||
}
|
||||
PADDLE_MoveSmooth(obj);
|
||||
break;
|
||||
case KeyboardControl:
|
||||
leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys);
|
||||
rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
|
||||
|
||||
if (leftKeyPressed && (!rightKeyPressed)) {
|
||||
((obj->TargetRect).x) -= (obj->Speed);
|
||||
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
||||
((obj->TargetRect).x) += (obj->Speed);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
printf("Unknown Paddle Control Mode: %d!\n", obj->Mode);
|
||||
break;
|
||||
}
|
||||
|
||||
if (leftKeyPressed && (!rightKeyPressed)) {
|
||||
((obj->TargetRect).x) -= (obj->Speed);
|
||||
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
||||
((obj->TargetRect).x) += (obj->Speed);
|
||||
}
|
||||
INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
|
||||
} /* switch */
|
||||
INT_Constrain(&((obj->TargetRect).x), 0, (width - ((obj->TargetRect).w)));
|
||||
} /* PADDLE_Update */
|
||||
|
||||
void PADDLE_DestroyObject(Paddle * obj){
|
||||
@ -487,6 +613,14 @@ Block BLOCK_CreateDefault() {
|
||||
}; // Objekt für die Eigenschaften des Balls
|
||||
}
|
||||
|
||||
void BLOCK_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index){
|
||||
if (index > BLOCK_TextureCount || index < 0) {
|
||||
printf("Block with unkown texture index %d aus [0,...,%d]\n", index, (BLOCK_TextureCount - 1));
|
||||
return;
|
||||
}
|
||||
SDL_RenderCopy(renderer, BLOCK_Texture, BLOCK_SourceRects + index, dstRect);
|
||||
}
|
||||
|
||||
void BLOCK_Draw(SDL_Renderer * renderer, Block * obj){
|
||||
if ((obj->HP) > 0) {
|
||||
// printf("Block drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).y);
|
||||
|
19
breakout.h
@ -39,22 +39,28 @@ typedef struct sceneryStruct {
|
||||
Ball ball;
|
||||
Paddle paddle;
|
||||
Block * blocks;
|
||||
int BlockCount, Lives, StartCountdown;
|
||||
int BlockCount, Lives, StartCountdown, Frames, Score, DestroyedBlocks;
|
||||
bool IsPaused, IsGameOver;
|
||||
} Scenery; // Objekt für die Objekte und Eigenschaften einer Szenerie
|
||||
// End Structs
|
||||
|
||||
// Prototypes
|
||||
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height);
|
||||
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer);
|
||||
void BREAKOUT_TogglePause(Scenery * scenery);
|
||||
void BREAKOUT_KeyPressed(Scenery * scenery, SDL_KeyboardEvent * b);
|
||||
Scenery BREAKOUT_CreateDefault();
|
||||
void BREAKOUT_ChangeSize(int width, int height);
|
||||
void BREAKOUT_IncreaseScoreBy(Scenery * scenery, int scoreInc);
|
||||
void TEXTURE_RenderCenteredSpriteSheet(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, float Scale);
|
||||
void TEXTURE_RenderCentered(SDL_Renderer * renderer, SDL_Texture * texture, float Scale);
|
||||
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate);
|
||||
void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer);
|
||||
void BREAKOUT_DrawLivesHUD(SDL_Renderer * renderer, Scenery * scenery);
|
||||
void BREAKOUT_DEINITIALIZE();
|
||||
void BREAKOUT_DestroyObject(Scenery * scenery);
|
||||
void BALL_Initialize(SDL_Renderer * renderer);
|
||||
Ball BALL_CreateDefault();
|
||||
void BALL_ResetPosition(Ball * obj);
|
||||
void BALL_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index);
|
||||
void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
|
||||
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect);
|
||||
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2);
|
||||
@ -64,21 +70,26 @@ SDL_Point BALL_GetCenter(Ball * obj);
|
||||
void BALL_CollideWithBorders(Ball * obj);
|
||||
void BALL_MoveAwayFromBoundaries(Ball * obj);
|
||||
bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle);
|
||||
void BALL_AdaptSpeedGradient(Ball * obj, int FrameCount);
|
||||
void BALL_Update(Ball * obj, Scenery * scenery);
|
||||
void BALL_DestroyObject(Ball * obj);
|
||||
void BALL_Deinitialize();
|
||||
void PADDLE_Initialize(SDL_Renderer * renderer);
|
||||
Paddle PADDLE_CreateDefault();
|
||||
void PADDLE_ResetPosition(Paddle * obj);
|
||||
void PADDLE_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index);
|
||||
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj);
|
||||
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray);
|
||||
void INT_Constrain(int * variable, int min, int max);
|
||||
void DOUBLE_Constrain(double * variable, double min, double max);
|
||||
void PADDLE_Update(Paddle * obj, const Uint8 * keystate);
|
||||
void PADDLE_MoveSmooth(Paddle * obj);
|
||||
void PADDLE_AdaptSpeedGradient(Paddle * obj, int FrameCount);
|
||||
void PADDLE_Update(Paddle * obj, Scenery * scenery, const Uint8 * keystate);
|
||||
void PADDLE_DestroyObject(Paddle * obj);
|
||||
void PADDLE_Deinitialize();
|
||||
void BLOCK_Initialize(SDL_Renderer * renderer);
|
||||
Block BLOCK_CreateDefault() ;
|
||||
void BLOCK_DrawTexture(SDL_Renderer * renderer, SDL_Rect * dstRect, int index);
|
||||
void BLOCK_Draw(SDL_Renderer * renderer, Block * obj);
|
||||
void BLOCK_DealDamage(Block * obj, int dmg);
|
||||
void BLOCK_Update(Block * obj);
|
||||
|
155
gameover.c
Normal file
@ -0,0 +1,155 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
#include "gameover.h"
|
||||
#include "gamestate.h"
|
||||
#include "main.h"
|
||||
|
||||
#define GAMEOVER_TexturePath "assets/images/gameover.png"
|
||||
#define GAMEOVER_NumbersTexturePath "assets/images/numbers.png"
|
||||
#define GAMEOVER_ScoreTexturePath "assets/images/yourscore.png"
|
||||
#define GAMEOVER_HUDScale 16.0f
|
||||
#define GAMEOVER_Scale 4.0f
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
#include "breakout.h"
|
||||
#include "vector.h"
|
||||
#include "background.h"
|
||||
|
||||
int GAMEOVER_HUDMargin = 5;
|
||||
SDL_Texture * GAMEOVER_Texture;
|
||||
SDL_Texture * GAMEOVER_Numbers;
|
||||
SDL_Texture * GAMEOVER_ScoreTexture;
|
||||
SDL_Rect * GAMEOVER_NumberRects;
|
||||
SDL_Rect GAMEOVER_TargetRect;
|
||||
SDL_Rect GAMEOVER_ScoreTargetRect;
|
||||
SDL_Rect GAMEOVER_HUDScoreTargetRect;
|
||||
int * GAMEOVER_Digits;
|
||||
bool GAMEOVER_IsInit = false;
|
||||
|
||||
void GAMEOVER_Initialize(SDL_Renderer * renderer){
|
||||
if (!GAMEOVER_IsInit) {
|
||||
printf("Initializing Gameover...\n");
|
||||
GAMEOVER_Texture = IMG_LoadTexture(renderer, GAMEOVER_TexturePath);
|
||||
if (!GAMEOVER_Texture) printf("Gameover Texture couldn't be loaded!\n");
|
||||
GAMEOVER_Numbers = IMG_LoadTexture(renderer, GAMEOVER_NumbersTexturePath);
|
||||
if (!GAMEOVER_Numbers) printf("Gameover Numbers couldn't be loaded!\n");
|
||||
GAMEOVER_ScoreTexture = IMG_LoadTexture(renderer, GAMEOVER_ScoreTexturePath);
|
||||
if (!GAMEOVER_ScoreTexture) printf("Gameover Score Texture couldn't be loaded!\n");
|
||||
int w, h;
|
||||
SDL_QueryTexture(GAMEOVER_Texture, NULL, NULL, &w, &h);
|
||||
w /= 2;
|
||||
h /= 2;
|
||||
GAMEOVER_TargetRect.x = ((1920 - w) / 2);
|
||||
GAMEOVER_TargetRect.y = 50;
|
||||
GAMEOVER_TargetRect.w = w;
|
||||
GAMEOVER_TargetRect.h = h;
|
||||
GAMEOVER_NumberRects = calloc(10, sizeof(SDL_Rect));
|
||||
if (!GAMEOVER_NumberRects) printf("FATAL: Memory Allocation Failed!\n");
|
||||
GAMEOVER_NumberRects[0] = (SDL_Rect) {.x = 446, .y = 668, .w = 442, .h = 665 };
|
||||
GAMEOVER_NumberRects[1] = (SDL_Rect) {.x = 1299, .y = 1335, .w = 242, .h = 665 };
|
||||
GAMEOVER_NumberRects[2] = (SDL_Rect) {.x = 1, .y = 1, .w = 443, .h = 665 };
|
||||
GAMEOVER_NumberRects[3] = (SDL_Rect) {.x = 1, .y = 668, .w = 443, .h = 665 };
|
||||
GAMEOVER_NumberRects[4] = (SDL_Rect) {.x = 1, .y = 1335, .w = 443, .h = 665 };
|
||||
GAMEOVER_NumberRects[5] = (SDL_Rect) {.x = 446, .y = 1, .w = 443, .h = 665 };
|
||||
GAMEOVER_NumberRects[6] = (SDL_Rect) {.x = 891, .y = 1, .w = 443, .h = 665 };
|
||||
GAMEOVER_NumberRects[7] = (SDL_Rect) {.x = 890, .y = 1335, .w = 407, .h = 665 };
|
||||
GAMEOVER_NumberRects[8] = (SDL_Rect) {.x = 446, .y = 1335, .w = 442, .h = 665 };
|
||||
GAMEOVER_NumberRects[9] = (SDL_Rect) {.x = 890, .y = 668, .w = 442, .h = 665 };
|
||||
GAMEOVER_ScoreTargetRect.y = 450;
|
||||
GAMEOVER_ScoreTargetRect.h = 183;
|
||||
GAMEOVER_ScoreTargetRect.w = 1000;
|
||||
GAMEOVER_HUDScoreTargetRect = (SDL_Rect) {.x = GAMEOVER_HUDMargin, .y = GAMEOVER_HUDMargin, .w = 250, .h = 46 };
|
||||
GAMEOVER_Digits = malloc(25 * sizeof(int));
|
||||
printf("Gameover initialized!\n");
|
||||
GAMEOVER_IsInit = true;
|
||||
} else
|
||||
printf("Gameover already initialized!\n");
|
||||
} /* GAMEOVER_Initialize */
|
||||
|
||||
void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){
|
||||
int i, count;
|
||||
|
||||
SDL_RenderCopy(renderer, GAMEOVER_Texture, NULL, &GAMEOVER_TargetRect);
|
||||
GAMEOVER_GetDigits((scenery->Score), &count);
|
||||
int totalWidth = GAMEOVER_ScoreTargetRect.w;
|
||||
for (i = (count - 1); i >= 0; i--) {
|
||||
totalWidth += (int)roundf((float)GAMEOVER_NumberRects[i].w / GAMEOVER_Scale);
|
||||
}
|
||||
GAMEOVER_ScoreTargetRect.x = ((1920 - totalWidth) / 2);
|
||||
SDL_RenderCopy(renderer, GAMEOVER_ScoreTexture, NULL, &GAMEOVER_ScoreTargetRect);
|
||||
int xOffset = GAMEOVER_ScoreTargetRect.x + GAMEOVER_ScoreTargetRect.w;
|
||||
SDL_Rect target;
|
||||
target.y = 450;
|
||||
for (i = (count - 1); i >= 0; i--) {
|
||||
target.x = xOffset;
|
||||
target.h = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].h) / GAMEOVER_Scale);
|
||||
target.w = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].w) / GAMEOVER_Scale);
|
||||
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
|
||||
xOffset += target.w;
|
||||
}
|
||||
} /* GAMEOVER_Draw */
|
||||
|
||||
void GAMEOVER_GetDigits(int input, int * digitCount){
|
||||
int score = input;
|
||||
int count = 0;
|
||||
|
||||
while (score != 0) {
|
||||
GAMEOVER_Digits[(count++)] = (score % 10);
|
||||
score /= 10;
|
||||
}
|
||||
if (count == 0) {
|
||||
count = 1;
|
||||
GAMEOVER_Digits[0] = 0;
|
||||
}
|
||||
*digitCount = count;
|
||||
}
|
||||
|
||||
void SCORE_DrawHUD(SDL_Renderer * renderer, Scenery * scenery){
|
||||
int i, count;
|
||||
|
||||
GAMEOVER_GetDigits((scenery->Score), &count);
|
||||
int totalWidth = GAMEOVER_HUDScoreTargetRect.w;
|
||||
for (i = (count - 1); i >= 0; i--) {
|
||||
totalWidth += (int)roundf((float)GAMEOVER_NumberRects[i].w / GAMEOVER_HUDScale);
|
||||
}
|
||||
GAMEOVER_HUDScoreTargetRect.x = GAMEOVER_HUDMargin;
|
||||
SDL_RenderCopy(renderer, GAMEOVER_ScoreTexture, NULL, &GAMEOVER_HUDScoreTargetRect);
|
||||
int xOffset = GAMEOVER_HUDScoreTargetRect.x + GAMEOVER_HUDScoreTargetRect.w;
|
||||
SDL_Rect target;
|
||||
target.y = GAMEOVER_HUDMargin;
|
||||
for (i = (count - 1); i >= 0; i--) {
|
||||
target.x = xOffset;
|
||||
target.h = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].h) / GAMEOVER_HUDScale);
|
||||
target.w = (int)roundf((float)(GAMEOVER_NumberRects[GAMEOVER_Digits[i]].w) / GAMEOVER_HUDScale);
|
||||
SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target);
|
||||
xOffset += target.w;
|
||||
}
|
||||
} /* SCORE_DrawHUD */
|
||||
|
||||
void GAMEOVER_Deinitialize(){
|
||||
if (GAMEOVER_IsInit) {
|
||||
printf("De-initializing Gameover...\n");
|
||||
free(GAMEOVER_Digits);
|
||||
SDL_DestroyTexture(GAMEOVER_Texture);
|
||||
SDL_DestroyTexture(GAMEOVER_ScoreTexture);
|
||||
SDL_DestroyTexture(GAMEOVER_Numbers);
|
||||
printf("Gameover de-initialized!\n");
|
||||
GAMEOVER_IsInit = false;
|
||||
} else
|
||||
printf("Gameover already de-initialized!\n");
|
||||
}
|
25
gameover.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef __gameover_h__
|
||||
#define __gameover_h__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
#include "gameover.h"
|
||||
#include "gamestate.h"
|
||||
#include "main.h"
|
||||
|
||||
// Prototypes
|
||||
void GAMEOVER_Initialize(SDL_Renderer * renderer);
|
||||
void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery);
|
||||
void GAMEOVER_GetDigits(int input, int * digitCount);
|
||||
void SCORE_DrawHUD(SDL_Renderer * renderer, Scenery * scenery);
|
||||
void GAMEOVER_Deinitialize();
|
||||
// End Prototypes
|
||||
|
||||
#endif // __gameover_h__
|
@ -1,6 +1,6 @@
|
||||
#ifndef __gamestate_h__
|
||||
#define __gamestate_h__
|
||||
|
||||
typedef enum gameStateEnum { MainMenu = 1, Game = 2, LevelSelect = 3, SkinSelect = 4, Settings = 5, Highscores = 6 } GameState;
|
||||
typedef enum gameStateEnum { MainMenu = 1, Game = 2, LevelSelect = 3, SkinSelect = 4, Settings = 5, Highscores = 6 , GameOver = 7 } GameState;
|
||||
|
||||
#endif
|
||||
|
@ -67,7 +67,7 @@ void HIGHSCORES_Deinitialize(){
|
||||
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer){
|
||||
char * buffer = calloc(100, sizeof(char));
|
||||
int count = 0;
|
||||
char format[20] = "| %-58s | %-10s |";
|
||||
char format[20] = "| %-54s | %-14s |";
|
||||
SDL_Rect Message_rect;
|
||||
SDL_Surface * HIGHSCORES_TableSurface = SDL_CreateRGBSurface(0, 1920, 1080, 32, 0, 0, 0, 0);
|
||||
|
||||
|
19
main.c
@ -12,6 +12,7 @@
|
||||
#include "startmenu.h"
|
||||
#include "gamestate.h"
|
||||
#include "highscores.h"
|
||||
#include "gameover.h"
|
||||
#include "settings.h"
|
||||
#include "background.h"
|
||||
|
||||
@ -55,12 +56,14 @@ int main(int argc, char * args[]){
|
||||
HIGHSCORES_Draw(renderer);
|
||||
break;
|
||||
case Settings:
|
||||
Settings_Draw(renderer);
|
||||
Settings_Draw(renderer, &scenery);
|
||||
case GameOver:
|
||||
GAMEOVER_Draw(renderer, &scenery);
|
||||
break;
|
||||
default:
|
||||
printf("Unknow state was updated: %d\n", gameState);
|
||||
break;
|
||||
}
|
||||
} /* switch */
|
||||
SDL_RenderPresent(renderer);
|
||||
fps_frames++;
|
||||
if (fps_lasttime < SDL_GetTicks() - 1000) {
|
||||
@ -134,6 +137,14 @@ void keyPress(SDL_KeyboardEvent b){ // Debug prop
|
||||
printf("Key pressed: ID is %d\n", b.keysym.scancode);
|
||||
if (b.keysym.scancode == SDL_SCANCODE_F11 || b.keysym.scancode == SDL_SCANCODE_5) {
|
||||
toggleFullscreen();
|
||||
} else {
|
||||
switch (gameState) {
|
||||
case Game:
|
||||
BREAKOUT_KeyPressed(&scenery, &b);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -179,17 +190,19 @@ void INITIALIZE() {
|
||||
printf("Window was created!\n");
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
printf("Renderer was created!\n");
|
||||
BREAKOUT_INITIALIZE(renderer, width, height);
|
||||
BREAKOUT_INITIALIZE(renderer);
|
||||
scenery = BREAKOUT_CreateDefault();
|
||||
Load_Textures(renderer);
|
||||
HIGHSCORES_Initialize();
|
||||
BACKGROUND_Initialize(renderer, width, height);
|
||||
Settings_Initialize(renderer);
|
||||
GAMEOVER_Initialize(renderer);
|
||||
printf("Initializing finished!\n");
|
||||
} /* INITIALIZE */
|
||||
|
||||
void QUIT(){
|
||||
printf("De-initializing started...\n");
|
||||
GAMEOVER_Deinitialize();
|
||||
BACKGROUND_Deinitialize();
|
||||
Settings_Deinitialize();
|
||||
HIGHSCORES_Deinitialize();
|
||||
|
3
main.h
@ -15,6 +15,9 @@
|
||||
#include "startmenu.h"
|
||||
#include "gamestate.h"
|
||||
#include "highscores.h"
|
||||
#include "gameover.h"
|
||||
#include "settings.h"
|
||||
#include "background.h"
|
||||
|
||||
#ifndef __nullptr__
|
||||
#define Nullptr(type) (type *)0
|
||||
|
41
settings.c
@ -9,6 +9,7 @@
|
||||
#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;
|
||||
@ -23,8 +24,8 @@ 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,1,2,&BS);
|
||||
Initialize_Slider(400,700,Scalar_width,Bar_width,Slider_height,1,2,&BT);
|
||||
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");
|
||||
@ -32,14 +33,19 @@ void Settings_Initialize (SDL_Renderer* renderer) {
|
||||
Settings_IsInit = true;
|
||||
}
|
||||
|
||||
void Settings_Draw (SDL_Renderer* renderer) {
|
||||
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(){
|
||||
@ -55,19 +61,19 @@ void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
|
||||
SDL_RenderDrawRect(renderer,&beta->Bar_rect);
|
||||
int x,y;
|
||||
Uint32 Mousestate=SDL_GetMouseState(&x,&y);
|
||||
if(y<=((beta->y)+(beta->h))&&y>=(beta->y)&&x<=(beta->bw+beta->x)&&x>=(beta->x)&&(Mousestate & SDL_BUTTON(SDL_BUTTON_LEFT))){
|
||||
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->x+beta->bw-(beta->sw)/2)){
|
||||
beta->Scalar_rect.x=(beta->x+beta->bw-(beta->sw));
|
||||
beta->Slider_value=(beta->x+beta->bw-(beta->sw)/2);
|
||||
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->x+(beta->sw)/2){
|
||||
beta->Scalar_rect.x=beta->x;
|
||||
beta->Slider_value=beta->x+(beta->sw)/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->sw/2);
|
||||
beta->Scalar_rect.x=x-(beta->Scalar_rect.w/2);
|
||||
beta->Slider_value=x;
|
||||
}
|
||||
}
|
||||
@ -76,19 +82,18 @@ void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
|
||||
}
|
||||
}
|
||||
|
||||
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->bw-(beta->sw)))*(beta->Slider_value-beta->x-beta->sw/2)+beta->min;
|
||||
*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->x=x;
|
||||
beta->y=y;
|
||||
beta->sw=sw;
|
||||
beta->bw=bw;
|
||||
beta->h=h;
|
||||
beta->max=max;
|
||||
beta->min=min;
|
||||
beta->Slider_value=x+sw/2;
|
||||
beta->Slider_value=x+beta->Scalar_rect.w/2;
|
||||
}
|
||||
|
15
settings.h
@ -7,24 +7,27 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <stdbool.h>
|
||||
#include "breakout.h"
|
||||
|
||||
typedef struct sliderstruct {
|
||||
SDL_Rect Bar_rect;
|
||||
SDL_Rect Scalar_rect;
|
||||
int x,y,bw,sw,h;
|
||||
double Slider_value,min,max;
|
||||
} Slider;
|
||||
|
||||
void Settings_Initialize (SDL_Renderer* renderer);
|
||||
|
||||
void Settings_Draw (SDL_Renderer* renderer);
|
||||
|
||||
void Settings_Deinitialize();
|
||||
void Settings_Draw (SDL_Renderer* renderer,Scenery* scenery);
|
||||
|
||||
void Draw_Slider(SDL_Renderer* renderer,Slider* beta);
|
||||
|
||||
void mapping(double *x,Slider* beta);
|
||||
void Draw_Ballstate(SDL_Renderer* renderer,Scenery* scenery);
|
||||
|
||||
void Settings_Initialize (SDL_Renderer* renderer);
|
||||
|
||||
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta);
|
||||
|
||||
void Settings_Deinitialize();
|
||||
|
||||
void mapping(double *x,Slider* beta);
|
||||
|
||||
#endif
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "gamestate.h"
|
||||
#include "main.h"
|
||||
|
||||
extern float XScale, YScale;
|
||||
|
||||
SDL_Texture * TITLE_Texture;
|
||||
SDL_Texture * PLAYBUTTON_Texture;
|
||||
@ -23,7 +24,11 @@ SDL_Rect HIGHSCORESBUTTON_Rect;
|
||||
SDL_Rect QUITBUTTON_Rect;
|
||||
|
||||
int clickInRect(SDL_MouseButtonEvent b, SDL_Rect * area_rect) {
|
||||
return (((b.x) >= (area_rect->x)) && ((b.x) <= ((area_rect->x) + (area_rect->w))) && ((b.y) >= (area_rect->y)) && ((b.y) <= ((area_rect->y) + (area_rect->h))));
|
||||
int clickx, clicky;
|
||||
|
||||
clickx = (int)roundf((float)b.x / XScale);
|
||||
clicky = (int)roundf((float)b.y / YScale);
|
||||
return ((clickx >= (area_rect->x)) && (clickx <= ((area_rect->x) + (area_rect->w))) && (clicky >= (area_rect->y)) && (clicky <= ((area_rect->y) + (area_rect->h))));
|
||||
}
|
||||
|
||||
void Load_Textures(SDL_Renderer * renderer) {
|
||||
|