Updated z-layers for drawing. Changed box size to signle global variable

This commit is contained in:
Michael Chen 2017-12-27 21:08:37 +01:00
parent 428a21fb6d
commit 1088424048
2 changed files with 40 additions and 37 deletions

View File

@ -41,7 +41,7 @@ const SDL_Scancode SHIP_ShootButton = SDL_SCANCODE_SPACE;
// Vars // Vars
Bullet NULLET; 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_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_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; SDL_Point SHIP_RotationCenter_Flight, SHIP_RotationCenter_Explosion;
@ -60,8 +60,8 @@ void BULLET_Initialize(SDL_Renderer * renderer, int width, int height){
.IsDestroyed = true, .IsDestroyed = true,
.Type = AllyBullet .Type = AllyBullet
}; // Default or null bullet }; // Default or null bullet
BULLET_BoxWidth = width; Asteroids_BoxWidth = width;
BULLET_BoxHeight = height; Asteroids_BoxHeight = height;
BULLET_Texture_Ally = IMG_LoadTexture(renderer, "assets/images/bullet.png"); BULLET_Texture_Ally = IMG_LoadTexture(renderer, "assets/images/bullet.png");
if (!BULLET_Texture_Ally) printf("Ally bullet texture cannot be loaded!\n"); if (!BULLET_Texture_Ally) printf("Ally bullet texture cannot be loaded!\n");
BULLET_Texture_Enemy = IMG_LoadTexture(renderer, "assets/images/enemyBullet.png"); 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; 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 = BULLET_BoxWidth; // Check Boundaries if ((bul->Location).x < 0.0f) (bul->Location).x = Asteroids_BoxWidth; // Check Boundaries
else if ((bul->Location).x > BULLET_BoxWidth) (bul->Location).x = 0.0f; else if ((bul->Location).x > Asteroids_BoxWidth) (bul->Location).x = 0.0f;
if ((bul->Location).y < 0.0f) (bul->Location).y = BULLET_BoxHeight; if ((bul->Location).y < 0.0f) (bul->Location).y = Asteroids_BoxHeight;
else if ((bul->Location).y > BULLET_BoxHeight) (bul->Location).y = 0.0f; 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).x = (int)round((bul->Location).x); // Update render Target
(bul->TargetRect).y = (int)round((bul->Location).y); (bul->TargetRect).y = (int)round((bul->Location).y);
int i; int i;
@ -124,11 +124,12 @@ void BULLET_UpdateEnemy(Bullet * bul, Ship * shp, int * shpCount) {
(bul->IsDestroyed) = true; (bul->IsDestroyed) = true;
return; return;
} }
// TODO: Export triple use of boundary check to method using pointers
(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 = BULLET_BoxWidth; // Check Boundaries if ((bul->Location).x < 0.0f) (bul->Location).x = Asteroids_BoxWidth; // Check Boundaries
else if ((bul->Location).x > BULLET_BoxWidth) (bul->Location).x = 0.0f; else if ((bul->Location).x > Asteroids_BoxWidth) (bul->Location).x = 0.0f;
if ((bul->Location).y < 0.0f) (bul->Location).y = BULLET_BoxHeight; if ((bul->Location).y < 0.0f) (bul->Location).y = Asteroids_BoxHeight;
else if ((bul->Location).y > BULLET_BoxHeight) (bul->Location).y = 0.0f; 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).x = (int)round((bul->Location).x); // Update render Target
(bul->TargetRect).y = (int)round((bul->Location).y); (bul->TargetRect).y = (int)round((bul->Location).y);
int i; int i;
@ -157,8 +158,8 @@ void BULLET_Draw(Bullet * bul, SDL_Renderer * renderer) {
void ASTEROID_Initialize(SDL_Renderer * renderer, int width, int height){ void ASTEROID_Initialize(SDL_Renderer * renderer, int width, int height){
printf("Initializing Asteroid...\n"); printf("Initializing Asteroid...\n");
ASTEROID_BoxWidth = width; Asteroids_BoxWidth = width;
ASTEROID_BoxHeight = height; Asteroids_BoxHeight = height;
ASTEROID_Texture = IMG_LoadTexture(renderer, "assets/images/asteroid.png"); ASTEROID_Texture = IMG_LoadTexture(renderer, "assets/images/asteroid.png");
ASTEROIDULLET = (Asteroid) { ASTEROIDULLET = (Asteroid) {
.Rotation = 0.0f, .Rotation = 0.0f,
@ -192,7 +193,8 @@ Asteroid ASTEROID_CreateRandom(){
.Rotation = (double)(rand() % 360), .Rotation = (double)(rand() % 360),
.Size = 200, .Size = 200,
.RotationValue = fmod((double)(rand()), 4.0f) - 2.0f, .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 }, .Location = (Vector) {.x = 0, .y = 0 },
.Momentum = getScaledDirectionalUnitVector((double)(rand() % 360), fmod((double)rand(), 2.0f) + 1.0f), .Momentum = getScaledDirectionalUnitVector((double)(rand() % 360), fmod((double)rand(), 2.0f) + 1.0f),
.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 200, .h = 200 }, .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 200, .h = 200 },
@ -270,11 +272,12 @@ void ASTEROID_Deinitialize(){
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 = ASTEROID_BoxWidth; // Check Boundaries if ((ast->Location).x < -(ast->Size)) (ast->Location).x = Asteroids_BoxWidth; // Check Boundaries
else if ((ast->Location).x > ASTEROID_BoxWidth) (ast->Location).x = -(ast->Size); else if ((ast->Location).x > Asteroids_BoxWidth) (ast->Location).x = -(ast->Size);
if ((ast->Location).y < -(ast->Size)) (ast->Location).y = ASTEROID_BoxHeight; if ((ast->Location).y < -(ast->Size)) (ast->Location).y = Asteroids_BoxHeight;
else if ((ast->Location).y > ASTEROID_BoxHeight) (ast->Location).y = -(ast->Size); else if ((ast->Location).y > Asteroids_BoxHeight) (ast->Location).y = -(ast->Size);
(ast->TargetRect).x = (int)round((ast->Location).x); (ast->TargetRect).x = (int)round((ast->Location).x);
(ast->TargetRect).y = (int)round((ast->Location).y); (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){ void ENEMY_Initialize(SDL_Renderer * renderer, int width, int height){
printf("Initializing Enemy...\n"); printf("Initializing Enemy...\n");
BULLET_Initialize(renderer, width, height); BULLET_Initialize(renderer, width, height);
ENEMY_BoxWidth = width; Asteroids_BoxWidth = width;
ENEMY_BoxHeight = height; Asteroids_BoxHeight = height;
ENEMY_Texture = IMG_LoadTexture(renderer, "assets/images/enemy.png"); ENEMY_Texture = IMG_LoadTexture(renderer, "assets/images/enemy.png");
if (!ENEMY_Texture) printf("Enemy texture cannot be loaded!\n"); if (!ENEMY_Texture) printf("Enemy texture cannot be loaded!\n");
ENEMY_MaxBulletCount = ((BULLET_DecayTime_Ally / ENEMY_ShootIntevale) + 10); ENEMY_MaxBulletCount = ((BULLET_DecayTime_Ally / ENEMY_ShootIntevale) + 10);
@ -305,7 +308,7 @@ Enemy ENEMY_GetDefault(){
for (i = 0; i < ENEMY_MaxBulletCount; i++) { for (i = 0; i < ENEMY_MaxBulletCount; i++) {
temp.Bullets[i] = NULLET; // Nullet is set only if BULLET_Initialize() was called! 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.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.TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 80, .h = 40 };
temp.WeaponCooldown = ENEMY_ShootIntevale; 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 (enm->Location) = vectorAdd((enm->Location), (enm->Momentum)); // Update Position
if ((enm->Location).x < -(enm->TargetRect).w) (enm->Location).x = ENEMY_BoxWidth; if ((enm->Location).x < -(enm->TargetRect).w) (enm->Location).x = Asteroids_BoxWidth;
else if ((enm->Location).x > ENEMY_BoxWidth + (enm->TargetRect).w) (enm->Location).x = 0.0f; 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 = ENEMY_BoxHeight; if ((enm->Location).y < -(enm->TargetRect).h) (enm->Location).y = Asteroids_BoxHeight;
else if ((enm->Location).y > ENEMY_BoxHeight + (enm->TargetRect).h) (enm->Location).y = 0.0f; 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).x = (int)round((enm->Location).x);
(enm->TargetRect).y = (int)round((enm->Location).y); (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){ void SHIP_Initialize(SDL_Renderer * renderer, int width, int height){
printf("Initializing Ship...\n"); printf("Initializing Ship...\n");
BULLET_Initialize(renderer, width, height); BULLET_Initialize(renderer, width, height);
SHIP_BoxWidth = width; Asteroids_BoxWidth = width;
SHIP_BoxHeight = height; Asteroids_BoxHeight = height;
SHIP_Texture_Gliding = IMG_LoadTexture(renderer, "assets/images/ship.png"); SHIP_Texture_Gliding = IMG_LoadTexture(renderer, "assets/images/ship.png");
if (!SHIP_Texture_Gliding) printf("Ship texture 1 cannot be loaded!\n"); if (!SHIP_Texture_Gliding) printf("Ship texture 1 cannot be loaded!\n");
SHIP_Texture_Accelerating = IMG_LoadTexture(renderer, "assets/images/shipAcc.png"); 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 (shp->Location) = vectorAdd((shp->Location), (shp->Momentum)); // Update Position
if ((shp->Location).x < -(shp->TargetRect_Gliding).w) (shp->Location).x = SHIP_BoxWidth; if ((shp->Location).x < -(shp->TargetRect_Gliding).w) (shp->Location).x = Asteroids_BoxWidth;
else if ((shp->Location).x > SHIP_BoxWidth + (shp->TargetRect_Gliding).w) (shp->Location).x = 0.0f; 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 = SHIP_BoxHeight; if ((shp->Location).y < -(shp->TargetRect_Gliding).h) (shp->Location).y = Asteroids_BoxHeight;
else if ((shp->Location).y > SHIP_BoxHeight + (shp->TargetRect_Gliding).h) (shp->Location).y = 0.0f; 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).x = (int)round((shp->Location).x);
(shp->TargetRect_Gliding).y = (int)round((shp->Location).y); (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;

12
main.c
View File

@ -93,15 +93,15 @@ void mousePress(SDL_MouseButtonEvent b){
void DrawFrame(){ void DrawFrame(){
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
STARFIELD_Draw(&starfield, renderer); STARFIELD_Draw(&starfield, renderer); // Z-Layer Background
SHIP_Draw(&ship, renderer);
int i; int i;
for (i = 0; i < enemyCount; i++) { for (i = 0; i < asteroidCount; i++) { // Z-Layer Bottom
ENEMY_Draw(&(enemies[i]), renderer);
}
for (i = 0; i < asteroidCount; i++) {
ASTEROID_Draw(&(asteroids[i]), renderer); 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_RenderCopy(renderer, messageTexture, NULL, &messageRect);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }