[Super-tux-commit] supertux/src sprite_manager.cpp,NONE,1.1 sprite_manager.h,NONE,1.1 Makefile.am,1.
Brought to you by:
wkendrick
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3202/src Modified Files: Makefile.am badguy.cpp badguy.h resources.cpp resources.h sprite.cpp sprite.h Added Files: sprite_manager.cpp sprite_manager.h Log Message: - moved badguys to use sprite class Index: resources.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/resources.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- resources.h 13 Apr 2004 02:19:13 -0000 1.4 +++ resources.h 17 Apr 2004 10:48:04 -0000 1.5 @@ -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_super_bkgd; extern Surface* img_red_glow; +extern SpriteManager* sprite_manager; + void loadshared(); void unloadshared(); Index: Makefile.am =================================================================== RCS file: /cvsroot/super-tux/supertux/src/Makefile.am,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- Makefile.am 13 Apr 2004 02:19:13 -0000 1.17 +++ Makefile.am 17 Apr 2004 10:48:03 -0000 1.18 @@ -67,6 +67,8 @@ gameobjs.h \ gameobjs.cpp \ sprite.h \ -sprite.cpp +sprite.cpp \ +sprite_manager.cpp \ +sprite_manager.h # EOF # --- NEW FILE: sprite_manager.cpp --- // $Id: sprite_manager.cpp,v 1.1 2004/04/17 10:48:04 grumbel Exp $ // // Pingus - A free Lemmings clone // Copyright (C) 2002 Ingo Ruhnke <gr...@gm...> // // 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 <iostream> #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 */ Index: sprite.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sprite.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- sprite.h 13 Apr 2004 12:25:22 -0000 1.3 +++ sprite.h 17 Apr 2004 10:48:04 -0000 1.4 @@ -28,6 +28,8 @@ class Sprite { private: + std::string name; + int x_hotspot; int y_hotspot; @@ -47,10 +49,18 @@ /** 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 Index: badguy.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/badguy.h,v retrieving revision 1.25 retrieving revision 1.26 diff -u -d -r1.25 -r1.26 --- badguy.h 16 Apr 2004 21:16:12 -0000 1.25 +++ badguy.h 17 Apr 2004 10:48:04 -0000 1.26 @@ -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 @@ 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 @@ /** 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); }; Index: badguy.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/badguy.cpp,v retrieving revision 1.34 retrieving revision 1.35 diff -u -d -r1.34 -r1.35 --- badguy.cpp 16 Apr 2004 21:16:12 -0000 1.34 +++ badguy.cpp 17 Apr 2004 10:48:04 -0000 1.35 @@ -20,43 +20,45 @@ #include "screen.h" #include "world.h" #include "tile.h" +#include "resources.h" +#include "sprite_manager.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]; +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 @@ 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 @@ 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 @@ 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 @@ 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 @@ } 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 @@ 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 @@ && 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 @@ 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 @@ 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 @@ // 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 @@ // 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 @@ 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 @@ } 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 @@ /* 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 @@ } 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 // Index: sprite.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sprite.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- sprite.cpp 13 Apr 2004 02:19:13 -0000 1.2 +++ sprite.cpp 17 Apr 2004 10:48:04 -0000 1.3 @@ -27,9 +27,10 @@ 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<std::string> images; reader.read_string_vector("images", &images); surfaces.resize(images.size()); @@ -38,6 +39,8 @@ { surfaces[i] = new Surface(datadir + "/images/" + images[i], USE_ALPHA); } + + frame_delay = 1000.0f/fps; } void @@ -45,32 +48,51 @@ { 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<int>(fmodf(time, surfaces.size()*frame_delay)/frame_delay); - - /* - std::cout << "Frame: " - << frame << " " - << time << " " - << surfaces.size() << " " - << frame_delay << " " - << static_cast<int>(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<int>(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 */ --- NEW FILE: sprite_manager.h --- // $Id: sprite_manager.h,v 1.1 2004/04/17 10:48:04 grumbel Exp $ // // SuperTux // Copyright (C) 2004 Ingo Ruhnke <gr...@gm...> // // 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 <map> #include "sprite.h" class SpriteManager { private: typedef std::map<std::string, Sprite*> 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: */ Index: resources.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/resources.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- resources.cpp 13 Apr 2004 02:19:13 -0000 1.8 +++ resources.cpp 17 Apr 2004 10:48:04 -0000 1.9 @@ -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_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 @@ /* Free shared data: */ void unloadshared(void) { + delete sprite_manager; + int i; free_special_gfx(); |