From f136e07b437748f4ead64c086ae30fc177e0c5b0 Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Wed, 10 Jan 2018 23:56:47 +0100 Subject: [PATCH] Basic Ball functionality --- breakout.c | 8 ++++++-- main.c | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/breakout.c b/breakout.c index 4c33667..f763558 100644 --- a/breakout.c +++ b/breakout.c @@ -24,7 +24,7 @@ void BREAKOUT_INITIALIZE(SDL_Renderer * renderer, int width, int height){ srand(time(NULL)); BREAKOUT_BoxWidth = width; BREAKOUT_BoxHeight = height; - if (!BALL_Texture) printf("Ball texture cannot be loaded!\n"); + BALL_Initialize(renderer); printf("Game initialized!\n"); } @@ -57,7 +57,7 @@ Ball BALL_CreateDefault(){ return (Ball) { .Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }, - .Momentum = getScaledDirectionalUnitVector(rotation, 5), + .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, .Rotation = rotation, @@ -67,6 +67,7 @@ Ball BALL_CreateDefault(){ } void BALL_Draw(SDL_Renderer * renderer, Ball * obj){ + 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); } @@ -74,6 +75,9 @@ 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); + obj->Rotation += obj->RotationValue; + if ((obj->Location).y > BREAKOUT_BoxHeight) + obj->Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }; } void BALL_DestroyObject(Ball * obj){ } diff --git a/main.c b/main.c index 031798c..47ca155 100644 --- a/main.c +++ b/main.c @@ -38,8 +38,6 @@ int main(int argc, char * args[]){ // system("bhi.exe"); INITIALIZE(); while (running) { // Gameloop - GAMELOOP(); - DrawFrame(); while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: @@ -57,6 +55,8 @@ int main(int argc, char * args[]){ break; } } + GAMELOOP(); + DrawFrame(); } QUIT(); return 0; @@ -121,10 +121,12 @@ void INITIALIZE() { printf("Window was created!\n"); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); printf("Renderer was created!\n"); + BREAKOUT_INITIALIZE(renderer, width, height); } /* INITIALIZE */ void QUIT(){ printf("De-initializing started...\n"); + BREAKOUT_DEINITIALIZE(); free(keystate); TTF_Quit(); IMG_Quit();