6 Replies - 1536 Views - Last Post: 24 August 2009 - 01:11 AM Rate Topic: -----

#1 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

SDL Animation problem

Posted 23 August 2009 - 02:37 PM

Ok i am having a problem with SDL and animation clips.

What determines that your sprite will go to the top of the screen or to the bottom.

I did the lesson for the sdl animation and it has only for Left and right clips.

No matter how much i tried i can't make it work and go up and down.

I think the problem is with the set_clips() function but i still can't understand what.


.The guy isn't using x and y he just using the x by naming it as velocity. I just wanted to modified the code and see how it is to make the sprite go up and down and then re write the whole code by self for making a sprite animate and go up down left and right but i just can't do it.

Any help ??? I don't need code just if the problem is on set_clip() function making something animate and going up and down.

This post has been edited by Hellbroth: 23 August 2009 - 09:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: SDL Animation problem

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: SDL Animation problem

Posted 23 August 2009 - 03:12 PM

Hi

Do you mean a sprite sheet that is vertical rather than horizontal? such as this
Attached File  yoshi.bmp (32.12K)
Number of downloads: 87

or just moving up and down, if this is the case then you just need to create more clips and move further along the sprite sheet to show the up and down animations.

This post has been edited by stayscrisp: 23 August 2009 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

Re: SDL Animation problem

Posted 23 August 2009 - 03:23 PM

This is my sprite i want to make it animate and go on the way top of the screen and back again down.

Posted Image

I know how to make this one
Posted Image

animate and go left and right but i can't make the other one go up and down
so i can combine them and make a fully animated sprite.

They are two seperate images if i can do it seperate i know how to do it combined.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: SDL Animation problem

Posted 23 August 2009 - 03:44 PM

You really should combine the images, you don't really want to load up 2 separate images for the same object when it can be done with one it is just unnecessary.

So combine the images like so
Posted Image

In fact just use that one :)

You said you could do it with 1 image so give it a go, if you get stuck then I will be happy to help.
Was This Post Helpful? 0
  • +
  • -

#5 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

Re: SDL Animation problem

Posted 23 August 2009 - 04:12 PM

No no no this isn't my problem.

Ok look

Posted Image

I have this sprite i know how to animate it

But i cannot make it go up or down on the screen.



switch(event.key.keysym.sym)
{
case SDLK_RIGHT: xVel += HERO_WIDTH /3; break;
case SDLK_LEFT: xVel -= HERO_WIDTH /3; break;
}



This one moves it right

So you add or remove width and the sprite goes right or left and i thought
if you do the same with the height it will work so it can go up and down.


switch(event.key.keysym.sym)
{
case SDLK_UP: yVel -= HERO_HEIGHT / 3; break;
case SDLK_DOWN: yVel += HERO_HEIGHT / 3; break;
}



But the picture it's just animates and goes right or left i just can't make it go up and down on the screen.

