summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/animatedimage.cpp
blob: 680b603e4d2aa28322b4a647b6dc40cf1fb9fffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include "constants.h"
#include "animatedimage.h"

AnimatedImage :: AnimatedImage( string file, int nFrames )
{
	nrFrames = nFrames;
	currentFrame = 0;

	// Load image
	image = IMG_Load( (const char *)file.c_str() );
	if ( !image )
	{
		nrFrames = 0;
		image = 0;
		return;
	}

	SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( image->format, 0, 0, 0 ) );
	frameWidth = image->w/nrFrames;
	frameHeight = image->h;
}

AnimatedImage :: ~AnimatedImage()
{
	if ( image != 0 )
		SDL_FreeSurface( image );
}

bool AnimatedImage :: nextFrame()
{
	bool rc = true;
	currentFrame ++;
	if ( currentFrame >= nrFrames )
	{
		currentFrame --;
		rc = false;
	}

	return rc;
}

void AnimatedImage :: draw( SDL_Surface *screen, int x, int y )
{
	if ( !image )
		return;

	SDL_Rect dst;
	dst.x = currentFrame * frameWidth;
	dst.y = 0;
	dst.w = frameWidth;
	dst.h = frameHeight;

	SDL_Rect dst2;
	dst2.x = x - (frameWidth/2);
	dst2.y = y - (frameHeight/2);;
	SDL_BlitSurface( image, &dst, screen, &dst2 );
}

bool AnimatedImage :: AtEnd()
{
	if ( currentFrame +1 >= nrFrames || image == 0 )
		return true;
	return false;
}