From 8761e2ea313a12d7de3a1a8d2d5d7a1fcec32158 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tobias=20Gl=C3=A4=C3=9Fer?= Date: Wed, 21 Jan 2004 22:51:32 +0000 Subject: [PATCH] More code cleanups. Fixed a few bugs. SVN-Revision: 99 --- src/badguy.c | 27 ++++++++----- src/badguy.h | 2 +- src/defines.h | 8 ++-- src/gameloop.c | 13 ++----- src/player.c | 24 ++++++------ src/scene.c | 118 ++++++--------------------------------------------------- src/scene.h | 4 +- src/special.c | 42 ++++++++++++++------ src/special.h | 4 +- src/type.c | 2 +- src/world.c | 57 +++++++++++++++++++++++----- src/world.h | 9 +++-- 12 files changed, 140 insertions(+), 170 deletions(-) diff --git a/src/badguy.c b/src/badguy.c index 508569505..5c9448ef3 100644 --- a/src/badguy.c +++ b/src/badguy.c @@ -21,13 +21,22 @@ void badguy_create_bitmasks() /*bm_bsod = img_bsod_left[0];*/ } -void badguy_init(bad_guy_type* pbad) +void badguy_init(bad_guy_type* pbad, float x, float y, int kind) { pbad->base.updated = SDL_GetTicks(); - pbad->base.alive = NO; pbad->base.width = 32; pbad->base.height = 32; - + pbad->base.alive = YES; + pbad->mode = NORMAL; + pbad->dying = NO; + pbad->kind = kind; + pbad->base.x = x; + pbad->base.y = y; + pbad->base.xm = 1.3; + pbad->base.ym = 1.5; + pbad->dir = LEFT; + pbad->seen = NO; + timer_init(&pbad->timer); } void badguy_action(bad_guy_type* pbad) @@ -113,11 +122,11 @@ void badguy_action(bad_guy_type* pbad) } else if (pbad->mode == KICK) { - /* Obsolete - if (pbad->dir == RIGHT) - pbad->base.x = pbad->base.x + 16; - else if (pbad->dir == LEFT) - pbad->base.x = pbad->base.x - 16;*/ + /* Obsolete + if (pbad->dir == RIGHT) + pbad->base.x = pbad->base.x + 16; + else if (pbad->dir == LEFT) + pbad->base.x = pbad->base.x - 16;*/ } else if (pbad->mode == HELD) { /* FIXME: The pbad object shouldn't know about pplayer objects. */ @@ -522,7 +531,7 @@ void badguy_collision(bad_guy_type* pbad, void *p_c_object, int c_object) play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER); pbad->mode = FLAT; - pbad->base.xm = 4; + pbad->base.xm = 4; timer_start(&pbad->timer,10000); diff --git a/src/badguy.h b/src/badguy.h index a45a27ca5..1d04a3ce5 100644 --- a/src/badguy.h +++ b/src/badguy.h @@ -62,7 +62,7 @@ bitmask *bm_bsod; void badguy_create_bitmasks(); -void badguy_init(bad_guy_type* pbad); +void badguy_init(bad_guy_type* pbad, float x, float y, int kind); void badguy_action(bad_guy_type* pbad); void badguy_draw(bad_guy_type* pbad); void badguy_collision(bad_guy_type* pbad, void* p_c_object, int c_object); diff --git a/src/defines.h b/src/defines.h index a9ee056d2..b23e03c9b 100644 --- a/src/defines.h +++ b/src/defines.h @@ -65,8 +65,8 @@ /* Speed constraints: */ -#define MAX_WALK_XM 8 -#define MAX_RUN_XM 3 +#define MAX_WALK_XM 2.3 +#define MAX_RUN_XM 3.2 #define MAX_YM 10 #define MAX_JUMP_TIME 1000 #define MAX_LIVES 4 @@ -79,8 +79,8 @@ #define YM_FOR_JUMP 50 #define KILL_BOUNCE_YM 8 -#define SKID_XM 4 -#define SKID_TIME 8 +#define SKID_XM 2 +#define SKID_TIME 4 /* Size constraints: */ diff --git a/src/gameloop.c b/src/gameloop.c index 2851b5751..5bdd201ec 100644 --- a/src/gameloop.c +++ b/src/gameloop.c @@ -384,13 +384,6 @@ int i; bullet_action(&bullets[i]); } - - /* Handle background timer: */ - - if (super_bkgd_time) - super_bkgd_time--; - - /* Handle upgrades: */ for (i = 0; i < num_upgrades; i++) @@ -424,10 +417,10 @@ void game_draw() clearscreen(255, 255, 255); else { - if (super_bkgd_time == 0) - clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue); - else + if (timer_check(&super_bkgd_timer)) texture_draw(&img_super_bkgd, 0, 0, NO_UPDATE); + else + clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue); } /* Draw background: */ diff --git a/src/player.c b/src/player.c index d56d14763..a4665ba7b 100644 --- a/src/player.c +++ b/src/player.c @@ -441,17 +441,17 @@ void player_input(player_type *pplayer) if (pplayer->input.fire == UP) { - pplayer->base.xm = pplayer->base.xm + RUN_SPEED; + pplayer->base.xm = pplayer->base.xm + WALK_SPEED; - if (pplayer->base.xm > MAX_RUN_XM) - pplayer->base.xm = MAX_RUN_XM; + if (pplayer->base.xm > MAX_WALK_XM) + pplayer->base.xm = MAX_WALK_XM; } else if ( pplayer->input.fire == DOWN) { - pplayer->base.xm = pplayer->base.xm + WALK_SPEED; + pplayer->base.xm = pplayer->base.xm + RUN_SPEED; - if (pplayer->base.xm > MAX_WALK_XM) - pplayer->base.xm = MAX_WALK_XM; + if (pplayer->base.xm > MAX_RUN_XM) + pplayer->base.xm = MAX_RUN_XM; } } else @@ -493,17 +493,17 @@ void player_input(player_type *pplayer) if (pplayer->input.fire == UP) { - pplayer->base.xm = pplayer->base.xm - RUN_SPEED; + pplayer->base.xm = pplayer->base.xm - WALK_SPEED; - if (pplayer->base.xm < -MAX_RUN_XM) - pplayer->base.xm = -MAX_RUN_XM; + if (pplayer->base.xm < -MAX_WALK_XM) + pplayer->base.xm = -MAX_WALK_XM; } else if (pplayer->input.fire == DOWN) { - pplayer->base.xm = pplayer->base.xm - WALK_SPEED; + pplayer->base.xm = pplayer->base.xm - RUN_SPEED; - if (pplayer->base.xm < -MAX_WALK_XM) - pplayer->base.xm = -MAX_WALK_XM; + if (pplayer->base.xm < -MAX_RUN_XM) + pplayer->base.xm = -MAX_RUN_XM; } } else diff --git a/src/scene.c b/src/scene.c index bcbfb0129..40dedc098 100644 --- a/src/scene.c +++ b/src/scene.c @@ -46,47 +46,12 @@ free(bullets); void set_defaults(void) { - int i; - - /* Reset arrays: */ - - for (i = 0; i < num_bouncy_distros; i++) - bouncy_distros[i].base.alive = NO; - - for (i = 0; i < num_broken_bricks; i++) - broken_bricks[i].base.alive = NO; - - for (i = 0; i < num_bouncy_bricks; i++) - bouncy_bricks[i].base.alive = NO; - - for (i = 0; i < num_bad_guys; i++) - { - /*bad_guys[i].alive = NO;*/ - badguy_init(&bad_guys[i]); - } - - for (i = 0; i < num_floating_scores; i++) - floating_scores[i].base.alive = NO; - - for (i = 0; i < num_upgrades; i++) - { - /*upgrades[i].alive = NO;*/ - upgrade_init(&upgrades[i]); - } - - for (i = 0; i < num_bullets; i++) - { - /*bullets[i].alive = NO;*/ - bullet_init(&bullets[i]); - } - - /* Set defaults: */ scroll_x = 0; score_multiplier = 1; - super_bkgd_time = 0; + timer_init(&super_bkgd_timer); counting_distros = NO; distro_counter = 0; @@ -156,10 +121,7 @@ void add_bouncy_distro(float x, float y) if (found != -1) { - bouncy_distros[found].base.alive = YES; - bouncy_distros[found].base.x = x; - bouncy_distros[found].base.y = y; - bouncy_distros[found].base.ym = -6; + bouncy_distro_init(&bouncy_distros[found],x,y); } } @@ -168,11 +130,11 @@ void add_bouncy_distro(float x, float y) void add_broken_brick(float x, float y) { - add_broken_brick_piece(x, y, -4, -16); - add_broken_brick_piece(x, y + 16, -6, -12); + add_broken_brick_piece(x, y, -1, -4); + add_broken_brick_piece(x, y + 16, -1.5, -3); - add_broken_brick_piece(x + 16, y, 4, -16); - add_broken_brick_piece(x + 16, y + 16, 6, -12); + add_broken_brick_piece(x + 16, y, 1, -4); + add_broken_brick_piece(x + 16, y + 16, 1.5, -3); } @@ -199,11 +161,7 @@ void add_broken_brick_piece(float x, float y, float xm, float ym) if (found != -1) { - broken_bricks[found].base.alive = YES; - broken_bricks[found].base.x = x; - broken_bricks[found].base.y = y; - broken_bricks[found].base.xm = xm; - broken_bricks[found].base.ym = ym; + broken_brick_init(&broken_bricks[found], x, y, xm, ym); } } @@ -231,12 +189,7 @@ void add_bouncy_brick(float x, float y) if (found != -1) { - bouncy_bricks[found].base.alive = YES; - bouncy_bricks[found].base.x = x; - bouncy_bricks[found].base.y = y; - bouncy_bricks[found].offset = 0; - bouncy_bricks[found].offset_m = -BOUNCY_BRICK_SPEED; - bouncy_bricks[found].shape = shape(x, y); + bouncy_brick_init(&bouncy_bricks[found],x,y); } } @@ -259,23 +212,12 @@ void add_bad_guy(float x, float y, int kind) { ++num_bad_guys; bad_guys = realloc(bad_guys,num_bad_guys*sizeof(bad_guy_type)); - badguy_init(&bad_guys[num_bad_guys-1]); found = num_bad_guys - 1; } if (found != -1) { - bad_guys[found].base.alive = YES; - bad_guys[found].mode = NORMAL; - bad_guys[found].dying = NO; - bad_guys[found].kind = kind; - bad_guys[found].base.x = x; - bad_guys[found].base.y = y; - bad_guys[found].base.xm = 1.3; - bad_guys[found].base.ym = 1.5; - bad_guys[found].dir = LEFT; - bad_guys[found].seen = NO; - timer_init(&bad_guys[found].timer); + badguy_init(&bad_guys[found], x, y, kind); } } @@ -283,12 +225,9 @@ void add_bad_guy(float x, float y, int kind) void add_upgrade(float x, float y, int kind) { - int i, r, found; - /* we use this pointer to check, if realloc() returned a new memory address */ - upgrade_type * pointee = upgrades; + int i, found; found = -1; - r = 0; for (i = 0; i < num_upgrades && found == -1; i++) { @@ -300,29 +239,12 @@ void add_upgrade(float x, float y, int kind) { ++num_upgrades; upgrades = realloc(upgrades,num_upgrades*sizeof(upgrade_type)); - if(upgrades != pointee) - r = 1; - upgrade_init(&upgrades[num_upgrades-1]); found = num_upgrades - 1; } if (found != -1) { - if(r == 1) - { - for (i = 0; i < num_upgrades && found == -1; i++) - { - upgrade_init(&upgrades[i]); - } - } - upgrade_init(&upgrades[found]); - upgrades[found].base.alive = YES; - upgrades[found].kind = kind; - upgrades[found].base.x = x; - upgrades[found].base.y = y; - upgrades[found].base.xm = 2; - upgrades[found].base.ym = -2; - upgrades[found].base.height = 0; + upgrade_init(&upgrades[found], x, y, kind); } } @@ -344,28 +266,12 @@ void add_bullet(float x, float y, float xm, int dir) { ++num_bullets; bullets = realloc(bullets,num_bullets*sizeof(bullet_type)); - bullet_init(&bullets[num_bullets-1]); found = num_bullets - 1; } if (found != -1) { - bullet_init(&bullets[found]); - bullets[found].base.alive = YES; - - if (dir == RIGHT) - { - bullets[found].base.x = x + 32; - bullets[found].base.xm = BULLET_XM + xm; - } - else - { - bullets[found].base.x = x; - bullets[found].base.xm = -BULLET_XM + xm; - } - - bullets[found].base.y = y; - bullets[found].base.ym = BULLET_STARTING_YM; + bullet_init(&bullets[found], x, y, xm, dir); play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER); } diff --git a/src/scene.h b/src/scene.h index caa87932c..0242e01cc 100644 --- a/src/scene.h +++ b/src/scene.h @@ -23,8 +23,8 @@ #define FRAME_RATE 10 // 100 Frames per second (10ms) int score, highscore, distros, level, next_level, game_pause, -done, quit, score_multiplier, super_bkgd_time, endpos, -counting_distros, distro_counter; +done, quit, score_multiplier, endpos, counting_distros, distro_counter; +timer_type super_bkgd_timer; float scroll_x; int frame; bouncy_distro_type *bouncy_distros; diff --git a/src/special.c b/src/special.c index 66877fe30..eff54e574 100644 --- a/src/special.c +++ b/src/special.c @@ -25,12 +25,26 @@ void create_special_bitmasks() bm_bullet = bitmask_create_SDL(img_bullet.sdl_surface); } -void bullet_init(bullet_type* pbullet) +void bullet_init(bullet_type* pbullet, float x, float y, float xm, int dir) { pbullet->base.width = 4; pbullet->base.height = 4; pbullet->base.updated = SDL_GetTicks(); - pbullet->base.alive = NO; + pbullet->base.alive = YES; + + if (dir == RIGHT) + { + pbullet->base.x = x + 32; + pbullet->base.xm = BULLET_XM + xm; + } + else + { + pbullet->base.x = x; + pbullet->base.xm = -BULLET_XM + xm; + } + + pbullet->base.y = y; + pbullet->base.ym = BULLET_STARTING_YM; } void bullet_action(bullet_type* pbullet) @@ -87,12 +101,18 @@ void bullet_collision(bullet_type* pbullet, int c_object) } -void upgrade_init(upgrade_type *pupgrade) +void upgrade_init(upgrade_type *pupgrade, float x, float y, int kind) { pupgrade->base.width = 32; pupgrade->base.height = 0; + pupgrade->base.alive = YES; + pupgrade->kind = kind; + pupgrade->base.x = x; + pupgrade->base.y = y; + pupgrade->base.xm = 2; + pupgrade->base.ym = -2; + pupgrade->base.height = 0; pupgrade->base.updated = SDL_GetTicks(); - pupgrade->base.alive = NO; } void upgrade_action(upgrade_type *pupgrade) @@ -148,8 +168,8 @@ void upgrade_action(upgrade_type *pupgrade) if (pupgrade->base.x < scroll_x) pupgrade->base.alive = NO; - - } + + } } } @@ -204,7 +224,7 @@ void upgrade_draw(upgrade_type* pupgrade) void upgrade_collision(upgrade_type* pupgrade, void* p_c_object, int c_object) { -player_type* pplayer = NULL; + player_type* pplayer = NULL; switch (c_object) { @@ -213,7 +233,7 @@ player_type* pplayer = NULL; /* p_c_object is CO_PLAYER, so assign it to pplayer */ pplayer = p_c_object; - + pupgrade->base.alive = NO; /* Affect the player: */ @@ -222,19 +242,19 @@ player_type* pplayer = NULL; { play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER); pplayer->size = BIG; - super_bkgd_time = 8; + timer_start(&super_bkgd_timer, 350); } else if (pupgrade->kind == UPGRADE_COFFEE) { play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER); pplayer->got_coffee = YES; - super_bkgd_time = 4; + timer_start(&super_bkgd_timer, 250); } else if (pupgrade->kind == UPGRADE_HERRING) { play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER); timer_start(&tux.invincible_timer,TUX_INVINCIBLE_TIME); - super_bkgd_time = 4; + timer_start(&super_bkgd_timer, 250); /* play the herring song ^^ */ if (current_music != HURRYUP_MUSIC) { diff --git a/src/special.h b/src/special.h index 1a729066b..2c9661e89 100644 --- a/src/special.h +++ b/src/special.h @@ -42,12 +42,12 @@ void create_special_bitmasks(); texture_type img_golden_herring; -void upgrade_init(upgrade_type *pupgrade); +void upgrade_init(upgrade_type *pupgrade, float x, float y, int kind); void upgrade_action(upgrade_type *pupgrade); void upgrade_draw(upgrade_type *pupgrade); void upgrade_collision(upgrade_type *pupgrade, void* p_c_object, int c_object); -void bullet_init(bullet_type *pbullet); +void bullet_init(bullet_type* pbullet, float x, float y, float xm, int dir); void bullet_action(bullet_type *pbullet); void bullet_draw(bullet_type *pbullet); void bullet_collision(bullet_type *pbullet, int c_object); diff --git a/src/type.c b/src/type.c index de070664f..e18a4757c 100644 --- a/src/type.c +++ b/src/type.c @@ -23,5 +23,5 @@ double get_frame_ratio(base_type* pbase) unsigned int cur_time = SDL_GetTicks(); double frame_ratio = (float)(cur_time-pbase->updated)/(float)(FRAME_RATE); pbase->updated = cur_time; - return frame_ratio; + return (frame_ratio == 0 ? 1 : frame_ratio); } diff --git a/src/world.c b/src/world.c index dd4fe6787..3b44d02d4 100644 --- a/src/world.c +++ b/src/world.c @@ -18,6 +18,15 @@ #include "defines.h" #include "world.h" + +void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y) +{ + pbouncy_distro->base.alive = YES; + pbouncy_distro->base.x = x; + pbouncy_distro->base.y = y; + pbouncy_distro->base.ym = -6; +} + void bouncy_distro_action(bouncy_distro_type* pbouncy_distro) { if (pbouncy_distro->base.alive) @@ -42,16 +51,27 @@ void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro) } } +void broken_brick_init(broken_brick_type* pbroken_brick, float x, float y, float xm, float ym) +{ + pbroken_brick->base.alive = YES; + pbroken_brick->base.x = x; + pbroken_brick->base.y = y; + pbroken_brick->base.xm = xm; + pbroken_brick->base.ym = ym; + timer_start(&pbroken_brick->timer,200); + pbroken_brick->base.updated = SDL_GetTicks(); +} + void broken_brick_action(broken_brick_type* pbroken_brick) { + double frame_ratio = get_frame_ratio(&pbroken_brick->base); + if (pbroken_brick->base.alive) { - pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm; - pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym; + pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio; + pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio; - pbroken_brick->base.ym++; - - if (pbroken_brick->base.ym >= 0) + if (!timer_check(&pbroken_brick->timer)) pbroken_brick->base.alive = NO; } } @@ -74,12 +94,27 @@ void broken_brick_draw(broken_brick_type* pbroken_brick) } } +void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y) +{ + pbouncy_brick->base.alive = YES; + pbouncy_brick->base.x = x; + pbouncy_brick->base.y = y; + pbouncy_brick->offset = 0; + pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED; + pbouncy_brick->shape = shape(x, y); + pbouncy_brick->base.updated = SDL_GetTicks(); +} + void bouncy_brick_action(bouncy_brick_type* pbouncy_brick) { + + double frame_ratio = get_frame_ratio(&pbouncy_brick->base); + if (pbouncy_brick->base.alive) { + pbouncy_brick->offset = (pbouncy_brick->offset + - pbouncy_brick->offset_m); + pbouncy_brick->offset_m * frame_ratio); /* Go back down? */ @@ -89,7 +124,7 @@ void bouncy_brick_action(bouncy_brick_type* pbouncy_brick) /* Stop bouncing? */ - if (pbouncy_brick->offset == 0) + if (pbouncy_brick->offset >= 0) pbouncy_brick->base.alive = NO; } } @@ -118,20 +153,24 @@ void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick) } } -void floating_score_init(floating_score_type* pfloating_score, int x, int y, int s) +void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s) { pfloating_score->base.alive = YES; pfloating_score->base.x = x; pfloating_score->base.y = y - 16; timer_start(&pfloating_score->timer,1000); pfloating_score->value = s; + pfloating_score->base.updated = SDL_GetTicks(); + } void floating_score_action(floating_score_type* pfloating_score) { +double frame_ratio = get_frame_ratio(&pfloating_score->base); + if (pfloating_score->base.alive) { - pfloating_score->base.y = pfloating_score->base.y - 2; + pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio; if(!timer_check(&pfloating_score->timer)) pfloating_score->base.alive = NO; diff --git a/src/world.h b/src/world.h index f754876e3..e85ccdaaa 100644 --- a/src/world.h +++ b/src/world.h @@ -24,20 +24,22 @@ bouncy_distro_type; texture_type img_distro[4]; -void bouncy_distro_init(bouncy_distro_type* pbouncy_distro); +void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y); void bouncy_distro_action(bouncy_distro_type* pbouncy_distro); void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro); void bouncy_distro_collision(bouncy_distro_type* pbouncy_distro, int c_object); #define BOUNCY_BRICK_MAX_OFFSET 8 -#define BOUNCY_BRICK_SPEED 4 +#define BOUNCY_BRICK_SPEED 0.9 typedef struct broken_brick_type { base_type base; + timer_type timer; } broken_brick_type; +void broken_brick_init(broken_brick_type* pbroken_brick, float x, float y, float xm, float ym); void broken_brick_action(broken_brick_type* pbroken_brick); void broken_brick_draw(broken_brick_type* pbroken_brick); @@ -50,6 +52,7 @@ typedef struct bouncy_brick_type } bouncy_brick_type; +void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y); void bouncy_brick_action(bouncy_brick_type* pbouncy_brick); void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick); @@ -61,7 +64,7 @@ typedef struct floating_score_type } floating_score_type; -void floating_score_init(floating_score_type* pfloating_score, int x, int y, int s); +void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s); void floating_score_action(floating_score_type* pfloating_score); void floating_score_draw(floating_score_type* pfloating_score); -- 2.11.0