From 10884240486ec5c8c9589d0a095505d3d8891b13 Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Wed, 27 Dec 2017 21:08:37 +0100 Subject: [PATCH] Updated z-layers for drawing. Changed box size to signle global variable --- asteroids.c | 65 ++++++++++++++++++++++++++++------------------------- main.c | 12 +++++----- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/asteroids.c b/asteroids.c index eef0266..e23ff30 100644 --- a/asteroids.c +++ b/asteroids.c @@ -41,7 +41,7 @@ const SDL_Scancode SHIP_ShootButton = SDL_SCANCODE_SPACE; // Vars Bullet NULLET; -int SHIP_BoxWidth, SHIP_BoxHeight, SHIP_MaxBulletCount, ASTEROID_BoxWidth, ASTEROID_BoxHeight, BULLET_BoxWidth, BULLET_BoxHeight, ENEMY_BoxWidth, ENEMY_BoxHeight, ENEMY_MaxBulletCount; // TODO: Explosion Texture +int Asteroids_BoxWidth, Asteroids_BoxHeight, SHIP_MaxBulletCount, ENEMY_MaxBulletCount; // TODO: Explosion Texture SDL_Rect SHIP_SourceRect_Gliding, SHIP_SourceRect_Accelerating, * SHIP_SourceRect_Explosion, * ASTEROID_SourceRect_Size1, * ASTEROID_SourceRect_Size2, * ASTEROID_SourceRect_Size3; SDL_Texture * SHIP_Texture_Gliding, * SHIP_Texture_Accelerating, * SHIP_Texture_Explosion, * ASTEROID_Texture, * BULLET_Texture_Ally, * BULLET_Texture_Enemy, * ENEMY_Texture; SDL_Point SHIP_RotationCenter_Flight, SHIP_RotationCenter_Explosion; @@ -60,8 +60,8 @@ void BULLET_Initialize(SDL_Renderer * renderer, int width, int height){ .IsDestroyed = true, .Type = AllyBullet }; // Default or null bullet - BULLET_BoxWidth = width; - BULLET_BoxHeight = height; + Asteroids_BoxWidth = width; + Asteroids_BoxHeight = height; BULLET_Texture_Ally = IMG_LoadTexture(renderer, "assets/images/bullet.png"); if (!BULLET_Texture_Ally) printf("Ally bullet texture cannot be loaded!\n"); BULLET_Texture_Enemy = IMG_LoadTexture(renderer, "assets/images/enemyBullet.png"); @@ -97,10 +97,10 @@ 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 = BULLET_BoxWidth; // Check Boundaries - else if ((bul->Location).x > BULLET_BoxWidth) (bul->Location).x = 0.0f; - if ((bul->Location).y < 0.0f) (bul->Location).y = BULLET_BoxHeight; - else if ((bul->Location).y > BULLET_BoxHeight) (bul->Location).y = 0.0f; + if ((bul->Location).x < 0.0f) (bul->Location).x = Asteroids_BoxWidth; // Check Boundaries + else if ((bul->Location).x > Asteroids_BoxWidth) (bul->Location).x = 0.0f; + if ((bul->Location).y < 0.0f) (bul->Location).y = Asteroids_BoxHeight; + else if ((bul->Location).y > Asteroids_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; @@ -124,11 +124,12 @@ void BULLET_UpdateEnemy(Bullet * bul, Ship * shp, int * shpCount) { (bul->IsDestroyed) = true; return; } + // TODO: Export triple use of boundary check to method using pointers (bul->Location) = vectorAdd((bul->Location), (bul->Momentum)); // Move - if ((bul->Location).x < 0.0f) (bul->Location).x = BULLET_BoxWidth; // Check Boundaries - else if ((bul->Location).x > BULLET_BoxWidth) (bul->Location).x = 0.0f; - if ((bul->Location).y < 0.0f) (bul->Location).y = BULLET_BoxHeight; - else if ((bul->Location).y > BULLET_BoxHeight) (bul->Location).y = 0.0f; + if ((bul->Location).x < 0.0f) (bul->Location).x = Asteroids_BoxWidth; // Check Boundaries + else if ((bul->Location).x > Asteroids_BoxWidth) (bul->Location).x = 0.0f; + if ((bul->Location).y < 0.0f) (bul->Location).y = Asteroids_BoxHeight; + else if ((bul->Location).y > Asteroids_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; @@ -157,8 +158,8 @@ void BULLET_Draw(Bullet * bul, SDL_Renderer * renderer) { void ASTEROID_Initialize(SDL_Renderer * renderer, int width, int height){ printf("Initializing Asteroid...\n"); - ASTEROID_BoxWidth = width; - ASTEROID_BoxHeight = height; + Asteroids_BoxWidth = width; + Asteroids_BoxHeight = height; ASTEROID_Texture = IMG_LoadTexture(renderer, "assets/images/asteroid.png"); ASTEROIDULLET = (Asteroid) { .Rotation = 0.0f, @@ -192,7 +193,8 @@ Asteroid ASTEROID_CreateRandom(){ .Rotation = (double)(rand() % 360), .Size = 200, .RotationValue = fmod((double)(rand()), 4.0f) - 2.0f, - // .Location = (_vector) {.x = (double)(rand() % ASTEROID_BoxWidth), .y = (double)(rand() % ASTEROID_BoxHeight) }, + // TODO: Reimplement random start pos here later + // .Location = (_vector) {.x = (double)(rand() % Asteroids_BoxWidth), .y = (double)(rand() % Asteroids_BoxHeight) }, .Location = (Vector) {.x = 0, .y = 0 }, .Momentum = getScaledDirectionalUnitVector((double)(rand() % 360), fmod((double)rand(), 2.0f) + 1.0f), .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 200, .h = 200 }, @@ -270,11 +272,12 @@ void ASTEROID_Deinitialize(){ 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 = ASTEROID_BoxWidth; // Check Boundaries - else if ((ast->Location).x > ASTEROID_BoxWidth) (ast->Location).x = -(ast->Size); - if ((ast->Location).y < -(ast->Size)) (ast->Location).y = ASTEROID_BoxHeight; - else if ((ast->Location).y > ASTEROID_BoxHeight) (ast->Location).y = -(ast->Size); + if ((ast->Location).x < -(ast->Size)) (ast->Location).x = Asteroids_BoxWidth; // Check Boundaries + else if ((ast->Location).x > Asteroids_BoxWidth) (ast->Location).x = -(ast->Size); + if ((ast->Location).y < -(ast->Size)) (ast->Location).y = Asteroids_BoxHeight; + else if ((ast->Location).y > Asteroids_BoxHeight) (ast->Location).y = -(ast->Size); (ast->TargetRect).x = (int)round((ast->Location).x); (ast->TargetRect).y = (int)round((ast->Location).y); } @@ -289,8 +292,8 @@ void ASTEROID_Draw(Asteroid * ast, SDL_Renderer * renderer){ void ENEMY_Initialize(SDL_Renderer * renderer, int width, int height){ printf("Initializing Enemy...\n"); BULLET_Initialize(renderer, width, height); - ENEMY_BoxWidth = width; - ENEMY_BoxHeight = height; + Asteroids_BoxWidth = width; + Asteroids_BoxHeight = height; ENEMY_Texture = IMG_LoadTexture(renderer, "assets/images/enemy.png"); if (!ENEMY_Texture) printf("Enemy texture cannot be loaded!\n"); ENEMY_MaxBulletCount = ((BULLET_DecayTime_Ally / ENEMY_ShootIntevale) + 10); @@ -305,7 +308,7 @@ Enemy ENEMY_GetDefault(){ for (i = 0; i < ENEMY_MaxBulletCount; i++) { temp.Bullets[i] = NULLET; // Nullet is set only if BULLET_Initialize() was called! } - temp.Location = (Vector) {.x = (double)(rand() % ENEMY_BoxWidth), .y = (double)(rand() % ENEMY_BoxHeight) }; + temp.Location = (Vector) {.x = (double)(rand() % Asteroids_BoxWidth), .y = (double)(rand() % Asteroids_BoxHeight) }; temp.Momentum = getScaledDirectionalUnitVector((rand() % 360), fmod((double)rand(), ENEMY_MaxSpeed / 2.0f) + (ENEMY_MaxSpeed / 2.0f)); temp.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 80, .h = 40 }; temp.WeaponCooldown = ENEMY_ShootIntevale; @@ -344,10 +347,10 @@ 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 = ENEMY_BoxWidth; - else if ((enm->Location).x > ENEMY_BoxWidth + (enm->TargetRect).w) (enm->Location).x = 0.0f; - if ((enm->Location).y < -(enm->TargetRect).h) (enm->Location).y = ENEMY_BoxHeight; - else if ((enm->Location).y > ENEMY_BoxHeight + (enm->TargetRect).h) (enm->Location).y = 0.0f; + if ((enm->Location).x < -(enm->TargetRect).w) (enm->Location).x = Asteroids_BoxWidth; + else if ((enm->Location).x > Asteroids_BoxWidth + (enm->TargetRect).w) (enm->Location).x = 0.0f; + if ((enm->Location).y < -(enm->TargetRect).h) (enm->Location).y = Asteroids_BoxHeight; + else if ((enm->Location).y > Asteroids_BoxHeight + (enm->TargetRect).h) (enm->Location).y = 0.0f; (enm->TargetRect).x = (int)round((enm->Location).x); (enm->TargetRect).y = (int)round((enm->Location).y); @@ -378,8 +381,8 @@ void ENEMY_Draw(Enemy * enm, SDL_Renderer * renderer){ void SHIP_Initialize(SDL_Renderer * renderer, int width, int height){ printf("Initializing Ship...\n"); BULLET_Initialize(renderer, width, height); - SHIP_BoxWidth = width; - SHIP_BoxHeight = height; + Asteroids_BoxWidth = width; + Asteroids_BoxHeight = height; SHIP_Texture_Gliding = IMG_LoadTexture(renderer, "assets/images/ship.png"); if (!SHIP_Texture_Gliding) printf("Ship texture 1 cannot be loaded!\n"); SHIP_Texture_Accelerating = IMG_LoadTexture(renderer, "assets/images/shipAcc.png"); @@ -471,10 +474,10 @@ 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 = SHIP_BoxWidth; - else if ((shp->Location).x > SHIP_BoxWidth + (shp->TargetRect_Gliding).w) (shp->Location).x = 0.0f; - if ((shp->Location).y < -(shp->TargetRect_Gliding).h) (shp->Location).y = SHIP_BoxHeight; - else if ((shp->Location).y > SHIP_BoxHeight + (shp->TargetRect_Gliding).h) (shp->Location).y = 0.0f; + if ((shp->Location).x < -(shp->TargetRect_Gliding).w) (shp->Location).x = Asteroids_BoxWidth; + else if ((shp->Location).x > Asteroids_BoxWidth + (shp->TargetRect_Gliding).w) (shp->Location).x = 0.0f; + if ((shp->Location).y < -(shp->TargetRect_Gliding).h) (shp->Location).y = Asteroids_BoxHeight; + else if ((shp->Location).y > Asteroids_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; diff --git a/main.c b/main.c index 01f239b..1178b00 100644 --- a/main.c +++ b/main.c @@ -93,15 +93,15 @@ void mousePress(SDL_MouseButtonEvent b){ void DrawFrame(){ SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); - STARFIELD_Draw(&starfield, renderer); - SHIP_Draw(&ship, renderer); + STARFIELD_Draw(&starfield, renderer); // Z-Layer Background int i; - for (i = 0; i < enemyCount; i++) { - ENEMY_Draw(&(enemies[i]), renderer); - } - for (i = 0; i < asteroidCount; i++) { + for (i = 0; i < asteroidCount; i++) { // Z-Layer Bottom ASTEROID_Draw(&(asteroids[i]), renderer); } + for (i = 0; i < enemyCount; i++) { // Z-Layer Mid + ENEMY_Draw(&(enemies[i]), renderer); + } + SHIP_Draw(&ship, renderer); // Z-Layer Top // SDL_RenderCopy(renderer, messageTexture, NULL, &messageRect); SDL_RenderPresent(renderer); }