Jump to content
  • Advertisement
Sign in to follow this  
DominicHughes

SDL Animation issue with Loops?

This topic is 2565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I'm trying to create a basic snow animation in my game and when I try to make a while loop of hardcoded animation it crashes after a few repeats of showing the images on the framebuffer heres my code



//Main.cpp
//Main Libarys
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


//Other Files
#include "SurfaceStorage.h"
#include "Animation.h"


//On and Off Switch For Main Program
int main(int argc, char *argv[])
{

//Image Loading
TitleScreen = IMG_Load("ArtWork/TitleScreen.png");
NotActiveScreen = IMG_Load("ArtWork/NotActive.png");


//On And Off Switch For Application
bool done = false;
bool SnowToggle = false;
bool TitleScreenToggle = false;



//Event Creation
SDL_Event event;



int Screen_Width = 640;
int Screen_Height = 480;

//Screen Setup
Screen = SDL_SetVideoMode(Screen_Width,Screen_Height,32,SDL_HWSURFACE|SDL_RESIZABLE);

//Screen Title
SDL_WM_SetCaption("Game",NULL);



//Basic SnowAnimation
//SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void* param);

//Uint32 callback(Uint32 interval, void* param);
// {

// fprintf(stderr,"Print this shit!");
// SnowAnimation();
// return(100);
// }


while(!TitleScreenToggle)
{
SnowAnimation();
}

/////////////////////////////////////////////////////////////



//While done is equal to false
while(!done)
{
//make events continous
while(SDL_PollEvent(&event))
{
//make events switchable
switch(event.type)
{
//if user presses close button it closes application
case SDL_QUIT:

//turns application off
return 0;



//breaks case
break;
//////////////////////////////////////////////////////////
//While App Screen Active On First State
case SDL_APPACTIVE:
if(!TitleScreenToggle)
{
SDL_BlitSurface(TitleScreen,NULL,Screen,NULL);
SDL_Flip(Screen);
}



break;

/////////////////////////////////////////////////////
//While Mouse Is OutSide Window
case SDL_APPMOUSEFOCUS:
SDL_BlitSurface(NotActiveScreen,NULL,Screen,NULL);
SDL_Flip(Screen);

break;
///////////////////////////////////////////////////



case SDL_VIDEORESIZE:

Screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, SDL_HWSURFACE|SDL_RESIZABLE);











}
}
}
}


//SurfaceStorage.h

//Main Libary
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


SDL_Surface *Screen = NULL;
SDL_Surface *TitleScreen = NULL;
SDL_Surface *NotActiveScreen = NULL;



//Animation.h

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


//////////////////////////////////////////////////////////////////
void SnowAnimation(void)
{

//Keep TitleScreen On Window
if(SnowAnimation)
{

bool TitleScreenToggle = false;
if(!TitleScreenToggle)
{
SDL_BlitSurface(TitleScreen,NULL,Screen,NULL);
SDL_Flip(Screen);
}
///////////////////////////////////////////////////////////////


///////////////////////////////////
//Frame 1
SDL_Surface *Snow1 = NULL;

Snow1 = IMG_Load("ArtWork/Snow/Snow1.png");

SDL_BlitSurface(Snow1,NULL,Screen,NULL);

SDL_Flip(Screen);

SDL_Delay(100);
///////////////////////////////////

///////////////////////////////////
//Frame 2
SDL_Surface *Snow2 = NULL;

Snow2 = IMG_Load("ArtWork/Snow/Snow2.png");

SDL_BlitSurface(Snow2,NULL,Screen,NULL);

SDL_Flip(Screen);

SDL_Delay(100);
///////////////////////////////////

///////////////////////////////////
//Frame 3
SDL_Surface *Snow3 = NULL;

Snow3 = IMG_Load("ArtWork/Snow/Snow3.png");

SDL_BlitSurface(Snow3,NULL,Screen,NULL);

SDL_Flip(Screen);

SDL_Delay(100);
///////////////////////////////////
}
////////////////////////////////////////////////////////
}

Share this post


Link to post
Share on other sites
Advertisement
Well, one of the main issues is you are loading images, but not freeing them. If you do an IMG_Load you should do a SDL_FreeSurface.

However, you really should re-design your animation system. You should only load the images once, and you should just rotate through an index of images. It seems you're working in C, so I'd do something like this (just a rough sketch):


// in a header file
typedef struct
{
SDL_Surface **pAnimationSurfaces; // holds array of SDL surfacepointers which represent the animation frames
int NumAnimations; // the total number of animation frames
unsigned int AnimationTime; // the time in milliseconds each frame is rendered

} tAnimationData;

// in source file somewhere
tAnimationData *CreateSnowAnimations(void)
{
tAnimationData *pAnimationData = malloc(sizeof(tAnimationData));

// Initialze the animation data
pAnimationData->NumAnimations = 3;
pAnimationData->AnimationTime = 100;

// Allocate the SDL surfaces array.
pAnimationData->pAnimationSurfaces = malloc(sizeof(SDL_Surface *)*3);
pAnimationData->pAnimationSurfaces[0] = IMG_Load("ArtWork/Snow/Snow1.png");
pAnimationData->pAnimationSurfaces[1] = IMG_Load("ArtWork/Snow/Snow2.png");
pAnimationData->pAnimationSurfaces[2] = IMG_Load("ArtWork/Snow/Snow3.png");

return pAnimationData;
}

// in main file do something like this:
int AnimationIndex = 0; // holds current animation frame displayed
tAnimationData *pSnowAnimation = CreateSnowAnimations();

