Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
487b668ebf | |||
09ab6dd027 | |||
c6608cd3e0 | |||
21012dbc70 | |||
2a3c056909 | |||
a369855a27 | |||
b33a6478e6 | |||
681b6dcd2b | |||
34fb88a86a | |||
8a449ac7cd | |||
df535bb7d0 | |||
a2af143d23 | |||
1c227c0dce | |||
6aefc442d7 | |||
c577dcc2d7 | |||
45221cb72b | |||
d7e5845e4e | |||
3b2a0c8741 | |||
9e1738c05d | |||
affda45ac5 | |||
9088047c68 | |||
fc805e5c31 | |||
ca643ec62f | |||
2ebac3c167 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,6 +8,7 @@ sdl2-config
|
|||||||
*.o
|
*.o
|
||||||
*.psd
|
*.psd
|
||||||
*.exe
|
*.exe
|
||||||
|
*.json
|
||||||
!bhi.exe
|
!bhi.exe
|
||||||
.tags*
|
.tags*
|
||||||
*.txt
|
*.txt
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 155 KiB After Width: | Height: | Size: 2.5 MiB |
BIN
bin/assets/images/return_button.png
Normal file
BIN
bin/assets/images/return_button.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
BIN
bin/assets/images/text.png
Normal file
BIN
bin/assets/images/text.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 184 KiB |
118
breakout.c
118
breakout.c
@ -12,20 +12,30 @@
|
|||||||
|
|
||||||
extern float XScale, YScale;
|
extern float XScale, YScale;
|
||||||
|
|
||||||
#define BALL_TexturePath "assets/images/ball.png"
|
#define BALL_TexturePath "assets/images/ball.png"
|
||||||
#define PADDLE_TexturePath "assets/images/paddle.png"
|
#define PADDLE_TexturePath "assets/images/paddle.png"
|
||||||
#define BLOCK_TexturePath "assets/images/spritesheet.png"
|
#define BLOCK_TexturePath "assets/images/spritesheet.png"
|
||||||
|
#define BRAEKOUT_CountdownTexturePath "assets/images/text.png"
|
||||||
|
#define BALL_MinSpeed 8.0f
|
||||||
|
#define BALL_MaxSpeed 50.0f
|
||||||
|
#define BALL_AccelerationTime 18000
|
||||||
|
|
||||||
#ifndef __nullptr__
|
#ifndef __nullptr__
|
||||||
#define Nullptr(type) (type *)0
|
#define Nullptr(type) (type *)0
|
||||||
#endif // __nullptr__
|
#endif // __nullptr__
|
||||||
|
|
||||||
|
float PADDLE_SmoothFactor = 0.1f;
|
||||||
int BLOCK_TextureCount = 24;
|
int BLOCK_TextureCount = 24;
|
||||||
|
int BALL_TextureCount = 9;
|
||||||
|
int GAME_CountdownTextureCount = 4;
|
||||||
|
int PADDLE_TextureCount = 9;
|
||||||
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
|
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
|
||||||
SDL_Texture * BALL_Texture;
|
SDL_Texture * BALL_Texture;
|
||||||
|
SDL_Texture * GAME_CountdownTexture;
|
||||||
SDL_Texture * PADDLE_Texture;
|
SDL_Texture * PADDLE_Texture;
|
||||||
SDL_Texture * BLOCK_Texture;
|
SDL_Texture * BLOCK_Texture;
|
||||||
SDL_Rect * BALL_SourceRects;
|
SDL_Rect * BALL_SourceRects;
|
||||||
|
SDL_Rect * GAME_CountdownSourceRects;
|
||||||
SDL_Rect * PADDLE_SourceRects;
|
SDL_Rect * PADDLE_SourceRects;
|
||||||
SDL_Rect * BLOCK_SourceRects;
|
SDL_Rect * BLOCK_SourceRects;
|
||||||
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
||||||
@ -43,6 +53,15 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
|||||||
BALL_Initialize(renderer);
|
BALL_Initialize(renderer);
|
||||||
PADDLE_Initialize(renderer);
|
PADDLE_Initialize(renderer);
|
||||||
BLOCK_Initialize(renderer);
|
BLOCK_Initialize(renderer);
|
||||||
|
GAME_CountdownTexture = IMG_LoadTexture(renderer, BRAEKOUT_CountdownTexturePath);
|
||||||
|
if (!GAME_CountdownTexture) printf("Coutndown texture failed to load!\n");
|
||||||
|
GAME_CountdownTextureCount = 4;
|
||||||
|
GAME_CountdownSourceRects = (SDL_Rect *)malloc(GAME_CountdownTextureCount * sizeof(SDL_Rect));
|
||||||
|
if (!GAME_CountdownSourceRects) printf("FATAL! Memory allocation failed!\n");
|
||||||
|
GAME_CountdownSourceRects[0] = (SDL_Rect) {.x = 1, .y = 668, .w = 1000, .h = 732 };
|
||||||
|
GAME_CountdownSourceRects[1] = (SDL_Rect) {.x = 1, .y = 1, .w = 242, .h = 665 };
|
||||||
|
GAME_CountdownSourceRects[2] = (SDL_Rect) {.x = 245, .y = 1, .w = 443, .h = 665 };
|
||||||
|
GAME_CountdownSourceRects[3] = (SDL_Rect) {.x = 690, .y = 1, .w = 443, .h = 665 };
|
||||||
printf("Game initialized!\n");
|
printf("Game initialized!\n");
|
||||||
BREAKOUT_IsInit = true;
|
BREAKOUT_IsInit = true;
|
||||||
} else printf("Game is already initialized!\n");
|
} else printf("Game is already initialized!\n");
|
||||||
@ -57,9 +76,12 @@ Scenery BREAKOUT_CreateDefault(){
|
|||||||
scenery.ball = BALL_CreateDefault();
|
scenery.ball = BALL_CreateDefault();
|
||||||
scenery.paddle = PADDLE_CreateDefault();
|
scenery.paddle = PADDLE_CreateDefault();
|
||||||
scenery.blocks = malloc(scenery.BlockCount * sizeof(Block));
|
scenery.blocks = malloc(scenery.BlockCount * sizeof(Block));
|
||||||
|
scenery.Frames = 0;
|
||||||
|
scenery.Score = 0;
|
||||||
if (!(scenery.blocks)) printf("FATAL! Memory allocation failed!\n");
|
if (!(scenery.blocks)) printf("FATAL! Memory allocation failed!\n");
|
||||||
scenery.IsPaused = false;
|
scenery.IsPaused = false;
|
||||||
scenery.Lives = 3;
|
scenery.Lives = 3;
|
||||||
|
scenery.DestroyedBlocks = 0;
|
||||||
int index;
|
int index;
|
||||||
for (int y = 0; y < 9; y++) {
|
for (int y = 0; y < 9; y++) {
|
||||||
index = 15 * y;
|
index = 15 * y;
|
||||||
@ -72,6 +94,13 @@ Scenery BREAKOUT_CreateDefault(){
|
|||||||
return scenery;
|
return scenery;
|
||||||
} /* BREAKOUT_CreateDefault */
|
} /* BREAKOUT_CreateDefault */
|
||||||
|
|
||||||
|
|
||||||
|
int BREAKOUT_RefreshScore(Scenery * scenery){
|
||||||
|
(scenery->Score) = (int)round((double)(scenery->Frames) * 0.005f * ((scenery->ball).Speed) + (double)(50 * (scenery->DestroyedBlocks)));
|
||||||
|
printf("Score: %d\n", (scenery->Score));
|
||||||
|
return (scenery->Score);
|
||||||
|
}
|
||||||
|
|
||||||
// This Function is obsolete! Do not use it!
|
// This Function is obsolete! Do not use it!
|
||||||
void BREAKOUT_ChangeSize(int width, int height){
|
void BREAKOUT_ChangeSize(int width, int height){
|
||||||
BREAKOUT_BoxWidth = width;
|
BREAKOUT_BoxWidth = width;
|
||||||
@ -87,11 +116,14 @@ void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate){
|
|||||||
// Render "Countdown"
|
// Render "Countdown"
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
(scenery->Frames)++;
|
||||||
|
BREAKOUT_RefreshScore(scenery);
|
||||||
if (scenery->IsGameOver) {
|
if (scenery->IsGameOver) {
|
||||||
BALL_ResetPosition(&(scenery->ball));
|
BALL_ResetPosition(&(scenery->ball));
|
||||||
PADDLE_ResetPosition(&(scenery->paddle));
|
PADDLE_ResetPosition(&(scenery->paddle));
|
||||||
scenery->StartCountdown = 240;
|
scenery->StartCountdown = 240;
|
||||||
scenery->IsGameOver = false;
|
scenery->IsGameOver = false;
|
||||||
|
scenery->Frames = 0;
|
||||||
if (--(scenery->Lives) <= 0)
|
if (--(scenery->Lives) <= 0)
|
||||||
printf("Game over, no lives left!\n");
|
printf("Game over, no lives left!\n");
|
||||||
else
|
else
|
||||||
@ -111,16 +143,25 @@ void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer){
|
|||||||
}
|
}
|
||||||
BALL_Draw(renderer, &(scenery->ball));
|
BALL_Draw(renderer, &(scenery->ball));
|
||||||
PADDLE_Draw(renderer, &(scenery->paddle));
|
PADDLE_Draw(renderer, &(scenery->paddle));
|
||||||
|
if ((scenery->StartCountdown) > 0) { // ! Render Z-Layer !
|
||||||
|
SDL_Rect * rect = GAME_CountdownSourceRects + (((scenery->StartCountdown) - 1) / 60);
|
||||||
|
SDL_Rect target = *rect;
|
||||||
|
target.x = ((BREAKOUT_BoxWidth - (rect->w)) / 2);
|
||||||
|
target.y = ((BREAKOUT_BoxHeight - (rect->h)) / 2);
|
||||||
|
SDL_RenderCopy(renderer, GAME_CountdownTexture, rect, &target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BREAKOUT_DEINITIALIZE(){
|
void BREAKOUT_DEINITIALIZE(){
|
||||||
if (BREAKOUT_IsInit) {
|
if (BREAKOUT_IsInit) {
|
||||||
printf("De-initializing Game...\n");
|
printf("De-initializing Game...\n");
|
||||||
|
SDL_DestroyTexture(GAME_CountdownTexture);
|
||||||
free(PADDLE_MoveLeftKeys);
|
free(PADDLE_MoveLeftKeys);
|
||||||
free(PADDLE_MoveRightKeys);
|
free(PADDLE_MoveRightKeys);
|
||||||
free(BALL_SourceRects);
|
free(BALL_SourceRects);
|
||||||
free(PADDLE_SourceRects);
|
free(PADDLE_SourceRects);
|
||||||
free(BLOCK_SourceRects);
|
free(BLOCK_SourceRects);
|
||||||
|
free(GAME_CountdownSourceRects);
|
||||||
BALL_Deinitialize();
|
BALL_Deinitialize();
|
||||||
PADDLE_Deinitialize();
|
PADDLE_Deinitialize();
|
||||||
BLOCK_Deinitialize();
|
BLOCK_Deinitialize();
|
||||||
@ -143,24 +184,33 @@ void BALL_Initialize(SDL_Renderer * renderer){
|
|||||||
printf("Initializing Ball...\n");
|
printf("Initializing Ball...\n");
|
||||||
BALL_Texture = IMG_LoadTexture(renderer, BALL_TexturePath);
|
BALL_Texture = IMG_LoadTexture(renderer, BALL_TexturePath);
|
||||||
if (!BALL_Texture) printf("Ball texture failed to load!\n");
|
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");
|
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");
|
printf("Ball initialized!\n");
|
||||||
BALL_IsInit = true;
|
BALL_IsInit = true;
|
||||||
} else printf("Ball is already initialized!\n");
|
} else printf("Ball is already initialized!\n");
|
||||||
}
|
} /* BALL_Initialize */
|
||||||
|
|
||||||
Ball BALL_CreateDefault(){
|
Ball BALL_CreateDefault(){
|
||||||
double rotation = (double)(rand() % 360);
|
double rotation = (double)(rand() % 360);
|
||||||
|
|
||||||
return (Ball) {
|
return (Ball) {
|
||||||
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 130 },
|
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 132 },
|
||||||
.Momentum = (Vector) {.x = 0.0f, .y = 15.0f },
|
.Momentum = (Vector) {.x = 0.0f, .y = 15.0f },
|
||||||
.TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 130, .w = 30, .h = 30 },
|
.TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2 - 15, .y = BREAKOUT_BoxHeight - 130, .w = 30, .h = 30 },
|
||||||
.Size = 15.0f,
|
.Size = 15.0f,
|
||||||
.Rotation = rotation,
|
.Rotation = rotation,
|
||||||
.RotationValue = 2,
|
.RotationValue = 5,
|
||||||
.TextureIndex = 0,
|
.TextureIndex = 0,
|
||||||
.Speed = 15.0f
|
.Speed = 15.0f
|
||||||
}; // Objekt für die Eigenschaften des Balls
|
}; // Objekt für die Eigenschaften des Balls
|
||||||
@ -170,6 +220,7 @@ void BALL_ResetPosition(Ball * obj){
|
|||||||
(obj->Location).x = BREAKOUT_BoxWidth / 2 - 15;
|
(obj->Location).x = BREAKOUT_BoxWidth / 2 - 15;
|
||||||
(obj->Location).y = BREAKOUT_BoxHeight - 130;
|
(obj->Location).y = BREAKOUT_BoxHeight - 130;
|
||||||
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
|
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
|
||||||
|
(obj->Momentum) = VECTOR_GetScaledDirectionalUnitVector(0.0f, (obj->Speed));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
|
void BALL_Draw(SDL_Renderer * renderer, Ball * obj){
|
||||||
@ -289,7 +340,16 @@ bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle){
|
|||||||
return false;
|
return false;
|
||||||
} /* BALL_CollideWithPaddle */
|
} /* 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){
|
void BALL_Update(Ball * obj, Scenery * scenery){
|
||||||
|
BALL_AdaptSpeedGradient(obj, (scenery->Frames));
|
||||||
|
(obj->Momentum) = VECTOR_ChangeScaleTo((obj->Momentum), (obj->Speed));
|
||||||
Block * blocks = (scenery->blocks);
|
Block * blocks = (scenery->blocks);
|
||||||
Paddle * paddle = &(scenery->paddle);
|
Paddle * paddle = &(scenery->paddle);
|
||||||
int BlockCount = scenery->BlockCount;
|
int BlockCount = scenery->BlockCount;
|
||||||
@ -307,6 +367,8 @@ void BALL_Update(Ball * obj, Scenery * scenery){
|
|||||||
oldLocation = obj->Location;
|
oldLocation = obj->Location;
|
||||||
if (BALL_CollideWithRect(obj, &(blocks[i].TargetRect))) {
|
if (BALL_CollideWithRect(obj, &(blocks[i].TargetRect))) {
|
||||||
BLOCK_DealDamage(blocks + i, 1);
|
BLOCK_DealDamage(blocks + i, 1);
|
||||||
|
if (blocks[i].HP <= 0)
|
||||||
|
(scenery->DestroyedBlocks)++;
|
||||||
(obj->Location) = VECTOR_Add(oldLocation, (obj->Momentum));
|
(obj->Location) = VECTOR_Add(oldLocation, (obj->Momentum));
|
||||||
BALL_MoveAwayFromBoundaries(obj);
|
BALL_MoveAwayFromBoundaries(obj);
|
||||||
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
|
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
|
||||||
@ -361,7 +423,7 @@ Paddle PADDLE_CreateDefault(){
|
|||||||
.TextureIndex = 0,
|
.TextureIndex = 0,
|
||||||
.Speed = 10,
|
.Speed = 10,
|
||||||
.SteeringAngle = 40.0f,
|
.SteeringAngle = 40.0f,
|
||||||
.Mode = KeyboardControl
|
.Mode = MouseControl
|
||||||
}; // Objekt für die Eigenschaften des Balls
|
}; // Objekt für die Eigenschaften des Balls
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -373,8 +435,6 @@ void PADDLE_ResetPosition(Paddle * obj){
|
|||||||
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){
|
void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){
|
||||||
// printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
|
// 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_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){
|
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray){
|
||||||
@ -398,36 +458,36 @@ void DOUBLE_Constrain(double * variable, double min, double max){
|
|||||||
*variable = min;
|
*variable = min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_Update(Paddle * obj, const Uint8 * keystate){
|
void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
|
||||||
bool leftKeyPressed = false, rightKeyPressed = false;
|
bool leftKeyPressed, rightKeyPressed;
|
||||||
int paddleXMid = (obj->TargetRect).x + ((obj->TargetRect).w / 2);
|
|
||||||
int mouseX;
|
|
||||||
|
|
||||||
switch (obj->Mode) {
|
switch (obj->Mode) {
|
||||||
case MouseControl:
|
case MouseControl:
|
||||||
SDL_GetMouseState(&mouseX, NULL);
|
PADDLE_MoveSmooth(obj);
|
||||||
mouseX = (int)roundf((float)mouseX / XScale);
|
|
||||||
if (abs(mouseX - paddleXMid) > (obj->Speed)) {
|
|
||||||
if (mouseX > paddleXMid)
|
|
||||||
rightKeyPressed = true;
|
|
||||||
else
|
|
||||||
leftKeyPressed = true;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case KeyboardControl:
|
case KeyboardControl:
|
||||||
leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys);
|
leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys);
|
||||||
rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
|
rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
|
||||||
|
|
||||||
|
if (leftKeyPressed && (!rightKeyPressed)) {
|
||||||
|
((obj->TargetRect).x) -= (obj->Speed);
|
||||||
|
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
||||||
|
((obj->TargetRect).x) += (obj->Speed);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Unknown Paddle Control Mode: %d!\n", obj->Mode);
|
printf("Unknown Paddle Control Mode: %d!\n", obj->Mode);
|
||||||
break;
|
break;
|
||||||
}
|
} /* switch */
|
||||||
|
|
||||||
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)));
|
INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
|
||||||
} /* PADDLE_Update */
|
} /* PADDLE_Update */
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ typedef struct sceneryStruct {
|
|||||||
Ball ball;
|
Ball ball;
|
||||||
Paddle paddle;
|
Paddle paddle;
|
||||||
Block * blocks;
|
Block * blocks;
|
||||||
int BlockCount, Lives, StartCountdown;
|
int BlockCount, Lives, StartCountdown, Frames, Score, DestroyedBlocks;
|
||||||
bool IsPaused, IsGameOver;
|
bool IsPaused, IsGameOver;
|
||||||
} Scenery; // Objekt für die Objekte und Eigenschaften einer Szenerie
|
} Scenery; // Objekt für die Objekte und Eigenschaften einer Szenerie
|
||||||
// End Structs
|
// End Structs
|
||||||
@ -47,6 +47,7 @@ typedef struct sceneryStruct {
|
|||||||
// Prototypes
|
// Prototypes
|
||||||
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height);
|
void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height);
|
||||||
Scenery BREAKOUT_CreateDefault();
|
Scenery BREAKOUT_CreateDefault();
|
||||||
|
int BREAKOUT_RefreshScore(Scenery * scenery);
|
||||||
void BREAKOUT_ChangeSize(int width, int height);
|
void BREAKOUT_ChangeSize(int width, int height);
|
||||||
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate);
|
void BREAKOUT_Update(Scenery * scenery, const Uint8 * keystate);
|
||||||
void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer);
|
void BREAKOUT_Draw(Scenery * scenery, SDL_Renderer * renderer);
|
||||||
@ -64,6 +65,7 @@ SDL_Point BALL_GetCenter(Ball * obj);
|
|||||||
void BALL_CollideWithBorders(Ball * obj);
|
void BALL_CollideWithBorders(Ball * obj);
|
||||||
void BALL_MoveAwayFromBoundaries(Ball * obj);
|
void BALL_MoveAwayFromBoundaries(Ball * obj);
|
||||||
bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle);
|
bool BALL_CollideWithPaddle(Ball * obj, Paddle * paddle);
|
||||||
|
void BALL_AdaptSpeedGradient(Ball * obj, int FrameCount);
|
||||||
void BALL_Update(Ball * obj, Scenery * scenery);
|
void BALL_Update(Ball * obj, Scenery * scenery);
|
||||||
void BALL_DestroyObject(Ball * obj);
|
void BALL_DestroyObject(Ball * obj);
|
||||||
void BALL_Deinitialize();
|
void BALL_Deinitialize();
|
||||||
@ -74,6 +76,7 @@ void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj);
|
|||||||
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray);
|
bool KeyPressed(const Uint8 * keystate, Uint8 * keyArray);
|
||||||
void INT_Constrain(int * variable, int min, int max);
|
void INT_Constrain(int * variable, int min, int max);
|
||||||
void DOUBLE_Constrain(double * variable, double min, double max);
|
void DOUBLE_Constrain(double * variable, double min, double max);
|
||||||
|
void PADDLE_MoveSmooth(Paddle * obj);
|
||||||
void PADDLE_Update(Paddle * obj, const Uint8 * keystate);
|
void PADDLE_Update(Paddle * obj, const Uint8 * keystate);
|
||||||
void PADDLE_DestroyObject(Paddle * obj);
|
void PADDLE_DestroyObject(Paddle * obj);
|
||||||
void PADDLE_Deinitialize();
|
void PADDLE_Deinitialize();
|
||||||
|
@ -67,7 +67,7 @@ void HIGHSCORES_Deinitialize(){
|
|||||||
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer){
|
void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer){
|
||||||
char * buffer = calloc(100, sizeof(char));
|
char * buffer = calloc(100, sizeof(char));
|
||||||
int count = 0;
|
int count = 0;
|
||||||
char format[20] = "| %-58s | %-10s |";
|
char format[20] = "| %-54s | %-14s |";
|
||||||
SDL_Rect Message_rect;
|
SDL_Rect Message_rect;
|
||||||
SDL_Surface * HIGHSCORES_TableSurface = SDL_CreateRGBSurface(0, 1920, 1080, 32, 0, 0, 0, 0);
|
SDL_Surface * HIGHSCORES_TableSurface = SDL_CreateRGBSurface(0, 1920, 1080, 32, 0, 0, 0, 0);
|
||||||
|
|
||||||
|
4
main.c
4
main.c
@ -55,7 +55,7 @@ int main(int argc, char * args[]){
|
|||||||
HIGHSCORES_Draw(renderer);
|
HIGHSCORES_Draw(renderer);
|
||||||
break;
|
break;
|
||||||
case Settings:
|
case Settings:
|
||||||
Settings_Draw(renderer);
|
Settings_Draw(renderer,&scenery);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Unknow state was updated: %d\n", gameState);
|
printf("Unknow state was updated: %d\n", gameState);
|
||||||
@ -184,7 +184,7 @@ void INITIALIZE() {
|
|||||||
Load_Textures(renderer);
|
Load_Textures(renderer);
|
||||||
HIGHSCORES_Initialize();
|
HIGHSCORES_Initialize();
|
||||||
BACKGROUND_Initialize(renderer, width, height);
|
BACKGROUND_Initialize(renderer, width, height);
|
||||||
Settings_Initialize(renderer);
|
Settings_Initialize(renderer,&scenery);
|
||||||
printf("Initializing finished!\n");
|
printf("Initializing finished!\n");
|
||||||
} /* INITIALIZE */
|
} /* INITIALIZE */
|
||||||
|
|
||||||
|
76
settings.c
76
settings.c
@ -9,37 +9,47 @@
|
|||||||
#define Slider_height 100
|
#define Slider_height 100
|
||||||
#define Scalar_width 20
|
#define Scalar_width 20
|
||||||
#define Bar_width 400
|
#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))))
|
||||||
|
|
||||||
SDL_Texture* Settings_Texture;
|
SDL_Texture* Settings_Texture;
|
||||||
SDL_Texture* Settings_Ball_Texture;
|
SDL_Texture* Settings_Return_Button_Texture;
|
||||||
|
|
||||||
SDL_Rect Settings_rect;
|
SDL_Rect Settings_rect;
|
||||||
SDL_Rect Settings_Ball_rect;
|
SDL_Rect Settings_Return_Button_rect;
|
||||||
Slider BV;
|
Slider BV;
|
||||||
Slider BS;
|
Slider BS;
|
||||||
Slider BT;
|
Slider BT;
|
||||||
|
Uint32 Mousestate;
|
||||||
bool Settings_IsInit=false;
|
bool Settings_IsInit=false;
|
||||||
|
|
||||||
void Settings_Initialize (SDL_Renderer* renderer) {
|
void Settings_Initialize (SDL_Renderer* renderer,Scenery* scenery) {
|
||||||
Initialize_Slider(400,300,Scalar_width,Bar_width,Slider_height,1,2,&BV);
|
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,1,2,&BS);
|
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,1,2,&BT);
|
Initialize_Slider(400,700,Scalar_width,Bar_width,Slider_height,0,8,&BT,scenery->ball.TextureIndex);
|
||||||
Settings_Texture = IMG_LoadTexture(renderer, "assets/images/settings_title.png");
|
Settings_Texture = IMG_LoadTexture(renderer, "assets/images/settings_title.png");
|
||||||
Settings_rect = (SDL_Rect){.x = 800, .y = 180, .w=313, .h=100};
|
Settings_rect = (SDL_Rect){.x = 800, .y = 180, .w=313, .h=100};
|
||||||
Settings_Ball_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png");
|
Settings_Return_Button_Texture = IMG_LoadTexture(renderer, "assets/images/return_button.png");
|
||||||
Settings_Ball_rect = (SDL_Rect){.x = 1200, .y = 300, .w=100, .h=100};
|
Settings_Return_Button_rect = (SDL_Rect){.x = 200, .y = 200, .w=75, .h=75};
|
||||||
Settings_IsInit = true;
|
Settings_IsInit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings_Draw (SDL_Renderer* renderer) {
|
void Settings_Draw (SDL_Renderer* renderer,Scenery* scenery) {
|
||||||
double x;
|
double x;
|
||||||
|
scenery->ball.TargetRect.x=900;
|
||||||
|
scenery->ball.TargetRect.y=700;
|
||||||
SDL_RenderCopy(renderer, Settings_Texture, NULL, &Settings_rect);
|
SDL_RenderCopy(renderer, Settings_Texture, NULL, &Settings_rect);
|
||||||
SDL_RenderCopy(renderer, Settings_Ball_Texture, NULL, &Settings_Ball_rect);
|
SDL_RenderCopy(renderer, Settings_Return_Button_Texture, NULL, &Settings_Return_Button_rect);
|
||||||
Draw_Slider(renderer,&BV);
|
Draw_Slider(renderer,&BV);
|
||||||
Draw_Slider(renderer,&BS);
|
Draw_Slider(renderer,&BS);
|
||||||
Draw_Slider(renderer,&BT);
|
Draw_Slider(renderer,&BT);
|
||||||
|
Draw_Ballstate(renderer,scenery);
|
||||||
mapping(&x,&BT);
|
mapping(&x,&BT);
|
||||||
|
scenery->ball.TextureIndex=round(x);
|
||||||
|
mapping(&x,&BS);
|
||||||
|
scenery->ball.TargetRect.w=x;
|
||||||
|
scenery->ball.TargetRect.h=x;
|
||||||
|
Settings_Return();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings_Deinitialize(){
|
void Settings_Deinitialize(){
|
||||||
@ -54,20 +64,20 @@ void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
|
|||||||
SDL_SetRenderDrawColor(renderer,255,255,255,255);
|
SDL_SetRenderDrawColor(renderer,255,255,255,255);
|
||||||
SDL_RenderDrawRect(renderer,&beta->Bar_rect);
|
SDL_RenderDrawRect(renderer,&beta->Bar_rect);
|
||||||
int x,y;
|
int x,y;
|
||||||
Uint32 Mousestate=SDL_GetMouseState(&x,&y);
|
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_RenderFillRect(renderer,&beta->Scalar_rect);
|
||||||
SDL_RenderDrawRect(renderer,&beta->Scalar_rect);
|
SDL_RenderDrawRect(renderer,&beta->Scalar_rect);
|
||||||
if(x>(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->x+beta->bw-(beta->sw));
|
beta->Scalar_rect.x=(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w));
|
||||||
beta->Slider_value=(beta->x+beta->bw-(beta->sw)/2);
|
beta->Slider_value=(beta->Bar_rect.x+beta->Bar_rect.w-(beta->Scalar_rect.w)/2);
|
||||||
}
|
}
|
||||||
else if(x<beta->x+(beta->sw)/2){
|
else if(x<beta->Bar_rect.x+(beta->Scalar_rect.w)/2){
|
||||||
beta->Scalar_rect.x=beta->x;
|
beta->Scalar_rect.x=beta->Bar_rect.x;
|
||||||
beta->Slider_value=beta->x+(beta->sw)/2;
|
beta->Slider_value=beta->Bar_rect.x+(beta->Scalar_rect.w)/2;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
beta->Scalar_rect.x=x-(beta->sw/2);
|
beta->Scalar_rect.x=x-(beta->Scalar_rect.w/2);
|
||||||
beta->Slider_value=x;
|
beta->Slider_value=x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,19 +86,25 @@ void Draw_Slider(SDL_Renderer* renderer,Slider* beta){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mapping(double *x,Slider* beta){
|
void Draw_Ballstate(SDL_Renderer* renderer,Scenery* scenery){
|
||||||
*x=((beta->max-beta->min)/(beta->bw-(beta->sw)))*(beta->Slider_value-beta->x-beta->sw/2)+beta->min;
|
BALL_Draw(renderer, &(scenery->ball));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta){
|
void mapping(double *x,Slider* beta){
|
||||||
beta->Scalar_rect = (SDL_Rect){.x=x,.y=y,.w=sw,.h=h};
|
*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,double defaultvalue){
|
||||||
|
beta->Scalar_rect = (SDL_Rect){.x=(defaultvalue-min)/(max-min)*(bw-sw/2)+x,.y=y,.w=sw,.h=h};
|
||||||
beta->Bar_rect = (SDL_Rect){.x=x,.y=y,.w=bw,.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->max=max;
|
||||||
beta->min=min;
|
beta->min=min;
|
||||||
beta->Slider_value=x+sw/2;
|
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);
|
||||||
}
|
}
|
||||||
|
20
settings.h
20
settings.h
@ -7,24 +7,32 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "breakout.h"
|
||||||
|
#include "gamestate.h"
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
typedef struct sliderstruct {
|
typedef struct sliderstruct {
|
||||||
SDL_Rect Bar_rect;
|
SDL_Rect Bar_rect;
|
||||||
SDL_Rect Scalar_rect;
|
SDL_Rect Scalar_rect;
|
||||||
int x,y,bw,sw,h;
|
|
||||||
double Slider_value,min,max;
|
double Slider_value,min,max;
|
||||||
} Slider;
|
} Slider;
|
||||||
|
|
||||||
void Settings_Initialize (SDL_Renderer* renderer);
|
|
||||||
|
|
||||||
void Settings_Draw (SDL_Renderer* renderer);
|
void Settings_Draw (SDL_Renderer* renderer,Scenery* scenery);
|
||||||
|
|
||||||
void Settings_Deinitialize();
|
|
||||||
|
|
||||||
void Draw_Slider(SDL_Renderer* renderer,Slider* beta);
|
void Draw_Slider(SDL_Renderer* renderer,Slider* beta);
|
||||||
|
|
||||||
|
void Draw_Ballstate(SDL_Renderer* renderer,Scenery* scenery);
|
||||||
|
|
||||||
|
void Settings_Initialize (SDL_Renderer* renderer,Scenery* scenery);
|
||||||
|
|
||||||
|
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta,double defaultvalue);
|
||||||
|
|
||||||
|
void Settings_Deinitialize();
|
||||||
|
|
||||||
void mapping(double *x,Slider* beta);
|
void mapping(double *x,Slider* beta);
|
||||||
|
|
||||||
void Initialize_Slider(int x,int y,int sw,int bw,int h,double min,double max,Slider* beta);
|
void Settings_Return();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "gamestate.h"
|
#include "gamestate.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
|
extern float XScale, YScale;
|
||||||
|
|
||||||
SDL_Texture * TITLE_Texture;
|
SDL_Texture * TITLE_Texture;
|
||||||
SDL_Texture * PLAYBUTTON_Texture;
|
SDL_Texture * PLAYBUTTON_Texture;
|
||||||
@ -23,7 +24,11 @@ SDL_Rect HIGHSCORESBUTTON_Rect;
|
|||||||
SDL_Rect QUITBUTTON_Rect;
|
SDL_Rect QUITBUTTON_Rect;
|
||||||
|
|
||||||
int clickInRect(SDL_MouseButtonEvent b, SDL_Rect * area_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) {
|
void Load_Textures(SDL_Renderer * renderer) {
|
||||||
|
Reference in New Issue
Block a user