Finished conversion to malloc/free.

This commit is contained in:
Julie Marchant 2019-05-19 18:22:53 -04:00
parent 0ba13df0d7
commit 8fc30e61f3
6 changed files with 48 additions and 22 deletions

View File

@ -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;

View File

@ -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++)
{

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;
}