From 96680b22a0068e230deb2ce39fa74fbc03b456c9 Mon Sep 17 00:00:00 2001 From: James Turk Date: Tue, 25 Nov 2003 01:31:36 +0000 Subject: [PATCH] ZAnimation --- include/ZE_ZAnimation.h | 75 ++++++++++++++++++++ src/ZE_ZAnimation.cpp | 149 ++++++++++++++++++++++++++++++++++++++++ vc7/ZEngine.vcproj | 6 ++ 3 files changed, 230 insertions(+) create mode 100644 include/ZE_ZAnimation.h create mode 100644 src/ZE_ZAnimation.cpp diff --git a/include/ZE_ZAnimation.h b/include/ZE_ZAnimation.h new file mode 100644 index 0000000..807369b --- /dev/null +++ b/include/ZE_ZAnimation.h @@ -0,0 +1,75 @@ +/******************************************************************************* + This file is Part of the ZEngine Library for 2D game development. + Copyright (C) 2002, 2003 James Turk + + Licensed under a BSD-style license. + + The maintainer of this library is James Turk (james@conceptofzero.net) + and the home of this Library is http://www.zengine.sourceforge.net +*******************************************************************************/ + +/*! + \file ZE_ZAnimation.h + \brief Definition file for ZAnimation. + + Definition file for ZAnimation, a class for animations using ZImage. +
$Id: ZE_ZAnimation.h,v 1.1 2003/11/25 01:31:36 cozman Exp $
+ \author James Turk +**/ + +#ifndef __ze_zanimation_h__ +#define __ze_zanimation_h__ + +#include "ZE_ZEngine.h" +#include "ZE_ZImage.h" + +namespace ZE +{ + +enum ZAnimationType +{ + ZANIM_ONCE, + ZANIM_LOOP, + ZANIM_REV_ONCE, + ZANIM_REV_LOOP +}; + +class ZAnimation +{ + protected: + ZEngine *rEngine; + ZImage *rAnimImages; + float rAnimWidth; + float rAnimHeight; + int rCurFrame; + int rNumFrames; + int rFrameStep; + Uint32 rFrameDelay; + Uint32 rNextFrameTime; + bool rLoop; + bool rBackwards; + public: + ZAnimation(); + ZAnimation(ZImage *images, int numFrames, Uint32 frameDelay, bool loop=false, bool backwards=false, float width=0, float height=0); + + void Create(ZImage *images, int numFrames, Uint32 frameDelay, bool loop=false, bool backwards=false, float width=0, float height=0); + void SetAnimImages(ZImage *images, int numFrames); + void SetFrameDelay(Uint32 frameDelay); + void SetAnimType(bool loop, bool backwards); + void SetAnimSize(float width, float height); + + void Start(); + void Stop(); + void Pause(); + void Unpause(); + void SetFrame(int frame); + + void Update(); + void Draw(float x, float y); + + bool Stopped(); +}; + +} + +#endif //__ze_zanimation_h__ diff --git a/src/ZE_ZAnimation.cpp b/src/ZE_ZAnimation.cpp new file mode 100644 index 0000000..cf09af5 --- /dev/null +++ b/src/ZE_ZAnimation.cpp @@ -0,0 +1,149 @@ +/******************************************************************************* + This file is Part of the ZEngine Library for 2D game development. + Copyright (C) 2002, 2003 James Turk + + Licensed under a BSD-style license. + + The maintainer of this library is James Turk (james@conceptofzero.net) + and the home of this Library is http://www.zengine.sourceforge.net +*******************************************************************************/ + +/** + \file ZE_ZAnimation.cpp + \brief Source file for ZAnimation. + + Implementation of ZAnimation, ZEngine's class for ZImage based animations. +
$Id: ZE_ZAnimation.cpp,v 1.1 2003/11/25 01:31:37 cozman Exp $
+ \author James Turk +**/ + +#include "ZE_ZAnimation.h" + +namespace ZE +{ + +ZAnimation::ZAnimation() : + rEngine(ZEngine::GetInstance()), + rAnimImages(NULL), + rAnimWidth(0),rAnimHeight(0), + rCurFrame(0),rNumFrames(0),rFrameStep(0), + rFrameDelay(0),rNextFrameTime(0), + rLoop(false), rBackwards(false) +{ +} + +ZAnimation::ZAnimation(ZImage *images, int numFrames, Uint32 frameDelay, bool loop, bool backwards, float width, float height) +{ + rEngine = ZEngine::GetInstance(); + Create(images,numFrames,frameDelay,loop,backwards,width,height); +} + +void ZAnimation::Create(ZImage *images, int numFrames, Uint32 frameDelay, bool loop, bool backwards, float width, float height) +{ + rAnimImages = images; + rNumFrames = numFrames; + rFrameDelay = frameDelay; + rLoop = loop; + rBackwards = backwards; + rAnimWidth = width; + rAnimHeight = height; + Stop(); +} + +void ZAnimation::SetAnimImages(ZImage *images, int numFrames) +{ + rAnimImages = images; + rNumFrames = numFrames; +} + +void ZAnimation::SetFrameDelay(Uint32 frameDelay) +{ + rFrameDelay = frameDelay; +} + +void ZAnimation::SetAnimType(bool loop, bool backwards) +{ + rLoop = loop; + rBackwards = backwards; +} + +void ZAnimation::SetAnimSize(float width, float height) +{ + rAnimWidth = width; + rAnimHeight = height; +} + +void ZAnimation::Start() +{ + rCurFrame = rBackwards ? rNumFrames-1 : 0; + Unpause(); +} + +void ZAnimation::Stop() +{ + rCurFrame = rBackwards ? rNumFrames-1 : 0; + Pause(); +} + +void ZAnimation::Pause() +{ + rFrameStep = 0; +} + +void ZAnimation::Unpause() +{ + rFrameStep = rBackwards ? -1 : 1; + rNextFrameTime = rEngine->GetTime()+rFrameDelay; +} + +void ZAnimation::SetFrame(int frame) +{ + if(frame >= 0 && frame < rNumFrames) + rCurFrame = frame; + else if(frame < 0 && frame >= -rNumFrames) + SetFrame(rNumFrames+frame); + else + { + //invalid frame + } +} + +void ZAnimation::Update() +{ + if(rEngine->GetTime() >= rNextFrameTime) + { + rCurFrame += rFrameStep; + + if(rCurFrame < 0 || rCurFrame >= rNumFrames) + { + if(rLoop) + rCurFrame = rBackwards ? rNumFrames-1 : 0; + else + { + Stop(); + SetFrame(-1); //set to last frame + } + } + rNextFrameTime = rEngine->GetTime()+rFrameDelay; + } +} + +void ZAnimation::Draw(float x, float y) +{ + if(rAnimImages) + { +#if (GFX_BACKEND == ZE_OGL) + rAnimImages[rCurFrame].Resize(rAnimWidth,rAnimHeight); + rAnimImages[rCurFrame].Draw(x,y); +#elif (GFX_BACKEND == ZE_SDL) + rAnimImages[rCurFrame].Draw(static_cast(x),static_cast(y)); +#endif + } +} + +bool ZAnimation::Stopped() +{ + return rFrameStep == 0; +} + +} diff --git a/vc7/ZEngine.vcproj b/vc7/ZEngine.vcproj index 8b7dcdf..56d5375 100644 --- a/vc7/ZEngine.vcproj +++ b/vc7/ZEngine.vcproj @@ -61,6 +61,9 @@ + + @@ -104,6 +107,9 @@ + +