Block struct works, TODO: Fix rectangle collisions! Fix texture
This commit is contained in:
parent
ab98ff12c5
commit
cc5211f84f
2
Makefile
2
Makefile
@ -10,4 +10,4 @@ args = -o
|
|||||||
|
|
||||||
all:
|
all:
|
||||||
$(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target)
|
$(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target)
|
||||||
cd $(dir) && $(target)
|
start cmd /K "cd $(dir) && $(target)"
|
||||||
|
BIN
bin/assets/images/blocks_debug.png
Normal file
BIN
bin/assets/images/blocks_debug.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 58 KiB |
96
breakout.c
96
breakout.c
@ -14,6 +14,7 @@
|
|||||||
#define Nullptr(type) (type *)0
|
#define Nullptr(type) (type *)0
|
||||||
#endif // __nullptr__
|
#endif // __nullptr__
|
||||||
|
|
||||||
|
int BLOCK_TextureCount = 24;
|
||||||
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
|
int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight;
|
||||||
SDL_Texture * BALL_Texture;
|
SDL_Texture * BALL_Texture;
|
||||||
SDL_Texture * PADDLE_Texture;
|
SDL_Texture * PADDLE_Texture;
|
||||||
@ -23,6 +24,8 @@ SDL_Rect * PADDLE_SourceRects;
|
|||||||
SDL_Rect * BLOCK_SourceRects;
|
SDL_Rect * BLOCK_SourceRects;
|
||||||
Ball ball;
|
Ball ball;
|
||||||
Paddle paddle;
|
Paddle paddle;
|
||||||
|
Block * blocks;
|
||||||
|
int BlockCount = 25; // Move to scenery
|
||||||
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys;
|
||||||
double BALL_Speed = 15.0f;
|
double BALL_Speed = 15.0f;
|
||||||
int PADDLE_Speed = 10;
|
int PADDLE_Speed = 10;
|
||||||
@ -39,12 +42,23 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){
|
|||||||
BREAKOUT_BoxHeight = height;
|
BREAKOUT_BoxHeight = height;
|
||||||
BALL_Initialize(renderer);
|
BALL_Initialize(renderer);
|
||||||
PADDLE_Initialize(renderer);
|
PADDLE_Initialize(renderer);
|
||||||
|
BLOCK_Initialize(renderer);
|
||||||
ball = BALL_CreateDefault();
|
ball = BALL_CreateDefault();
|
||||||
paddle = PADDLE_CreateDefault();
|
paddle = PADDLE_CreateDefault();
|
||||||
|
blocks = malloc(BlockCount * sizeof(Block));
|
||||||
|
int index;
|
||||||
|
for (int y = 0; y < 5; y++) {
|
||||||
|
index = 5 * y;
|
||||||
|
for (int x = 0; x < 5; x++) {
|
||||||
|
blocks[x + index] = BLOCK_CreateDefault();
|
||||||
|
blocks[x + index].TargetRect.x = 200 * x;
|
||||||
|
blocks[x + index].TargetRect.y = 100 * y;
|
||||||
|
}
|
||||||
|
}
|
||||||
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");
|
||||||
}
|
} /* BREAKOUT_INITIALIZE */
|
||||||
|
|
||||||
void BREAKOUT_ChangeSize(int width, int height){
|
void BREAKOUT_ChangeSize(int width, int height){
|
||||||
BREAKOUT_BoxWidth = width;
|
BREAKOUT_BoxWidth = width;
|
||||||
@ -54,9 +68,15 @@ void BREAKOUT_ChangeSize(int width, int height){
|
|||||||
void BREAKOUT_Update(Uint8 * keystate){
|
void BREAKOUT_Update(Uint8 * keystate){
|
||||||
BALL_Update(&ball, &paddle);
|
BALL_Update(&ball, &paddle);
|
||||||
PADDLE_Update(&paddle, keystate);
|
PADDLE_Update(&paddle, keystate);
|
||||||
|
for (size_t i = 0; i < BlockCount; i++) {
|
||||||
|
BLOCK_Update(blocks + i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BREAKOUT_Draw(SDL_Renderer * renderer){
|
void BREAKOUT_Draw(SDL_Renderer * renderer){
|
||||||
|
for (size_t i = 0; i < BlockCount; i++) {
|
||||||
|
BLOCK_Draw(renderer, blocks + i);
|
||||||
|
}
|
||||||
BALL_Draw(renderer, &ball);
|
BALL_Draw(renderer, &ball);
|
||||||
PADDLE_Draw(renderer, &paddle);
|
PADDLE_Draw(renderer, &paddle);
|
||||||
}
|
}
|
||||||
@ -64,8 +84,18 @@ void BREAKOUT_Draw(SDL_Renderer * renderer){
|
|||||||
void BREAKOUT_DEINITIALIZE(){
|
void BREAKOUT_DEINITIALIZE(){
|
||||||
if (BREAKOUT_IsInit) {
|
if (BREAKOUT_IsInit) {
|
||||||
printf("De-initializing Game...\n");
|
printf("De-initializing Game...\n");
|
||||||
SDL_DestroyTexture(BALL_Texture);
|
for (size_t i = 0; i < BlockCount; i++) {
|
||||||
SDL_DestroyTexture(PADDLE_Texture);
|
BLOCK_DestroyObject(blocks + i);
|
||||||
|
}
|
||||||
|
free(blocks);
|
||||||
|
free(PADDLE_MoveLeftKeys);
|
||||||
|
free(PADDLE_MoveRightKeys);
|
||||||
|
free(BALL_SourceRects);
|
||||||
|
free(PADDLE_SourceRects);
|
||||||
|
free(BLOCK_SourceRects);
|
||||||
|
BALL_Deinitialize();
|
||||||
|
PADDLE_Deinitialize();
|
||||||
|
BLOCK_Deinitialize();
|
||||||
printf("Game de-initialized!\n");
|
printf("Game de-initialized!\n");
|
||||||
BREAKOUT_IsInit = false;
|
BREAKOUT_IsInit = false;
|
||||||
} else printf("Game is already de-initialized!\n");
|
} else printf("Game is already de-initialized!\n");
|
||||||
@ -88,7 +118,7 @@ Ball BALL_CreateDefault(){
|
|||||||
double rotation = (double)(rand() % 360);
|
double rotation = (double)(rand() % 360);
|
||||||
|
|
||||||
return (Ball) {
|
return (Ball) {
|
||||||
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 },
|
.Location = (Vector) {.x = BREAKOUT_BoxWidth / 2 + 300, .y = BREAKOUT_BoxHeight / 2 },
|
||||||
.Momentum = (Vector) {.x = 0.0f, .y = BALL_Speed },
|
.Momentum = (Vector) {.x = 0.0f, .y = BALL_Speed },
|
||||||
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 },
|
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 },
|
||||||
.Size = 25.0f,
|
.Size = 25.0f,
|
||||||
@ -166,9 +196,16 @@ void BALL_Update(Ball * obj, Paddle * paddle){
|
|||||||
BALL_SteerMomentum(obj, paddle);
|
BALL_SteerMomentum(obj, paddle);
|
||||||
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this
|
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this
|
||||||
}
|
}
|
||||||
|
oldMomentum = obj->Momentum;
|
||||||
|
for (size_t i = 0; i < BlockCount; i++) {
|
||||||
|
BALL_CollideWithRect(obj, &(blocks[i].TargetRect));
|
||||||
|
(obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this
|
||||||
|
(obj->Location) = vectorAdd((obj->Location), (obj->Momentum));
|
||||||
|
oldMomentum = obj->Momentum;
|
||||||
|
}
|
||||||
|
|
||||||
if ((obj->Location).y > BREAKOUT_BoxHeight)
|
if ((obj->Location).y > BREAKOUT_BoxHeight)
|
||||||
(obj->Location) = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; // Dead
|
(obj->Location) = (Vector) {.x = BREAKOUT_BoxWidth / 2 + 300, .y = BREAKOUT_BoxHeight / 2 }; // Dead
|
||||||
if ((obj->Location).y < 0.0f)
|
if ((obj->Location).y < 0.0f)
|
||||||
(obj->Momentum).y = -(obj->Momentum).y;
|
(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 > BREAKOUT_BoxWidth - (2 * (obj->Size)))
|
||||||
@ -181,7 +218,7 @@ void BALL_DestroyObject(Ball * obj){
|
|||||||
void BALL_Deinitialize(){
|
void BALL_Deinitialize(){
|
||||||
if (BALL_IsInit) {
|
if (BALL_IsInit) {
|
||||||
printf("De-initializing Ball...\n");
|
printf("De-initializing Ball...\n");
|
||||||
|
SDL_DestroyTexture(BALL_Texture);
|
||||||
printf("Ball de-initialized!\n");
|
printf("Ball de-initialized!\n");
|
||||||
BALL_IsInit = false;
|
BALL_IsInit = false;
|
||||||
} else printf("Ball is already de-initialized!\n");
|
} else printf("Ball is already de-initialized!\n");
|
||||||
@ -255,6 +292,7 @@ void PADDLE_DestroyObject(Paddle * obj){
|
|||||||
void PADDLE_Deinitialize(){
|
void PADDLE_Deinitialize(){
|
||||||
if (PADDLE_IsInit) {
|
if (PADDLE_IsInit) {
|
||||||
printf("De-initializing Paddle...\n");
|
printf("De-initializing Paddle...\n");
|
||||||
|
SDL_DestroyTexture(PADDLE_Texture);
|
||||||
printf("Paddle de-initialized!\n");
|
printf("Paddle de-initialized!\n");
|
||||||
PADDLE_IsInit = false;
|
PADDLE_IsInit = false;
|
||||||
} else printf("Paddle is already de-initialized!\n");
|
} else printf("Paddle is already de-initialized!\n");
|
||||||
@ -263,12 +301,13 @@ void PADDLE_Deinitialize(){
|
|||||||
void BLOCK_Initialize(SDL_Renderer * renderer){
|
void BLOCK_Initialize(SDL_Renderer * renderer){
|
||||||
if (!BLOCK_IsInit) {
|
if (!BLOCK_IsInit) {
|
||||||
printf("Initializing Block...\n");
|
printf("Initializing Block...\n");
|
||||||
BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/blocks.png");
|
BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/blocks_debug.png");
|
||||||
if (!BLOCK_Texture) printf("Block texture failed to load!\n");
|
if (!BLOCK_Texture) printf("Block texture failed to load!\n");
|
||||||
BLOCK_SourceRects = (SDL_Rect *)malloc(24 * sizeof(SDL_Rect));
|
BLOCK_SourceRects = (SDL_Rect *)malloc(BLOCK_TextureCount * sizeof(SDL_Rect));
|
||||||
if (!BLOCK_SourceRects) printf("FATAL! Memory allocation failed!\n");
|
if (!BLOCK_SourceRects) printf("FATAL! Memory allocation failed!\n");
|
||||||
for (int i = 0; i < 24; i++) {
|
for (int i = 0; i < BLOCK_TextureCount; i++) {
|
||||||
BLOCK_SourceRects[i] = (SDL_Rect) {.x = 0, .y = 500 * i, .w = 1000, .h = 500 };
|
// TODO: All textures!
|
||||||
|
BLOCK_SourceRects[i] = (SDL_Rect) {.x = 0, .y = 500 * i * 0, .w = 1000, .h = 500 };
|
||||||
}
|
}
|
||||||
printf("Block initialized!\n");
|
printf("Block initialized!\n");
|
||||||
BLOCK_IsInit = true;
|
BLOCK_IsInit = true;
|
||||||
@ -279,35 +318,26 @@ Block BLOCK_CreateDefault(){
|
|||||||
int defaultpaddlewidth = 300;
|
int defaultpaddlewidth = 300;
|
||||||
|
|
||||||
return (Block) {
|
return (Block) {
|
||||||
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 },
|
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = 150, .h = 75 },
|
||||||
.TextureIndex = 0
|
.TextureIndex = (rand() % BLOCK_TextureCount)
|
||||||
}; // Objekt für die Eigenschaften des Balls
|
}; // Objekt für die Eigenschaften des Balls
|
||||||
}
|
}
|
||||||
|
|
||||||
void BLOCK_Draw(SDL_Renderer * renderer, Paddle * obj){
|
void BLOCK_Draw(SDL_Renderer * renderer, Block * obj){
|
||||||
// printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x);
|
// printf("Block drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).y);
|
||||||
// SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect));
|
SDL_RenderCopy(renderer, BLOCK_Texture, (BLOCK_SourceRects + (obj->TextureIndex)), &(obj->TargetRect));
|
||||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
||||||
SDL_RenderDrawRect(renderer, &(obj->TargetRect));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BLOCK_Update(Paddle * obj, Uint8 * keystate){
|
void BLOCK_Update(Block * obj){
|
||||||
bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
|
// Do nothing currently
|
||||||
|
|
||||||
if (leftKeyPressed && (!rightKeyPressed)) {
|
|
||||||
((obj->TargetRect).x) -= PADDLE_Speed;
|
|
||||||
} else if ((!leftKeyPressed) && rightKeyPressed) {
|
|
||||||
((obj->TargetRect).x) += PADDLE_Speed;
|
|
||||||
}
|
}
|
||||||
constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
|
void BLOCK_DestroyObject(Block * obj){
|
||||||
}
|
|
||||||
void BLOCK_DestroyObject(Paddle * obj){
|
|
||||||
}
|
}
|
||||||
void BLOCK_Deinitialize(){
|
void BLOCK_Deinitialize(){
|
||||||
if (PADDLE_IsInit) {
|
if (BLOCK_IsInit) {
|
||||||
printf("De-initializing Paddle...\n");
|
printf("De-initializing Block...\n");
|
||||||
|
SDL_DestroyTexture(BLOCK_Texture);
|
||||||
printf("Paddle de-initialized!\n");
|
printf("Block de-initialized!\n");
|
||||||
PADDLE_IsInit = false;
|
BLOCK_IsInit = false;
|
||||||
} else printf("Paddle is already de-initialized!\n");
|
} else printf("Block is already de-initialized!\n");
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ Ball BALL_CreateDefault();
|
|||||||
void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
|
void BALL_Draw(SDL_Renderer * renderer, Ball * obj);
|
||||||
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect);
|
bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect);
|
||||||
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2);
|
bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2);
|
||||||
|
void BALL_SteerMomentum(Ball * obj, Paddle * paddle);
|
||||||
void BALL_Update(Ball * obj, Paddle * paddle);
|
void BALL_Update(Ball * obj, Paddle * paddle);
|
||||||
void BALL_DestroyObject(Ball * obj);
|
void BALL_DestroyObject(Ball * obj);
|
||||||
void BALL_Deinitialize();
|
void BALL_Deinitialize();
|
||||||
@ -50,6 +51,12 @@ void constrain(int * variable, int min, int max);
|
|||||||
void PADDLE_Update(Paddle * obj, Uint8 * keystate);
|
void PADDLE_Update(Paddle * obj, Uint8 * keystate);
|
||||||
void PADDLE_DestroyObject(Paddle * obj);
|
void PADDLE_DestroyObject(Paddle * obj);
|
||||||
void PADDLE_Deinitialize();
|
void PADDLE_Deinitialize();
|
||||||
|
void BLOCK_Initialize(SDL_Renderer * renderer);
|
||||||
|
Block BLOCK_CreateDefault() ;
|
||||||
|
void BLOCK_Draw(SDL_Renderer * renderer, Block * obj);
|
||||||
|
void BLOCK_Update(Block * obj);
|
||||||
|
void BLOCK_DestroyObject(Block * obj);
|
||||||
|
void BLOCK_Deinitialize();
|
||||||
// End Prototypes
|
// End Prototypes
|
||||||
|
|
||||||
#endif // __breakout_h__
|
#endif // __breakout_h__
|
||||||
|
Loading…
Reference in New Issue
Block a user