Skip to content

Commit

Permalink
Music now plays correctly when loading a saved game. Removed atexit f…
Browse files Browse the repository at this point in the history
…unction call. Fixed a rare crash when drawing entities. Fixed a rare crash when loading a game. Newly joysticks are now detected whilst the application is running.
  • Loading branch information
riksweeney committed Dec 28, 2020
1 parent e76b3c5 commit 92db219
Show file tree
Hide file tree
Showing 25 changed files with 210 additions and 135 deletions.
2 changes: 2 additions & 0 deletions src/audio/music.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ void playMapMusic()
Mix_PlayMusic(music, -1);

Mix_VolumeMusic(game.musicDefaultVolume * VOLUME_STEPS);

Mix_ResumeMusic();
}

void stopMusic()
Expand Down
3 changes: 2 additions & 1 deletion src/boss/azriel.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
#include "../graphics/decoration.h"
#include "../graphics/graphics.h"
#include "../hud.h"
#include "../init.h"
#include "../inventory.h"
#include "../item/key_items.h"
#include "../map.h"
Expand Down Expand Up @@ -2052,7 +2053,7 @@ static void becomeTransparent()
{
printf("%s cannot become transparent!\n", self->name);

exit(1);
cleanup(1);
}

self->endX--;
Expand Down
2 changes: 1 addition & 1 deletion src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.

#define TABLE_SIZE 255

#define YEAR 2020
#define YEAR 2021

#ifdef TRUE
#undef TRUE
Expand Down
5 changes: 5 additions & 0 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ void draw()
}
break;
}

if (game.frames % 300 == 0)
{
removeUnreferencedEntities();
}
}

else
Expand Down
11 changes: 7 additions & 4 deletions src/enemy/ground_spear.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
#include "../collisions.h"
#include "../entity.h"
#include "../graphics/animation.h"
#if DEV == 1
#include "../init.h"
#endif
#include "../system/error.h"
#include "../system/properties.h"

Expand All @@ -31,7 +34,7 @@ extern Entity *self;
static void entityWait(void);
static void rise(void);
static void sink(void);
static void init(void);
static void initialise(void);

Entity *addGroundSpear(int x, int y, char *name)
{
Expand All @@ -47,7 +50,7 @@ Entity *addGroundSpear(int x, int y, char *name)
e->x = x;
e->y = y;

e->action = &init;
e->action = &initialise;

e->draw = &drawLoopingAnimationToMap;
e->touch = &entityTouch;
Expand All @@ -59,7 +62,7 @@ Entity *addGroundSpear(int x, int y, char *name)
return e;
}

static void init()
static void initialise()
{
switch (self->mental)
{
Expand Down Expand Up @@ -173,7 +176,7 @@ static void rise()
{
printf("Loop time %d\n", self->health);

exit(0);
cleanup(0);
}
#endif
}
Expand Down
85 changes: 44 additions & 41 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ void doEntities()

void drawEntities(int depth)
{
int i, drawn, removeCount;
EntityList *el, *prev, *el2;
int i, drawn;
EntityList *el;

/* Draw standard entities */

Expand Down Expand Up @@ -318,45 +318,6 @@ void drawEntities(int depth)
}
}
}

if (game.frames % 300 == 0)
{
removeCount = 0;

prev = entities;

for (el=entities->next;el!=NULL;el=el2)
{
el2 = el->next;

if (el->entity->inUse == FALSE && isReferenced(el->entity) == FALSE)
{
prev->next = el2;

removeCount++;

free(el->entity);

el->entity = NULL;

free(el);

el = NULL;
}

else
{
prev = prev->next;
}
}

#if DEV == 1
if (removeCount != 0)
{
printf("Removed %d entities taking up %d bytes\n", removeCount, (int)sizeof(Entity) * removeCount);
}
#endif
}
}

void removeEntity()
Expand Down Expand Up @@ -392,6 +353,48 @@ void removeAllSpawnedIn()
}
}

void removeUnreferencedEntities()
{
int removeCount;
EntityList *el, *prev, *el2;

removeCount = 0;

prev = entities;

for (el=entities->next;el!=NULL;el=el2)
{
el2 = el->next;

if (el->entity->inUse == FALSE && isReferenced(el->entity) == FALSE)
{
prev->next = el2;

removeCount++;

free(el->entity);

el->entity = NULL;

free(el);

el = NULL;
}

else
{
prev = prev->next;
}
}

#if DEV == 1
if (removeCount != 0)
{
printf("Removed %d entities taking up %d bytes\n", removeCount, (int)sizeof(Entity) * removeCount);
}
#endif
}

