-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.cpp
135 lines (116 loc) · 2.99 KB
/
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 "Sprite.h"
BaseObject::BaseObject() {
trans = ofVec3f(0,0,0);
scale = ofVec3f(1, 1, 1);
rot = 0;
}
void BaseObject::setPosition(ofVec3f pos) {
trans = pos;
}
//
// Basic Sprite Object
//
Sprite::Sprite() {
speed = 0;
velocity = ofVec3f(0, 0, 0);
lifespan = -1; // lifespan of -1 => immortal
birthtime = 0;
bSelected = false;
haveImage = false;
name = "UnamedSprite";
width = 20;
height = 20;
}
// Return a sprite's age in milliseconds
//
float Sprite::age() {
return (ofGetElapsedTimeMillis() - birthtime);
}
// Set an image for the sprite. If you don't set one, a rectangle
// gets drawn.
//
void Sprite::setImage(ofImage img) {
image = img;
haveImage = true;
width = image.getWidth();
height = image.getHeight();
}
// Render the sprite
//
void Sprite::draw() {
ofSetColor(255, 255, 255, 255);
// draw image centered and add in translation amount
//
if (haveImage) {
image.draw(-width / 2.0 + trans.x, -height / 2.0 + trans.y);
}
else {
// in case no image is supplied, draw something.
//
ofNoFill();
ofSetColor(255, 0, 0);
ofDrawRectangle(-width / 2.0 + trans.x, -height / 2.0 + trans.y, width, height);
}
}
// Add a Sprite to the Sprite System
//
void SpriteSystem::add(Sprite s) {
sprites.push_back(s);
}
// Remove a sprite from the sprite system. Note that this function is not currently
// used. The typical case is that sprites automatically get removed when the reach
// their lifespan.
//
void SpriteSystem::remove(int i) {
sprites.erase(sprites.begin() + i);
}
// remove all sprites within a given dist of point, return number removed
//
int SpriteSystem::removeNear(ofVec3f point, float dist) {
vector<Sprite>::iterator s = sprites.begin();
vector<Sprite>::iterator tmp;
int count = 0;
while (s != sprites.end()) {
ofVec3f v = s->trans - point;
if (v.length() < dist) {
tmp = sprites.erase(s);
count++;
s = tmp;
}
else s++;
}
return count;
}
// Update the SpriteSystem by checking which sprites have exceeded their
// lifespan (and deleting). Also the sprite is moved to it's next
// location based on velocity and direction.
//
void SpriteSystem::update() {
if (sprites.size() == 0) return;
vector<Sprite>::iterator s = sprites.begin();
vector<Sprite>::iterator tmp;
// check which sprites have exceed their lifespan and delete
// from list. When deleting multiple objects from a vector while
// traversing at the same time, use an iterator.
//
while (s != sprites.end()) {
if (s->lifespan != -1 && s->age() > s->lifespan) {
// cout << "deleting sprite: " << s->name << endl;
tmp = sprites.erase(s);
s = tmp;
}
else s++;
}
// Move sprite
//
for (int i = 0; i < sprites.size(); i++) {
sprites[i].trans += sprites[i].velocity / ofGetFrameRate();
}
}
// Render all the sprites
//
void SpriteSystem::draw() {
for (int i = 0; i < sprites.size(); i++) {
sprites[i].draw();
}
}