Skip to content

Commit

Permalink
Fix checks when allocating/creating media
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktm committed Mar 4, 2019
1 parent 22c03ec commit 365bbb7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ S2D_Image *S2D_CreateImage(const char *path) {
// Allocate the image structure
S2D_Image *img = (S2D_Image *) malloc(sizeof(S2D_Image));
if (!img) {
S2D_Error("IMG_Load", "Out of memory!");
S2D_Error("S2D_CreateImage", "Out of memory!");
return NULL;
}

// Load image from file as SDL_Surface
img->surface = IMG_Load(path);
if (!img->surface) {
S2D_Error("IMG_Load", IMG_GetError());
free(img);
return NULL;
}

Expand Down
4 changes: 4 additions & 0 deletions src/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ S2D_Sound *S2D_CreateSound(const char *path) {

// Allocate the sound structure
S2D_Sound *snd = (S2D_Sound *) malloc(sizeof(S2D_Sound));
if (!snd) {
S2D_Error("S2D_CreateSound", "Out of memory!");
return NULL;
}

// Load the sound data from file
snd->data = Mix_LoadWAV(path);
Expand Down
13 changes: 12 additions & 1 deletion src/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ S2D_Sprite *S2D_CreateSprite(const char *path) {
return NULL;
}

// Allocate the sprite structure and create the image
// Allocate the sprite structure
S2D_Sprite *spr = (S2D_Sprite *) malloc(sizeof(S2D_Sprite));
if (!spr) {
S2D_Error("S2D_CreateSprite", "Out of memory!");
return NULL;
}

// Load the sprite image file
spr->img = S2D_CreateImage(path);
if (!spr->img) {
S2D_Error("S2D_CreateSprite", "Cannot create sprite image `%s`", path);
free(spr);
return NULL;
}

// Initialize values
spr->path = path;
Expand Down

0 comments on commit 365bbb7

Please sign in to comment.