Windows Boundary Check exported as function (3 calls)

This commit is contained in:
Michael Chen 2017-12-28 22:08:29 +01:00
parent 39d6786dc0
commit cab35cb5e0
2 changed files with 22 additions and 33 deletions

View File

@ -225,12 +225,7 @@ void BULLET_UpdateAlly(Bullet * bul, Asteroid * asts, int * astCount, Enemy * en
return; return;
} }
(bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move (bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move
if ((bul->Location).x < 0.0f) (bul->Location).x = GAME_BoxWidth; // Check Boundaries RECT_CheckBoundaries(&(bul->Location), &(bul->TargetRect)); // 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);
int i; int i;
for (i = 0; i < (*astCount); i++) { // Collide with Asteroids for (i = 0; i < (*astCount); i++) { // Collide with Asteroids
if (!(asts[i].IsDestroyed)) { 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)); 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 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 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))); 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){ void ASTEROID_Update(Asteroid * ast){
if (!(ast->IsDestroyed)) { if (!(ast->IsDestroyed)) {
(ast->Rotation) += (ast->RotationValue); // Rotate (ast->Rotation) += (ast->RotationValue); // Rotate
(ast->Location) = vectorAdd(ast->Location, ast->Momentum); // Update Position (ast->Location) = vectorAdd(ast->Location, ast->Momentum); // Update Position
if ((ast->Location).x < -(ast->Size)) (ast->Location).x = GAME_BoxWidth; // Check Boundaries RECT_CheckBoundaries(&(ast->Location), &(ast->TargetRect)); // Reset position if object exits window
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);
} }
} // ASTEROID_Update } // ASTEROID_Update
@ -414,10 +412,8 @@ void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount){
} }
(enm->Location) = vectorAdd((enm->Location), (enm->Momentum)); // Update Position (enm->Location) = vectorAdd((enm->Location), (enm->Momentum)); // Update Position
if ((enm->Location).x < -(enm->TargetRect).w) (enm->Location).x = GAME_BoxWidth; RECT_CheckBoundaries(&(enm->Location), &(enm->TargetRect)); // Check Boundaries
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;
(enm->TargetRect).x = (int)round((enm->Location).x); (enm->TargetRect).x = (int)round((enm->Location).x);
(enm->TargetRect).y = (int)round((enm->Location).y); (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 (shp->Location) = vectorAdd((shp->Location), (shp->Momentum)); // Update Position
if ((shp->Location).x < -(shp->TargetRect_Gliding).w) (shp->Location).x = GAME_BoxWidth; RECT_CheckBoundaries(&(shp->Location), &(shp->TargetRect_Gliding)); // Check Boundaries
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);
(shp->TargetRect_Accelerating).x = (shp->TargetRect_Gliding).x; (shp->TargetRect_Accelerating).x = (shp->TargetRect_Gliding).x;
(shp->TargetRect_Accelerating).y = (shp->TargetRect_Gliding).y; (shp->TargetRect_Accelerating).y = (shp->TargetRect_Gliding).y;

View File

@ -47,30 +47,28 @@ typedef struct asteroidStruct {
void ASTEROIDS_InitializeGame(SDL_Renderer * renderer, int width, int height); void ASTEROIDS_InitializeGame(SDL_Renderer * renderer, int width, int height);
void ASTEROIDS_DeinitializeGame(); void ASTEROIDS_DeinitializeGame();
void BULLET_Initialize(SDL_Renderer * renderer); void BULLET_Initialize(SDL_Renderer * renderer);
void BULLET_Deinitialize(); void ENEMY_Initialize(SDL_Renderer * renderer);
Bullet BULLET_Fire(Vector location, double direction, BulletType type); void SHIP_Initialize(SDL_Renderer * renderer);
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 ASTEROID_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(); Asteroid ASTEROID_CreateRandom();
bool ASTEROID_TryDestroy(Asteroid * asts, int * astCount, int index); bool ASTEROID_TryDestroy(Asteroid * asts, int * astCount, int index);
Asteroid ASTEROID_CreateFromSplit(Asteroid * creator); Asteroid ASTEROID_CreateFromSplit(Asteroid * creator);
bool ASTEROID_CanSplit(Asteroid * ast); bool ASTEROID_CanSplit(Asteroid * ast);
bool RECT_CircleCollide(SDL_Rect * rect1, SDL_Rect * rect2); 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); double distance(SDL_Rect * r1, SDL_Rect * r2);
void ASTEROID_Deinitialize();
void ASTEROID_Update(Asteroid * ast); void ASTEROID_Update(Asteroid * ast);
void ASTEROID_Draw(Asteroid * ast, SDL_Renderer * renderer); void ASTEROID_Draw(Asteroid * ast, SDL_Renderer * renderer);
void ENEMY_Initialize(SDL_Renderer * renderer);
Enemy ENEMY_GetDefault(); Enemy ENEMY_GetDefault();
void ENEMY_Deinitialize();
void ENEMY_DestroyObject(Enemy * enm); void ENEMY_DestroyObject(Enemy * enm);
void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount); void ENEMY_Update(Enemy * enm, Ship * targets, int targetCount);
void ENEMY_Draw(Enemy * enm, SDL_Renderer * renderer); void ENEMY_Draw(Enemy * enm, SDL_Renderer * renderer);
void SHIP_Initialize(SDL_Renderer * renderer);
Ship SHIP_CreateDefault(); Ship SHIP_CreateDefault();
void SHIP_Deinitialize();
void SHIP_DestroyObject(Ship * shp); void SHIP_DestroyObject(Ship * shp);
void SHIP_Update(Ship * shp, Uint8 * keystate, Asteroid * asts, int * astCount, Enemy * enemies, int * enemyCount); void SHIP_Update(Ship * shp, Uint8 * keystate, Asteroid * asts, int * astCount, Enemy * enemies, int * enemyCount);
void SHIP_Draw(Ship * shp, SDL_Renderer * renderer); void SHIP_Draw(Ship * shp, SDL_Renderer * renderer);