From 98a74e0d442b70e9c3667d8f7e25bd217969a1a3 Mon Sep 17 00:00:00 2001 From: onpon4 Date: Thu, 30 Apr 2015 18:51:26 -0400 Subject: [PATCH] More code cleanup. --- Makefile | 2 +- src/Starfighter.h | 2 +- src/collectable.cpp | 2 +- src/{explosions.cpp => explosion.cpp} | 66 +------------------ src/{explosions.h => explosion.h} | 5 +- src/game.cpp | 91 +++++++++++++++++++++------ src/game.h | 1 + src/script.cpp | 4 +- src/ship.cpp | 2 +- src/title.cpp | 4 +- 10 files changed, 86 insertions(+), 93 deletions(-) rename src/{explosions.cpp => explosion.cpp} (51%) rename src/{explosions.h => explosion.h} (86%) diff --git a/Makefile b/Makefile index 84ea8f9..7d7806a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CXXFLAGS ?= -O2 -Wall -g CXXFLAGS += `pkg-config --cflags sdl2 SDL2_image SDL2_mixer` LIBS = `pkg-config --libs sdl2 SDL2_image SDL2_mixer` -OBJS = alien.o audio.o bullet.o cargo.o collectable.o explosions.o game.o globals.o graphics.o init.o intermission.o loadSave.o messages.o misc.o missions.o player.o resources.o script.o ship.o shop.o Starfighter.o title.o weapons.o +OBJS = alien.o audio.o bullet.o cargo.o collectable.o explosion.o game.o globals.o graphics.o init.o intermission.o loadSave.o messages.o misc.o missions.o player.o resources.o script.o ship.o shop.o Starfighter.o title.o weapons.o VERSION = 1.3.2-dev PROG = starfighter diff --git a/src/Starfighter.h b/src/Starfighter.h index 128506e..53b07dd 100644 --- a/src/Starfighter.h +++ b/src/Starfighter.h @@ -40,7 +40,7 @@ along with this program. If not, see . #include "bullet.h" #include "cargo.h" #include "collectable.h" -#include "explosions.h" +#include "explosion.h" #include "game.h" #include "globals.h" #include "graphics.h" diff --git a/src/collectable.cpp b/src/collectable.cpp index 6752553..19ae2cc 100644 --- a/src/collectable.cpp +++ b/src/collectable.cpp @@ -213,7 +213,7 @@ void collectable_explode(collectables *collectable) audio_playSound(SFX_EXPLOSION, collectable->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(collectable->x + rand() % 25 - rand() % 25, + explosion_add(collectable->x + rand() % 25 - rand() % 25, collectable->y + rand() % 25 - rand() % 25, E_BIG_EXPLOSION); player_checkShockDamage(collectable->x, collectable->y); diff --git a/src/explosions.cpp b/src/explosion.cpp similarity index 51% rename from src/explosions.cpp rename to src/explosion.cpp index d4de258..d56eef7 100644 --- a/src/explosions.cpp +++ b/src/explosion.cpp @@ -25,7 +25,7 @@ The "type" will actually be used as an explosion frame check. All explosion types have 4 images. The "thinktime" will be used to change frames on a 21, 14, 7 basis. */ -void addExplosion(float x, float y, int type) +void explosion_add(float x, float y, int type) { object *explosion = new object; @@ -45,7 +45,7 @@ void addExplosion(float x, float y, int type) * This very simply just adds a tiny explosion at the coordinate specified. * It creates a small engine like effect. */ -void addEngine(object *craft) +void explosion_addEngine(object *craft) { if (rand() % 2 == 0) return; @@ -54,65 +54,5 @@ void addEngine(object *craft) float y = craft->y + craft->engineY; y += RANDRANGE(-3, 3); - addExplosion(x, y, E_TINY_EXPLOSION); + explosion_add(x, y, E_TINY_EXPLOSION); } - -static bool isOnScreen(int x, int y, int w, int h) -{ - return (x + w > 0) && (x < 800) && (y + h > 0) && (y < 600); -} - -/* -Loops through active explosions and decrements their think time. -If their thinktime is divisable by 5, then the frame is changed to -the next one up (for example 0->1->2-3). When their think time is 0, -the explosion is killed off. -*/ -void doExplosions() -{ - object *prevExplosion = engine.explosionHead; - object *explosion = engine.explosionHead; - engine.explosionTail = engine.explosionHead; - - while (explosion->next != NULL) - { - explosion = explosion->next; - - if (explosion->active) - { - explosion->x += engine.ssx + engine.smx; - explosion->y += engine.ssy + engine.smy; - - if (isOnScreen((int)explosion->x, (int)explosion->y, explosion->image[0]->w, explosion->image[0]->h)) - blit(explosion->image[0], (int)explosion->x, (int)explosion->y); - - if(rand() % 7 == 0) - { - explosion->thinktime -= 7; - - if(explosion->thinktime < 1) - { - explosion->active = false; - } - else - { - explosion->face++; - explosion->image[0] = shape[explosion->face]; - } - } - } - - if (explosion->active) - { - prevExplosion = explosion; - engine.explosionTail = explosion; - } - else - { - prevExplosion->next = explosion->next; - delete explosion; - explosion = prevExplosion; - } - } -} - diff --git a/src/explosions.h b/src/explosion.h similarity index 86% rename from src/explosions.h rename to src/explosion.h index 1ae127c..95b80a7 100644 --- a/src/explosions.h +++ b/src/explosion.h @@ -20,8 +20,7 @@ along with this program. If not, see . #ifndef EXPLOSIONS_H #define EXPLOSIONS_H -extern void addExplosion(float x, float y, int type); -extern void addEngine(object *craft); -extern void doExplosions(); +void explosion_add(float x, float y, int type); +void explosion_addEngine(object *craft); #endif diff --git a/src/game.cpp b/src/game.cpp index 38d76b0..ae2d89e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -460,11 +460,11 @@ static void game_doBullets() if (bullet->id == WT_ROCKET) { - addExplosion(bullet->x, bullet->y, E_SMALL_EXPLOSION); + explosion_add(bullet->x, bullet->y, E_SMALL_EXPLOSION); } else if (bullet->id == WT_MICROROCKET) { - addExplosion(bullet->x, bullet->y, E_TINY_EXPLOSION); + explosion_add(bullet->x, bullet->y, E_TINY_EXPLOSION); } if ((bullet->flags & WF_AIMED)) @@ -561,7 +561,7 @@ static void game_doBullets() bullet->shield = 0; audio_playSound(SFX_EXPLOSION, bullet->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(bullet->x + RANDRANGE(-35, 35), + explosion_add(bullet->x + RANDRANGE(-35, 35), bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION); } @@ -573,9 +573,9 @@ static void game_doBullets() } if (bullet->id == WT_ROCKET) - addExplosion(bullet->x, bullet->y, E_BIG_EXPLOSION); + explosion_add(bullet->x, bullet->y, E_BIG_EXPLOSION); else - addExplosion(bullet->x, bullet->y, E_SMALL_EXPLOSION); + explosion_add(bullet->x, bullet->y, E_SMALL_EXPLOSION); } } } @@ -613,7 +613,7 @@ static void game_doBullets() bullet->shield = 0; audio_playSound(SFX_EXPLOSION, bullet->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(bullet->x + RANDRANGE(-35, 35), + explosion_add(bullet->x + RANDRANGE(-35, 35), bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION); } } @@ -626,9 +626,9 @@ static void game_doBullets() audio_playSound(SFX_HIT, player.x); if (bullet->id == WT_ROCKET) - addExplosion(bullet->x, bullet->y, E_BIG_EXPLOSION); + explosion_add(bullet->x, bullet->y, E_BIG_EXPLOSION); else - addExplosion(bullet->x, bullet->y, E_SMALL_EXPLOSION); + explosion_add(bullet->x, bullet->y, E_SMALL_EXPLOSION); } } } @@ -643,14 +643,14 @@ static void game_doBullets() if (collision(bullet, &cargo[j])) { bullet->active = false; - addExplosion(bullet->x, bullet->y, E_SMALL_EXPLOSION); + explosion_add(bullet->x, bullet->y, E_SMALL_EXPLOSION); audio_playSound(SFX_HIT, cargo[j].x); if (cargo[j].collectType != P_PHOEBE) { cargo[j].active = false; audio_playSound(SFX_EXPLOSION, cargo[j].x); for (int i = 0 ; i < 10 ; i++) - addExplosion(cargo[j].x + RANDRANGE(-15, 15), + explosion_add(cargo[j].x + RANDRANGE(-15, 15), cargo[j].y + RANDRANGE(-15, 15), E_BIG_EXPLOSION); updateMissionRequirements(M_PROTECT_PICKUP, @@ -716,7 +716,7 @@ static void game_doBullets() { audio_playSound(SFX_EXPLOSION, bullet->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(bullet->x + RANDRANGE(-35, 35), + explosion_add(bullet->x + RANDRANGE(-35, 35), bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION); player_checkShockDamage(bullet->x, bullet->y); @@ -1008,14 +1008,14 @@ static void game_doAliens() if ((!(aliens[i].flags & FL_DISABLED)) && (aliens[i].classDef != CD_ASTEROID) && (aliens[i].classDef != CD_ASTEROID2)) - addEngine(&aliens[i]); + explosion_addEngine(&aliens[i]); if ((!(aliens[i].flags & FL_ISCLOAKED)) || (aliens[i].hit > 0)) blit(shipShape[shapeToUse], (int)aliens[i].x, (int)aliens[i].y); if (aliens[i].flags & FL_DISABLED) { if ((rand() % 10) == 0) - addExplosion(aliens[i].x + (rand() % aliens[i].image[0]->w), + explosion_add(aliens[i].x + (rand() % aliens[i].image[0]->w), aliens[i].y + (rand() % aliens[i].image[0]->h), E_ELECTRICAL); } @@ -1032,7 +1032,7 @@ static void game_doAliens() { blit(aliens[i].image[aliens[i].face], (int)aliens[i].x, (int)aliens[i].y); - addExplosion(aliens[i].x + (rand() % aliens[i].image[0]->w), + explosion_add(aliens[i].x + (rand() % aliens[i].image[0]->w), aliens[i].y + (rand() % aliens[i].image[0]->h), E_BIG_EXPLOSION); } @@ -1293,7 +1293,7 @@ static void game_doPlayer() } if ((player.maxShield <= 1) || (player.shield > engine.lowShield)) - addEngine(&player); + explosion_addEngine(&player); shapeToUse = player.face; @@ -1305,7 +1305,7 @@ static void game_doPlayer() blit(shipShape[shapeToUse], (int)player.x, (int)player.y); if ((player.maxShield > 1) && (player.shield <= engine.lowShield) && (rand() % 5 < 1)) - addExplosion(player.x + RANDRANGE(-10, 10), + explosion_add(player.x + RANDRANGE(-10, 10), player.y + RANDRANGE(-10, 20), E_SMOKE); } else @@ -1330,7 +1330,7 @@ static void game_doPlayer() engine.keyState[KEY_UP] = engine.keyState[KEY_DOWN] = engine.keyState[KEY_LEFT] = engine.keyState[KEY_RIGHT] = 0; if ((rand() % 3) == 0) - addExplosion(player.x + RANDRANGE(-10, 10), + explosion_add(player.x + RANDRANGE(-10, 10), player.y + RANDRANGE(-10, 10), E_BIG_EXPLOSION); if (player.shield == -99) game_addDebris((int)player.x, (int)player.y, player.maxShield); @@ -1415,7 +1415,7 @@ static void game_doDebris() debris->x += debris->dx; debris->y += debris->dy; - addExplosion(debris->x + RANDRANGE(-10, 10), debris->y + RANDRANGE(-10, 10), E_BIG_EXPLOSION); + explosion_add(debris->x + RANDRANGE(-10, 10), debris->y + RANDRANGE(-10, 10), E_BIG_EXPLOSION); } if (debris->thinktime < 1) @@ -1433,6 +1433,59 @@ static void game_doDebris() } } +/* +Loops through active explosions and decrements their think time. +If their thinktime is divisable by 5, then the frame is changed to +the next one up (for example 0->1->2-3). When their think time is 0, +the explosion is killed off. +*/ +void game_doExplosions() +{ + object *prevExplosion = engine.explosionHead; + object *explosion = engine.explosionHead; + engine.explosionTail = engine.explosionHead; + + while (explosion->next != NULL) + { + explosion = explosion->next; + + if (explosion->active) + { + explosion->x += engine.ssx + engine.smx; + explosion->y += engine.ssy + engine.smy; + + blit(explosion->image[0], (int)explosion->x, (int)explosion->y); + + if(rand() % 7 == 0) + { + explosion->thinktime -= 7; + + if(explosion->thinktime < 1) + { + explosion->active = false; + } + else + { + explosion->face++; + explosion->image[0] = shape[explosion->face]; + } + } + } + + if (explosion->active) + { + prevExplosion = explosion; + engine.explosionTail = explosion; + } + else + { + prevExplosion->next = explosion->next; + delete explosion; + explosion = prevExplosion; + } + } +} + /* Checked during the main game loop. When the game is paused it goes into a constant loop checking this routine. If escape is @@ -1735,7 +1788,7 @@ int mainGameLoop() game_doPlayer(); game_doCargo(); game_doDebris(); - doExplosions(); + game_doExplosions(); doInfo(); WRAP_ADD(engine.eventTimer, -1, 0, 60); diff --git a/src/game.h b/src/game.h index 17eb3ae..b51ff83 100644 --- a/src/game.h +++ b/src/game.h @@ -23,6 +23,7 @@ along with this program. If not, see . extern Game currentGame; extern void newGame(); +void game_doExplosions(); extern int mainGameLoop(); #endif diff --git a/src/script.cpp b/src/script.cpp index d3664f5..de6b504 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -244,13 +244,13 @@ void doCutscene(int scene) unBuffer(); getPlayerInput(); doStarfield(); - doExplosions(); + game_doExplosions(); for (int i = 0 ; i < 15 ; i++) { if (aliens[i].active) { - addEngine(&aliens[i]); + explosion_addEngine(&aliens[i]); if (scene == 0 && i > 0 && (timer % 15) == i) { aliens[i].dx += (drand48() - 0.5) * 0.1; aliens[i].dy += (drand48() - 0.5) * 0.1; diff --git a/src/ship.cpp b/src/ship.cpp index da38f4e..7dff728 100644 --- a/src/ship.cpp +++ b/src/ship.cpp @@ -155,7 +155,7 @@ void ship_fireRay(object *ship) } player.shield--; - addExplosion(player.x, player.y, E_SMALL_EXPLOSION); + explosion_add(player.x, player.y, E_SMALL_EXPLOSION); audio_playSound(SFX_HIT, player.x); if (player.shield < 1) { diff --git a/src/title.cpp b/src/title.cpp index 690a8d7..5fb2b9b 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -276,11 +276,11 @@ int doTitle() now = SDL_GetTicks(); doStarfield(); - doExplosions(); + game_doExplosions(); for (int i = 0 ; i < 15 ; i++) { - addEngine(&aliens[i]); + explosion_addEngine(&aliens[i]); aliens[i].x += aliens[i].dx; blit(aliens[i].image[0], (int)aliens[i].x, (int)aliens[i].y); if (aliens[i].x > 830)