From: Ingo Ruhnke Date: Sat, 17 Apr 2004 10:48:04 +0000 (+0000) Subject: - moved badguys to use sprite class X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=5af08553c4cfa80081b85909b4a9cdc3b01f9cb9;p=supertux.git - moved badguys to use sprite class SVN-Revision: 539 --- diff --git a/data/supertux.strf b/data/supertux.strf new file mode 100644 index 000000000..64b0e2990 --- /dev/null +++ b/data/supertux.strf @@ -0,0 +1,162 @@ +(supertux-resources + (sprite (name "smalltux") + (fps 15.0) + (x-hotspot 5) + (y-hotspot 9) + (images "shared/smalltux-right-1.png" + "shared/smalltux-right-2.png" + "shared/smalltux-right-3.png" + "shared/smalltux-right-4.png" + "shared/smalltux-right-5.png" + "shared/smalltux-right-6.png" + "shared/smalltux-right-7.png" + "shared/smalltux-right-8.png")) + (sprite (name "bsod-left") + (images "shared/bsod-left-0.png" + "shared/bsod-left-1.png" + "shared/bsod-left-2.png" + "shared/bsod-left-3.png")) + + (sprite (name "bsod-right") + (images "shared/bsod-right-0.png" + "shared/bsod-right-1.png" + "shared/bsod-right-2.png" + "shared/bsod-right-3.png")) + + (sprite (name "bsod-squished-left") + (images "shared/bsod-squished-left.png")) + + (sprite (name "bsod-squished-right") + (images "shared/bsod-squished-right.png")) + + (sprite (name "bsod-falling-left") + (images "shared/bsod-falling-left.png")) + + (sprite (name "bsod-falling-right") + (images "shared/bsod-falling-right.png")) + + (sprite (name "laptop-left") + (images "shared/mriceblock-left-0.png" + "shared/mriceblock-left-1.png" + "shared/mriceblock-left-2.png" + "shared/mriceblock-left-1.png")) + + (sprite (name "laptop-right") + (images "shared/mriceblock-right-0.png" + "shared/mriceblock-right-1.png" + "shared/mriceblock-right-2.png" + "shared/mriceblock-right-1.png")) + + (sprite (name "laptop-flat-left") + (images "shared/laptop-flat-left.png")) + + (sprite (name "laptop-flat-right") + (images "shared/laptop-flat-right.png")) + + (sprite (name "laptop-falling-left") + (images "shared/laptop-falling-left.png")) + + (sprite (name "laptop-falling-right") + (images "shared/laptop-falling-right.png")) + + (sprite (name "img-jumpy-left-up") + (images "shared/jumpy-left-up-0.png")) + (sprite (name "img-jumpy-left-down") + (images "shared/jumpy-left-down-0.png")) + (sprite (name "img-jumpy-middle-up") + (images "shared/jumpy-left-middle-0.png")) + + (sprite (name "mrbomb-left") + (images "shared/mrbomb-left-0.png") + (images "shared/mrbomb-left-1.png") + (images "shared/mrbomb-left-2.png") + (images "shared/mrbomb-left-3.png")) + + (sprite (name "mrbomb-right") + (images "shared/mrbomb-right-0.png") + (images "shared/mrbomb-right-1.png") + (images "shared/mrbomb-right-2.png") + (images "shared/mrbomb-right-3.png")) + + + (sprite (name "mrbomb-ticking-left") + (images "shared/mrbombx-left-0.png")) + + (sprite (name "mrbomb-ticking-right") + (images "shared/mrbombx-right-0.png")) + + (sprite (name "mrbomb-explosion") + (images "shared/mrbomb-explosion.png")) + + (sprite (name "stalactite") + (images "shared/stalactite.png")) + + (sprite (name "stalactite-broken") + (images "shared/stalactite-broken.png")) + + (sprite (name "flame") + (images "shared/flame-0.png" + "shared/flame-1.png")) + + (sprite (name "fish") + (images "shared/fish-left-0.png" + "shared/fish-left-1.png")) + + (sprite (name "fish") + (images "shared/fish-down-0.png")) + + (sprite (name "bouncingsnowball-left") + (images "shared/bouncingsnowball-left-0.png" + "shared/bouncingsnowball-left-1.png" + "shared/bouncingsnowball-left-2.png" + "shared/bouncingsnowball-left-3.png" + "shared/bouncingsnowball-left-4.png" + "shared/bouncingsnowball-left-5.png")) + + (sprite (name "bouncingsnowball-right") + (images "shared/bouncingsnowball-right-0.png" + "shared/bouncingsnowball-right-1.png" + "shared/bouncingsnowball-right-2.png" + "shared/bouncingsnowball-right-3.png" + "shared/bouncingsnowball-right-4.png" + "shared/bouncingsnowball-right-5.png")) + + (sprite (name "bouncingsnowball-squished") + (images "shared/bsod-squished-left.png")) + + (sprite (name "flyingsnowball") + (images "shared/flyingsnowball-left-0.png" + "shared/flyingsnowball-left-1.png")) + + (sprite (name "flyingsnowball-squished") + (images "shared/bsod-squished-left.png")) + + (sprite (name "spiky-left") + (images "shared/spiky-left-0.png" + "shared/spiky-left-1.png" + "shared/spiky-left-2.png")) + (sprite (name "spiky-right") + (images "shared/spiky-right-0.png" + "shared/spiky-right-1.png" + "shared/spiky-right-2.png")) + + (sprite (name "snowball-left") + (images "shared/snowball-left-0.png" + "shared/snowball-left-1.png" + "shared/snowball-left-2.png" + "shared/snowball-left-1.png")) + + (sprite (name "snowball-right") + (images "shared/snowball-right-0.png" + "shared/snowball-right-1.png" + "shared/snowball-right-2.png" + "shared/snowball-right-1.png")) + + (sprite (name "bsod-squished-left") + (images "shared/bsod-squished-left.png")) + + (sprite (name "bsod-squished-right") + (images "shared/bsod-squished-right.png")) + ) + +;; EOF ;; diff --git a/src/Makefile.am b/src/Makefile.am index 81d52a67e..4f05fa78d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -67,6 +67,8 @@ resources.cpp \ gameobjs.h \ gameobjs.cpp \ sprite.h \ -sprite.cpp +sprite.cpp \ +sprite_manager.cpp \ +sprite_manager.h # EOF # diff --git a/src/badguy.cpp b/src/badguy.cpp index 09a522fb3..4c30fe616 100644 --- a/src/badguy.cpp +++ b/src/badguy.cpp @@ -20,43 +20,45 @@ #include "screen.h" #include "world.h" #include "tile.h" - -Surface* img_bsod_squished_left[1]; -Surface* img_bsod_squished_right[1]; -Surface* img_bsod_falling_left[1]; -Surface* img_bsod_falling_right[1]; -Surface* img_laptop_flat_left[1]; -Surface* img_laptop_flat_right[1]; -Surface* img_laptop_falling_left[1]; -Surface* img_laptop_falling_right[1]; -Surface* img_bsod_left[4]; -Surface* img_bsod_right[4]; -Surface* img_laptop_left[4]; -Surface* img_laptop_right[4]; -Surface* img_jumpy_left_up; -Surface* img_jumpy_left_down; -Surface* img_jumpy_left_middle; -Surface* img_mrbomb_left[4]; -Surface* img_mrbomb_right[4]; -Surface* img_mrbomb_ticking_left[1]; -Surface* img_mrbomb_ticking_right[1]; -Surface* img_mrbomb_explosion[1]; -Surface* img_stalactite[1]; -Surface* img_stalactite_broken[1]; -Surface* img_flame[2]; -Surface* img_fish[2]; -Surface* img_fish_down[1]; -Surface* img_bouncingsnowball_left[6]; -Surface* img_bouncingsnowball_right[6]; -Surface* img_bouncingsnowball_squished[1]; -Surface* img_flyingsnowball[2]; -Surface* img_flyingsnowball_squished[1]; -Surface* img_spiky_left[3]; -Surface* img_spiky_right[3]; -Surface* img_snowball_left[4]; -Surface* img_snowball_right[4]; -Surface* img_snowball_squished_left[1]; -Surface* img_snowball_squished_right[1]; +#include "resources.h" +#include "sprite_manager.h" + +Sprite* img_bsod_squished_left; +Sprite* img_bsod_squished_right; +Sprite* img_bsod_falling_left; +Sprite* img_bsod_falling_right; +Sprite* img_laptop_flat_left; +Sprite* img_laptop_flat_right; +Sprite* img_laptop_falling_left; +Sprite* img_laptop_falling_right; +Sprite* img_bsod_left; +Sprite* img_bsod_right; +Sprite* img_laptop_left; +Sprite* img_laptop_right; +Sprite* img_jumpy_left_up; +Sprite* img_jumpy_left_down; +Sprite* img_jumpy_left_middle; +Sprite* img_mrbomb_left; +Sprite* img_mrbomb_right; +Sprite* img_mrbomb_ticking_left; +Sprite* img_mrbomb_ticking_right; +Sprite* img_mrbomb_explosion; +Sprite* img_stalactite; +Sprite* img_stalactite_broken; +Sprite* img_flame; +Sprite* img_fish; +Sprite* img_fish_down; +Sprite* img_bouncingsnowball_left; +Sprite* img_bouncingsnowball_right; +Sprite* img_bouncingsnowball_squished; +Sprite* img_flyingsnowball; +Sprite* img_flyingsnowball_squished; +Sprite* img_spiky_left; +Sprite* img_spiky_right; +Sprite* img_snowball_left; +Sprite* img_snowball_right; +Sprite* img_snowball_squished_left; +Sprite* img_snowball_squished_right; BadGuyKind badguykind_from_string(const std::string& str) { @@ -150,47 +152,47 @@ BadGuy::init(float x, float y, BadGuyKind kind_) animation_speed = 1; animation_length = 1; animation_offset = 0; - texture_left = texture_right = 0; + sprite_left = sprite_right = 0; physic.reset(); timer.init(true); if(kind == BAD_BSOD) { physic.set_velocity(-1.3, 0); - set_texture(img_bsod_left, img_bsod_right, 4); + set_sprite(img_bsod_left, img_bsod_right, 4); } else if(kind == BAD_MRBOMB) { physic.set_velocity(-1.3, 0); - set_texture(img_mrbomb_left, img_mrbomb_right, 4); + set_sprite(img_mrbomb_left, img_mrbomb_right, 4); } else if (kind == BAD_LAPTOP) { physic.set_velocity(-1.3, 0); - set_texture(img_laptop_left, img_laptop_right, 4, 5); + set_sprite(img_laptop_left, img_laptop_right, 4, 5); } else if(kind == BAD_MONEY) { - set_texture(&img_jumpy_left_up, &img_jumpy_left_up, 1); + set_sprite(img_jumpy_left_up, img_jumpy_left_up, 1); } else if(kind == BAD_BOMB) { - set_texture(img_mrbomb_ticking_left, img_mrbomb_ticking_right, 1); + set_sprite(img_mrbomb_ticking_left, img_mrbomb_ticking_right, 1); // hack so that the bomb doesn't hurt until it expldes... dying = DYING_SQUISHED; } else if(kind == BAD_FLAME) { base.ym = 0; // we misuse base.ym as angle for the flame physic.enable_gravity(false); - set_texture(img_flame, img_flame, 2, 0.5); + set_sprite(img_flame, img_flame, 2, 0.5); } else if(kind == BAD_BOUNCINGSNOWBALL) { physic.set_velocity(-1.3, 0); - set_texture(img_bouncingsnowball_left, img_bouncingsnowball_right, 6); + set_sprite(img_bouncingsnowball_left, img_bouncingsnowball_right, 6); } else if(kind == BAD_STALACTITE) { physic.enable_gravity(false); - set_texture(img_stalactite, img_stalactite, 1); + set_sprite(img_stalactite, img_stalactite, 1); } else if(kind == BAD_FISH) { - set_texture(img_fish, img_fish, 2, 1); + set_sprite(img_fish, img_fish, 2, 1); physic.enable_gravity(true); } else if(kind == BAD_FLYINGSNOWBALL) { - set_texture(img_flyingsnowball, img_flyingsnowball, 2, 5); + set_sprite(img_flyingsnowball, img_flyingsnowball, 2, 5); physic.enable_gravity(false); } else if(kind == BAD_SPIKY) { physic.set_velocity(-1.3, 0); - set_texture(img_spiky_left, img_spiky_right, 3); + set_sprite(img_spiky_left, img_spiky_right, 3); } else if(kind == BAD_SNOWBALL) { physic.set_velocity(-1.3, 0); - set_texture(img_snowball_left, img_snowball_right, 4, 5); + set_sprite(img_snowball_left, img_snowball_right, 4, 5); } // if we're in a solid tile at start correct that now @@ -277,7 +279,7 @@ BadGuy::action_laptop(float frame_ratio) old_base = base; mode=KICK; - set_texture(img_laptop_flat_left, img_laptop_flat_right, 1); + set_sprite(img_laptop_flat_left, img_laptop_flat_right, 1); physic.set_velocity((dir == LEFT) ? -8 : 8, -8); play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER); } @@ -305,7 +307,7 @@ BadGuy::action_laptop(float frame_ratio) if(!timer.check()) { mode = NORMAL; - set_texture(img_laptop_left, img_laptop_right, 4, 5); + set_sprite(img_laptop_left, img_laptop_right, 4, 5); physic.set_velocity( (dir == LEFT) ? -1.3 : 1.3, 0); } } @@ -395,11 +397,11 @@ void BadGuy::action_money(float frame_ratio) { if (fabsf(physic.get_velocity_y()) < 2.5f) - set_texture(&img_jumpy_left_middle, &img_jumpy_left_middle, 1); + set_sprite(img_jumpy_left_middle, img_jumpy_left_middle, 1); else if (physic.get_velocity_y() < 0) - set_texture(&img_jumpy_left_up, &img_jumpy_left_up, 1); + set_sprite(img_jumpy_left_up, img_jumpy_left_up, 1); else - set_texture(&img_jumpy_left_down, &img_jumpy_left_down, 1); + set_sprite(img_jumpy_left_down, img_jumpy_left_down, 1); Player& tux = *World::current()->get_tux(); @@ -458,7 +460,7 @@ BadGuy::action_bomb(float frame_ratio) } else if(!timer.check()) { if(mode == BOMB_TICKING) { mode = BOMB_EXPLODE; - set_texture(img_mrbomb_explosion, img_mrbomb_explosion, 1); + set_sprite(img_mrbomb_explosion, img_mrbomb_explosion, 1); dying = DYING_NOT; // now the bomb hurts timer.start(EXPLODETIME); } else if(mode == BOMB_EXPLODE) { @@ -501,7 +503,7 @@ BadGuy::action_stalactite(float frame_ratio) timer.start(2000); dying = DYING_SQUISHED; mode = FLAT; - set_texture(img_stalactite_broken, img_stalactite_broken, 1); + set_sprite(img_stalactite_broken, img_stalactite_broken, 1); } } else if(mode == FLAT) { fall(); @@ -536,7 +538,7 @@ BadGuy::action_fish(float frame_ratio) && physic.get_velocity_y() <= 0 && mode == NORMAL) { mode = FISH_WAIT; - set_texture(0, 0); + set_sprite(0, 0); physic.set_velocity(0, 0); physic.enable_gravity(false); timer.start(WAITTIME); @@ -544,7 +546,7 @@ BadGuy::action_fish(float frame_ratio) else if(mode == FISH_WAIT && !timer.check()) { // jump again - set_texture(img_fish, img_fish, 2, 2); + set_sprite(img_fish, img_fish, 2, 2); animation_offset = global_frame_counter; // restart animation mode = NORMAL; physic.set_velocity(0, JUMPV); @@ -556,7 +558,7 @@ BadGuy::action_fish(float frame_ratio) collision_swept_object_map(&old_base, &base); if(physic.get_velocity_y() < 0) - set_texture(img_fish_down, img_fish_down); + set_sprite(img_fish_down, img_fish_down); } void @@ -748,23 +750,26 @@ BadGuy::draw() // Don't try to draw stuff that is outside of the screen if(base.x <= scroll_x - base.width || base.x >= scroll_x + screen->w) return; - if(texture_left == 0 || texture_right == 0) - return; + + if(sprite_left == 0 || sprite_right == 0) + { + std::cout << "BadGuy: Error no sprite loaded" << std::endl; + return; + } float global_frame = (float(global_frame_counter - animation_offset) / 10); global_frame *= animation_speed; - size_t frame = size_t(global_frame) % animation_length; - Surface* texture = - (dir == LEFT) ? texture_left[frame] : texture_right[frame]; - texture->draw(base.x - scroll_x, base.y); + //size_t frame = size_t(global_frame) % animation_length; + Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right; + sprite->draw(base.x - scroll_x, base.y); if (debug_mode) fillrect(base.x - scroll_x, base.y, base.width, base.height, 75,0,75, 150); } void -BadGuy::set_texture(Surface** left, Surface** right, - int nanimlength, float nanimspeed) +BadGuy::set_sprite(Sprite* left, Sprite* right, + int nanimlength, float nanimspeed) { if (1) { @@ -778,13 +783,13 @@ BadGuy::set_texture(Surface** left, Surface** right, // representation if(left != 0) { if(base.width == 0 && base.height == 0) { - base.width = left[0]->w; - base.height = left[0]->h; - } else if(base.width != left[0]->w || base.height != left[0]->h) { - base.x -= (left[0]->w - base.width) / 2; - base.y -= left[0]->h - base.height; - base.width = left[0]->w; - base.height = left[0]->h; + base.width = left->get_width(); + base.height = left->get_height(); + } else if(base.width != left->get_width() || base.height != left->get_height()) { + base.x -= (left->get_width() - base.width) / 2; + base.y -= left->get_height() - base.height; + base.width = left->get_width(); + base.height = left->get_height(); old_base = base; } } else { @@ -795,8 +800,8 @@ BadGuy::set_texture(Surface** left, Surface** right, animation_length = nanimlength; animation_speed = nanimspeed; animation_offset = 0; - texture_left = left; - texture_right = right; + sprite_left = left; + sprite_right = right; } void @@ -847,7 +852,7 @@ BadGuy::squish(Player* player) } else if(kind == BAD_BSOD) { squish_me(player); - set_texture(img_bsod_squished_left, img_bsod_squished_right, 1); + set_sprite(img_bsod_squished_left, img_bsod_squished_right, 1); physic.set_velocity(0, physic.get_velocity_y()); return; @@ -857,7 +862,7 @@ BadGuy::squish(Player* player) /* Flatten! */ play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER); mode = FLAT; - set_texture(img_laptop_flat_left, img_laptop_flat_right, 1); + set_sprite(img_laptop_flat_left, img_laptop_flat_right, 1); physic.set_velocity(0, physic.get_velocity_y()); timer.start(4000); @@ -874,7 +879,7 @@ BadGuy::squish(Player* player) } mode = KICK; - set_texture(img_laptop_flat_left, img_laptop_flat_right, 1); + set_sprite(img_laptop_flat_left, img_laptop_flat_right, 1); } make_player_jump(player); @@ -897,15 +902,15 @@ BadGuy::squish(Player* player) return; } else if(kind == BAD_BOUNCINGSNOWBALL) { squish_me(player); - set_texture(img_bouncingsnowball_squished,img_bouncingsnowball_squished,1); + set_sprite(img_bouncingsnowball_squished,img_bouncingsnowball_squished,1); return; } else if(kind == BAD_FLYINGSNOWBALL) { squish_me(player); - set_texture(img_flyingsnowball_squished,img_flyingsnowball_squished,1); + set_sprite(img_flyingsnowball_squished,img_flyingsnowball_squished,1); return; } else if(kind == BAD_SNOWBALL) { squish_me(player); - set_texture(img_snowball_squished_left, img_snowball_squished_right, 1); + set_sprite(img_snowball_squished_left, img_snowball_squished_right, 1); return; } } @@ -918,9 +923,9 @@ BadGuy::kill_me() dying = DYING_FALLING; if(kind == BAD_LAPTOP) - set_texture(img_laptop_falling_left, img_laptop_falling_right, 1); + set_sprite(img_laptop_falling_left, img_laptop_falling_right, 1); else if(kind == BAD_BSOD) - set_texture(img_bsod_falling_left, img_bsod_falling_right, 1); + set_sprite(img_bsod_falling_left, img_bsod_falling_right, 1); physic.enable_gravity(true); physic.set_velocity(physic.get_velocity_x(), 0); @@ -976,52 +981,89 @@ BadGuy::collision(void *p_c_object, int c_object, CollisionType type) void load_badguy_gfx() { + img_bsod_squished_left = sprite_manager->load("bsod-squished-left"); + img_bsod_squished_right = sprite_manager->load("bsod-squished-right"); + img_bsod_falling_left = sprite_manager->load("bsod-falling-left"); + img_bsod_falling_right = sprite_manager->load("bsod-falling-right"); + img_laptop_flat_left = sprite_manager->load("laptop-flat-left"); + img_laptop_flat_right = sprite_manager->load("laptop-flat-right"); + img_laptop_falling_left = sprite_manager->load("laptop-falling-left"); + img_laptop_falling_right = sprite_manager->load("laptop-falling-right"); + img_bsod_left = sprite_manager->load("bsod-left"); + img_bsod_right = sprite_manager->load("bsod-right"); + img_laptop_left = sprite_manager->load("laptop-left"); + img_laptop_right = sprite_manager->load("laptop-right"); + img_jumpy_left_up = sprite_manager->load("jumpy-left-up"); + img_jumpy_left_down = sprite_manager->load("jumpy-left-down"); + img_jumpy_left_middle = sprite_manager->load("jumpy-left-middle"); + img_mrbomb_left = sprite_manager->load("mrbomb-left"); + img_mrbomb_right = sprite_manager->load("mrbomb-right"); + img_mrbomb_ticking_left = sprite_manager->load("mrbomb-ticking-left"); + img_mrbomb_ticking_right = sprite_manager->load("mrbomb-ticking-right"); + img_mrbomb_explosion = sprite_manager->load("mrbomb-explosion"); + img_stalactite = sprite_manager->load("stalactite"); + img_stalactite_broken = sprite_manager->load("stalactite-broken"); + img_flame = sprite_manager->load("flame"); + img_fish = sprite_manager->load("fish"); + img_fish_down = sprite_manager->load("fish-down"); + img_bouncingsnowball_left = sprite_manager->load("bouncingsnowball-left"); + img_bouncingsnowball_right = sprite_manager->load("bouncingsnowball-right"); + img_bouncingsnowball_squished = sprite_manager->load("bouncingsnowball-squished"); + img_flyingsnowball = sprite_manager->load("flyingsnowball"); + img_flyingsnowball_squished = sprite_manager->load("flyingsnowball-squished"); + img_spiky_left = sprite_manager->load("spiky-left"); + img_spiky_right = sprite_manager->load("spiky-right"); + img_snowball_left = sprite_manager->load("snowball-left"); + img_snowball_right = sprite_manager->load("snowball-right"); + img_snowball_squished_left = sprite_manager->load("snowball-squished-left"); + img_snowball_squished_right = sprite_manager->load("snowball-squished-right"); +#if 0 /* (BSOD) */ img_bsod_left[0] = new Surface(datadir + "/images/shared/bsod-left-0.png", USE_ALPHA); img_bsod_left[1] = new Surface(datadir + - "/images/shared/bsod-left-1.png", - USE_ALPHA); + "/images/shared/bsod-left-1.png", + USE_ALPHA); img_bsod_left[2] = new Surface(datadir + - "/images/shared/bsod-left-2.png", - USE_ALPHA); + "/images/shared/bsod-left-2.png", + USE_ALPHA); img_bsod_left[3] = new Surface(datadir + - "/images/shared/bsod-left-3.png", - USE_ALPHA); + "/images/shared/bsod-left-3.png", + USE_ALPHA); img_bsod_right[0] = new Surface(datadir + - "/images/shared/bsod-right-0.png", - USE_ALPHA); + "/images/shared/bsod-right-0.png", + USE_ALPHA); img_bsod_right[1] = new Surface(datadir + - "/images/shared/bsod-right-1.png", - USE_ALPHA); + "/images/shared/bsod-right-1.png", + USE_ALPHA); img_bsod_right[2] = new Surface(datadir + - "/images/shared/bsod-right-2.png", - USE_ALPHA); + "/images/shared/bsod-right-2.png", + USE_ALPHA); img_bsod_right[3] = new Surface(datadir + - "/images/shared/bsod-right-3.png", - USE_ALPHA); + "/images/shared/bsod-right-3.png", + USE_ALPHA); img_bsod_squished_left[0] = new Surface(datadir + - "/images/shared/bsod-squished-left.png", - USE_ALPHA); + "/images/shared/bsod-squished-left.png", + USE_ALPHA); img_bsod_squished_right[0] = new Surface(datadir + - "/images/shared/bsod-squished-right.png", - USE_ALPHA); + "/images/shared/bsod-squished-right.png", + USE_ALPHA); img_bsod_falling_left[0] = new Surface(datadir + - "/images/shared/bsod-falling-left.png", - USE_ALPHA); + "/images/shared/bsod-falling-left.png", + USE_ALPHA); img_bsod_falling_right[0] = new Surface(datadir + - "/images/shared/bsod-falling-right.png", - USE_ALPHA); + "/images/shared/bsod-falling-right.png", + USE_ALPHA); /* (Laptop) */ @@ -1037,101 +1079,101 @@ void load_badguy_gfx() img_laptop_right[3] = new Surface(datadir + "/images/shared/mriceblock-right-1.png", USE_ALPHA); img_laptop_flat_left[0] = new Surface( - datadir + "/images/shared/laptop-flat-left.png", - USE_ALPHA); + datadir + "/images/shared/laptop-flat-left.png", + USE_ALPHA); img_laptop_flat_right[0] = new Surface(datadir + - "/images/shared/laptop-flat-right.png", - USE_ALPHA); + "/images/shared/laptop-flat-right.png", + USE_ALPHA); img_laptop_falling_left[0] = new Surface(datadir + - "/images/shared/laptop-falling-left.png", - USE_ALPHA); + "/images/shared/laptop-falling-left.png", + USE_ALPHA); img_laptop_falling_right[0] = new Surface(datadir + - "/images/shared/laptop-falling-right.png", - USE_ALPHA); + "/images/shared/laptop-falling-right.png", + USE_ALPHA); /* (Money) */ img_jumpy_left_up = new Surface(datadir + - "/images/shared/jumpy-left-up-0.png", - USE_ALPHA); + "/images/shared/jumpy-left-up-0.png", + USE_ALPHA); img_jumpy_left_down = new Surface(datadir + "/images/shared/jumpy-left-down-0.png", USE_ALPHA); img_jumpy_left_middle = new Surface(datadir + - "/images/shared/jumpy-left-middle-0.png", - USE_ALPHA); + "/images/shared/jumpy-left-middle-0.png", + USE_ALPHA); /* Mr. Bomb */ for(int i=0; i<4; ++i) { - char num[4]; - snprintf(num, 4, "%d", i); - img_mrbomb_left[i] = new Surface( - datadir + "/images/shared/mrbomb-left-" + num + ".png", USE_ALPHA); - img_mrbomb_right[i] = new Surface( - datadir + "/images/shared/mrbomb-right-" + num + ".png", USE_ALPHA); + char num[4]; + snprintf(num, 4, "%d", i); + img_mrbomb_left[i] = new Surface( + datadir + "/images/shared/mrbomb-left-" + num + ".png", USE_ALPHA); + img_mrbomb_right[i] = new Surface( + datadir + "/images/shared/mrbomb-right-" + num + ".png", USE_ALPHA); } img_mrbomb_ticking_left[0] = new Surface( - datadir + "/images/shared/mrbombx-left-0.png", USE_ALPHA); + datadir + "/images/shared/mrbombx-left-0.png", USE_ALPHA); img_mrbomb_ticking_right[0] = new Surface( - datadir + "/images/shared/mrbombx-right-0.png", USE_ALPHA); + datadir + "/images/shared/mrbombx-right-0.png", USE_ALPHA); img_mrbomb_explosion[0] = new Surface( - datadir + "/images/shared/mrbomb-explosion.png", USE_ALPHA); + datadir + "/images/shared/mrbomb-explosion.png", USE_ALPHA); /* stalactite */ img_stalactite[0] = new Surface( - datadir + "/images/shared/stalactite.png", USE_ALPHA); + datadir + "/images/shared/stalactite.png", USE_ALPHA); img_stalactite_broken[0] = new Surface( - datadir + "/images/shared/stalactite-broken.png", USE_ALPHA); + datadir + "/images/shared/stalactite-broken.png", USE_ALPHA); /* flame */ img_flame[0] = new Surface( - datadir + "/images/shared/flame-0.png", USE_ALPHA); + datadir + "/images/shared/flame-0.png", USE_ALPHA); img_flame[1] = new Surface( - datadir + "/images/shared/flame-1.png", USE_ALPHA); + datadir + "/images/shared/flame-1.png", USE_ALPHA); /* fish */ img_fish[0] = new Surface( - datadir + "/images/shared/fish-left-0.png", USE_ALPHA); + datadir + "/images/shared/fish-left-0.png", USE_ALPHA); img_fish[1] = new Surface( - datadir + "/images/shared/fish-left-1.png", USE_ALPHA); + datadir + "/images/shared/fish-left-1.png", USE_ALPHA); img_fish_down[0] = new Surface( - datadir + "/images/shared/fish-down-0.png", USE_ALPHA); + datadir + "/images/shared/fish-down-0.png", USE_ALPHA); /* bouncing snowball */ for(int i=0; i<6; ++i) { - char num[4]; - snprintf(num, 4, "%d", i); - img_bouncingsnowball_left[i] = new Surface( - datadir + "/images/shared/bouncingsnowball-left-" + num + ".png", - USE_ALPHA); - img_bouncingsnowball_right[i] = new Surface( - datadir + "/images/shared/bouncingsnowball-right-" + num + ".png", - USE_ALPHA); + char num[4]; + snprintf(num, 4, "%d", i); + img_bouncingsnowball_left[i] = new Surface( + datadir + "/images/shared/bouncingsnowball-left-" + num + ".png", + USE_ALPHA); + img_bouncingsnowball_right[i] = new Surface( + datadir + "/images/shared/bouncingsnowball-right-" + num + ".png", + USE_ALPHA); } img_bouncingsnowball_squished[0] = new Surface( - datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA); + datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA); /* flying snowball */ img_flyingsnowball[0] = new Surface( - datadir + "/images/shared/flyingsnowball-left-0.png", USE_ALPHA); + datadir + "/images/shared/flyingsnowball-left-0.png", USE_ALPHA); img_flyingsnowball[1] = new Surface( - datadir + "/images/shared/flyingsnowball-left-1.png", USE_ALPHA); + datadir + "/images/shared/flyingsnowball-left-1.png", USE_ALPHA); img_flyingsnowball_squished[0] = new Surface( - datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA); + datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA); /* spiky */ for(int i = 0; i < 3; ++i) { - char num[4]; - snprintf(num, 4, "%d", i); - img_spiky_left[i] = new Surface( - datadir + "/images/shared/spiky-left-" + num + ".png", - USE_ALPHA); - img_spiky_right[i] = new Surface( - datadir + "/images/shared/spiky-right-" + num + ".png", - USE_ALPHA); + char num[4]; + snprintf(num, 4, "%d", i); + img_spiky_left[i] = new Surface( + datadir + "/images/shared/spiky-left-" + num + ".png", + USE_ALPHA); + img_spiky_right[i] = new Surface( + datadir + "/images/shared/spiky-right-" + num + ".png", + USE_ALPHA); } /** snowball */ @@ -1146,79 +1188,14 @@ void load_badguy_gfx() img_snowball_right[3] = new Surface(datadir + "/images/shared/snowball-right-1.png", USE_ALPHA); img_snowball_squished_left[0] = new Surface( - datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA); + datadir + "/images/shared/bsod-squished-left.png", USE_ALPHA); img_snowball_squished_right[0] = new Surface( - datadir + "/images/shared/bsod-squished-right.png", USE_ALPHA); + datadir + "/images/shared/bsod-squished-right.png", USE_ALPHA); +#endif } void free_badguy_gfx() { - for (int i = 0; i < 4; i++) - { - delete img_bsod_left[i]; - delete img_bsod_right[i]; - } - - delete img_bsod_squished_left[0]; - delete img_bsod_squished_right[0]; - - delete img_bsod_falling_left[0]; - delete img_bsod_falling_right[0]; - - for (int i = 0; i < 4; i++) - { - delete img_laptop_left[i]; - delete img_laptop_right[i]; - } - - delete img_laptop_flat_left[0]; - delete img_laptop_flat_right[0]; - - delete img_laptop_falling_left[0]; - delete img_laptop_falling_right[0]; - - delete img_jumpy_left_up; - delete img_jumpy_left_down; - - for(int i = 0; i < 4; i++) { - delete img_mrbomb_left[i]; - delete img_mrbomb_right[i]; - } - - delete img_mrbomb_ticking_left[0]; - delete img_mrbomb_ticking_right[0]; - delete img_mrbomb_explosion[0]; - - delete img_stalactite[0]; - delete img_stalactite_broken[0]; - - delete img_flame[0]; - delete img_flame[1]; - - delete img_fish[0]; - delete img_fish[1]; - delete img_fish_down[0]; - - for(int i=0; i<6; ++i) { - delete img_bouncingsnowball_left[i]; - delete img_bouncingsnowball_right[i]; - } - delete img_bouncingsnowball_squished[0]; - - delete img_flyingsnowball[0]; - delete img_flyingsnowball[1]; - delete img_flyingsnowball_squished[0]; - - for(int i = 0; i<3; ++i) { - delete img_spiky_left[i]; - delete img_spiky_right[i]; - } - for(int i = 0; i<4; ++i) { - delete img_snowball_left[i]; - delete img_snowball_right[i]; - } - delete img_snowball_squished_left[0]; - delete img_snowball_squished_right[0]; } // EOF // diff --git a/src/badguy.h b/src/badguy.h index 5b60688a8..c95d706a6 100644 --- a/src/badguy.h +++ b/src/badguy.h @@ -20,13 +20,11 @@ #include "texture.h" #include "physic.h" #include "collision.h" +#include "sprite.h" -extern Surface* img_bsod_left[4]; -extern Surface* img_bsod_right[4]; -extern Surface* img_laptop_left[4]; -extern Surface* img_jumpy_left_up; -extern Surface* img_jumpy_left_down; -extern Surface* img_jumpy_left_middle; +extern Sprite* img_bsod_left; +extern Sprite* img_bsod_right; +extern Sprite* img_laptop_left; /* Enemy modes: */ enum { @@ -101,8 +99,9 @@ private: Timer timer; Physic physic; - Surface** texture_left; - Surface** texture_right; + Sprite* sprite_left; + Sprite* sprite_right; + int animation_offset; size_t animation_length; float animation_speed; @@ -156,7 +155,7 @@ private: /** squish ourself, give player score and set dying to DYING_SQICHED */ void squish_me(Player* player); /** set image of the badguy */ - void set_texture(Surface** left, Surface** right, + void set_sprite(Sprite* left, Sprite* right, int animlength = 1, float animspeed = 1); }; diff --git a/src/resources.cpp b/src/resources.cpp index 9dc9d2217..ed01dc6e4 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -5,6 +5,7 @@ #include "gameobjs.h" #include "special.h" #include "resources.h" +#include "sprite_manager.h" Surface* img_waves[3]; Surface* img_water; @@ -17,11 +18,15 @@ Surface* img_box_full; Surface* img_box_empty; Surface* img_red_glow; +SpriteManager* sprite_manager = 0; + /* Load graphics/sounds shared between all levels: */ void loadshared() { int i; + sprite_manager = new SpriteManager(datadir + "/supertux.strf"); + /* Tuxes: */ smalltux_stand_left = new Surface(datadir + "/images/shared/smalltux-left-6.png", USE_ALPHA); smalltux_stand_right = new Surface(datadir + "/images/shared/smalltux-right-6.png", USE_ALPHA); @@ -287,6 +292,8 @@ void loadshared() /* Free shared data: */ void unloadshared(void) { + delete sprite_manager; + int i; free_special_gfx(); diff --git a/src/resources.h b/src/resources.h index c4d2340af..8f41c1c01 100644 --- a/src/resources.h +++ b/src/resources.h @@ -1,6 +1,8 @@ #ifndef SUPERTUX_RESOURCES_H #define SUPERTUX_RESOURCES_H +class SpriteManager; + extern Surface* img_waves[3]; extern Surface* img_water; extern Surface* img_pole; @@ -13,6 +15,8 @@ extern Surface* img_box_empty; extern Surface* img_super_bkgd; extern Surface* img_red_glow; +extern SpriteManager* sprite_manager; + void loadshared(); void unloadshared(); diff --git a/src/sprite.cpp b/src/sprite.cpp index 871006f73..b5a091dbe 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -27,9 +27,10 @@ Sprite::Sprite(lisp_object_t* cur) LispReader reader(cur); + reader.read_string("name", &name); reader.read_int("x-hotspot", &x_hotspot); reader.read_int("y-hotspot", &y_hotspot); - reader.read_float("fps", &fps); + reader.read_float("fps", &fps); std::vector images; reader.read_string_vector("images", &images); surfaces.resize(images.size()); @@ -38,6 +39,8 @@ Sprite::Sprite(lisp_object_t* cur) { surfaces[i] = new Surface(datadir + "/images/" + images[i], USE_ALPHA); } + + frame_delay = 1000.0f/fps; } void @@ -45,32 +48,51 @@ Sprite::init_defaults() { x_hotspot = 0; y_hotspot = 0; - fps = 15; + fps = 10; time = 0; frame_delay = 1000.0f/fps; } void -Sprite::update(float delta) +Sprite::update(float /*delta*/) { - time += 10*delta; + //time += 10*delta; //std::cout << "Delta: " << delta << std::endl; } void -Sprite::draw(int x, int y) +Sprite::draw(float x, float y) { - unsigned int frame = static_cast(fmodf(time, surfaces.size()*frame_delay)/frame_delay); - - /* - std::cout << "Frame: " - << frame << " " - << time << " " - << surfaces.size() << " " - << frame_delay << " " - << static_cast(fmodf(time, surfaces.size()*frame_delay)/frame_delay) << std::endl;*/ + time = SDL_GetTicks(); + unsigned int frame = get_current_frame(); + if (frame < surfaces.size()) surfaces[frame]->draw(x - x_hotspot, y - y_hotspot); } +void +Sprite::reset() +{ + time = 0; +} + +int +Sprite::get_current_frame() const +{ + unsigned int frame = static_cast(fmodf(time, surfaces.size()*frame_delay)/frame_delay); + return frame % surfaces.size(); +} + +int +Sprite::get_width() const +{ + return surfaces[get_current_frame()]->w; +} + +int +Sprite::get_height() const +{ + return surfaces[get_current_frame()]->h; +} + /* EOF */ diff --git a/src/sprite.h b/src/sprite.h index 214b03656..c17439614 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -28,6 +28,8 @@ class Sprite { private: + std::string name; + int x_hotspot; int y_hotspot; @@ -47,10 +49,18 @@ class Sprite /** cur has to be a pointer to data in the form of ((x-hotspot 5) (y-hotspot 10) ...) */ Sprite(lisp_object_t* cur); + + void reset(); /** Update the sprite and process to the next frame */ void update(float delta); - void draw(int x, int y); + void draw(float x, float y); + + int get_current_frame() const; + + std::string get_name() const { return name; } + int get_width() const; + int get_height() const; }; #endif diff --git a/src/sprite_manager.cpp b/src/sprite_manager.cpp new file mode 100644 index 000000000..e0ac01209 --- /dev/null +++ b/src/sprite_manager.cpp @@ -0,0 +1,82 @@ +// $Id$ +// +// Pingus - A free Lemmings clone +// Copyright (C) 2002 Ingo Ruhnke +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include "lispreader.h" +#include "sprite_manager.h" + +SpriteManager::SpriteManager(const std::string& filename) +{ + load_resfile(filename); +} + +void +SpriteManager::load_resfile(const std::string& filename) +{ + lisp_object_t* cur = lisp_read_from_file(filename); + + if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-resources") != 0) + return; + cur = lisp_cdr(cur); + + while(cur) + { + lisp_object_t* el = lisp_car(cur); + + if (strcmp(lisp_symbol(lisp_car(el)), "sprite") == 0) + { + Sprite* sprite = new Sprite(lisp_cdr(el)); + + Sprites::iterator i = sprites.find(sprite->get_name()); + if (i == sprites.end()) + { + sprites[sprite->get_name()] = sprite; + } + else + { + delete i->second; + i->second = sprite; + std::cout << "Warning: dulpicate entry: '" << sprite->get_name() << "'" << std::endl; + } + } + else + { + std::cout << "SpriteManager: Unknown tag" << std::endl; + } + + cur = lisp_cdr(cur); + } +} + +Sprite* +SpriteManager::load(const std::string& name) +{ + Sprites::iterator i = sprites.find(name); + if (i != sprites.end()) + { + return i->second; + } + else + { + std::cout << "SpriteManager: Sprite '" << name << "' not found" << std::endl; + return 0; + } +} + +/* EOF */ diff --git a/src/sprite_manager.h b/src/sprite_manager.h new file mode 100644 index 000000000..1187adacc --- /dev/null +++ b/src/sprite_manager.h @@ -0,0 +1,42 @@ +// $Id$ +// +// SuperTux +// Copyright (C) 2004 Ingo Ruhnke +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef HEADER_SPRITE_MANAGER_HXX +#define HEADER_SPRITE_MANAGER_HXX + +#include +#include "sprite.h" + +class SpriteManager +{ + private: + typedef std::map Sprites; + Sprites sprites; + public: + SpriteManager(const std::string& filename); + + void load_resfile(const std::string& filename); + Sprite* load(const std::string& name); +}; + +#endif + +/* Local Variables: */ +/* mode:c++ */ +/* End: */