:(:(:(
Was This Post Helpful? 0
  • +
  • -

#6 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

Re: SDL Animation problem

Posted 23 August 2009 - 05:23 PM

After one whole day i manage to master the animate sprite thing with SDL haha.

Here is the code for an animated sprite :

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>


const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;


const int FRAMES_PER_SECOND = 10;


const int HERO_WIDTH = 32;
const int HERO_HEIGHT = 48;


const int HERO_UP = 0;
const int HERO_DOWN = 1;
const int HERO_RIGHT = 2;
const int HERO_LEFT = 3;


SDL_Surface *hero = NULL;
SDL_Surface *screen = NULL;


SDL_Event event;


SDL_Rect clipsUp[ 3 ];
SDL_Rect clipsDown[ 3 ];
SDL_Rect clipsRight[3];
SDL_Rect clipsLeft[3];


class Hero
{
	private:
   
	int x,y;

	
	int yvel,xvel;

	
	int frame;

	
	int status;

	public:
	
	Hero();

	
	void handle_events();

	
	void move();

	
	void show();
};


class Timer
{
	private:
	
	int startTicks;

	
	int pausedTicks;

  
	bool paused;
	bool started;

	public:
	
	Timer();

	
	void start();
	void stop();
	void pause();
	void unpause();

	
	int get_ticks();

	
	bool is_started();
	bool is_paused();
};

SDL_Surface *load_image( std::string filename )
{
	
	SDL_Surface* loadedImage = NULL;

	
	SDL_Surface* optimizedImage = NULL;

	
	loadedImage = IMG_Load( filename.c_str() );

   
	if( loadedImage != NULL )
	{
		
		optimizedImage = SDL_DisplayFormat( loadedImage );

		
		SDL_FreeSurface( loadedImage );

	   
		if( optimizedImage != NULL )
		{
			
			SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF ) );
		}
	}

	
	return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
	
	SDL_Rect offset;

	
	offset.x = x;
	offset.y = y;

	
	SDL_BlitSurface( source, clip, destination, &offset );
}

void set_clips()
{
	
	clipsUp[ 0 ].x = 0;
	clipsUp[ 0 ].y = 0;
	clipsUp[ 0 ].w = HERO_WIDTH;
	clipsUp[ 0 ].h = HERO_HEIGHT;

	clipsUp[ 1 ].x = HERO_WIDTH;
	clipsUp[ 1 ].y = 0;
	clipsUp[ 1 ].w = HERO_WIDTH;
	clipsUp[ 1 ].h = HERO_HEIGHT;

	clipsUp[ 2 ].x = HERO_WIDTH * 2;
	clipsUp[ 2 ].y = 0;
	clipsUp[ 2 ].w = HERO_WIDTH;
	clipsUp[ 2 ].h = HERO_HEIGHT;
	
	clipsDown[ 0 ].x = 0;
	clipsDown[ 0 ].y = HERO_HEIGHT;
	clipsDown[ 0 ].w = HERO_WIDTH;
	clipsDown[ 0 ].h = HERO_HEIGHT;

	clipsDown[ 1 ].x = HERO_WIDTH;
	clipsDown[ 1 ].y = HERO_HEIGHT;
	clipsDown[ 1 ].w = HERO_WIDTH;
	clipsDown[ 1 ].h = HERO_HEIGHT;

	clipsDown[ 2 ].x = HERO_WIDTH * 2;
	clipsDown[ 2 ].y = HERO_HEIGHT;
	clipsDown[ 2 ].w = HERO_WIDTH;
	clipsDown[ 2 ].h = HERO_HEIGHT;
	
	clipsRight[ 0 ].x = 0;
	clipsRight[ 0 ].y = 144;
	clipsRight[ 0 ].w = HERO_WIDTH;
	clipsRight[ 0 ].h = HERO_HEIGHT;

	clipsRight[ 1 ].x = HERO_WIDTH;
	clipsRight[ 1 ].y = 144;
	clipsRight[ 1 ].w = HERO_WIDTH;
	clipsRight[ 1 ].h = HERO_HEIGHT;

	clipsRight[ 2 ].x = HERO_WIDTH * 2;
	clipsRight[ 2 ].y = 144;
	clipsRight[ 2 ].w = HERO_WIDTH;
	clipsRight[ 2 ].h = HERO_HEIGHT;
	
	clipsLeft[ 0 ].x = 0;
	clipsLeft[ 0 ].y = 96;
	clipsLeft[ 0 ].w = HERO_WIDTH;
	clipsLeft[ 0 ].h = HERO_HEIGHT;

	clipsLeft[ 1 ].x = HERO_WIDTH;
	clipsLeft[ 1 ].y = 96;
	clipsLeft[ 1 ].w = HERO_WIDTH;
	clipsLeft[ 1 ].h = HERO_HEIGHT;

	clipsLeft[ 2 ].x = HERO_WIDTH * 2;
	clipsLeft[ 2 ].y = 96;
	clipsLeft[ 2 ].w = HERO_WIDTH;
	clipsLeft[ 2 ].h = HERO_HEIGHT;

	 
}

bool init()
{
   
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return false;
	}

	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

	
	if( screen == NULL )
	{
		return false;
	}

	
	SDL_WM_SetCaption( "My hero", NULL );

	
	return true;
}

bool load_files()
{
	
	hero = load_image( "elliot.png" );

	
	if( hero == NULL )
	{
		return false;
	}
	
	return true;
}

void clean_up()
{
   
	SDL_FreeSurface( hero );
	

	
	SDL_Quit();
}

Hero::Hero()
{
	
	x = 0;
	y =0;
	yvel = 0;
	xvel = 0;

	
	frame = 0;
	status = HERO_UP;
}

