From 6644be553eecda76bc1e53af70f8ce3215ce15f9 Mon Sep 17 00:00:00 2001 From: Julie Marchant Date: Sun, 19 May 2019 13:40:50 -0400 Subject: [PATCH 1/2] Converted some new and delete calls to malloc and free. This is the last step to converting Starfighter to C. Almost there! --- src/alien.cpp | 9 +++++++-- src/bullet.cpp | 4 +++- src/engine.cpp | 26 ++++++++++++++++++++++++-- src/engine.h | 2 ++ src/game.cpp | 2 +- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/alien.cpp b/src/alien.cpp index 8077b75..be00488 100644 --- a/src/alien.cpp +++ b/src/alien.cpp @@ -1073,7 +1073,12 @@ int alien_add() int *alienArray; int numberOfAliens = 1; - alienArray = new int[8]; + alienArray = (int*)malloc(8 * sizeof(int)); + if (alienArray == NULL) + { + engine_warn("WARNING: Failed to allocate memory for aliens"); + return 0; + } switch(game.area) { @@ -1192,7 +1197,7 @@ int alien_add() randEnemy = CD_TRANSPORTSHIP; } - delete[] alienArray; + free(alienArray); aliens[index] = alien_defs[randEnemy]; aliens[index].active = 1; diff --git a/src/bullet.cpp b/src/bullet.cpp index 0a5a49a..17aee1f 100644 --- a/src/bullet.cpp +++ b/src/bullet.cpp @@ -34,7 +34,9 @@ void bullet_add(Object *theWeapon, Object *attacker, int y, int dy) int imageIndex; int tempX, tempY, steps; - bullet = new Object; + bullet = (Object*)malloc(sizeof(Object)); + if (bullet == NULL) + return; if (attacker == &player) game.shots++; diff --git a/src/engine.cpp b/src/engine.cpp index 9584f53..e2124aa 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -60,7 +60,11 @@ void engine_init() engine.smx = 0; engine.smy = 0; - engine.bulletHead = new Object; + engine.bulletHead = (Object*)malloc(sizeof(Object)); + if (engine.bulletHead == NULL) + { + engine_error("Failed to allocate memory for bullet head."); + } engine.bulletHead->next = NULL; engine.bulletTail = engine.bulletHead; @@ -154,6 +158,24 @@ void engine_showError(int errorId, const char *name) exit(1); } +/* +Show a warning. Used when non-fatal things go wrong. +*/ +void engine_warn(const char *msg) +{ + printf("WARNING: %s", msg); +} + +/* +Show an error and exit. Used for critical errors that should definitely +never happen. +*/ +void engine_error(const char *msg) +{ + printf("ERROR: %s\nAborting", msg); + exit(1); +} + /* This gets the user's home directory, then creates the config directory. */ @@ -359,7 +381,7 @@ void engine_cleanup() SDL_FreeSurface(gfx_background); audio_free(); engine_resetLists(); - delete(engine.bulletHead); + free(engine.bulletHead); delete(engine.explosionHead); delete(engine.collectableHead); delete(screen_bufferHead); diff --git a/src/engine.h b/src/engine.h index 574ffde..143e27b 100644 --- a/src/engine.h +++ b/src/engine.h @@ -108,6 +108,8 @@ extern Engine engine; void engine_init(); void engine_showError(int errorId, const char *name); +void engine_warn(const char *msg); +void engine_error(const char *msg); void engine_setupConfigDirectory(); void engine_setMode(); void engine_resetLists(); diff --git a/src/game.cpp b/src/game.cpp index a97c5e1..907d9c4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -935,7 +935,7 @@ static void game_doBullets() else { prevBullet->next = bullet->next; - delete bullet; + free(bullet); bullet = prevBullet; } } From 8fc30e61f3cea588a0fc1b82d833ce730a6c56d7 Mon Sep 17 00:00:00 2001 From: Julie Marchant Date: Sun, 19 May 2019 18:22:53 -0400 Subject: [PATCH 2/2] Finished conversion to malloc/free. --- src/collectable.cpp | 7 ++++++- src/engine.cpp | 34 +++++++++++++++++++++++----------- src/explosion.cpp | 7 ++++++- src/game.cpp | 10 +++++----- src/gfx.cpp | 6 +++++- src/screen.cpp | 6 +++--- 6 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/collectable.cpp b/src/collectable.cpp index d7ac000..3282123 100644 --- a/src/collectable.cpp +++ b/src/collectable.cpp @@ -143,7 +143,12 @@ void collectable_add(float x, float y, int type, int value, int life) } } - Collectable *collectable = new Collectable; + Collectable *collectable = (Collectable*)malloc(sizeof(Collectable)); + if (collectable == NULL) + { + engine_warn("Failed to allocate memory for collectable"); + return; + } collectable->next = NULL; collectable->active = 1; diff --git a/src/engine.cpp b/src/engine.cpp index e2124aa..677bced 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -68,15 +68,27 @@ void engine_init() engine.bulletHead->next = NULL; engine.bulletTail = engine.bulletHead; - engine.explosionHead = new Object; + engine.explosionHead = (Object*)malloc(sizeof(Object)); + if (engine.explosionHead == NULL) + { + engine_error("Failed to allocate memory for explosion head."); + } engine.explosionHead->next = NULL; engine.explosionTail = engine.explosionHead; - engine.collectableHead = new Collectable; + engine.collectableHead = (Collectable*)malloc(sizeof(Collectable)); + if (engine.collectableHead == NULL) + { + engine_error("Failed to allocate memory for collectable head."); + } engine.collectableHead->next = NULL; engine.collectableTail = engine.collectableHead; - engine.debrisHead = new Object; + engine.debrisHead = (Object*)malloc(sizeof(Object)); + if (engine.debrisHead == NULL) + { + engine_error("Failed to allocate memory for debris head."); + } engine.debrisHead->next = NULL; engine.debrisTail = engine.debrisHead; @@ -322,7 +334,7 @@ void engine_resetLists() { ob2 = ob; ob = ob->next; - delete ob2; + free(ob2); } engine.bulletHead->next = NULL; engine.bulletTail = engine.bulletHead; @@ -332,7 +344,7 @@ void engine_resetLists() { ob2 = ob; ob = ob->next; - delete ob2; + free(ob2); } engine.explosionHead->next = NULL; engine.explosionTail = engine.explosionHead; @@ -342,7 +354,7 @@ void engine_resetLists() { c2 = c1; c1 = c1->next; - delete c2; + free(c2); } engine.collectableHead->next = NULL; @@ -353,7 +365,7 @@ void engine_resetLists() { r2 = r1; r1 = r1->next; - delete r2; + free(r2); } screen_bufferHead->next = NULL; @@ -364,7 +376,7 @@ void engine_resetLists() { ob2 = ob; ob = ob->next; - delete ob2; + free(ob2); } engine.debrisHead->next = NULL; engine.debrisTail = engine.debrisHead; @@ -382,9 +394,9 @@ void engine_cleanup() audio_free(); engine_resetLists(); free(engine.bulletHead); - delete(engine.explosionHead); - delete(engine.collectableHead); - delete(screen_bufferHead); + free(engine.explosionHead); + free(engine.collectableHead); + free(screen_bufferHead); for (int i = 0 ; i < FONT_MAX ; i++) { diff --git a/src/explosion.cpp b/src/explosion.cpp index f40a610..6c84fcf 100644 --- a/src/explosion.cpp +++ b/src/explosion.cpp @@ -33,7 +33,12 @@ to change frames on a 21, 14, 7 basis. */ void explosion_add(float x, float y, int type) { - Object *explosion = new Object; + Object *explosion = (Object*)malloc(sizeof(Object)); + if (explosion == NULL) + { + engine_warn("Failed to allocate memory for explosion."); + return; + } explosion->next = NULL; explosion->active = 1; diff --git a/src/game.cpp b/src/game.cpp index 06e698c..10e1a02 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -233,7 +233,7 @@ static void game_addDebris(int x, int y, int amount) for (int i = 0 ; i < amount ; i++) { - debris = new Object; + debris = (Object*)malloc(sizeof(Object)); debris->next = NULL; debris->x = x; @@ -589,7 +589,7 @@ static void game_doCollectables() (collectable->y <= screen->h)) collectable_explode(collectable); prevCollectable->next = collectable->next; - delete collectable; + free(collectable); collectable = prevCollectable; } } @@ -897,7 +897,7 @@ static void game_doBullets() { collectable_explode(collectable); prevCollectable->next = collectable->next; - delete collectable; + free(collectable); collectable = prevCollectable; } } @@ -1638,7 +1638,7 @@ static void game_doDebris() if (debris->thinktime < 1) { prevDebris->next = debris->next; - delete debris; + free(debris); debris = prevDebris; } else @@ -1697,7 +1697,7 @@ void game_doExplosions() else { prevExplosion->next = explosion->next; - delete explosion; + free(explosion); explosion = prevExplosion; } } diff --git a/src/gfx.cpp b/src/gfx.cpp index f6a2b7c..9c8ac7d 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -44,7 +44,11 @@ SDL_Surface *gfx_messageBox; void gfx_init() { - screen_bufferHead = new LinkedRect; + screen_bufferHead = (LinkedRect*)malloc(sizeof(LinkedRect)); + if (screen_bufferHead == NULL) + { + engine_error("Failed to allocate memory for buffer head."); + } screen_bufferHead->next = NULL; screen_bufferTail = screen_bufferHead; diff --git a/src/screen.cpp b/src/screen.cpp index a939ba6..256821e 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -56,7 +56,7 @@ void screen_drawBackground() void screen_addBuffer(int x, int y, int w, int h) { - LinkedRect *rect = new LinkedRect; + LinkedRect *rect = (LinkedRect*)malloc(sizeof(LinkedRect)); rect->next = NULL; rect->x = x; @@ -79,7 +79,7 @@ void screen_flushBuffer() rect = rect->next; prevRect->next = rect->next; - delete rect; + free(rect); rect = prevRect; } @@ -110,7 +110,7 @@ void screen_unBuffer() } prevRect->next = rect->next; - delete rect; + free(rect); rect = prevRect; }