- added simple sprite class
authorIngo Ruhnke <grumbel@gmx.de>
Mon, 12 Apr 2004 12:46:18 +0000 (12:46 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Mon, 12 Apr 2004 12:46:18 +0000 (12:46 +0000)
SVN-Revision: 503

src/Makefile.am
src/sprite.cpp [new file with mode: 0644]
src/sprite.h [new file with mode: 0644]

index e0d711e..e4fe095 100644 (file)
@@ -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 (file)
index 0000000..f7aed2c
--- /dev/null
@@ -0,0 +1,76 @@
+//  $Id$
+//
+//  SuperTux
+//  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
+//
+//  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 "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<std::string> images;
+  reader.read_string_vector("images", &images);
+  surfaces.resize(images.size());
+
+  for(std::vector<std::string>::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<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;*/
+  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 (file)
index 0000000..dcd4b72
--- /dev/null
@@ -0,0 +1,60 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
+//
+//  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 <string>
+#include <vector>
+#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<texture_type> 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 */