From cab35cb5e0a860b89dc315f0072de9e8a8695416 Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Thu, 28 Dec 2017 22:08:29 +0100 Subject: [PATCH] Windows Boundary Check exported as function (3 calls) --- asteroids.c | 37 ++++++++++++++----------------------- asteroids.h | 18 ++++++++---------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/asteroids.c b/asteroids.c index 00c89d4..69caff1 100644 --- a/asteroids.c +++ b/asteroids.c @@ -225,12 +225,7 @@ void BULLET_UpdateAlly(Bullet * bul, Asteroid * asts, int * astCount, Enemy * en return; } (bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move - if ((bul->Location).x < 0.0f) (bul->Location).x = GAME_BoxWidth; // Check Boundaries - else if ((bul->Location).x > GAME_BoxWidth) (bul->Location).x = 0.0f; - if ((bul->Location).y < 0.0f) (bul->Location).y = GAME_BoxHeight; - else if ((bul->Location).y > GAME_BoxHeight) (bul->Location).y = 0.0f; - (bul->TargetRect).x = (int)round((bul->Location).x); // Update render Target - (bul->TargetRect).y = (int)round((bul->Location).y); + RECT_CheckBoundaries(&(bul->Location), &(bul->TargetRect)); // Check Boundaries int i; for (i = 0; i < (*astCount); i++) { // Collide with Asteroids if (!(asts[i].IsDestroyed)) { @@ -348,6 +343,15 @@ bool RECT_CircleCollide(SDL_Rect * rect1, SDL_Rect * rect2){ return (distance(rect1, rect2) < round(((double)(rect1->w + rect2->w)) / 2.0f)); } +void RECT_CheckBoundaries(Vector * location, SDL_Rect * Target){ + if ((location->x) < -(Target->w)) (location->x) = GAME_BoxWidth; // Check Boundaries + else if ((location->x) > GAME_BoxWidth) (location->x) = -(Target->w); + if ((location->y) < -(Target->h)) (location->x) = GAME_BoxHeight; + else if ((location->y) > GAME_BoxHeight) (location->y) = -(Target->h); + (Target->x) = (int)round((location->x)); + (Target->y) = (int)round((location->y)); +} + double distance(SDL_Rect * r1, SDL_Rect * r2){ double dx = (((double)(r2->x) + ((double)(r2->w) / 2.0f)) - ((double)(r1->x) + ((double)(r1->w) / 2.0f))); double dy = (((double)(r2->y) + ((double)(r2->h) / 2.0f)) - ((double)(r1->y) + ((double)(r1->h) / 2.0f))); @@ -358,14 +362,8 @@ double distance(SDL_Rect * r1, SDL_Rect * r2){ void ASTEROID_Update(Asteroid * ast){ if (!(ast->IsDestroyed)) { (ast->Rotation) += (ast->RotationValue); // Rotate - (ast->Location) = vectorAdd(ast->Location, ast->Momentum); // Update Position - if ((ast->Location).x < -(ast->Size)) (ast->Location).x = GAME_BoxWidth; // Check Boundaries - else if ((ast->Location).x > GAME_BoxWidth) (ast->Location).x = -(ast->Size); - if ((ast->Location).y < -(ast->Size)) (ast->Location).y = GAME_BoxHeight; - else if ((ast->Location).y > GAME_BoxHeight) (ast->Location).y = -(ast->Size); - (ast->TargetRect).x = (int)round((ast->Location).x); - (ast->TargetRect).y = (int)round((ast->Location).y); + RECT_CheckBoundaries(&(ast->Location), &(ast->TargetRect)); // Reset position if object exits window } } // ASTEROID_Update @@ -414,10 +412,8 @@ void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount){ } (enm->Location) = vectorAdd((enm->Location), (enm->Momentum)); // Update Position - if ((enm->Location).x < -(enm->TargetRect).w) (enm->Location).x = GAME_BoxWidth; - else if ((enm->Location).x > GAME_BoxWidth + (enm->TargetRect).w) (enm->Location).x = 0.0f; - if ((enm->Location).y < -(enm->TargetRect).h) (enm->Location).y = GAME_BoxHeight; - else if ((enm->Location).y > GAME_BoxHeight + (enm->TargetRect).h) (enm->Location).y = 0.0f; + RECT_CheckBoundaries(&(enm->Location), &(enm->TargetRect)); // Check Boundaries + (enm->TargetRect).x = (int)round((enm->Location).x); (enm->TargetRect).y = (int)round((enm->Location).y); @@ -504,12 +500,7 @@ void SHIP_Update(Ship * shp, Uint8 * keystate, Asteroid * asts, int * astCount, } (shp->Location) = vectorAdd((shp->Location), (shp->Momentum)); // Update Position - if ((shp->Location).x < -(shp->TargetRect_Gliding).w) (shp->Location).x = GAME_BoxWidth; - else if ((shp->Location).x > GAME_BoxWidth + (shp->TargetRect_Gliding).w) (shp->Location).x = 0.0f; - if ((shp->Location).y < -(shp->TargetRect_Gliding).h) (shp->Location).y = GAME_BoxHeight; - else if ((shp->Location).y > GAME_BoxHeight + (shp->TargetRect_Gliding).h) (shp->Location).y = 0.0f; - (shp->TargetRect_Gliding).x = (int)round((shp->Location).x); - (shp->TargetRect_Gliding).y = (int)round((shp->Location).y); + RECT_CheckBoundaries(&(shp->Location), &(shp->TargetRect_Gliding)); // Check Boundaries (shp->TargetRect_Accelerating).x = (shp->TargetRect_Gliding).x; (shp->TargetRect_Accelerating).y = (shp->TargetRect_Gliding).y; diff --git a/asteroids.h b/asteroids.h index fe37d08..51a4db1 100644 --- a/asteroids.h +++ b/asteroids.h @@ -47,30 +47,28 @@ typedef struct asteroidStruct { void ASTEROIDS_InitializeGame(SDL_Renderer * renderer, int width, int height); void ASTEROIDS_DeinitializeGame(); void BULLET_Initialize(SDL_Renderer * renderer); -void BULLET_Deinitialize(); -Bullet BULLET_Fire(Vector location, double direction, BulletType type); -void BULLET_UpdateAlly(Bullet * bul, Asteroid * asts, int * astCount, Enemy * enemies, int * enemyCount); -void BULLET_UpdateEnemy(Bullet * bul, Ship * shp, int * shpCount); -void BULLET_Draw(Bullet * bul, SDL_Renderer * renderer); +void ENEMY_Initialize(SDL_Renderer * renderer); +void SHIP_Initialize(SDL_Renderer * renderer); void ASTEROID_Initialize(SDL_Renderer * renderer); +void BULLET_Deinitialize(); +void ENEMY_Deinitialize(); +void ASTEROID_Deinitialize(); +void SHIP_Deinitialize(); +Bullet BULLET_Fire(Vector location, double direction, BulletType type); Asteroid ASTEROID_CreateRandom(); bool ASTEROID_TryDestroy(Asteroid * asts, int * astCount, int index); Asteroid ASTEROID_CreateFromSplit(Asteroid * creator); bool ASTEROID_CanSplit(Asteroid * ast); bool RECT_CircleCollide(SDL_Rect * rect1, SDL_Rect * rect2); +void RECT_CheckBoundaries(Vector * location, SDL_Rect * Target); double distance(SDL_Rect * r1, SDL_Rect * r2); -void ASTEROID_Deinitialize(); void ASTEROID_Update(Asteroid * ast); void ASTEROID_Draw(Asteroid * ast, SDL_Renderer * renderer); -void ENEMY_Initialize(SDL_Renderer * renderer); Enemy ENEMY_GetDefault(); -void ENEMY_Deinitialize(); void ENEMY_DestroyObject(Enemy * enm); void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount); void ENEMY_Draw(Enemy * enm, SDL_Renderer * renderer); -void SHIP_Initialize(SDL_Renderer * renderer); Ship SHIP_CreateDefault(); -void SHIP_Deinitialize(); void SHIP_DestroyObject(Ship * shp); void SHIP_Update(Ship * shp, Uint8 * keystate, Asteroid * asts, int * astCount, Enemy * enemies, int * enemyCount); void SHIP_Draw(Ship * shp, SDL_Renderer * renderer);