diff --git a/.gitignore b/.gitignore index f8a2da4..33adfb7 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ sdl2-config *.exe !bhi.exe .tags* +*.txt diff --git a/Makefile b/Makefile index 2ae71aa..8c8184e 100644 --- a/Makefile +++ b/Makefile @@ -12,5 +12,4 @@ all: $(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target) run: - cd $(dir) && \ - $(target) + cd $(dir) && $(target) diff --git a/assets/images/ball.png b/bin/assets/images/ball.png similarity index 100% rename from assets/images/ball.png rename to bin/assets/images/ball.png diff --git a/bin/zlib1.dll b/bin/zlib1.dll index a364c4c..1aa57ce 100644 Binary files a/bin/zlib1.dll and b/bin/zlib1.dll differ diff --git a/breakout.c b/breakout.c index 47a7b3d..4c33667 100644 --- a/breakout.c +++ b/breakout.c @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -14,25 +16,69 @@ int BREAKOUT_BoxWidth, BREAKOUT_BoxHeight; SDL_Texture * BALL_Texture; +SDL_Rect * BALL_SourceRects; +Ball ball; void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ printf("Initializing Game...\n"); + srand(time(NULL)); BREAKOUT_BoxWidth = width; BREAKOUT_BoxHeight = height; - BALL_Texture = IMG_LoadTexture(renderer, "assets/images/ball.png"); if (!BALL_Texture) printf("Ball texture cannot be loaded!\n"); printf("Game initialized!\n"); } -void BREAKOUT_GAMELOOP(Uint8 * keystate){ - +void BREAKOUT_Update(Uint8 * keystate){ + BALL_Update(&ball); } -void BALL_DRAW(Ball * ball){ +void BREAKOUT_Draw(SDL_Renderer * renderer){ + BALL_Draw(renderer, &ball); } -void BREAKOUT_DEINITIALIZE(SDL_Renderer * renderer, int width, int height){ +void BREAKOUT_DEINITIALIZE(){ printf("De-initializing Game...\n"); SDL_DestroyTexture(BALL_Texture); printf("Game de-initialized!\n"); } + +void BALL_Initialize(SDL_Renderer * renderer){ + printf("Initializing Ball...\n"); + 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)); + BALL_SourceRects[0] = (SDL_Rect) {.x = 0, .y = 0, .w = 512, .h = 512 }; + ball = BALL_CreateDefault(); + printf("Ball initialized!\n"); +} + +Ball BALL_CreateDefault(){ + double rotation = (double)(rand() % 360); + + return (Ball) { + .Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }, + .Momentum = getScaledDirectionalUnitVector(rotation, 5), + .TargetRect = (SDL_Rect) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2, .w = 50, .h = 50 }, + .Size = 50.0f, + .Rotation = rotation, + .RotationValue = 2, + .TextureIndex = 0 + }; // Objekt für die Eigenschaften des Balls +} + +void BALL_Draw(SDL_Renderer * renderer, Ball * obj){ + 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->TargetRect).x = (int)round((obj->Location).x); + (obj->TargetRect).y = (int)round((obj->Location).y); +} +void BALL_DestroyObject(Ball * obj){ +} +void BALL_Deinitialize(){ + printf("De-initializing Ball...\n"); + + printf("Ball de-initialized!\n"); +} diff --git a/breakout.h b/breakout.h index f1eba4f..ce682d2 100644 --- a/breakout.h +++ b/breakout.h @@ -26,8 +26,15 @@ typedef struct blockStruct { // Prototypes void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height); -void BREAKOUT_GAMELOOP(Uint8 * keystate); -void BREAKOUT_DEINITIALIZE(SDL_Renderer * renderer, int width, int height); +void BREAKOUT_Update(Uint8 * keystate); +void BREAKOUT_Draw(SDL_Renderer * renderer); +void BREAKOUT_DEINITIALIZE(); +void BALL_Initialize(SDL_Renderer * renderer); +Ball BALL_CreateDefault(); +void BALL_Draw(SDL_Renderer * renderer, Ball * obj); +void BALL_Update(Ball * obj); +void BALL_DestroyObject(Ball * obj); +void BALL_Deinitialize(); // End Prototypes #endif // __breakout_h__ diff --git a/main.c b/main.c index e451d4e..031798c 100644 --- a/main.c +++ b/main.c @@ -64,6 +64,7 @@ int main(int argc, char * args[]){ void GAMELOOP() { keystate = SDL_GetKeyboardState(NULL); + BREAKOUT_Update(keystate); } /* GAMELOOP */ void mousePress(SDL_MouseButtonEvent b){ // Debug prop @@ -101,13 +102,9 @@ void windowChanged(SDL_WindowEvent b){ // Debug prop } void DrawFrame(){ - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); - // BALL_DRAW(); - SDL_RenderPresent(renderer); - + BREAKOUT_Draw(renderer); SDL_RenderPresent(renderer); }