-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
48 lines (44 loc) · 1.28 KB
/
Animation.cpp
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
#include "Animation.h"
#include "MyTexture.h"
#include <SDL2/SDL.h>
#include<iostream>
Animation::Animation( LTexture &texture , int width , int height) : animationTexture(texture)
{
mWidth = width ;
mHeight = height ;
mFrames = 0 ;
mBox = { 0 , 0 } ;
animationSpeed = 20 ;
int numberOfColumns = texture.getWidth() / width;
int numberOfRows = texture.getHeight() / height ;
numberOfAnimationImages = ( numberOfColumns*numberOfRows);
animationImages.resize( numberOfColumns*numberOfRows);
for( int row = 0 ; row < numberOfRows ; row ++ ){
for( int col = 0 ; col < numberOfColumns ; col ++ ){
animationImages[row*numberOfColumns + col ] = {
col*width,
row*height ,
width ,
height
} ;
}
}
}
Animation::~Animation()
{
cleanup() ;
}
void Animation::cleanup()
{
animationTexture.free() ;
}
void Animation::setBox( int x , int y )
{
mBox = { x , y } ;
}
int Animation::render(SDL_Renderer *renderer, SDL_Rect &camera)
{
animationTexture.render( renderer , mBox.x - camera.x, mBox.y - camera.y , &animationImages[mFrames/(animationSpeed)] ) ;
mFrames = (mFrames + 1) % (numberOfAnimationImages * animationSpeed);
return 0 ;
}