diff --git a/bin/assets/images/ball.png b/bin/assets/images/ball.png index 0b62c40..bcd3581 100644 Binary files a/bin/assets/images/ball.png and b/bin/assets/images/ball.png differ diff --git a/bin/assets/images/blocks.png b/bin/assets/images/blocks.png deleted file mode 100644 index bdc96f2..0000000 Binary files a/bin/assets/images/blocks.png and /dev/null differ diff --git a/bin/assets/images/paddle.png b/bin/assets/images/paddle.png index 4d54f4b..23792c6 100644 Binary files a/bin/assets/images/paddle.png and b/bin/assets/images/paddle.png differ diff --git a/bin/assets/images/spritesheet.png b/bin/assets/images/spritesheet.png new file mode 100644 index 0000000..2c0ebff Binary files /dev/null and b/bin/assets/images/spritesheet.png differ diff --git a/breakout.c b/breakout.c index a475746..64a9cf8 100644 --- a/breakout.c +++ b/breakout.c @@ -22,17 +22,17 @@ SDL_Texture * BLOCK_Texture; SDL_Rect * BALL_SourceRects; SDL_Rect * PADDLE_SourceRects; SDL_Rect * BLOCK_SourceRects; -Ball ball; -Paddle paddle; -Block * blocks; -int BlockCount = 60; // Move to scenery Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys; -double BALL_Speed = 15.0f; -int PADDLE_Speed = 10; bool BREAKOUT_IsInit = false; bool BALL_IsInit = false; bool PADDLE_IsInit = false; bool BLOCK_IsInit = false; +Ball ball; +Paddle paddle; +Block * blocks; +int BlockCount = 60; // Move to scenery +double BALL_Speed = 15.0f; +int PADDLE_Speed = 10; void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ if (!BREAKOUT_IsInit) { @@ -50,8 +50,15 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ for (int y = 0; y < 6; y++) { index = 10 * y; for (int x = 0; x < 10; x++) { - blocks[x + index] = BLOCK_CreateDefault(); - blocks[x + index].TargetRect = (SDL_Rect) {.x = ((192 * x) + 4), .y = ((96 * y) + 2), .w = 184, .h = 92 }; + if (x + y == 0) { + blocks[x + index] = BLOCK_CreateDefault(); + blocks[x + index].TargetRect = (SDL_Rect) {.x = 1, 1, .w = 184, .h = 92 }; + blocks[x + index].TextureIndex = y + x; + } else { + blocks[x + index] = BLOCK_CreateDefault(); + blocks[x + index].TargetRect = (SDL_Rect) {.x = ((192 * x) + 4), .y = ((96 * y) + 2), .w = 184, .h = 92 }; + blocks[x + index].TextureIndex = y + x; + } } } printf("Game initialized!\n"); @@ -59,6 +66,7 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ } else printf("Game is already initialized!\n"); } /* BREAKOUT_INITIALIZE */ +// This Function is obsolete! Do not use it! void BREAKOUT_ChangeSize(int width, int height){ BREAKOUT_BoxWidth = width; BREAKOUT_BoxHeight = height; @@ -167,6 +175,7 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){ (obj->Momentum).x = -(obj->Momentum).x; if (xMid) (obj->Momentum).y = -(obj->Momentum).y; + if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n"); if (yMid || xMid) { // Ball collides with Edge } else { // Ball collides with corner /* @@ -274,7 +283,7 @@ void PADDLE_Initialize(SDL_Renderer * renderer){ if (!PADDLE_Texture) printf("Paddle texture failed to load!\n"); PADDLE_SourceRects = (SDL_Rect *)malloc(1 * sizeof(SDL_Rect)); if (!PADDLE_SourceRects) printf("FATAL! Memory allocation failed!\n"); - PADDLE_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 }; + PADDLE_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 1000, .h = 100 }; PADDLE_MoveLeftKeys = (Uint8 *)malloc(2 * sizeof(Uint8)); if (!PADDLE_MoveLeftKeys) printf("FATAL! Memory allocation failed!\n"); PADDLE_MoveRightKeys = (Uint8 *)malloc(2 * sizeof(Uint8)); @@ -301,9 +310,9 @@ Paddle PADDLE_CreateDefault(){ 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)); + 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(Uint8 * keystate, Uint8 * keyArray){ @@ -346,24 +355,42 @@ void PADDLE_Deinitialize(){ void BLOCK_Initialize(SDL_Renderer * renderer){ if (!BLOCK_IsInit) { printf("Initializing Block...\n"); - BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/blocks_debug.png"); + BLOCK_Texture = IMG_LoadTexture(renderer, "assets/images/spritesheet.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 < BLOCK_TextureCount; i++) { -// TODO: All textures! - BLOCK_SourceRects[i] = (SDL_Rect) {.x = 0, .y = 500 * i * 0, .w = 1000, .h = 500 }; - } + BLOCK_SourceRects[0] = (SDL_Rect) {.x = 2000, .y = 1500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[1] = (SDL_Rect) {.x = 2000, .y = 2000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[2] = (SDL_Rect) {.x = 2000, .y = 2500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[3] = (SDL_Rect) {.x = 0, .y = 3000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[4] = (SDL_Rect) {.x = 1000, .y = 3000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[5] = (SDL_Rect) {.x = 2000, .y = 3000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[6] = (SDL_Rect) {.x = 0, .y = 3500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[7] = (SDL_Rect) {.x = 1000, .y = 3500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[8] = (SDL_Rect) {.x = 2000, .y = 3500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[9] = (SDL_Rect) {.x = 0, .y = 500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[10] = (SDL_Rect) {.x = 2000, .y = 0, .w = 1000, .h = 500 }; + BLOCK_SourceRects[11] = (SDL_Rect) {.x = 0, .y = 1000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[12] = (SDL_Rect) {.x = 0, .y = 1500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[13] = (SDL_Rect) {.x = 1000, .y = 0, .w = 1000, .h = 500 }; + BLOCK_SourceRects[14] = (SDL_Rect) {.x = 1000, .y = 500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[15] = (SDL_Rect) {.x = 1000, .y = 1000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[16] = (SDL_Rect) {.x = 1000, .y = 1500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[17] = (SDL_Rect) {.x = 0, .y = 2000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[18] = (SDL_Rect) {.x = 1000, .y = 2000, .w = 1000, .h = 500 }; + BLOCK_SourceRects[19] = (SDL_Rect) {.x = 0, .y = 2500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[20] = (SDL_Rect) {.x = 1000, .y = 2500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[21] = (SDL_Rect) {.x = 0, .y = 0, .w = 1000, .h = 500 }; + BLOCK_SourceRects[22] = (SDL_Rect) {.x = 2000, .y = 500, .w = 1000, .h = 500 }; + BLOCK_SourceRects[23] = (SDL_Rect) {.x = 2000, .y = 1000, .w = 1000, .h = 500 }; printf("Block initialized!\n"); BLOCK_IsInit = true; } else printf("Block is already initialized!\n"); } /* PADDLE_Initialize */ Block BLOCK_CreateDefault() { - int defaultpaddlewidth = 300; - return (Block) { - .TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = 150, .h = 75 }, + .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 100, .h = 50 }, .TextureIndex = (rand() % BLOCK_TextureCount), .HP = 1 }; // Objekt für die Eigenschaften des Balls diff --git a/main.c b/main.c index 873e5e4..cdc617b 100644 --- a/main.c +++ b/main.c @@ -23,6 +23,7 @@ void INITIALIZE(); void QUIT(); +void HandleSDLEvents(); void DrawText(SDL_Renderer * renderer, const char * text); void DrawBackground(SDL_Renderer * renderer); void printFontStyle(TTF_Font * ffont); @@ -52,23 +53,7 @@ int main(int argc, char * args[]){ gameState = readIntFromIO("W"ae "hle einen Spielbereich aus, den du testen m"oe "chtest:", "Fehlerhafte Eingabe!\n", "%d ist kein g"ue "ltiger Spielbereich!\n", 1, 5); INITIALIZE(); while (running) { // Gameloop - while (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_QUIT: - running = false; - break; - case SDL_KEYDOWN: - if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false; - else keyPress(event.key); - break; - case SDL_MOUSEBUTTONDOWN: - mousePress(event.button); - break; - case SDL_WINDOWEVENT: - windowChanged(event.window); - break; - } - } + HandleSDLEvents(); keystate = SDL_GetKeyboardState(NULL); DrawBackground(renderer); switch (gameState) { @@ -90,6 +75,26 @@ int main(int argc, char * args[]){ return 0; } /* main */ +void HandleSDLEvents(){ + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + running = false; + break; + case SDL_KEYDOWN: + if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false; + else keyPress(event.key); + break; + case SDL_MOUSEBUTTONDOWN: + mousePress(event.button); + break; + case SDL_WINDOWEVENT: + windowChanged(event.window); + break; + } + } +} + void mousePress(SDL_MouseButtonEvent b){ // Debug prop if (b.button == SDL_BUTTON_LEFT) { printf("Left mouse pressed at %d, %d\n", b.x, b.y);