From cf8bf29d39d117783c3068270a3564595b21a53a Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Mon, 12 Apr 2004 12:46:18 +0000 Subject: [PATCH] - added simple sprite class SVN-Revision: 503 --- src/Makefile.am | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/sprite.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sprite.h | 60 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 src/sprite.cpp create mode 100644 src/sprite.h diff --git a/src/Makefile.am b/src/Makefile.am index e0d711edf..e4fe0953e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,74 @@ bin_PROGRAMS = supertux supertux_SOURCES = \ -badguy.cpp badguy.h bitmask.cpp bitmask.h button.cpp button.h collision.cpp collision.h configfile.cpp configfile.h defines.h gameloop.cpp gameloop.h globals.cpp globals.h high_scores.cpp high_scores.h intro.cpp intro.h level.cpp level.h leveleditor.cpp leveleditor.h lispreader.cpp lispreader.h menu.cpp menu.h particlesystem.cpp particlesystem.h physic.cpp physic.h player.cpp player.h scene.cpp scene.h screen.cpp screen.h setup.cpp setup.h sound.cpp sound.h special.cpp special.h supertux.cpp supertux.h text.cpp text.h texture.cpp texture.h timer.cpp timer.h title.cpp title.h type.cpp type.h world.cpp world.h worldmap.cpp worldmap.h tile.h tile.cpp mousecursor.cpp mousecursor.h resources.h resources.cpp gameobjs.h gameobjs.cpp +badguy.cpp \ +badguy.h \ +bitmask.cpp \ +bitmask.h \ +button.cpp \ +button.h \ +collision.cpp \ +collision.h \ +configfile.cpp \ +configfile.h \ +defines.h \ +gameloop.cpp \ +gameloop.h \ +globals.cpp \ +globals.h \ +high_scores.cpp \ +high_scores.h \ +intro.cpp \ +intro.h \ +level.cpp \ +level.h \ +leveleditor.cpp \ +leveleditor.h \ +lispreader.cpp \ +lispreader.h \ +menu.cpp \ +menu.h \ +particlesystem.cpp \ +particlesystem.h \ +physic.cpp \ +physic.h \ +player.cpp \ +player.h \ +scene.cpp \ +scene.h \ +screen.cpp \ +screen.h \ +setup.cpp \ +setup.h \ +sound.cpp \ +sound.h \ +special.cpp \ +special.h \ +supertux.cpp \ +supertux.h \ +text.cpp \ +text.h \ +texture.cpp \ +texture.h \ +timer.cpp \ +timer.h \ +title.cpp \ +title.h \ +type.cpp \ +type.h \ +world.cpp \ +world.h \ +worldmap.cpp \ +worldmap.h \ +tile.h \ +tile.cpp \ +mousecursor.cpp \ +mousecursor.h \ +resources.h \ +resources.cpp \ +gameobjs.h \ +gameobjs.cpp \ +sprite.h \ +sprite.cpp # EOF # -noinst_HEADERS = diff --git a/src/sprite.cpp b/src/sprite.cpp new file mode 100644 index 000000000..f7aed2c45 --- /dev/null +++ b/src/sprite.cpp @@ -0,0 +1,76 @@ +// $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. + +#include +#include "globals.h" +#include "sprite.h" + +Sprite::Sprite(lisp_object_t* cur) +{ + init_defaults(); + + LispReader reader(cur); + + reader.read_int("x-hotspot", &x_hotspot); + reader.read_int("y-hotspot", &y_hotspot); + reader.read_float("fps", &fps); + std::vector images; + reader.read_string_vector("images", &images); + surfaces.resize(images.size()); + + for(std::vector::size_type i = 0; i < images.size(); ++i) + { + texture_load(&surfaces[i], datadir + "/images/" + images[i], USE_ALPHA); + } +} + +void +Sprite::init_defaults() +{ + x_hotspot = 0; + y_hotspot = 0; + fps = 15; + time = 0; + frame_delay = 1000.0f/fps; +} + +void +Sprite::update(float delta) +{ + time += 10*delta; + //std::cout << "Delta: " << delta << std::endl; +} + +void +Sprite::draw(int x, int 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;*/ + if (frame < surfaces.size()) + texture_draw(&surfaces[frame], x - x_hotspot, y - y_hotspot); +} + +/* EOF */ diff --git a/src/sprite.h b/src/sprite.h new file mode 100644 index 000000000..dcd4b72a3 --- /dev/null +++ b/src/sprite.h @@ -0,0 +1,60 @@ +// $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_HXX +#define HEADER_SPRITE_HXX + +#include +#include +#include "lispreader.h" +#include "texture.h" + +class Sprite +{ + private: + int x_hotspot; + int y_hotspot; + + /** Frames per second */ + float fps; + + /** Number of seconds that a frame is displayed until it is switched + to the next frame */ + float frame_delay; + + float time; + + std::vector surfaces; + + void init_defaults(); + public: + /** cur has to be a pointer to data in the form of ((x-hotspot 5) + (y-hotspot 10) ...) */ + Sprite(lisp_object_t* cur); + + /** Update the sprite and process to the next frame */ + void update(float delta); + void draw(int x, int y); +}; + +#endif + +/* Local Variables: */ +/* mode:c++ */ +/* End */ -- 2.11.0