From 4bb2c012650e4982b93e3200268d95e22c90939c Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 10 Nov 2003 09:26:40 +0000 Subject: [PATCH] * libee/sprite.c: + More robust sprite loader. + Added ee_set_sprite_frame() and ee_get_sprite_frame(). + Free all structures in ee_free_sprite(). * src/aliens.c src/bonus.c: + Use ee_draw_sprite() instead of our manual sprite rendering. * src/box.c: + Use ee_draw_line() instead of the manual equivalent. * data/: + Added foo_fighter, baz_fighter, item_gem and item_heart sprites. --- data/Makefile.am | 2 +- data/baz_fighter | 28 ++++++++++ data/foo_fighter | 29 +++++++++++ data/item_gem | 14 +++++ data/item_heart | 14 +++++ libee/ee.h | 2 + libee/sprite.c | 28 ++++++++++ src/aliens.c | 130 +++++------------------------------------------ src/bonus.c | 32 ++++-------- src/box.c | 51 ++++++------------- 10 files changed, 155 insertions(+), 175 deletions(-) create mode 100644 data/baz_fighter create mode 100644 data/foo_fighter create mode 100644 data/item_gem create mode 100644 data/item_heart diff --git a/data/Makefile.am b/data/Makefile.am index 97f2eb6..bc36072 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -1 +1 @@ -EXTRA_DIST = bar_fighter bar_boss +EXTRA_DIST = foo_fighter bar_fighter bar_boss baz_fighter item_gem item_heart diff --git a/data/baz_fighter b/data/baz_fighter new file mode 100644 index 0000000..66ee216 --- /dev/null +++ b/data/baz_fighter @@ -0,0 +1,28 @@ +6 3 2 1 + __ + /oo\ +//'`\\ + cc + ceec +cccccc +6 3 2 1 + __ + /oo\ +/(~~)\ + cc + ceec +cccccc +6 3 2 1 + __ + /oo\ +((^^)) + cc + ceec +cccccc +4 3 1 1 + __ +/oo\ +\\// + cc +ceec +cccc diff --git a/data/foo_fighter b/data/foo_fighter new file mode 100644 index 0000000..962546d --- /dev/null +++ b/data/foo_fighter @@ -0,0 +1,29 @@ +7 3 3 1 +. , + \oXo/ + `V' +d d + deded + ddd +9 3 4 1 +. , + `-oXo-' + `V' +d d + ddededd + ddd +9 2 4 0 +`--oXo--' + `V' +dddededdd + ddd +9 2 4 0 + ,-oXo-. +' V ` + ddededd +d d d +7 2 3 0 + ,oXo. +/ V \ + deded +d d d diff --git a/data/item_gem b/data/item_gem new file mode 100644 index 0000000..72c81a1 --- /dev/null +++ b/data/item_gem @@ -0,0 +1,14 @@ +3 2 1 1 + _ +/g\ +\_/ + c +cec +ccc +3 2 1 1 + _ +/g\ +\_/ + e +eee +eee diff --git a/data/item_heart b/data/item_heart new file mode 100644 index 0000000..5a7cdcd --- /dev/null +++ b/data/item_heart @@ -0,0 +1,14 @@ +5 3 2 1 + _ _ +( '^) + `v' + h h +hhheh + hhh +5 3 2 1 + _ _ +( '^) + `v' + e e +eeeee + eee diff --git a/libee/ee.h b/libee/ee.h index 5067199..e69a29d 100644 --- a/libee/ee.h +++ b/libee/ee.h @@ -87,6 +87,8 @@ int ee_rand(int, int); int ee_sqrt(int); struct ee_sprite * ee_load_sprite(const char *); +void ee_set_sprite_frame(struct ee_sprite *, int); +int ee_get_sprite_frame(struct ee_sprite *); void ee_draw_sprite(int, int, struct ee_sprite *); void ee_free_sprite(struct ee_sprite *); diff --git a/libee/sprite.c b/libee/sprite.c index faac57c..e1187e3 100644 --- a/libee/sprite.c +++ b/libee/sprite.c @@ -24,6 +24,7 @@ #include #include +#include #include "ee.h" @@ -87,6 +88,8 @@ struct ee_sprite *ee_load_sprite(const char *file) for(y = 0; y < h; y++) { + memset(buf, ' ', w); + buf[w] = '\0'; if(!fgets(buf, BUFSIZ, fd)) goto failed; @@ -96,6 +99,8 @@ struct ee_sprite *ee_load_sprite(const char *file) for(y = 0; y < h; y++) { + memset(buf, ' ', w); + buf[w] = '\0'; if(!fgets(buf, BUFSIZ, fd)) goto failed; @@ -123,6 +128,19 @@ struct ee_sprite *ee_load_sprite(const char *file) return sprite; } +void ee_set_sprite_frame(struct ee_sprite *sprite, int f) +{ + if(f < 0 || f >= sprite->nf) + return; + + sprite->f = f; +} + +int ee_get_sprite_frame(struct ee_sprite *sprite) +{ + return sprite->f; +} + void ee_draw_sprite(int x, int y, struct ee_sprite *sprite) { int i, j; @@ -145,6 +163,16 @@ void ee_draw_sprite(int x, int y, struct ee_sprite *sprite) void ee_free_sprite(struct ee_sprite *sprite) { + int i; + + for(i = sprite->nf; i--;) + { + struct ee_frame *frame = &sprite->frames[i]; + free(frame->chars); + free(frame->color); + } + + free(sprite->frames); free(sprite); } diff --git a/src/aliens.c b/src/aliens.c index 78abaa8..74ebad5 100644 --- a/src/aliens.c +++ b/src/aliens.c @@ -30,6 +30,10 @@ static void draw_alien_foo(game *, int, int, int); static void draw_alien_bar(game *, int, int, int); static void draw_alien_baz(game *, int, int, int); +struct ee_sprite *foo_sprite; +struct ee_sprite *bar_sprite; +struct ee_sprite *baz_sprite; + void init_aliens(game *g, aliens *al) { int i; @@ -38,6 +42,10 @@ void init_aliens(game *g, aliens *al) { al->type[i] = ALIEN_NONE; } + + foo_sprite = ee_load_sprite("data/foo_fighter"); + bar_sprite = ee_load_sprite("data/bar_fighter"); + baz_sprite = ee_load_sprite("data/baz_fighter"); } void draw_aliens(game *g, aliens *al) @@ -132,130 +140,20 @@ void add_alien(game *g, aliens *al, int x, int y, int type) static void draw_alien_bar(game *g, int x, int y, int frame) { - switch(frame) - { - case 0: - ee_color(EE_MAGENTA); - ee_goto(x, y); - ee_putstr(",---."); - ee_goto(x, y+1); - ee_putchar('\\'); - ee_color(EE_WHITE); - ee_putstr("o O"); - ee_color(EE_MAGENTA); - ee_putchar('/'); - ee_goto(x, y+2); - ee_putstr("^^^^^"); - break; - case 1: - ee_color(EE_MAGENTA); - ee_goto(x, y); - ee_putstr(",---."); - ee_goto(x, y+1); - ee_putchar('\\'); - ee_color(EE_WHITE); - ee_putstr("O o"); - ee_color(EE_MAGENTA); - ee_putchar('/'); - ee_goto(x, y+2); - ee_putstr("^^^^^"); - break; - } + ee_set_sprite_frame(bar_sprite, frame); + ee_draw_sprite(x, y, bar_sprite); } static void draw_alien_baz(game *g, int x, int y, int frame) { - ee_color(EE_GREEN); - ee_goto(x, y-1); - ee_putstr("__"); - - ee_goto(x-1, y); - ee_putchar('/'); - ee_goto(x+2, y); - ee_putchar('\\'); - - switch(frame) - { - case 3: - ee_goto(x-2, y+1); - ee_putstr("//'`\\\\"); - break; - case 4: - case 2: - ee_goto(x-2, y+1); - ee_putstr("/(~~)\\"); - break; - case 5: - case 1: - ee_goto(x-2, y+1); - ee_putstr("((^^))"); - break; - case 0: - ee_goto(x-1, y+1); - ee_putstr("\\\\//"); - break; - } - - ee_color(EE_WHITE); - ee_goto(x, y); - ee_putstr("oo"); + ee_set_sprite_frame(baz_sprite, frame); + ee_draw_sprite(x, y, baz_sprite); } static void draw_alien_foo(game *g, int x, int y, int frame) { - ee_color(EE_YELLOW); - - switch(frame) - { - case 0: - ee_goto(x, y); - ee_putchar('.'); - ee_goto(x+6, y); - ee_putchar(','); - ee_goto(x+1, y+1); - ee_putstr("\\ X /"); - break; - case 7: - case 1: - ee_goto(x-1, y); - ee_putchar('.'); - ee_goto(x+7, y); - ee_putchar(','); - ee_goto(x, y+1); - ee_putstr("`- X -'"); - break; - case 6: - case 2: - ee_goto(x-1, y+1); - ee_putstr("`-- X --'"); - break; - case 5: - case 3: - ee_goto(x, y+1); - ee_putstr(",- X -."); - ee_goto(x-1, y+2); - ee_putchar('\''); - ee_goto(x+7, y+2); - ee_putchar('`'); - break; - case 4: - ee_goto(x+1, y+1); - ee_putstr(", X ."); - ee_goto(x, y+2); - ee_putchar('/'); - ee_goto(x+6, y+2); - ee_putchar('\\'); - break; - } - - ee_goto(x+2, y+2); - ee_putstr("`V'"); - - ee_color(EE_WHITE); - ee_goto(x+2, y+1); - ee_putchar('o'); - ee_goto(x+4, y+1); - ee_putchar('o'); + ee_set_sprite_frame(foo_sprite, frame); + ee_draw_sprite(x, y, foo_sprite); } diff --git a/src/bonus.c b/src/bonus.c index 103c94e..75d3178 100644 --- a/src/bonus.c +++ b/src/bonus.c @@ -26,6 +26,9 @@ #include "common.h" +struct ee_sprite *heart_sprite; +struct ee_sprite *gem_sprite; + void init_bonus(game *g, bonus *bo) { int i; @@ -34,6 +37,9 @@ void init_bonus(game *g, bonus *bo) { bo->type[i] = BONUS_NONE; } + + heart_sprite = ee_load_sprite("data/heart"); + gem_sprite = ee_load_sprite("data/gem"); } void draw_bonus(game *g, bonus *bo) @@ -45,30 +51,12 @@ void draw_bonus(game *g, bonus *bo) switch(bo->type[i]) { case BONUS_GREEN: - ee_color((bo->n[i]/2 % 3) ? EE_GREEN : EE_WHITE); - ee_goto(bo->x[i]+1, bo->y[i]-1); - ee_putchar('_'); - ee_goto(bo->x[i], bo->y[i]); - ee_putstr("/ \\"); - ee_goto(bo->x[i], bo->y[i]+1); - ee_putstr("\\_/"); - ee_color(EE_WHITE); - ee_goto(bo->x[i]+1, bo->y[i]); - ee_putchar('g'); + ee_set_sprite_frame(gem_sprite, (bo->n[i]/2 % 3) ? 0 : 1); + ee_draw_sprite(bo->x[i], bo->y[i], gem_sprite); break; case BONUS_LIFE: - ee_color((bo->n[i] % 3) ? EE_RED : EE_WHITE); - ee_goto(bo->x[i]+1, bo->y[i]-1); - ee_putchar('_'); - ee_goto(bo->x[i]+3, bo->y[i]-1); - ee_putchar('_'); - ee_goto(bo->x[i], bo->y[i]); - ee_putstr("( ' )"); - ee_goto(bo->x[i]+1, bo->y[i]+1); - ee_putstr("`v'"); - ee_color(EE_WHITE); - ee_goto(bo->x[i]+3, bo->y[i]); - ee_putchar('^'); + ee_set_sprite_frame(heart_sprite, (bo->n[i] % 3) ? 0 : 1); + ee_draw_sprite(bo->x[i], bo->y[i], heart_sprite); break; case BONUS_NONE: break; diff --git a/src/box.c b/src/box.c index 5f5d74f..4bf10e2 100644 --- a/src/box.c +++ b/src/box.c @@ -41,60 +41,39 @@ box * create_box(game *g, int x, int y, int w, int h) void draw_box(game *g, box *b) { - int i, j, frame; + int j, frame; ee_color(EE_YELLOW); /* Draw the thin horizontal line */ if(b->frame < 8) { - for(i = b->x - b->w * b->frame / 16 ; - i < b->x + b->w * b->frame / 16 ; - i++) - { - ee_goto(i, b->y); - ee_putchar('X'); - } - + ee_draw_line(b->x - b->w * b->frame / 16, b->y, + b->x + b->w * b->frame / 16 - 1, b->y, 'X'); return; } /* Draw the frame */ frame = b->frame < 12 ? b->frame : 12; - for(i = b->x - b->w / 2 ; - i < b->x + b->w / 2 ; - i++) - { - ee_goto(i, b->y - b->h * (frame - 8) / 8); - ee_putchar('X'); - ee_goto(i, b->y + b->h * (frame - 8) / 8); - ee_putchar('X'); - } + ee_draw_line(b->x - b->w / 2, b->y - b->h * (frame - 8) / 8, + b->x + b->w / 2 - 1, b->y - b->h * (frame - 8) / 8, 'X'); + ee_draw_line(b->x - b->w / 2, b->y + b->h * (frame - 8) / 8, + b->x + b->w / 2 - 1, b->y + b->h * (frame - 8) / 8, 'X'); - for(j = b->y - b->h * (frame - 8) / 8 ; - j < b->y + b->h * (frame - 8) / 8 ; - j++) - { - ee_goto(b->x - b->w / 2, j); - ee_putchar('X'); - ee_goto(b->x + b->w / 2 - 1, j); - ee_putchar('X'); - } + ee_draw_line(b->x - b->w / 2, b->y - b->h * (frame - 8) / 8, + b->x - b->w / 2, b->y + b->h * (frame - 8) / 8 - 1, 'X'); + ee_draw_line(b->x + b->w / 2 - 1, b->y - b->h * (frame - 8) / 8, + b->x + b->w / 2 - 1, b->y + b->h * (frame - 8) / 8 - 1, 'X'); ee_color(EE_BLACK); - for(j = b->y - b->h * (frame - 8) / 8 + 1 ; - j < b->y + b->h * (frame - 8) / 8 ; + for(j = b->y - b->h * (frame - 8) / 8 + 1; + j < b->y + b->h * (frame - 8) / 8; j++) { - for(i = b->x - b->w / 2 + 1 ; - i < b->x + b->w / 2 - 1 ; - i++) - { - ee_goto(i, j); - ee_putchar('X'); - } + ee_draw_line(b->x - b->w / 2 + 1, j, + b->x + b->w / 2 - 2, j, 'X'); } if(b->frame < 12)