From 8fc30e61f3cea588a0fc1b82d833ce730a6c56d7 Mon Sep 17 00:00:00 2001 From: Julie Marchant Date: Sun, 19 May 2019 18:22:53 -0400 Subject: [PATCH] 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; }