diff --git a/Makefile b/Makefile index 64bd708..3d53552 100644 --- a/Makefile +++ b/Makefile @@ -10,4 +10,4 @@ args = -o all: $(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target) - cd $(dir) && $(target) + start cmd /K "cd $(dir) && $(target)" diff --git a/bin/assets/images/blocks_debug.png b/bin/assets/images/blocks_debug.png new file mode 100644 index 0000000..c52b126 Binary files /dev/null and b/bin/assets/images/blocks_debug.png differ diff --git a/breakout.c b/breakout.c index abb2958..605bf2d 100644 --- a/breakout.c +++ b/breakout.c @@ -14,6 +14,7 @@ #define Nullptr(type) (type *)0 #endif // __nullptr__ +int BLOCK_TextureCount = 24; int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight; SDL_Texture * BALL_Texture; SDL_Texture * PADDLE_Texture; @@ -23,6 +24,8 @@ SDL_Rect * PADDLE_SourceRects; SDL_Rect * BLOCK_SourceRects; Ball ball; Paddle paddle; +Block * blocks; +int BlockCount = 25; // Move to scenery Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys; double BALL_Speed = 15.0f; int PADDLE_Speed = 10; @@ -39,12 +42,23 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ BREAKOUT_BoxHeight = height; BALL_Initialize(renderer); PADDLE_Initialize(renderer); + BLOCK_Initialize(renderer); ball = BALL_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"); BREAKOUT_IsInit = true; } else printf("Game is already initialized!\n"); -} +} /* BREAKOUT_INITIALIZE */ void BREAKOUT_ChangeSize(int width, int height){ BREAKOUT_BoxWidth = width; @@ -54,9 +68,15 @@ void BREAKOUT_ChangeSize(int width, int height){ void BREAKOUT_Update(Uint8 * keystate){ BALL_Update(&ball, &paddle); PADDLE_Update(&paddle, keystate); + for (size_t i = 0; i < BlockCount; i++) { + BLOCK_Update(blocks + i); + } } void BREAKOUT_Draw(SDL_Renderer * renderer){ + for (size_t i = 0; i < BlockCount; i++) { + BLOCK_Draw(renderer, blocks + i); + } BALL_Draw(renderer, &ball); PADDLE_Draw(renderer, &paddle); } @@ -64,8 +84,18 @@ void BREAKOUT_Draw(SDL_Renderer * renderer){ void BREAKOUT_DEINITIALIZE(){ if (BREAKOUT_IsInit) { printf("De-initializing Game...\n"); - SDL_DestroyTexture(BALL_Texture); - SDL_DestroyTexture(PADDLE_Texture); + for (size_t i = 0; i < BlockCount; i++) { + 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"); BREAKOUT_IsInit = false; } else printf("Game is already de-initialized!\n"); @@ -88,7 +118,7 @@ Ball BALL_CreateDefault(){ double rotation = (double)(rand() % 360); 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 }, .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 }, .Size = 25.0f, @@ -166,9 +196,16 @@ void BALL_Update(Ball * obj, Paddle * paddle){ BALL_SteerMomentum(obj, paddle); (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) - (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) (obj->Momentum).y = -(obj->Momentum).y; 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(){ if (BALL_IsInit) { printf("De-initializing Ball...\n"); - + SDL_DestroyTexture(BALL_Texture); printf("Ball de-initialized!\n"); BALL_IsInit = false; } else printf("Ball is already de-initialized!\n"); @@ -255,6 +292,7 @@ void PADDLE_DestroyObject(Paddle * obj){ void PADDLE_Deinitialize(){ if (PADDLE_IsInit) { printf("De-initializing Paddle...\n"); + SDL_DestroyTexture(PADDLE_Texture); printf("Paddle de-initialized!\n"); PADDLE_IsInit = false; } else printf("Paddle is already de-initialized!\n"); @@ -263,51 +301,43 @@ void PADDLE_Deinitialize(){ void BLOCK_Initialize(SDL_Renderer * renderer){ if (!BLOCK_IsInit) { printf("Initializing Block...\n"); - BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/blocks.png"); - if (!BLOCK_Texture) printf("Blocktexture failed to load!\n"); - BLOCK_SourceRects = (SDL_Rect *)malloc(24 * sizeof(SDL_Rect)); + BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/blocks_debug.png"); + if (!BLOCK_Texture) printf("Block texture failed to load!\n"); + BLOCK_SourceRects = (SDL_Rect *)malloc(BLOCK_TextureCount * sizeof(SDL_Rect)); if (!BLOCK_SourceRects) printf("FATAL! Memory allocation failed!\n"); - for (int i = 0; i < 24; i++) { - BLOCK_SourceRects[i] = (SDL_Rect) {.x = 0, .y = 500 * i, .w = 1000, .h = 500 }; + for (int i = 0; i < BLOCK_TextureCount; i++) { +// TODO: All textures! + BLOCK_SourceRects[i] = (SDL_Rect) {.x = 0, .y = 500 * i * 0, .w = 1000, .h = 500 }; } printf("Block initialized!\n"); BLOCK_IsInit = true; } else printf("Block is already initialized!\n"); } /* PADDLE_Initialize */ -Block BLOCK_CreateDefault(){ +Block BLOCK_CreateDefault() { int defaultpaddlewidth = 300; return (Block) { - .TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 }, - .TextureIndex = 0 + .TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = 150, .h = 75 }, + .TextureIndex = (rand() % BLOCK_TextureCount) }; // Objekt für die Eigenschaften des Balls } -void BLOCK_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)); +void BLOCK_Draw(SDL_Renderer * renderer, Block * obj){ + // printf("Block drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).y); + SDL_RenderCopy(renderer, BLOCK_Texture, (BLOCK_SourceRects + (obj->TextureIndex)), &(obj->TargetRect)); } -void BLOCK_Update(Paddle * obj, Uint8 * keystate){ - bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys); - - 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_Update(Block * obj){ +// Do nothing currently } -void BLOCK_DestroyObject(Paddle * obj){ +void BLOCK_DestroyObject(Block * obj){ } void BLOCK_Deinitialize(){ - if (PADDLE_IsInit) { - printf("De-initializing Paddle...\n"); - - printf("Paddle de-initialized!\n"); - PADDLE_IsInit = false; - } else printf("Paddle is already de-initialized!\n"); + if (BLOCK_IsInit) { + printf("De-initializing Block...\n"); + SDL_DestroyTexture(BLOCK_Texture); + printf("Block de-initialized!\n"); + BLOCK_IsInit = false; + } else printf("Block is already de-initialized!\n"); } diff --git a/breakout.h b/breakout.h index f48a613..9148d7b 100644 --- a/breakout.h +++ b/breakout.h @@ -39,6 +39,7 @@ Ball BALL_CreateDefault(); 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); +void BALL_SteerMomentum(Ball * obj, Paddle * paddle); void BALL_Update(Ball * obj, Paddle * paddle); void BALL_DestroyObject(Ball * obj); void BALL_Deinitialize(); @@ -50,6 +51,12 @@ void constrain(int * variable, int min, int max); void PADDLE_Update(Paddle * obj, Uint8 * keystate); void PADDLE_DestroyObject(Paddle * obj); 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 #endif // __breakout_h__