-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
485 lines (416 loc) · 12.9 KB
/
main.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "main.h"
int level1[4*BLOCKSIZEY*BLOCKSIZEX] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, 3, 4, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, 3, 4, -1, -1, -1, -1, -1, -1
, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, -1, -1, 0, 0
, 5, 5, 5, 5, 5, 2, 5, 5, -1, -1, 2, -1, -1, -1, -1, -1
, 8, 8, 8, 6, 6, 2, 7, 6, -1, -1, 3, 4, -1, -1, -1, -1
, 9, 9, 9, 6, 6, 2, 6, 6, -1, -1, 3, 4, -1, -1, -1, -1
, 7, 6, 10, 6, 6, 2, 6, 6, 3, 4, 3, 4, -1, -1, -1, -1
, 6, 6, 6, 6, 6, 2, 6, 7, 3, 4, 3, 4, -1, -1, -1, -1
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, 3, 4, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, 3, 4, -1, -1, -1, -1, -1, -1
, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, -1, -1, 0, 0
, 5, 5, 5, 5, 5, 2, 5, 5, -1, -1, 2, -1, -1, -1, -1, -1
, 8, 8, 8, 6, 6, 2, 7, 6, -1, -1, 3, 4, -1, -1, -1, -1
, 9, 9, 9, 6, 6, 2, 6, 6, -1, -1, 3, 4, -1, -1, -1, -1
, 7, 6, 10, 6, 6, 2, 6, 6, 3, 4, 3, 4, -1, -1, -1, -1
, 6, 6, 6, 6, 6, 2, 6, 7, 3, 4, 3, 4, -1, -1, -1, -1
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, -1, -1, 3, 4, -1, -1, -1, -1, -1, -1
,-1, 0, 0, -1, -1, -1, -1, -1, 3, 4, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1
,-1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 0, 0, -1, -1
,-1, -1, -1, -1, 0, 0, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1
,-1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 0, 0
, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1
,-1, -1, -1, -1, -1, -1, -1, -1, 5, 5, 5, 5, 1, 2, 1, 1
,-1, -1, -1, -1, -1, -1, -1, -1, 7, 6, 7, 6, 1, 2, 1, 1
,-1, -1, -1, -1, -1, -1, -1, -1, 11, 10, 10, 10, 5, 2, 1, 1
,-1, -1, -1, -1, -1, -1, -1, -1, 6, 11, 10, 12, 7, 2, 1, 1
,-1, -1, -1, -1, 1, 2, 0, 6, 6, 0, 0, 0, 0, 0, 1, 1
,-1, -1, -1, -1, 1, 2, 1, 7, 7, 1, 1, 1, 1, 1, 1, 1
,-1, -1, -1, -1, 1, 2, 5, 6, 6, 5, 5, 5, 1, 1, 1, 1
,-1, -1, -1, -1, 1, 2, 6, 7, 6, 6, 7, 6, 1, 1, 1, 1
,-1, -1, -1, -1, 1, 2, 6, 6, 6, 7, 11, 12, 1, 1, 1, 1
,-1, -1, -1, -1, 1, 2, 0, 0, 0, 6, 6, 6, 1, 1, 1, 1
,-1, -1, -1, -1, 5, 5, 5, 7, 5, 6, 6, 0, 1, 1, 1, 1
,-1, -1, -1, -1, 7, 6, 10, 12, 7, 6, 7, 1, 1, 1, 1, 1
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
int level1_blocks = 4;
Tile::Tile() {
tileID = 0;
typeID = TILE_TYPE_NONE;
}
Map::Map() {
Surf_Tileset = NULL;
}
bool Map::load(int level[]) {
TileList.clear();
int col, row, block;
for(block = 0; block < level1_blocks; block++) {
for(row = 0; row < BLOCKSIZEY; row++) {
for(col = 0; col < BLOCKSIZEX; col++) {
Tile tmpTile;
tmpTile.tileID = level[(block*BLOCKSIZEX*BLOCKSIZEY)+(row*BLOCKSIZEX)+col];
switch(tmpTile.tileID) {
case -1:
tmpTile.typeID = TILE_TYPE_NONE;
break;
case 2:
tmpTile.typeID = TILE_TYPE_LADDER;
break;
case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12:
tmpTile.typeID = TILE_TYPE_NORMAL;
break;
default:
tmpTile.typeID = TILE_TYPE_BLOCK;
}
tmpTile.x = (block * BLOCKSIZEX + col) * SPRITESIZE;
tmpTile.y = row * SPRITESIZE;
TileList.push_back(tmpTile);
}
}
}
return true;
}
Tile Map::getTile(int px, int py) {
// @TODO: Check boundaries!
int block = px / SPRITESIZE / BLOCKSIZEX;
int col = (px / SPRITESIZE) - (block*BLOCKSIZEX);
int row = py / SPRITESIZE;
return TileList[(block*BLOCKSIZEX*BLOCKSIZEY)+(row*BLOCKSIZEX)+col];
}
void Map::render() {
// @TODO: Only draw visible tiles
Uint32 i;
for(i=0; i < TileList.size(); i++) {
gSpriteSheetTexture.render(TileList[i].x, TileList[i].y, TileList[i].tileID);
}
}
Player::Player() {
mTexture = NULL;
x = 100;
y = 12*SPRITESIZE-27;
hotSpot1 = {9, 27}; // Bottom left hotspot
hotSpot2 = {23, 27}; // Bottom right hotspot
printf("init: %0.2f %0.2f\n", y, yVel);
if(!loadFromFile("res/player.bmp")) {
printf("Failed to load sprite sheet texture!\n");
}
};
Map map;
void Player::animate() {
if(isJumping || !isGrounded) {
frame = 6;
return;
}
if(isMoving) {
if(lastAnimated + 100 > SDL_GetTicks()) return;
lastAnimated = SDL_GetTicks();
if(frame == 0) {
frame = 2;
}
else {
frame = frame == 3 ? 4: 3;
}
} else {
if(frame == 0 && lastAnimated + 2000 > SDL_GetTicks()) return;
if(frame == 1 && lastAnimated + 100 > SDL_GetTicks()) return;
lastAnimated = SDL_GetTicks();
frame = frame == 0 ? 1 : 0;
}
}
void Player::jump() {
if(!isGrounded) return;
yVel = -260.0f;
gravity = 700.0f;
isJumping = true;
}
void Player::setCamera() {
camera.x = (x + 32 / 2) - (INTERNAL_WIDTH /2);
if(camera.x < 0) camera.x=0;
else if(camera.x > (level1_blocks-1)*BLOCKSIZEX*SPRITESIZE) camera.x=(level1_blocks-1)*BLOCKSIZEX*SPRITESIZE;
}
void Player::move() {
float delta = (currentTime - lastTime) / 1000.0f;
if(isMoving) {
x += xVel * delta;
if(x < 0) x = 0;
else if(x > ((level1_blocks)*BLOCKSIZEX*SPRITESIZE) - 32)
x=((level1_blocks)*BLOCKSIZEX*SPRITESIZE) - 32;
setCamera();
}
// Check that at least one of the bottom two hotspots are in solid tiles.
if(yVel > 0 && (
map.getTile(hotSpot1.x+x, hotSpot1.y+y).typeID == TILE_TYPE_BLOCK ||
map.getTile(hotSpot2.x+x, hotSpot2.y+y).typeID == TILE_TYPE_BLOCK)) {
yVel = 50.0f;
isGrounded = true;
isJumping = false;
} else {
isGrounded = false;
yVel += gravity*delta;
y += yVel*delta;
printf("%0.2f %0.2f\n", y, yVel);
}
animate();
}
void Player::render() {
SDL_Rect clipRect = {
frame*32
,0
,32
,32
};
SDL_Rect renderRect = {
(int)x - camera.x
,(int)y
,32
,32
};
SDL_RenderCopyEx(gRenderer, mTexture, &clipRect, &renderRect, 0, NULL, flip);
// Debug hotspots
if(true) {
SDL_Rect r1 = {(int)x - camera.x + hotSpot1.x, (int)y + hotSpot1.y, 1, 1};
SDL_Rect r2 = {(int)x - camera.x + hotSpot2.x, (int)y + hotSpot2.y, 1, 1};
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0x0, 0xFF, 0xFF );
SDL_RenderFillRect(gRenderer, &r1);
SDL_RenderFillRect(gRenderer, &r2);
}
}
void Player::handleEvents(SDL_Event& e) {
if(e.type == SDL_KEYDOWN && e.key.repeat == 0) {
switch(e.key.keysym.sym) {
case SDLK_LEFT:
flip = SDL_FLIP_HORIZONTAL;
isMoving = true;
xVel = abs(xVel)*-1.0f;
break;
case SDLK_RIGHT:
flip = SDL_FLIP_NONE;
isMoving = true;
xVel = abs(xVel);
break;
case SDLK_SPACE:
jump();
break;
}
}
if(e.type == SDL_KEYUP && e.key.repeat == 0) {
switch(e.key.keysym.sym) {
case SDLK_LEFT:
case SDLK_RIGHT:
isMoving = false;
break;
}
}
}
bool Player::loadFromFile(std::string path) {
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if(loadedSurface == NULL) {
printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else {
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if(newTexture == NULL) {
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
SDL_FreeSurface(loadedSurface);
}
mTexture = newTexture;
return mTexture != NULL;
}
int Player::getTileAt(int px, int py) {
// @TODO: Check boundaries!
int block = px / SPRITESIZE / BLOCKSIZEX;
int col = (px / SPRITESIZE) - (block*BLOCKSIZEX);
int row = py / SPRITESIZE;
return level1[(block*BLOCKSIZEX*BLOCKSIZEY)+(row*BLOCKSIZEX)+col];
}
SpriteSheet::SpriteSheet() {
mTexture = NULL;
}
SpriteSheet::~SpriteSheet() {
free();
}
bool SpriteSheet::loadFromFile(std::string path) {
free();
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if(loadedSurface == NULL) {
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), SDL_GetError());
}
else {
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if(newTexture == NULL) {
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
SDL_FreeSurface(loadedSurface);
}
mTexture = newTexture;
return mTexture != NULL;
}
void SpriteSheet::free() {
if(mTexture != NULL) {
SDL_DestroyTexture(mTexture);
mTexture = NULL;
}
}
void SpriteSheet::render(int x, int y, int frameOffset) {
SDL_Rect clipRect = {
frameOffset * SPRITESIZE
,0
,SPRITESIZE
,SPRITESIZE
};
SDL_Rect renderRect = {
x - camera.x
,(y)
,SPRITESIZE
,SPRITESIZE
};
SDL_RenderCopy(gRenderer, mTexture, &clipRect, &renderRect);
}
bool init() {
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else {
gWindow = SDL_CreateWindow("Little blue robot guy", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, INTERNAL_WIDTH*4, INTERNAL_HEIGHT*4, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if(gWindow == NULL) {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else {
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer == NULL) {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else {
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0")) {
printf("Warning: Failed to set render hint!");
}
if(!SDL_SetHint( SDL_HINT_RENDER_VSYNC, "0"))
{
printf( "Warning: VSync not enabled!" );
}
if(!SDL_RenderSetLogicalSize(gRenderer, INTERNAL_WIDTH, INTERNAL_HEIGHT)) {
printf("Warning: Failed to set logical size\n");
}
}
}
}
return success;
}
bool loadMedia() {
if(!gSpriteSheetTexture.loadFromFile("res/sprites.bmp")) {
printf("Failed to load sprite sheet texture!\n");
return false;
}
return true;
}
void close() {
gSpriteSheetTexture.free();
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
//IMG_Quit();
SDL_Quit();
}
void toggleFullscreen() {
SDL_SetWindowFullscreen(gWindow, bIsFullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
bIsFullscreen = !bIsFullscreen;
}
// Check if two rectangles intersect
bool collides(SDL_Rect *a, SDL_Rect *b) {
int ax1 = a->x
,ax2 = a->x+a->w
,ay1 = a->y
,ay2 = a->y+a->h
,bx1 = b->x
,bx2 = b->x+b->w
,by1 = b->y
,by2 = b->y+b->h;
return (ax1 < bx2 && ax2 > bx1 && ay1 < by2 && ay2 > by1);
}
int main(int argc, char* args[]) {
if(!init()) {
printf("Failed to initialize!\n");
}
else {
if(!loadMedia()) {
printf("Failed to load media!\n");
}
else {
bool quit = false;
SDL_Event e;
Player player;
SDL_Rect outlineRect = {0, 0, INTERNAL_WIDTH, INTERNAL_HEIGHT};
map.load(level1);
currentTime = SDL_GetTicks();
while(!quit) {
lastTime = currentTime;
currentTime = SDL_GetTicks();
while(SDL_PollEvent(&e) != 0) {
switch(e.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
switch(e.key.keysym.sym) {
case SDLK_RETURN:
if(e.key.keysym.mod & KMOD_ALT)
toggleFullscreen();
break;
case SDLK_F11:
toggleFullscreen();
break;
}
break;
}
player.handleEvents(e);
}
player.move();
SDL_SetRenderDrawColor( gRenderer, 0x0, 0x0, 0x0, 0xFF );
SDL_RenderClear( gRenderer );
SDL_SetRenderDrawColor( gRenderer, 0x0, 0xE8, 0xD8, 0xFF );
SDL_RenderFillRect( gRenderer, {&outlineRect} );
map.render();
player.render();
SDL_RenderPresent( gRenderer );
}
}
}
close();
return 0;
}