Converted some new and delete calls to malloc and free.

This is the last step to converting Starfighter to C. Almost there!
This commit is contained in:
Julie Marchant 2019-05-19 13:40:50 -04:00
parent 828b56d4df
commit 6644be553e
5 changed files with 37 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -935,7 +935,7 @@ static void game_doBullets()
else
{
prevBullet->next = bullet->next;
delete bullet;
free(bullet);
bullet = prevBullet;
}
}