-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderable.cpp
More file actions
409 lines (322 loc) · 16.5 KB
/
Copy pathRenderable.cpp
File metadata and controls
409 lines (322 loc) · 16.5 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//Renderable.cpp
#include "Renderable.h"
#include "Object.h"
#include <iostream>
#include <math.h>
#include <sdlUtility/Renderer.h>
using namespace sdlUtility;
namespace sdlObjects {
bool Renderable::AnimationIsDone() {
if (Animation) return (AnimationTile >= Animation->GetTiles()-1 && AnimationProgress >= AnimationDuration); else return true;
}
sdlObjects::Animation *Renderable::GetAnimation() {
return Animation;
}
int Renderable::GetAnimationDuration() {
return AnimationDuration;
}
void Renderable::GetAnimationProgress(int &AnimationProgress, int &AnimationTile, int &AnimationTileProgress, float &BonusDuration) {
AnimationProgress = this->AnimationProgress;
AnimationTile = this->AnimationTile;
AnimationTileProgress = this->AnimationTileProgress;
BonusDuration = this->BonusDuration;
}
std::string Renderable::GetAnimationset() {
return Animationset;
}
int Renderable::GetAnimationQueue() {
return AnimationQueue;
}
int *Renderable::GetColor(int ColorMode) {
int *Return = NULL;
if (ColorMode < 0) {
Return = Color;
} else if (ColorMode == Color::Idle) {
Return = IdleColor;
} else if (ColorMode == Color::Hovered) {
Return = HoverColor;
} else if (ColorMode == Color::Disabled) {
Return = DisabledColor;
}
return Return;
}
sdlObjects::Texture *Renderable::GetTexture() {
return Texture;
}
int Renderable::GetTile() {
return Tile;
}
std::string Renderable::GetTextureID() {
return TextureID;
}
int Renderable::HandleEvents() {
int Event = Events::Idle;
if (EventQueue.size() > 0) {
Event = EventQueue[0];
EventQueue.erase(EventQueue.begin());
}
return Event;
}
void Renderable::ProcessAnimation() {
if (Animation) {
float TileDuration = Animation->GetWeight(AnimationTile)*AnimationDuration;
float RoundedDuration = roundf(TileDuration);
//if (Animationset == "MaleModel" && Animation->GetState() == 4) std::cout << AnimationTile << " " << Animation->GetTile(AnimationTile) << " " << Animation->GetWeight(AnimationTile) << std::endl;
//Add extra frame
if (AnimationTileProgress == 0 && TileDuration != RoundedDuration) BonusDuration += (RoundedDuration-TileDuration);
if (fabs(BonusDuration) > 0.99f) {
if (BonusDuration < 0) {
if (AnimationTileProgress >= RoundedDuration-1) {
RoundedDuration += 1;
BonusDuration += 1;
}
} else {
if (TileDuration > 1 && AnimationTileProgress == RoundedDuration-2) {
RoundedDuration -= 1;
BonusDuration -= 1;
}
}
}
AnimationProgress += 1;
AnimationTileProgress += 1;
//if (Animationset == "MaleModel" && Animation->GetState() == 4) std::cout << Animation->GetState() << ": " << AnimationProgress << "/" << AnimationDuration << " | " << AnimationTile+1 << "/" << Animation->GetTiles() << " " << Animation->GetWeight(AnimationTile) << " " << TileDuration << " " << AnimationTileProgress << "/" << RoundedDuration << " " << BonusDuration << std::endl;
//Reset Tile
if (AnimationTileProgress >= RoundedDuration) {
if (AnimationTile+1 < Animation->GetTiles()) { //Conditional to prevent moving out of bounds on extra animation duration
AnimationTile += 1;
AnimationTileProgress = 0;
}
}
Tile = Animation->GetTile(AnimationTile);
}
}
void Renderable::PushAnimation(int Animation) {
AnimationQueue = Animation;
}
void Renderable::Render(sdlObjects::Texture *Texture, float TargetX, float TargetY, float TargetWidth, float TargetHeight, int TargetTile, float ClipWidth, float ClipHeight, float ClipX, float ClipY) {
if (Texture) {
int TextureColumns = Texture->GetColumns();
int TextureRows = Texture->GetRows();
int TextureTiles = Texture->GetTilesNumber();
int TextureWidth = Texture->GetWidth(true);
int TextureHeight = Texture->GetHeight(true);
int TextureTileWidth = Texture->GetWidth();
int TextureTileHeight = Texture->GetHeight();
//Target dimensions
if (!TargetWidth) TargetWidth = TextureTileWidth;
if (!TargetHeight) TargetHeight = TextureTileHeight;
//Calculate target column and row
int TargetColumn = 1, TargetRow = 1;
if (TargetTile > 0 && TargetTile <= TextureTiles) {
TargetRow = TargetTile/TextureColumns;
if (TargetTile%TextureColumns != 0) TargetRow+=1;
TargetColumn = TargetTile-(TargetRow-1)*TextureColumns;
}
//Limit clipping
if (ClipWidth > 1) ClipWidth = 1; else if (ClipWidth < 0) ClipWidth = 0;
if (ClipHeight > 1) ClipHeight = 1; else if (ClipHeight < 0) ClipHeight = 0;
if (ClipX <= 0) ClipX = 0; else if (ClipX > 1) ClipX = 1;
if (ClipY <= 0) ClipY = 0; else if (ClipY > 1) ClipY = 1;
//Quad position
float X1 = 0, X2 = 0, X3 = 0, X4 = 0;
float Y1 = 0, Y2 = 0, Y3 = 0, Y4 = 0;
X1 = TargetX+ClipX*TargetWidth; Y1 = TargetY+ClipY*TargetHeight;
X2 = TargetX+ClipWidth*TargetWidth; Y2 = TargetY+ClipY*TargetHeight;
X3 = TargetX+ClipWidth*TargetWidth; Y3 = TargetY+ClipHeight*TargetHeight;
X4 = TargetX+ClipX*TargetWidth; Y4 = TargetY+ClipHeight*TargetHeight;
//Texture mapping
float MappingWidth = TextureTileWidth*(ClipWidth), MappingHeight = TextureTileHeight*(ClipHeight);
float TextureX1 = (float)(TargetColumn-1+ClipX)/TextureColumns, TextureY1 = (float)(TargetRow-1+ClipY)/TextureRows;
float TextureX2 = (float)TargetColumn*MappingWidth/TextureWidth, TextureY2 = (float)(TargetRow-1+ClipY)/TextureRows;
float TextureX3 = (float)TargetColumn*MappingWidth/TextureWidth, TextureY3 = (float)TargetRow*MappingHeight/TextureHeight;
float TextureX4 = (float)(TargetColumn-1+ClipX)/TextureColumns, TextureY4 = (float)TargetRow*MappingHeight/TextureHeight;
//Texture mapping fix
//float Modifier = 0.25f;
//TextureX1 += (float)Modifier/TextureWidth; TextureY1 += (float)Modifier/TextureHeight;
//TextureX2 -= (float)Modifier/TextureWidth; TextureY2 += (float)Modifier/TextureHeight;
//TextureX3 -= (float)Modifier/TextureWidth; TextureY3 -= (float)Modifier/TextureHeight;
//TextureX4 += (float)Modifier/TextureWidth; TextureY4 -= (float)Modifier/TextureHeight;
//if (FlipX) {Scale(GL_TEXTURE, -1, 1);}
//Render quad
//glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(TextureX1, TextureY1); glVertex2f(X1, Y1);
glTexCoord2f(TextureX2, TextureY2); glVertex2f(X2, Y2);
glTexCoord2f(TextureX3, TextureY3); glVertex2f(X3, Y3);
glTexCoord2f(TextureX4, TextureY4); glVertex2f(X4, Y4);
glEnd();
}
}
Renderable::Renderable() {
BonusDuration = 0.f;
AnimationDuration = AnimationProgress = AnimationTile = AnimationTileProgress = AnimationQueue = Tile = 0;
Color[0] = Color[1] = Color[2] = Color[3] = IdleColor[0] = IdleColor[1] = IdleColor[2] = IdleColor[3] = 255; DisabledColor[0] = DisabledColor[1] = DisabledColor[2] = DisabledColor[3] = 150;
HoverColor[0] = HoverColor[1] = HoverColor[2] = HoverColor[3] = -1;
Animation = NULL;
Texture = NULL;
}
void Renderable::RenderAsArea(std::vector< std::vector<int> > &TileMap, float TargetX, float TargetY, float TargetWidth, float TargetHeight, sdlObjects::Texture *Texture) {
if (!Texture) Texture = this->Texture;
if (Texture) {
float TileWidth = Texture->GetWidth();
float TileHeight = Texture->GetHeight();
int Columns = 0, Rows = 0;
if (TileMap.size() > 0) {
Columns = TileMap.size();
Rows = TileMap[0].size();
}
if (!TargetWidth) TargetWidth = TileWidth;
if (!TargetHeight) TargetHeight = TileHeight;
GLuint GLTexture = Texture->GetGLTexture();
glBindTexture(GL_TEXTURE_2D, GLTexture);
for (int i = 0; i<Columns; i++) {
for (int j = 0; j<Rows; j++) {
if (TileMap[i][j]) Render(Texture, TargetX + j*TargetWidth, TargetY + i*TargetHeight, TargetWidth, TargetHeight, TileMap[i][j]);
}
}
glBindTexture(GL_TEXTURE_2D, 0);
}
}
void Renderable::RenderAsDialog(float TargetX, float TargetY, int TargetWidth, int TargetHeight, sdlObjects::Texture *Texture) {
if (!Texture) Texture = this->Texture;
Renderer::SetColor(Color[0], Color[1], Color[2], Color[3]);
if (Texture) {
float TileWidth = Texture->GetWidth();
float TileHeight = Texture->GetHeight();
int Columns = TargetWidth/TileWidth-2;
int Rows = TargetHeight/TileHeight-2;
float WidthModulo = fmod(TargetWidth, TileWidth);
float HeightModulo = fmod(TargetHeight, TileHeight);
GLuint GLTexture = Texture->GetGLTexture();
glBindTexture(GL_TEXTURE_2D, GLTexture);
//Border corner (Top left)
Render(Texture, TargetX, TargetY, TileWidth, TileHeight, 1);
//Border corner (Bottom left)
Render(Texture, TargetX, TargetY+TargetHeight-TileHeight, TileWidth, TileHeight, 7);
//Border fill (Left)
if (HeightModulo && Rows > -1) Render(Texture, TargetX, TargetY+(Rows+1) *TileHeight, TileWidth, HeightModulo, 4);
//Border corner (Top right)
Render(Texture, TargetX+TargetWidth-TileWidth, TargetY, TileWidth, TileHeight, 3);
//Border corner (Bottom right)
Render(Texture, TargetX+TargetWidth-TileWidth, TargetY+TargetHeight-TileHeight, TileWidth, TileHeight, 9);
//Border fill (Right)
if (HeightModulo && Rows > -1) Render(Texture, TargetX+TargetWidth-TileWidth, TargetY+TileHeight *(Rows+1), TileWidth, HeightModulo, 6);
//Border fill (Top)
if (WidthModulo && Columns > -1) Render(Texture, TargetX+TileWidth*(Columns+1), TargetY, WidthModulo, TileHeight, 2);
//Border fill (Bottom)
if (WidthModulo && Columns > -1) Render(Texture, TargetX+TileWidth*(Columns+1), TargetY+TargetHeight-TileHeight, WidthModulo, TileHeight, 8);
for (int j = 0; j<Columns; j++) {
//Border (Top)
Render(Texture, TargetX+TileWidth *(j+1), TargetY, TileWidth, TileHeight, 2);
//Border (Bottom)
Render(Texture, TargetX+TileWidth *(j+1), TargetY+TargetHeight-TileHeight, TileWidth, TileHeight, 8);
//Content fill (Bottom)
if (HeightModulo && Rows > -1) Render(Texture, TargetX+TileWidth*(j+1), TargetY+(Rows+1)*TileHeight, TileWidth, HeightModulo, 5);
}
for (int i = 0; i<Rows; ++i) {
//Border (Left)
Render(Texture, TargetX, TargetY+TileHeight *(i+1), TileWidth, TileHeight, 4);
//Border (Right)
Render(Texture, TargetX+TargetWidth-TileWidth, TargetY+TileHeight*(i+1), TileWidth, TileHeight, 6);
//Content
for (int j = 0; j<Columns; j++) Render(Texture, TargetX+TileWidth*(j+1), TargetY+TileHeight*(i+1), TileWidth, TileHeight, 5);
//Content fill (Right)
if (WidthModulo && Columns > -1) Render(Texture, TargetX+TileWidth*(Columns+1), TargetY+TileHeight*(i+1), WidthModulo, TileHeight, 5);
}
//Content fill (Bottom right)
if (WidthModulo && HeightModulo && Columns > -1 && Rows > -1) Render(Texture, TargetX+TileWidth*(Columns+1), TargetY+TileHeight*(Rows+1), WidthModulo, HeightModulo, 5);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
void Renderable::RenderAsTexture(float TargetX, float TargetY, int TargetWidth, int TargetHeight, float ClipWidth, float ClipHeight, float ClipX, float ClipY, sdlObjects::Texture *Texture, bool OverrideColour) {
if (!Texture) Texture = this->Texture;
if (!OverrideColour) Renderer::SetColor(Color[0], Color[1], Color[2], Color[3]);
if (Texture) {
GLuint GLTexture = Texture->GetGLTexture();
glBindTexture(GL_TEXTURE_2D, GLTexture);
Render(Texture, TargetX, TargetY, TargetWidth, TargetHeight, Tile, ClipWidth, ClipHeight, ClipX, ClipY);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
void Renderable::ResetAnimation() {
AnimationProgress = 0;
AnimationTile = 0;
AnimationTileProgress = 0;
if (Animation) Tile = Animation->GetTile(0); else Tile = 0;
}
void Renderable::SetAnimation(sdlObjects::Animation *Animation) {
if (Animation && Animation->GetState() == AnimationQueue) {
//if (Animationset == "" && Animation->GetState() != 1) std::cout << Animationset << " set to " << Animation->GetState() << std::endl;
this->Animation = Animation;
AnimationDuration = Animation->GetDuration(); //Backup Animation Duration
//Reset Queue
AnimationQueue = 0;
//Reset Animation
AnimationProgress = 0;
AnimationTile = 0;
AnimationTileProgress = 0;
BonusDuration = 0.f;
EventQueue.push_back(Events::AnimationChange);
} else {
this->Animation = NULL;
//Reset Queue
AnimationQueue = 0;
//Reset Animation
AnimationProgress = 0;
AnimationTile = 0;
AnimationTileProgress = 0;
BonusDuration = 0.f;
}
}
void Renderable::SetAnimationDuration(int AnimationDuration) {
this->AnimationDuration = AnimationDuration;
}
void Renderable::SetAnimationProgress(int AnimationProgress, int AnimationTile, int AnimationTileProgress, float BonusDuration) {
this->AnimationProgress = AnimationProgress;
this->AnimationTile = AnimationTile;
this->AnimationTileProgress = AnimationTileProgress;
this->BonusDuration = BonusDuration;
}
void Renderable::SetAnimationset(std::string Animationset) {
this->Animationset = Animationset;
}
void Renderable::SetColor(int ColorMode) {
if (ColorMode == Color::Idle) {
for (int i = 0; i<4; i++) Color[i] = IdleColor[i];
} else if (ColorMode == Color::Hovered) {
for (int i = 0; i<4; i++) if (HoverColor[i] > -1) Color[i] = HoverColor[i];
} else if (ColorMode == Color::Disabled) {
for (int i = 0; i<4; i++) if (DisabledColor[i] > -1) Color[i] = DisabledColor[i];
}
}
void Renderable::SetHoverColor(int Color[4]) {
for (int i = 0; i<4; i++) HoverColor[i] = Color[i];
}
void Renderable::SetIdleColor(int Color[4]) {
for (int i = 0; i<4; i++) if (Color[i] > -1) IdleColor[i] = this->Color[i] = Color[i];
}
void Renderable::SetTile(int Tile) {
this->Tile = Tile;
}
void Renderable::SetTexture(sdlObjects::Texture *Texture) {
if (this->Texture) this->Texture->SetUses(-1);
this->Texture = Texture;
if (Texture) Texture->SetUses(1);
if (Texture) EventQueue.push_back(Events::TextureChange);
}
void Renderable::SetTextureID(std::string TextureID) {
this->TextureID = TextureID;
}
void Renderable::SetTransparency(float Transparency) {
Color[3] = Transparency*255.0f;
HoverColor[3] = Transparency*255.0f;
}
Renderable::~Renderable() {
BonusDuration = 0.f;
AnimationDuration = AnimationProgress = AnimationTile = AnimationTileProgress = AnimationQueue = Tile = 0;
Color[0] = Color[1] = Color[2] = Color[3] = IdleColor[0] = IdleColor[1] = IdleColor[2] = IdleColor[3] = 255;
HoverColor[0] = HoverColor[1] = HoverColor[2] = HoverColor[3] = -1;
Animation = NULL;
Texture = NULL;
}
}