I have been tinkering with animated textures. I thought I would share my progress here for discussion and to maybe give a head start for someone trying to implement this. I plan on making the effect.h more oop eventually but I was just aiming for functionality for now. here is my code feel free to comment.
First the animation loading function.
int (&LoadAnimation(int (&arr)[32],char* name, int frames))[32]
{
int ID=FindFreeImage();
char mediaC[80] = "media/effects/";
char file[40];
char path[128] ={0};
strncat(mediaC,name,10);
strncat(path,mediaC,80);
for(int x = 1; x < frames; x++)
{
//ID = FindFreeImage();
sprintf(file,"/Frame %i",x);
strcat(file,".png");
strcat(mediaC,file);
if(dbFileExist(mediaC)==1)
{
ID = FindFreeImage();
dbLoadImage(mediaC,ID);
arr[x-1]=ID;
}else
{
MessageBoxA(0,mediaC,"File not Found",0 );
}
mediaC[0] = 0;
strcat(mediaC,path);
}
return arr;
}
The FindFreeImage() Code is simply just
int start = 1;
while(dbObjectExist(start)==1)
{
start++;
}
return start;
Finally here is the effects class again this is the first stage. I am going to add more on to the class such as different types of animation and of course simultaneous animations.
#include "darkgdk.h"
#ifndef EFFECTS_H
#define EFFECTS_H
class Effect
{
public:
Effect()
{
}
void InitEffects()
{
for(int x = 0; x < 32; x++)
{
ImgAnim[x] = 0;
}
played=false;
timer = 0;
currentImg = 0;
AnimPlaneID = media.FindFreeObject();
dbMakeObjectPlane(AnimPlaneID,64,64);
dbSetObjectTransparency(AnimPlaneID,3);
dbRotateObject(AnimPlaneID,0,180,0);
dbFixObjectPivot(AnimPlaneID);
dbSetObjectLight(AnimPlaneID,0);
}
~Effect()
{
}
void SetAnimation(int xp, int y, int z, int imgs[],int size)
{
for(int x = 0; x < 32; x++)
{
ImgAnim[x] = imgs[x];
}
dbPositionObject(AnimPlaneID,xp,y,z);
dbScaleObject(AnimPlaneID,size*100,size*100,size*100);
played=false;
timer = 800;
}
void Update()
{
if(dbKeyState(18)==1) timer = 800;
dbPointObject(AnimPlaneID,dbCameraPositionX(),dbObjectPositionY(AnimPlaneID),dbCameraPositionZ());
dbRollObjectLeft(AnimPlaneID,3);
if(timer>0)
{
timer--;
if(timer%5==0)
{
dbTextureObject(AnimPlaneID,ImgAnim[currentImg]);
currentImg++;
if(ImgAnim[currentImg]==0)
{
currentImg=0;
//timer=0;
//played=true; this would be part of hiding certain types of animations such as explosions etc.
// or to continue to show a surface such as water
}
}
}
}
protected:
int ImgAnim[32];
int currentImg;
int AnimPlaneID;
bool played;
int timer;
};
#endif
So the basic code logic is to initialize an array of Image ids and iterate through them based on a timer. The files are loaded through the directory "media/effects/EffectName/Frame 1.png" and so on for max frames.
Example Code
int Explosion[32] = {0};
LoadAnimation(Explosion,"Explosion",15); // arr name, image, # of frames
effect.InitEffects();
effect.SetAnimation(x,y,z,Explosion,size);
//in the loop
while(LoopGDK())
{
effect.Update();
dbSync();
}