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