diff --git a/bin/assets/images/paddle.png b/bin/assets/images/paddle.png new file mode 100644 index 0000000..4d54f4b Binary files /dev/null and b/bin/assets/images/paddle.png differ diff --git a/breakout.c b/breakout.c index f763558..d540612 100644 --- a/breakout.c +++ b/breakout.c @@ -18,6 +18,10 @@ int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight; SDL_Texture * BALL_Texture; SDL_Rect * BALL_SourceRects; Ball ball; +Paddle paddle; +SDL_Texture * PADDLE_Texture; +SDL_Rect * PADDLE_SourceRects; +Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys; void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ printf("Initializing Game...\n"); @@ -25,15 +29,18 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ BREAKOUT_BoxWidth = width; BREAKOUT_BoxHeight = height; BALL_Initialize(renderer); + PADDLE_Initialize(renderer); printf("Game initialized!\n"); } void BREAKOUT_Update(Uint8 * keystate){ BALL_Update(&ball); + PADDLE_Update(&paddle, keystate); } void BREAKOUT_Draw(SDL_Renderer * renderer){ BALL_Draw(renderer, &ball); + PADDLE_Draw(renderer, &paddle); } void BREAKOUT_DEINITIALIZE(){ @@ -47,8 +54,10 @@ void BALL_Initialize(SDL_Renderer * renderer){ BALL_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png"); if (!BALL_Texture) printf("Ball texture failed to load!\n"); 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"); } @@ -58,8 +67,8 @@ Ball BALL_CreateDefault(){ return (Ball) { .Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }, .Momentum = (Vector) {.x = 0.0f, .y = 6.0f }, - .TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2, .w = 50, .h = 50 }, - .Size = 50.0f, + .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 }, + .Size = 25.0f, .Rotation = rotation, .RotationValue = 2, .TextureIndex = 0 @@ -67,17 +76,21 @@ Ball BALL_CreateDefault(){ } void BALL_Draw(SDL_Renderer * renderer, Ball * obj){ - printf("Ball drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x); + // printf("Ball drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x); SDL_RenderCopyEx(renderer, BALL_Texture, BALL_SourceRects + (obj->TextureIndex), &(obj->TargetRect), obj->Rotation, NULL, SDL_FLIP_NONE); } void BALL_Update(Ball * obj){ - obj->Location = vectorAdd(obj->Location, obj->Momentum); + (obj->Location) = vectorAdd((obj->Location), ( obj->Momentum)); (obj->TargetRect).x = (int)round((obj->Location).x); (obj->TargetRect).y = (int)round((obj->Location).y); - obj->Rotation += obj->RotationValue; - if ((obj->Location).y > BREAKOUT_BoxHeight) - obj->Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; + (obj->Rotation) += (obj->RotationValue); + if ((obj->Location).y > BREAKOUT_BoxHeight + (obj->Size)) + (obj->Location) = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; // Dead + if ((obj->Location).y < (obj->Size)) + (obj->Momentum).y = -(obj->Momentum).y; + if ((obj->Location).x < (obj->Size) || (obj->Location).x > BREAKOUT_BoxWidth - (obj->Size)) + (obj->Momentum).x = -(obj->Momentum).x; } void BALL_DestroyObject(Ball * obj){ } @@ -86,3 +99,71 @@ void BALL_Deinitialize(){ printf("Ball de-initialized!\n"); } + + +void PADDLE_Initialize(SDL_Renderer * renderer){ + printf("Initializing Paddle...\n"); + PADDLE_Texture = IMG_LoadTexture(renderer, "assets/images/paddle.png"); + 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_MoveLeftKeys = (Uint8 *)malloc(2 * sizeof(Uint8)); + if (!PADDLE_MoveLeftKeys) printf("FATAL! Memory allocation failed!\n"); + PADDLE_MoveRightKeys = (Uint8 *)malloc(2 * sizeof(Uint8)); + if (!PADDLE_MoveRightKeys) printf("FATAL! Memory allocation failed!\n"); + PADDLE_MoveLeftKeys[0] = 2; // Erster wert gibt größe des arrays an + PADDLE_MoveLeftKeys[1] = SDL_SCANCODE_LEFT; + PADDLE_MoveLeftKeys[2] = SDL_SCANCODE_A; + PADDLE_MoveRightKeys[0] = 2; + PADDLE_MoveRightKeys[1] = SDL_SCANCODE_RIGHT; + PADDLE_MoveRightKeys[2] = SDL_SCANCODE_D; + ball = BALL_CreateDefault(); + printf("Paddle initialized!\n"); +} + +Paddle PADDLE_CreateDefault(){ + int defaultpaddlewidth = 300; + + return (Paddle) { + .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 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)); +} + +bool KeyPressed(Uint8 * keystate, Uint8 * keyArray){ + for (int i = 0; i < (*keyArray); i++) { + if (keystate[keyArray[(i + 1)]]) return true; + } + return false; +} + +void constrain(int * variable, int min, int max){ + if (*variable > max) + *variable = max; + else if (*variable < min) + *variable = min; +} + +void PADDLE_Update(Paddle * obj, Uint8 * keystate){ + bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys); + + if (leftKeyPressed && (!rightKeyPressed)) { + ((obj->TargetRect).x) -= 5; + } else if ((!leftKeyPressed) && rightKeyPressed) { + ((obj->TargetRect).x) += 5; + } + constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w))); +} +void PADDLE_DestroyObject(Paddle * obj){ +} +void PADDLE_Deinitialize(){ + printf("De-initializing Ball...\n"); + + printf("Ball de-initialized!\n"); +} diff --git a/breakout.h b/breakout.h index ce682d2..49a26cd 100644 --- a/breakout.h +++ b/breakout.h @@ -1,5 +1,11 @@ #ifndef __breakout_h__ #define __breakout_h__ +#include +#include +#include +#include +#include +#include #include "vector.h" @@ -12,9 +18,8 @@ typedef struct ballStruct { } Ball; // Objekt für die Eigenschaften des Balls typedef struct paddleStruct { - double XLocation; // Notice: Locked Y-Coordinate SDL_Rect TargetRect; - int XSize, TextureIndex; + int TextureIndex; } Paddle; // Objekt für die Eigenschaften des Paddles typedef struct blockStruct { @@ -35,6 +40,15 @@ void BALL_Draw(SDL_Renderer * renderer, Ball * obj); void BALL_Update(Ball * obj); void BALL_DestroyObject(Ball * obj); void BALL_Deinitialize(); +void PADDLE_Initialize(SDL_Renderer * renderer); +Paddle PADDLE_CreateDefault(); +void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj); +bool KeyPressed(Uint8 * keystate, Uint8 * keyArray); +void constrain(int * variable, int min, int max); +void PADDLE_Update(Paddle * obj, Uint8 * keystate); +void PADDLE_DestroyObject(Paddle * obj); +void PADDLE_Deinitialize(); + // End Prototypes #endif // __breakout_h__