void disableSpawners(int disable)
{
Entity *e;
Expand Down
1 change: 1 addition & 0 deletions src/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void doEntities(void);
void drawEntities(int);
void removeEntity(void);
void removeAllSpawnedIn(void);
void removeUnreferencedEntities(void);
void disableSpawners(int);
void doNothing(void);
void entityDie(void);
Expand Down
3 changes: 2 additions & 1 deletion src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
#include "game.h"
#include "graphics/graphics.h"
#include "hud.h"
#include "init.h"
#include "inventory.h"
#include "map.h"
#include "medal.h"
Expand Down Expand Up @@ -616,7 +617,7 @@ void pauseGame()
break;

case IN_EDITOR:
exit(0);
cleanup(0);
break;

case IN_INVENTORY:
Expand Down
5 changes: 3 additions & 2 deletions src/graphics/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/

#include "../headers.h"
#include "../init.h"
#include "../system/pak.h"
#include "graphics.h"

Expand All @@ -31,7 +32,7 @@ TTF_Font *loadFont(char *name, int size)
{
printf("Failed to open Font %s: %s\n", name, TTF_GetError());

exit(1);
cleanup(1);
}

return font;
Expand All @@ -45,7 +46,7 @@ TTF_Font *loadCustomFont(char *name, int size)
{
printf("Failed to open Font %s: %s\n", name, TTF_GetError());

exit(1);
cleanup(1);
}

return font;
Expand Down
11 changes: 6 additions & 5 deletions src/graphics/save_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.

#include "../headers.h"

#include "../init.h"
#include "graphics.h"

#include <png.h>
Expand All @@ -39,7 +40,7 @@ static void writeData(char *name, png_bytep *rows, int w, int h, int colourtype,
{
printf("Could not save %s\n", name);

exit(1);
cleanup(1);
}

pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Expand All @@ -48,7 +49,7 @@ static void writeData(char *name, png_bytep *rows, int w, int h, int colourtype,
{
printf("Failed to create PNG structure");

exit(1);
cleanup(1);
}

infoPtr = png_create_info_struct(pngPtr);
Expand All @@ -57,14 +58,14 @@ static void writeData(char *name, png_bytep *rows, int w, int h, int colourtype,
{
printf("Failed to create PNG info");

exit(1);
cleanup(1);
}

if (setjmp(png_jmpbuf(pngPtr)) != 0)
{
printf("PNG Jump point failed\n");

exit(1);
cleanup(1);
}

png_init_io(pngPtr, fp);
Expand Down Expand Up @@ -116,7 +117,7 @@ void savePNG(SDL_Surface *surface, char *name)
{
printf("Ran out of memory when creating PNG rows\n");

exit(1);
cleanup(1);
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/i18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.

#include "headers.h"

#include "init.h"

static HashTable table;

static int hashCode(char *);
Expand Down Expand Up @@ -134,7 +136,7 @@ void setLanguage(char *applicationName, char *languageCode)
{
printf("Failed to allocate %d bytes for translation strings\n", (int)sizeof(MOEntry) * header.stringCount);

exit(1);
cleanup(1);
}

#if DEV == 1
Expand All @@ -151,7 +153,7 @@ void setLanguage(char *applicationName, char *languageCode)
{
printf("Failed to allocate a whole %d bytes for translation strings\n", (int)sizeof(char *) * header.stringCount);

exit(1);
cleanup(1);
}

for (i=0;i<header.stringCount;i++)
Expand All @@ -171,7 +173,7 @@ void setLanguage(char *applicationName, char *languageCode)
{
printf("Failed to allocate a whole %d bytes for translation string\n", original[i].length + 1);

exit(1);
cleanup(1);
}
}

Expand All @@ -194,7 +196,7 @@ void setLanguage(char *applicationName, char *languageCode)
{
printf("Failed to allocate a whole %d bytes for translation string\n", translation[i].length + 1);

exit(1);
cleanup(1);
}
}

Expand Down Expand Up @@ -279,7 +281,7 @@ static void initTable()
{
printf("Failed to allocate %d bytes for a HashTable\n", (int)sizeof(Bucket *) * TABLE_SIZE);

exit(1);
cleanup(1);
}

for (i=0;i<TABLE_SIZE;i++)
Expand Down Expand Up @@ -314,7 +316,7 @@ static void put(char *key, char *value)
{
printf("Failed to allocate a whole %d bytes for a HashTable bucket\n", (int)sizeof(Bucket));

exit(1);
cleanup(1);
}

newBucket->key = malloc(strlen(key) + 1);
Expand All @@ -324,7 +326,7 @@ static void put(char *key, char *value)
{
printf("Failed to allocate a whole %d bytes for a translation\n", (int)strlen(newBucket->key) + 1);

exit(1);
cleanup(1);
}

STRNCPY(newBucket->key, key, strlen(key) + 1);
Expand Down
Loading

0 comments on commit 92db219

Please sign in to comment.