while(!TitleScreenToggle)
{
SDL_BlitSurface(pSnowAnimation->pAnimationSurface[AnimationIndex] ,NULL,Screen,NULL);
SDL_Flip(Screen);
SDL_Delay(pSnowAnimation->AnimationTime);
AnimationIndex++;
if (AnimationIndex == pSnowAnimation->NumAnimations) {
AnimationIndex = 0;
}
}


// when done, free everything
int i;
for (i = 0 i < pSnowAnimation->NumAnimations; i++) {
SDL_SurfaceFree(pSnowAnimation->pAnimationSurface);
}
free(pSnowAnimation->pAnimationSurface);
free(pSnowAnimation);

}





Share this post


Link to post
Share on other sites

Hello I'm trying to create a basic snow animation in my game and when I try to make a while loop of hardcoded animation it crashes after a few repeats of showing the images on the framebuffer heres my code



//Main.cpp
//Main Libarys
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


//Other Files
#include "SurfaceStorage.h"
#include "Animation.h"


//On and Off Switch For Main Program
int main(int argc, char *argv[])
{

//Image Loading
TitleScreen = IMG_Load("ArtWork/TitleScreen.png");
NotActiveScreen = IMG_Load("ArtWork/NotActive.png");


//On And Off Switch For Application
bool done = false;
bool SnowToggle = false;
bool TitleScreenToggle = false;



//Event Creation
SDL_Event event;



int Screen_Width = 640;
int Screen_Height = 480;

//Screen Setup
Screen = SDL_SetVideoMode(Screen_Width,Screen_Height,32,SDL_HWSURFACE|SDL_RESIZABLE);

//Screen Title
SDL_WM_SetCaption("Game",NULL);



//Basic SnowAnimation
//SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void* param);

//Uint32 callback(Uint32 interval, void* param);
// {

// fprintf(stderr,"Print this shit!");
// SnowAnimation();
// return(100);
// }


while(!TitleScreenToggle)
{
SnowAnimation();
}

/////////////////////////////////////////////////////////////



//While done is equal to false
while(!done)
{
//make events continous
while(SDL_PollEvent(&event))
{
//make events switchable
switch(event.type)
{
//if user presses close button it closes application
case SDL_QUIT:

//turns application off
return 0;



//breaks case
break;
//////////////////////////////////////////////////////////
//While App Screen Active On First State
case SDL_APPACTIVE:
if(!TitleScreenToggle)
{
SDL_BlitSurface(TitleScreen,NULL,Screen,NULL);
SDL_Flip(Screen);
}



break;

/////////////////////////////////////////////////////
//While Mouse Is OutSide Window
case SDL_APPMOUSEFOCUS:
SDL_BlitSurface(NotActiveScreen,NULL,Screen,NULL);
SDL_Flip(Screen);

break;
///////////////////////////////////////////////////



case SDL_VIDEORESIZE:

Screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, SDL_HWSURFACE|SDL_RESIZABLE);











}
}
}
}


//SurfaceStorage.h

//Main Libary
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


SDL_Surface *Screen = NULL;
SDL_Surface *TitleScreen = NULL;
SDL_Surface *NotActiveScreen = NULL;



//Animation.h

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


//////////////////////////////////////////////////////////////////
void SnowAnimation(void)
{

//Keep TitleScreen On Window
if(SnowAnimation)
{

bool TitleScreenToggle = false;
if(!TitleScreenToggle)
{
SDL_BlitSurface(TitleScreen,NULL,Screen,NULL);
SDL_Flip(Screen);
}
///////////////////////////////////////////////////////////////


///////////////////////////////////
//Frame 1
SDL_Surface *Snow1 = NULL;

Snow1 = IMG_Load("ArtWork/Snow/Snow1.png");

SDL_BlitSurface(Snow1,NULL,Screen,NULL);

SDL_Flip(Screen);

SDL_Delay(100);
///////////////////////////////////

///////////////////////////////////
//Frame 2
SDL_Surface *Snow2 = NULL;

Snow2 = IMG_Load("ArtWork/Snow/Snow2.png");

SDL_BlitSurface(Snow2,NULL,Screen,NULL);

SDL_Flip(Screen);

SDL_Delay(100);
///////////////////////////////////

///////////////////////////////////
//Frame 3
SDL_Surface *Snow3 = NULL;

Snow3 = IMG_Load("ArtWork/Snow/Snow3.png");

SDL_BlitSurface(Snow3,NULL,Screen,NULL);

SDL_Flip(Screen);

SDL_Delay(100);
///////////////////////////////////
}
////////////////////////////////////////////////////////
}



You are repeatedly loading images from disc and never freeing them which will eventually cause you to run out of RAM.
also, inserting delays to control speed is a bad idea.

You should consider something like:


int snowAnimFrame = 0;
SDL_Surface *snowAnimation[3];
snowAnimation[0] = IMG_Load("ArtWork/Snow/Snow1.png");
snowAnimation[1] = IMG_Load("ArtWork/Snow/Snow2.png");
snowAnimation[2] = IMG_Load("ArtWork/Snow/Snow3.png");
....
lastFrameTime currentTime = SDL_GetTicks();
while (!done) {
currentTime = SDL_GetTicks();
while (SDL_PollEvent(&event)) {
....event code....
}
if (currentTime-lastFrameTime>100) {
//update animations
snowAnimFrame++;
if (snowAnimFrame>2) {
snowAnimFrame=0;
}
lastFrameTime+=100;
}
if (SnowAnimation) {
SDL_BlitSurface(SnowAnimation[snowAnimFrame],NULL,Screen,NULL);
}
//other blitting

SDL_Flip(Screen);
}


You shouldn't use SDL_Delay to control the speed of your application, EVER.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!