void Hero::handle_events()
{
	
	if( event.type == SDL_KEYDOWN )
	{
		
		switch( event.key.keysym.sym )
		{
			case SDLK_UP: yvel += HERO_HEIGHT / 3; break;
			case SDLK_DOWN: yvel -= HERO_HEIGHT / 3; break;
			case SDLK_RIGHT: xvel += HERO_WIDTH / 3; break;
			case SDLK_LEFT: xvel -= HERO_WIDTH / 3; break;
		}
	}
	
	else if( event.type == SDL_KEYUP )
	{
		
		switch( event.key.keysym.sym )
		{
			case SDLK_UP: yvel -= HERO_HEIGHT / 3; break;
			case SDLK_DOWN: yvel += HERO_HEIGHT / 3; break;
			case SDLK_RIGHT: xvel -= HERO_WIDTH / 3; break;
			case SDLK_LEFT: xvel += HERO_WIDTH / 3; break;
		}
	}
}

void Hero::move()
{
	
	
		y -= yvel;

	
	if( ( y < 0 ) || ( y + HERO_HEIGHT > SCREEN_HEIGHT ) )
	{
		
		y += yvel;
	}
	
	x += xvel;
	if( ( x < 0 ) || ( x + HERO_WIDTH > SCREEN_WIDTH ) )
	{
		
		x += xvel;
	}
	
}

void Hero::show()
{
	
	if( yvel < 0 )
	{
		
		status = HERO_UP;

		
		frame++;
	}
	
	else if( yvel > 1 )
	{
		
		status = HERO_DOWN;

		
		frame++;
	}
	else if( xvel > 0 )
	{
		
		status = HERO_RIGHT;

		
		frame++;
	}
	else if( xvel < 0 )
	{
		
		status = HERO_LEFT;

		
		frame++;
	}
	
   
	else
	{
		
		frame = 0;
	}

	
	if( frame >= 3 )
	{
		frame = 0;
	}

	
	if( status == HERO_UP )
	{
		apply_surface( x,y, hero, screen, &clipsUp[ frame ] );
	}
	else if( status == HERO_DOWN )
	{
		apply_surface( x,y, hero, screen, &clipsDown[ frame ] );
	}
	else if( status == HERO_RIGHT)
	{
		 apply_surface(x,y, hero, screen, &clipsRight[ frame ] );
	}
	else if( status == HERO_LEFT)
	{
		 apply_surface(x,y, hero, screen, &clipsLeft[ frame ] );
	}
}

Timer::Timer()
{
	
	startTicks = 0;
	pausedTicks = 0;
	paused = false;
	started = false;
}

void Timer::start()
{
	
	started = true;

	
	paused = false;

	
	startTicks = SDL_GetTicks();
}

void Timer::stop()
{
	
	started = false;

	
	paused = false;
}

void Timer::pause()
{
	
	if( ( started == true ) && ( paused == false ) )
	{
		
		paused = true;

		
		pausedTicks = SDL_GetTicks() - startTicks;
	}
}

void Timer::unpause()
{
	
	if( paused == true )
	{
	   
		paused = false;

		
		startTicks = SDL_GetTicks() - pausedTicks;

		
		pausedTicks = 0;
	}
}

int Timer::get_ticks()
{
	
	if( started == true )
	{
	   
		if( paused == true )
		{
			
			return pausedTicks;
		}
		else
		{
			
			return SDL_GetTicks() - startTicks;
		}
	}

	
	return 0;
}

bool Timer::is_started()
{
	return started;
}

bool Timer::is_paused()
{
	return paused;
}

int main( int argc, char* args[] )
{
	
	bool quit = false;

   
	if( init() == false )
	{
		return 1;
	}

	
	if( load_files() == false )
	{
		return 1;
	}

  
	set_clips();

	
	Timer fps;

	
	Hero walk;

	
	while( quit == false )
	{
		
		fps.start();

		
		while( SDL_PollEvent( &event ) )
		{
			
			walk.handle_events();

			
			if( event.type == SDL_QUIT )
			{
				
				quit = true;
			}
		}

		
		walk.move();

	   
		SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

		
		walk.show();

		if( SDL_Flip( screen ) == -1 )
		{
			return 1;
		}

		
		if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
		{
			SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
		}
	}

	
	clean_up();

	return 0;
}




And this is the picture i used :

Posted Image

I compile it and everything is ok.Anyone can use it if he wants don't give credit.

PS.I think i was speaking alone all the day on a forum haha...

This post has been edited by Hellbroth: 23 August 2009 - 09:50 PM

Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: SDL Animation problem

Posted 24 August 2009 - 01:11 AM

Glad you got it working :D you will have a greater understanding by working it out for yourself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1