-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAngularSprite.cpp
456 lines (394 loc) · 14.7 KB
/
AngularSprite.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include "AngularSprite.h"
#include "gameData.h"
#include "imageFactory.h"
#define OFCOURSEISTILLLOVEYOU std::cerr << __LINE__ << std::endl;
inline namespace {
inline void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
}
/*AngularSprite::AngularSprite( const std::string& name) :
Drawable(name,
Vector2f(Gamedata::getInstance().getXmlInt(name+"/startLoc/x"),
Gamedata::getInstance().getXmlInt(name+"/startLoc/y")),
Vector2f(Gamedata::getInstance().getXmlInt(name+"/speedX"),
Gamedata::getInstance().getXmlInt(name+"/speedY"))
),
image( ImageFactory::getInstance().getImage(name+"/tracks/Donut_Plains_1/full", name) ),
track( IMG_Load(Gamedata::getInstance().getXmlStr("road/tracks/Donut_Plains_1/full").c_str())),
sky( IMG_Load(Gamedata::getInstance().getXmlStr("sky/file").c_str())),
grass( IMG_Load(Gamedata::getInstance().getXmlStr("road/tracks/Donut_Plains_1/grass").c_str())),
bounds( IMG_Load(Gamedata::getInstance().getXmlStr("road/tracks/Donut_Plains_1/boundry").c_str())),
start( IMG_Load(Gamedata::getInstance().getXmlStr("road/tracks/Donut_Plains_1/start").c_str())),
worldWidth(Gamedata::getInstance().getXmlInt("background/width")),
worldHeight(Gamedata::getInstance().getXmlInt("background/height")),
cfp (FrustumPoints::getInstance())
{
} */
AngularSprite::AngularSprite( const std::string& name, Tracks& t) :
Drawable(name,
Vector2f(Gamedata::getInstance().getXmlInt(name+"/startLoc/x"),
Gamedata::getInstance().getXmlInt(name+"/startLoc/y")),
Vector2f(Gamedata::getInstance().getXmlInt(name+"/speedX"),
Gamedata::getInstance().getXmlInt(name+"/speedY"))
),
image( ImageFactory::getInstance().getImage(name+"/tracks/"+t.getName()+"/full", name) ),
track( t.getTrack() ),
sky( IMG_Load(Gamedata::getInstance().getXmlStr("sky/file").c_str())),
grass( t.getGrass() ),
bounds( t.getBounds() ),
start( t.getStart() ),
worldWidth(Gamedata::getInstance().getXmlInt("background/width")),
worldHeight(Gamedata::getInstance().getXmlInt("background/height")),
cfp (FrustumPoints::getInstance())
{ }
AngularSprite::AngularSprite(const AngularSprite& s) :
Drawable(s),
image(s.image),
track(s.track),
sky(s.sky),
grass(s.grass),
bounds(s.bounds),
start(s.start),
worldWidth( s.worldWidth ),
worldHeight( s.worldHeight ),
cfp(FrustumPoints::getInstance())
{
}
AngularSprite& AngularSprite::operator=(const AngularSprite& s) {
Drawable::operator=(s);
image = (s.image);
track = s.track;
sky = s.sky;
grass = s.grass;
bounds = s.bounds;
start = s.start;
worldWidth = ( s.worldWidth );
worldHeight = ( s.worldHeight );
//cfp = (FrustumPoints::getInstance());
return *this;
}
bool AngularSprite::checkLap(float p_x, float p_y, SDL_Surface *s)
{
if(mapCheck(p_x,p_y,s))
{
return true;
}
return false;
}
bool AngularSprite::checkVelocity(Uint32 ticks, int direction, float p_x, float p_y, float factor)
{
if(direction == 1)
{
//cfp.fNear -= 0.1f * ticks;
float fWorldA = cfp.getfWorldA();
cfp.setfWorldA(fWorldA-factor);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfWorldA(fWorldA);
return blocked;
}
else if (direction == 2)
{
float fWorldA = cfp.getfWorldA();
cfp.setfWorldA(fWorldA+factor);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfWorldA(fWorldA);
return blocked;
}
else if(direction ==3)
{
float fWorldX = cfp.getfWorldX();
float fWorldY = cfp.getfWorldY();
cfp.setfWorldX(fWorldX+cosf(cfp.getfWorldA()) * factor * ticks);
cfp.setfWorldY(fWorldY+sinf(cfp.getfWorldA()) * factor * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfWorldX(fWorldX);
cfp.setfWorldY(fWorldY);
return blocked;
}
else if (direction == 4)
{
float fWorldX = cfp.getfWorldX();
float fWorldY = cfp.getfWorldY();
cfp.setfWorldX(fWorldX-cosf(cfp.getfWorldA()) * factor * ticks);
cfp.setfWorldY(fWorldY-sinf(cfp.getfWorldA()) * factor * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfWorldX(fWorldX);
cfp.setfWorldY(fWorldY);
return blocked;
}
else if (direction == 5)
{
float fNear = cfp.getfNear();
cfp.setfNear(cfp.getfNear()-0.1f * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfNear(fNear);
return blocked;
}
else if (direction == 6)
{
float fNear = cfp.getfNear();
cfp.setfNear(cfp.getfNear()+0.1f * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfNear(fNear);
return blocked;
}
else if (direction == 7)
{
//cfp.fFoVHalf -= 0.2f * ticks;
float fFar = cfp.getfFar();
cfp.setfFar(cfp.getfFar()-0.1f * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfFar(fFar);
return blocked;
}
else if (direction == 8)
{
float fFar = cfp.getfFar();
cfp.setfFar(cfp.getfFar()+0.1f * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfFar(fFar);
return blocked;
}
else if (direction == 9)
{
//cfp.fFoVHalf -= 0.2f * ticks;
float fFoVHalf = cfp.getfFoVHalf();
cfp.setfFoVHalf(cfp.getfFoVHalf()-0.1f * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfFoVHalf(fFoVHalf);
return blocked;
}
else if (direction == 10)
{
float fFoVHalf = cfp.getfFoVHalf();
cfp.setfFoVHalf(cfp.getfFoVHalf()+0.1f * ticks);
bool blocked = CheckNewValue(p_x, p_y,bounds);
cfp.setfFoVHalf(fFoVHalf);
return blocked;
}
return true;
}
void AngularSprite::update(Uint32 ticks){}
void AngularSprite::update(Uint32 ticks, int direction, float factor)
{
if(direction == 1)
{
//cfp.fNear -= 0.1f * ticks;
cfp.setfWorldA(cfp.getfWorldA()-factor);
}
else if (direction == 2)
{
cfp.setfWorldA(cfp.getfWorldA()+factor);
}
else if(direction ==3)
{
float fWorldX = cfp.getfWorldX();
float fWorldY = cfp.getfWorldY();
cfp.setfWorldX(fWorldX+cosf(cfp.getfWorldA()) * factor * ticks);
cfp.setfWorldY(fWorldY+sinf(cfp.getfWorldA()) * factor * ticks);
}
else if (direction == 4)
{
float fWorldX = cfp.getfWorldX();
float fWorldY = cfp.getfWorldY();
cfp.setfWorldX(fWorldX-cosf(cfp.getfWorldA()) * factor * ticks);
cfp.setfWorldY(fWorldY-sinf(cfp.getfWorldA()) * factor * ticks);
//std::cout<<"values in Sprite "<<cfp.getfWorldX()<<" " <<cfp.getfWorldY()<<std::endl;
}
else if (direction == 5)
{
cfp.setfNear(cfp.getfNear()-0.1f * ticks);
}
else if (direction == 6)
{
//cfp.fFoVHalf += 0.2f * ticks;
cfp.setfNear(cfp.getfNear()+0.1f * ticks);
}
else if (direction == 7)
{
//cfp.fFoVHalf -= 0.2f * ticks;
cfp.setfFar(cfp.getfFar()-0.1f * ticks);
}
else if (direction == 8)
{
//cfp.fFoVHalf += 0.2f * ticks;
cfp.setfFar(cfp.getfFar()+0.1f * ticks);
}
else if (direction == 9)
{
//cfp.fFoVHalf -= 0.2f * ticks;
cfp.setfFoVHalf(cfp.getfFoVHalf()-0.1f);
}
else if (direction == 10)
{
//cfp.fFoVHalf += 0.2f * ticks;
cfp.setfFoVHalf(cfp.getfFoVHalf()+0.1f);
}
}
bool AngularSprite::mapCheck(int p_x, int p_y, SDL_Surface *toTest){
SDL_LockSurface(toTest);
Uint32 *pixels = (Uint32*)toTest->pixels;
SDL_Color color;
float fFarX1 = cfp.getfWorldX() + cosf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfFar();
float fFarY1 = cfp.getfWorldY() + sinf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfFar();
float fNearX1 = cfp.getfWorldX() + cosf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfNear();
float fNearY1 = cfp.getfWorldY() + sinf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfNear();
float fFarX2 = cfp.getfWorldX() + cosf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfFar();
float fFarY2 = cfp.getfWorldY() + sinf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfFar();
float fNearX2 = cfp.getfWorldX() + cosf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfNear();
float fNearY2 = cfp.getfWorldY() + sinf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfNear();
for(int x = p_x-2;x<= p_x+5; x++)
{
for(int y = p_y-2; y<= p_y+5; y++)
{
float fSampleDepth = (float)y / ((float)worldHeight / 2.0f);
float fSampleWidth = (float)x / (float)worldWidth;
float fStartX = (fFarX1 - fNearX1) / (fSampleDepth) + fNearX1;
float fStartY = (fFarY1 - fNearY1) / (fSampleDepth) + fNearY1;
float fEndX = (fFarX2 - fNearX2) / (fSampleDepth) + fNearX2;
float fEndY = (fFarY2 - fNearY2) / (fSampleDepth) + fNearY2;
float fSampleX = (fEndX - fStartX) * fSampleWidth + fStartX;
float fSampleY = (fEndY - fStartY) * fSampleWidth + fStartY;
//SDL_LockSurface(grass);
//Uint32 *pixels = (Uint32*)grass->pixels;
Uint32 pixel = pixels[(int(fSampleY) * toTest->pitch / 4) + int(fSampleX)];
SDL_GetRGBA(pixel, toTest->format, &color.r, &color.g, &color.b, &color.a);
}
}
SDL_UnlockSurface(toTest);
if ( int(color.a) == 255 ) {
return true;
}
else
return false;
}
float AngularSprite::GrassVelocity(int p_x, int p_y, SDL_Surface *g)
{
if(mapCheck(p_x, p_y, g))
{
return 0.2f;
}
return 0.5f;
}
bool AngularSprite::CheckNewValue(int p_x, int p_y, SDL_Surface *b)
{
if (b == nullptr) {
return true;
}
if(mapCheck(p_x, p_y, b))
{
return false;
}
return true;
}
void AngularSprite::draw(SDL_Renderer *renderer) const{
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 rmask = 0xff000000;
Uint32 gmask = 0x00ff0000;
Uint32 bmask = 0x0000ff00;
Uint32 amask = 0x000000ff;
#else
Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;
#endif
static SDL_Surface *surface3 = SDL_CreateRGBSurface(0, 1000, 1000, 32, rmask, gmask, bmask, amask);
// Create Frustum corner points
float fFarX1 = cfp.getfWorldX() + cosf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfFar();
float fFarY1 = cfp.getfWorldY() + sinf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfFar();
float fNearX1 = cfp.getfWorldX() + cosf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfNear();
float fNearY1 = cfp.getfWorldY() + sinf(cfp.getfWorldA() - cfp.getfFoVHalf() ) * cfp.getfNear();
float fFarX2 = cfp.getfWorldX() + cosf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfFar();
float fFarY2 = cfp.getfWorldY() + sinf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfFar();
float fNearX2 = cfp.getfWorldX() + cosf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfNear();
float fNearY2 = cfp.getfWorldY() + sinf(cfp.getfWorldA() + cfp.getfFoVHalf() ) * cfp.getfNear();
SDL_LockSurface(surface3);
Uint32 *pixels1 = (Uint32*)sky->pixels;
Uint32 *pixels2 = (Uint32*)track->pixels;
auto drawPixel = [&](int _x, int _y, Uint32 _pixel)
{
set_pixel(surface3, _x, _y, _pixel);
};
auto drawPixelColor = [&](int _x, int _y, Uint8 _r, Uint8 _g, Uint8 _b)
{
set_pixel(surface3, _x, _y, SDL_MapRGBA(surface3->format, _r, _g, _b, 255u));
};
for (int y = 0; y < worldHeight/2; y++)
{
// Take a sample point for depth linearly related to rows down screen
//std::cout<<fSampleDepth<<std::endl;
float fSampleDepth = (float)y / ((float)worldHeight / 2.0f);
//std::cout<<fSampleDepth<<std::endl;
if(fSampleDepth == 0)
fSampleDepth = 1;
// Use sample point in non-linear (1/x) way to enable perspective
// and grab start and end points for lines across the screen
float fStartX = (fFarX1 - fNearX1) / (fSampleDepth) + fNearX1;
float fStartY = (fFarY1 - fNearY1) / (fSampleDepth) + fNearY1;
float fEndX = (fFarX2 - fNearX2) / (fSampleDepth) + fNearX2;
float fEndY = (fFarY2 - fNearY2) / (fSampleDepth) + fNearY2;
// Linearly interpolate lines across the screen
for (int x = 0; x < worldWidth; x++)
{
//std::cout<<"drawing x "<<x<<std::endl;
float fSampleWidth = (float)x / (float)worldWidth;
float fSampleX = (fEndX - fStartX) * fSampleWidth + fStartX;
float fSampleY = (fEndY - fStartY) * fSampleWidth + fStartY;
// Wrap sample coordinates to give "infinite" periodicity on maps
//std::cout<<fSampleX<<" "<<fSampleY<<std::endl;
//fSampleX = fmod(fSampleX, 1024);
//fSampleY = fmod(fSampleY,1024);
SDL_Color color = {0,0,0,0};
if(fSampleX < 0 || fSampleX > 1024 || fSampleY<0 || fSampleY > 1024)
{
drawPixelColor(x, y+ worldHeight/2, color.r, color.g, color.b);
}
else
{
Uint32 pixel2 = pixels2[(int(fSampleY) * track->pitch / 4) + int(fSampleX)];
SDL_GetRGBA(pixel2, track->format, &color.r, &color.g, &color.b, &color.a);
drawPixelColor(x, y + worldHeight/2, color.r, color.g, color.b);
}
//std::cout<<(int(fSampleY) * sky->pitch / 4) + int(fSampleX)<<std::endl;
//std::cout<<(int(fSampleY) * track->pitch / 4) + int(fSampleX)<<std::endl;
// Sample symbol and colour from map sprite, and draw the
// pixel to the screen
fSampleX = fabs(fmod(fSampleX, 1022)+1);
fSampleY = fabs(fmod(fSampleY,1022)+1);
Uint32 pixel1 = pixels1[(int(fSampleY) * sky->pitch / 4) + int(fSampleX)];
SDL_GetRGBA(pixel1, sky->format, &color.r, &color.g, &color.b, &color.a);
drawPixelColor(x, y, color.r, color.g, color.b);
}
}
SDL_UnlockSurface(surface3);
SDL_Texture* frameTexture = SDL_CreateTextureFromSurface(renderer, surface3);
SDL_RenderCopy(renderer, frameTexture, NULL, NULL);
SDL_DestroyTexture(frameTexture);
}