r92 - trunk/src

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Tue Feb 22 08:36:33 EST 2005


Author: jonas
Date: 2005-02-22 08:36:33 -0500 (Tue, 22 Feb 2005)
New Revision: 92

Added:
   trunk/src/animation.cpp
   trunk/src/animation.h
Log:
100l, forgot the animation.* stuff

Added: trunk/src/animation.cpp
===================================================================
--- trunk/src/animation.cpp	2005-02-20 21:39:26 UTC (rev 91)
+++ trunk/src/animation.cpp	2005-02-22 13:36:33 UTC (rev 92)
@@ -0,0 +1,65 @@
+#include "common.h"
+#include "animation.h"
+
+Animation::Animation(SDL_Surface* anim_image, Uint16 max_num, Uint16 total_time, bool an_once):
+  size(max_num),
+  time(total_time),
+  num(0),
+  tcurrent(0),
+  once(an_once) {
+    if (time==0 || once) running=false;
+    else running=true;
+    frame.image=anim_image;
+    w=(Uint16)((frame.image->w)/size);
+    h=frame.image->h;
+    frame.pos.x=0;
+    frame.pos.y=0;
+    frame.pos.w=w;
+    frame.pos.h=h;
+}
+
+Animation::Animation(Uint16 width, SDL_Surface* anim_image, Uint16 max_num, Uint16 total_time, bool an_once):
+  size(max_num),
+  time(total_time),
+  num(0),
+  tcurrent(0),
+  once(an_once) {
+    if (time==0 || once) running=false;
+    else running=true;
+    frame.image=anim_image;
+    w=width;
+    h=frame.image->h;
+    frame.pos.x=0;
+    frame.pos.y=0;
+    frame.pos.w=w;
+    frame.pos.h=h;
+}
+
+Animation::~Animation() { }
+
+bool Animation::updateAnim(Uint16 dt) {
+    if (!running) return false;
+    tcurrent+=dt;
+    if (tcurrent < time) {
+        num=(Uint16)((tcurrent*size)/time);
+        if (num>=size) num=0;
+    } else if (once || time==0) {
+        num=size;
+        tcurrent=time;
+        running=false;
+    } else {
+        num=0;
+        tcurrent=0;
+    }
+    frame.pos.x=num*w;
+    if (running) return true;
+    else return false;
+//The solution below would be better but it won't work with the 2nd constructor
+//    frame.pos.x=(Uint16)((num*frame.image->w)/size);
+}
+
+const Frame& Animation::setFrame(Uint16 num) {
+    frame.pos.x=num*w;
+    return frame;
+}
+

Added: trunk/src/animation.h
===================================================================
--- trunk/src/animation.h	2005-02-20 21:39:26 UTC (rev 91)
+++ trunk/src/animation.h	2005-02-22 13:36:33 UTC (rev 92)
@@ -0,0 +1,76 @@
+#ifndef _ANIMATION_H
+#define _ANIMATION_H 1
+
+/** \brief A container for frames.
+
+    Animation format: a horizontal strip (image) with (num) frames
+    \todo Change this to an offset matrix or something similar or give each row a different animation
+*/
+class Animation {
+    public:
+        /// \brief Initialize the animation
+        /// \param image Image used for the animation
+        /// \param max_num Number of frames (will devide the image horizontally accordingly)
+        /// \param total_time Duration of the animation, if set to 0 assume it's one still image
+        /// \param once If true it run the animation only once when called by \fn start()
+        Animation(SDL_Surface* image, Uint16 max_num=1, Uint16 total_time=0, bool once=false);
+        /// \brief Initialize the animation
+        /// \param width Image width used from the left side
+        /// \param image Image used for the animation
+        /// \param max_num Number of frames (will devide the image horizontally accordingly)
+        /// \param total_time Duration of the animation, if set to 0 assume it's one still image
+        /// \param once If true: Run the animation only once when called by \fn start()
+        Animation(Uint16 width, SDL_Surface* image, Uint16 max_num=1, Uint16 total_time=0, bool once=false);
+        ~Animation();
+        /// Updates a running animation and stops it if necessary
+        /// return True if the animation is still running
+        /// \todo This should be more advanced
+        bool updateAnim(Uint16 dt);
+        /// Updates the frame status of a frame series (usually when not running)
+        /// return Current frame
+        const Frame& setFrame(Uint16 nr);
+        /// Starts an (usually non running) animation
+        void start() {
+            num=0;
+            tcurrent=0;  
+            running=true;
+        }
+        const Frame& getFrame() const {
+            return frame;
+        }
+        Frame getFrame(Uint16 nr) const {
+            Frame newframe=frame;
+            newframe.pos.x=nr*w;
+            return newframe;
+        }
+        Uint16 getWidth() const {
+            return w;
+        }
+        Uint16 getHeight() const {
+            return h;
+        }
+        /// return True if the animation is running
+        bool isRunning() const {
+            return running;
+        }   
+    private:
+        /// Frame pointer
+        Frame frame;
+        /// Number of frames
+        Uint16 size;
+        /// Total time (in ms)
+        Uint16 time;  
+        /// Frame size
+        Uint16 w;
+        Uint16 h;
+        /// Current frame number
+        Uint16 num;
+        /// Current time position (in ms)
+        Uint16 tcurrent;
+        /// frame series or running?
+        bool running;
+        /// play the animation only once?
+        bool once;
+};
+
+#endif




More information about the lostpenguins-commits mailing list