diff --git a/bin/assets/images/Texts.png b/bin/assets/images/Texts.png new file mode 100644 index 0000000..3aecc26 Binary files /dev/null and b/bin/assets/images/Texts.png differ diff --git a/bin/assets/images/blocks.png b/bin/assets/images/blocks.png new file mode 100644 index 0000000..bdc96f2 Binary files /dev/null and b/bin/assets/images/blocks.png differ diff --git a/breakout.c b/breakout.c index 19bdec2..abb2958 100644 --- a/breakout.c +++ b/breakout.c @@ -16,17 +16,20 @@ int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight; SDL_Texture * BALL_Texture; +SDL_Texture * PADDLE_Texture; +SDL_Texture * BLOCK_Texture; SDL_Rect * BALL_SourceRects; +SDL_Rect * PADDLE_SourceRects; +SDL_Rect * BLOCK_SourceRects; Ball ball; Paddle paddle; -SDL_Texture * PADDLE_Texture; -SDL_Rect * PADDLE_SourceRects; 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; void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ if (!BREAKOUT_IsInit) { @@ -36,6 +39,8 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ BREAKOUT_BoxHeight = height; BALL_Initialize(renderer); PADDLE_Initialize(renderer); + ball = BALL_CreateDefault(); + paddle = PADDLE_CreateDefault(); printf("Game initialized!\n"); BREAKOUT_IsInit = true; } else printf("Game is already initialized!\n"); @@ -74,8 +79,6 @@ void BALL_Initialize(SDL_Renderer * renderer){ BALL_SourceRects = (SDL_Rect *)malloc(1 * 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 = BALL_CreateDefault(); - paddle = PADDLE_CreateDefault(); printf("Ball initialized!\n"); BALL_IsInit = true; } else printf("Ball is already initialized!\n"); @@ -184,7 +187,6 @@ void BALL_Deinitialize(){ } else printf("Ball is already de-initialized!\n"); } - void PADDLE_Initialize(SDL_Renderer * renderer){ if (!PADDLE_IsInit) { printf("Initializing Paddle...\n"); @@ -203,7 +205,6 @@ void PADDLE_Initialize(SDL_Renderer * renderer){ PADDLE_MoveRightKeys[0] = 2; PADDLE_MoveRightKeys[1] = SDL_SCANCODE_RIGHT; PADDLE_MoveRightKeys[2] = SDL_SCANCODE_D; - ball = BALL_CreateDefault(); printf("Paddle initialized!\n"); PADDLE_IsInit = true; } else printf("Paddle is already initialized!\n"); @@ -252,6 +253,57 @@ void PADDLE_Update(Paddle * obj, Uint8 * keystate){ void PADDLE_DestroyObject(Paddle * obj){ } void PADDLE_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"); +} + +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)); + 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 }; + } + 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 = defaultpaddlewidth, .h = 30 }, + .TextureIndex = 0 + }; // 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_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_DestroyObject(Paddle * obj){ +} +void BLOCK_Deinitialize(){ if (PADDLE_IsInit) { printf("De-initializing Paddle...\n"); diff --git a/breakout.h b/breakout.h index 82fec60..f48a613 100644 --- a/breakout.h +++ b/breakout.h @@ -23,9 +23,8 @@ typedef struct paddleStruct { } Paddle; // Objekt für die Eigenschaften des Paddles typedef struct blockStruct { - Vector Location; SDL_Rect TargetRect; - int XSize, YSize, TextureIndex; + int TextureIndex; } Block; // Objekt für die Eigenschaften des Paddles // End Structs