-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.cpp
More file actions
129 lines (109 loc) · 2.18 KB
/
Sprite.cpp
File metadata and controls
129 lines (109 loc) · 2.18 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*******************************************************************************
CommanderTux
Penguin In Space
Released under the GNU Public License
2005 by Andr� Schnabel (thefrogs@web.de)
*******************************************************************************/
// Sprite.cpp
#include "Sprite.h"
//
// CSprite
// Constructor
//
CSprite::CSprite( SDL_Texture *image )
{
SetImage( image );
m_rect.x = m_rect.y = 0;
}
//
// CSprite
// Destructor
//
void CSprite::SetImage( SDL_Texture *image )
{
if( image )
{
m_image = image;
int w, h;
SDL_QueryTexture(image, nullptr, nullptr, &w, &h);
m_rect.w = w;
m_rect.h = h;
}
}
//
// SetPos
//
void CSprite::SetPos( int x, int y )
{
m_rect.x = x;
m_rect.y = y;
}
//
// GetRect
//
SDL_Rect *CSprite::GetRect()
{
return &m_rect;
}
//
// Draw
//
void CSprite::Draw()
{
g_framework->Draw( m_image, &m_rect );
}
//
// Draw
//
void CSprite::Draw( int *scroll_x, int *scroll_y )
{
// Draw the image at the right position
g_framework->Draw( m_image,
m_rect.x - *scroll_x,
m_rect.y - *scroll_y );
}
//
// CAnim_Sprite
// Constructor
//
CAnim_Sprite::CAnim_Sprite( SDL_Texture *image,
int image_w, int image_h,
Uint32 chg_time )
: CSprite( image )
{
// Copy parameter values to member vars
m_chg_time = chg_time;
m_num_images = m_rect.w / image_w;
m_src_rect.w = image_w;
m_src_rect.h = image_h;
m_act_image = 0;
m_src_rect.x = m_act_image * image_w;
m_src_rect.y = 0;
m_last_change = g_framework->GetTicks();
}
//
// Draw
//
void CAnim_Sprite::Draw( bool iterate)
{
if( iterate )
Iterate();
m_src_rect.x = m_act_image * m_src_rect.w;
m_rect.w = m_src_rect.w;
m_rect.h = m_src_rect.h;
g_framework->Draw( m_image, &m_src_rect, &m_rect );
}
//
// Iterate
//
void CAnim_Sprite::Iterate()
{
if( g_framework->GetTicks() - m_last_change > m_chg_time )
{
if( m_act_image < m_num_images -1 )
m_act_image++;
else
m_act_image = 0;
m_last_change = g_framework->GetTicks();
}
}