Add memory allocation checks

Closes #18.
This commit is contained in:
Marc Tiehuis 2017-08-12 11:09:07 +12:00
parent 912afdfe05
commit 984a5b9f18
4 changed files with 14 additions and 3 deletions

View File

@ -216,10 +216,10 @@ static int digits_ceiling(unsigned int n)
struct gamestate* gamestate_init(int argc, char **argv)
{
struct gameoptions *opt = gameoptions_default();
if (argc != 0) parse_options(opt, argc, argv);
if (!opt) return NULL;
if (argc != 0) parse_options(opt, argc, argv);
srand(time(NULL));
struct gamestate *g = malloc(sizeof(struct gamestate));

View File

@ -2,6 +2,7 @@
#define ENGINE_H
#include <stdio.h>
#include <stdlib.h>
#include "options.h"
#define fatal(msg)\

View File

@ -26,6 +26,8 @@ struct gfx_state* gfx_init(struct gamestate *g)
refresh();
struct gfx_state *s = malloc(sizeof(struct gfx_state));
if (!s) return NULL;
s->window_height = g->opts->grid_height * (g->print_width + 2) + 3;
s->window_width = g->opts->grid_width * (g->print_width + 2) + 1;
s->window = newwin(s->window_height, s->window_width, 1, 1);

View File

@ -14,10 +14,18 @@ void draw_then_sleep(struct gfx_state *s, struct gamestate *g)
int main(int argc, char **argv)
{
struct gamestate *g = gamestate_init(argc, argv);
if (!g) {
fatal("failed to allocate gamestate");
}
struct gfx_state *s = NULL;
if (g->opts->interactive)
if (g->opts->interactive) {
s = gfx_init(g);
if (!s) {
fatal("failed to allocate gfx state");
}
}
int game_running = true;
while (game_running) {