Browse Source

* Sanity checks after each malloc() call.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/ttyvaders/trunk@174 92316355-f0b4-4df1-b90c-862c8a59935f
master
sam 21 years ago
parent
commit
5c7bdf3288
6 changed files with 24 additions and 2 deletions
  1. +2
    -0
      src/box.c
  2. +10
    -0
      src/main.c
  3. +2
    -0
      src/player.c
  4. +2
    -0
      src/starfield.c
  5. +6
    -0
      src/tunnel.c
  6. +2
    -2
      src/weapons.c

+ 2
- 0
src/box.c View File

@@ -29,6 +29,8 @@
box * create_box(game *g, int x, int y, int w, int h) box * create_box(game *g, int x, int y, int w, int h)
{ {
box *b = malloc(sizeof(box)); box *b = malloc(sizeof(box));
if(b == NULL)
exit(1);


b->x = x; b->x = x;
b->y = y; b->y = y;


+ 10
- 0
src/main.c View File

@@ -36,6 +36,8 @@ static void start_game (game *);
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
game *g = malloc(sizeof(game)); game *g = malloc(sizeof(game));
if(g == NULL)
exit(1);


srand(time(NULL)); srand(time(NULL));


@@ -70,11 +72,19 @@ static void start_game (game *g)


g->sf = create_starfield(g); g->sf = create_starfield(g);
g->wp = malloc(sizeof(weapons)); g->wp = malloc(sizeof(weapons));
if(g->wp == NULL)
exit(1);
g->ex = malloc(sizeof(explosions)); g->ex = malloc(sizeof(explosions));
if(g->ex == NULL)
exit(1);
g->bo = malloc(sizeof(bonus)); g->bo = malloc(sizeof(bonus));
if(g->bo == NULL)
exit(1);
g->t = create_tunnel(g, g->w, g->h); g->t = create_tunnel(g, g->w, g->h);
g->p = create_player(g); g->p = create_player(g);
g->al = malloc(sizeof(aliens)); g->al = malloc(sizeof(aliens));
if(g->al == NULL)
exit(1);


init_bonus(g, g->bo); init_bonus(g, g->bo);
init_weapons(g, g->wp); init_weapons(g, g->wp);


+ 2
- 0
src/player.c View File

@@ -32,6 +32,8 @@ struct ee_sprite *ship_sprite;
player * create_player(game *g) player * create_player(game *g)
{ {
player *p = malloc(sizeof(player)); player *p = malloc(sizeof(player));
if(p == NULL)
exit(1);


p->x = g->w / 2; p->x = g->w / 2;
p->y = g->h - 3; p->y = g->h - 3;


+ 2
- 0
src/starfield.c View File

@@ -32,6 +32,8 @@ starfield * create_starfield(game *g)
starfield *s; starfield *s;


s = malloc(STARS * sizeof(starfield)); s = malloc(STARS * sizeof(starfield));
if(s == NULL)
exit(1);


for(i = 0; i < STARS; i++) for(i = 0; i < STARS; i++)
{ {


+ 6
- 0
src/tunnel.c View File

@@ -31,9 +31,15 @@ tunnel * create_tunnel(game *g, int w, int h)
{ {
int i; int i;
tunnel *t = malloc(sizeof(tunnel)); tunnel *t = malloc(sizeof(tunnel));
if(t == NULL)
exit(1);


t->left = malloc(h*sizeof(int)); t->left = malloc(h*sizeof(int));
if(t->left == NULL)
exit(1);
t->right = malloc(h*sizeof(int)); t->right = malloc(h*sizeof(int));
if(t->right == NULL)
exit(1);
t->w = w; t->w = w;
t->h = h; t->h = h;




+ 2
- 2
src/weapons.c View File

@@ -169,7 +169,7 @@ void update_weapons(game *g, weapons *wp)
/* Normalize direction */ /* Normalize direction */
if(dx | dy) if(dx | dy)
{ {
int norm = ee_sqrt(dx * dx + 4 * dy * dy);
unsigned int norm = ee_sqrt(dx * dx + 4 * dy * dy);
dx = dx * 32 / norm; dx = dx * 32 / norm;
dy = dy * 32 / norm; dy = dy * 32 / norm;
} }
@@ -181,7 +181,7 @@ void update_weapons(game *g, weapons *wp)
/* Normalize speed */ /* Normalize speed */
if(dx | dy) if(dx | dy)
{ {
int norm = ee_sqrt(dx * dx + 4 * dy * dy);
unsigned int norm = ee_sqrt(dx * dx + 4 * dy * dy);
wp->vx[i] = dx * 32 / norm; wp->vx[i] = dx * 32 / norm;
wp->vy[i] = dy * 32 / norm; wp->vy[i] = dy * 32 / norm;
} }


Loading…
Cancel
Save