-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mar_sprite.cpp
executable file
·136 lines (118 loc) · 2.72 KB
/
Mar_sprite.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
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
130
131
132
133
134
135
#include "Mar_sprite.h"
//
//
// CSprite is a class that allows you to create basic game objects that have a group of visual properties and logic
//
// To utilise the CSprite class you generally derive yuor own class from CSprite and implement the Update() method to
// provide game object specific functionality
//
//
// CSpriteManager is a class that manages a collection of CSprite objects. CReated CSprite objects are added to this
// manager to enable auto update, drawing and clean up
//
//
//
//
// CSprite implementation
//
//
void CSprite::Init()
{
Type = 0;
Position.x = 0;
Position.y = 0;
Angle = 0;
Scale = IW_GEOM_ONE;
Colour.r = 0xff;
Colour.g = 0xff;
Colour.b = 0xff;
Colour.a = 0xff;
}
bool CSprite::Update()
{
return true;
}
void CSprite::Draw()
{
// Do not render if not visible
if (Image == NULL || !Visible || Colour.a == 0)
return;
// Build the transform
// Set the rotation transform
Transform.SetRot(Angle);
// Scale the transform
Transform.ScaleRot(Scale);
// Translate the transform
Transform.SetTrans(Position);
// Set this transform as the active transform for Iw2D
Iw2DSetTransformMatrix(Transform);
// Set colour of sprite
Iw2DSetColour(Colour);
// Render the sprite (centered)
int x = -(Width / 2);
int y = -(Height / 2);
Iw2DDrawImage(Image, CIwSVec2(x, y), CIwSVec2(Width, Height));
}
//
//
// CSpriteManager implementation
//
//
void CSpriteManager::addSprite(CSprite* sprite)
{
// Add sprite to the sprite manager
Sprites.push_back(sprite);
sprite->setParent(this);
}
void CSpriteManager::removeSprite(CSprite* sprite)
{
// Locate the sprite and destory it
for (Iterator it = Sprites.begin(); it != Sprites.end(); ++it)
{
if (*it == sprite)
{
delete *it;
Sprites.erase(it);
break;
}
}
}
void CSpriteManager::Update()
{
CIwList<CSprite*> Removals;
// Update all sprites in the sprite manager
for (Iterator it = Sprites.begin(); it != Sprites.end(); ++it)
{
if (!(*it)->Update())
Removals.push_back(*it);
}
// Remove any sprites that requested deletion
// Sprites are removed in this manner as sprites could be interacting with sprites that
// are deleted during the game frame.
for (Iterator it = Removals.begin(); it != Removals.end(); ++it)
{
for (Iterator it2 = Sprites.begin(); it2 != Sprites.end(); ++it2)
{
if (*it2 == *it)
{
delete *it2;
Sprites.erase(it2);
break;
}
}
}
Removals.clear();
}
void CSpriteManager::Draw()
{
// Draw all sprites in the sprite manager
for (Iterator it = Sprites.begin(); it != Sprites.end(); ++it)
(*it)->Draw();
}
CSpriteManager::~CSpriteManager()
{
// Delete all sprites in the sprite manager
for (Iterator it = Sprites.begin(); it != Sprites.end(); ++it)
delete *it;
Sprites.clear();
}