Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
722 lines (571 loc) · 23.4 KB
/
Copy pathmain.cpp
File metadata and controls
722 lines (571 loc) · 23.4 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include <SDL.h>
#include <SDL_ttf.h>
#include <Eigen/Dense>
#include <Eigen/StdVector>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#define M_PI_F 3.14159265358979323846264338327950288f
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
static constexpr float mouseSensitivity = 1;
#else
uint32_t game_running = 0;
void emscripten_cancel_main_loop() { game_running = 0; }
void emscripten_set_main_loop_arg(void (*fcn)(void*), void* const arg, uint32_t const fps, int const infinite_loop)
{
uint32_t const MPF = fps > 0 ? 1000 / fps : 1000 / 60; // milliseconds per frame
// Only render a frame if at least the MPF has been passed in time
// If the time has not been passed, pop and process one event from
// the event queue. So, for fast computers, many events will be processed
// in a single frame. But for slow computers, only one event will be
// popped and processed in a single frame. Beneficial because on a slow
// computer, if all events were popped at once, if the W key were tapped
// for instance, it would add to the ydiff variable and subtract from it
// before MovePlayer would have a chance to do anything. In this case,
// the player might move too far, but at least the player moved.
game_running = 1;
while (game_running)
{
uint32_t const startTime = SDL_GetTicks();
fcn(arg);
uint32_t const endTime = SDL_GetTicks();
uint32_t const timeDiff = endTime - startTime;
if (timeDiff < MPF)
{
SDL_Delay(MPF - timeDiff); // Sleep for the remainder of the time to maintain FPS
}
}
}
static constexpr float mouseSensitivity = 0.1f;
#endif
struct Entity
{
Entity(std::string_view const _name, SDL_Surface* const _texture, Eigen::Vector3f const& _pos) noexcept
: name{_name}, texture{_texture}, pos{_pos}
{
}
std::string name;
SDL_Surface* texture;
Eigen::Vector3f pos; // x, y, h
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
struct GameResources
{
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture;
SDL_Surface* display;
SDL_Surface* floor;
SDL_Surface* skyTranspose;
SDL_Surface* wallTranspose;
SDL_Surface* columnSprite;
SDL_Surface* healthSprite;
SDL_Surface* vialSprite;
SDL_Surface* doomGuy[8];
TTF_Font* font;
std::vector<Entity, Eigen::aligned_allocator<Entity>> entities;
uint32_t windowID;
~GameResources() noexcept
{
TTF_CloseFont(font);
SDL_FreeSurface(floor);
SDL_FreeSurface(skyTranspose);
SDL_FreeSurface(wallTranspose);
SDL_FreeSurface(display);
SDL_FreeSurface(columnSprite);
SDL_FreeSurface(healthSprite);
SDL_FreeSurface(vialSprite);
for (size_t i = 0; i < 8; i++)
{
SDL_FreeSurface(doomGuy[i]);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
};
// Render properties
struct render
{
uint16_t xres; // X resolution
uint16_t yres; // Y resolution
} render = {640, 480}; // Defaults: 640x480 at 35fps native (DOOM), vsync for emscripten
// Key states
struct movement
{
int xdiff; // Whether A(-1) or D(1) is down
int ydiff; // Whether W(1) or S(-1) is down
int hdiff; // Whether T(1) or G(-1) is down
int cdiff; // Whether Y(-1) or H(1) is down
} move = {};
// Player location, direction, and speed
struct direction
{
// Direction vector
Eigen::Vector2f p;
// Normal vector (must point east in screenspace)
Eigen::Vector2f n;
// Player angle from X axis
float theta;
// Player position
Eigen::Vector3f pos;
// Camera properties
float cpos; // Distance from lens
// Player speed
float speed; // Units per millisecond
} dir = {{1.0f, 0.0f}, {0.0f, -1.0f}, 0.0f, {0.0f, 0.0f, 48.0f}, 400.0f, 192.0f / 1000.0f};
void MovePlayer(uint32_t const timediff)
{
dir.p.x() = std::cos((dir.theta / 180.0f) * M_PI_F);
dir.p.y() = std::sin((dir.theta / 180.0f) * M_PI_F);
dir.n.x() = dir.p.y();
dir.n.y() = -dir.p.x();
// Travel along normal
dir.pos.topRows<2>() += dir.speed * timediff * move.xdiff * dir.n;
// Travel along direction vector
dir.pos.topRows<2>() += dir.speed * timediff * move.ydiff * dir.p;
// Move height of camera
dir.pos(2) += move.hdiff * dir.speed * timediff;
// Adjust focal length of camera
dir.cpos += move.cdiff * dir.speed * timediff;
}
int ProcessEvent(uint32_t windowID)
{
char e = 0;
static SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_WINDOWEVENT:
if (event.window.windowID == windowID)
{
switch (event.window.event)
{
/*case SDL_WINDOWEVENT_SIZE_CHANGED: {
width = event.window.data1;
height = event.window.data2;
break;
}*/
case SDL_WINDOWEVENT_CLOSE:
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Window closed\n");
event.type = SDL_QUIT;
SDL_PushEvent(&event);
break;
default:
break;
}
}
break;
case SDL_QUIT:
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "SDL_QUIT!\n");
return 0; // 0 -- signal terminate program
case SDL_KEYUP:
case SDL_KEYDOWN:
if (event.key.repeat != 0)
{
break;
}
// Because I'm lazy: e is positive or negative depending
// on whether it's a keyup or keydown
e = SDL_KEYDOWN == event.type ? 1 : -1;
switch (event.key.keysym.sym)
{
case SDLK_w: // forward
move.ydiff += e;
break;
case SDLK_s: // backward
move.ydiff -= e;
break;
case SDLK_a: // left
move.xdiff -= e;
break;
case SDLK_d: // right
move.xdiff += e;
break;
case SDLK_t: // height up
move.hdiff += e;
break;
case SDLK_g: // height down
move.hdiff -= e;
break;
case SDLK_y: // viewer-to-camera distance decrease (zoom out)
move.cdiff -= e; // slower
break;
case SDLK_h: // viewer-to-camera distance increase (zoom in)
move.cdiff += e; // slower
break;
default:
break;
}
break;
case SDL_MOUSEMOTION:
if (SDL_GetRelativeMouseMode() == SDL_TRUE)
{
// dir.theta += event.motion.xrel;
// xrel: west negative, east positive
// yrel: north negative, south positive
// Use negative to map positive to west and negative to east (right
// handed system)
float const worldPixelAngle =
180.0f * (std::atan2(float(event.motion.xrel) * mouseSensitivity, dir.cpos) / M_PI_F);
dir.theta -= worldPixelAngle;
// Ugly, but it works
dir.theta /= 360.0f;
dir.theta = dir.theta - floor(dir.theta);
dir.theta *= 360.0f;
}
break;
case SDL_MOUSEBUTTONUP:
SDL_SetRelativeMouseMode(SDL_GetRelativeMouseMode() ? SDL_FALSE : SDL_TRUE);
break;
}
}
return 1; // 1 - signal something processed
}
// Tex must be 64x64.
// 0 <= x1 < x <= surface->w.
// 0 <= y < surface->h.
void DrawSpan(SDL_Surface* surface, SDL_Surface* tex, int x1, int x2, int y)
{
uint32_t* dst = ((uint32_t*)surface->pixels) + (surface->w) * y + x1;
int count = x2 - x1;
y = y - surface->h / 2; // South of screen is positive
x1 = x1 - surface->w / 2; // West of screen is negative
x2 = x2 - surface->w / 2; // East of screen is positive
//(tx, ty) = (h/y)(c*d + x*n) + pos
// float ty = (dir.hpos / y) * (dir.cpos * dir.p.y() + x1 * dir.n.y()) + dir.pos.y();
// float tx = (dir.hpos / y) * (dir.cpos * dir.p.x() + x1 * dir.n.x()) + dir.pos.x();
Eigen::Vector2f t = (dir.pos(2) / y) * (dir.cpos * dir.p + x1 * dir.n) + dir.pos.topRows<2>();
//(dx, dy) = (h/y)(n)
// float xfrac = (dir.hpos / y) * (dir.n.x());
// float yfrac = (dir.hpos / y) * (dir.n.y());
Eigen::Vector2f frac = dir.pos(2) * dir.n / y;
// printf("(xfrac, yfrac) = (%lf,%lf)\n", xfrac, yfrac);
// Assumes identical 32 bit pixel format
// Uint16 pitch = surface->pitch;
while (count >= 0)
{
/*remainder by 64 division
(this is roughly what DOOM did
Only they used the actual decimal 63 instead of 3F.
Description:
As we know, a division by 64 is a shift right by 6 bits.
And a multiplication by 64 is a shift left by 6 bits.
So, the remainder of a division, which would be expressed by
x = x - 64*(x/64)
can be converted into
x = x - (x >> 6 << 6)
Now, since we know that the lower 6 bits have no choice but to
be 0, we can optimize this by doing the following:
x = x - (x & 0xFFFFFFC0)
which will subtract x by itself without it's lower 6 bits.
But wait, we can optimize this further. When we're subtracting
x by itself with it's lower 6 bits set to 0, all we're getting
is...the lower 6 bits. So:
x = x & 0x3F
now we have a remainder division, valid at least for
positive numbers.
But, that's not too useful to us, is it? I mean, we have negative
numbers in our texture coordinates. Take (-1, 0), for instance.
What do we do? We'd like our result to be (63, 0) on the texture,
because the texture is aligned to the XY axis and starting at
(0,0). Therefore we count from the right for the texture instead
of the left, for negative numbers. We, in other words, add 64
to the negative numbers.
Fortunately, when casting to unsigned integers, it wraps from the highest
number, giving us the desired behaviour. So, first cast to int32_t to
remove fractional part (floor) while preserving sign, then cast to uint32_t
to get 2's complement semantics on all architectures, leading to the desired
behaviour. Note: emscripten complains if a direct conversion to uint32_t is
attempted and it's negative.
Now, if we only take the first 6 bits, then
this decrease is still present, AND we're counting backwards
from 64! Exactly what we want!
Example:
-5 (int) = 0xFFFFFFFB (uint32_t)
0xFFFFFFFB & 0x0000003F = 0x00000003B = 59
This operation therefore accepts any XY coordinate
and determines where in the texture it lies.
Only caveat: Texture is rendered upside down. Could load them
flipped to solve this, or subtract by 64 for positive case and
not for the negative case.
*/
uint32_t uintty = uint32_t(int32_t(t.y())) & 0x3F;
uint32_t uinttx = uint32_t(int32_t(t.x())) & 0x3F;
uint32_t* src = ((uint32_t*)tex->pixels) + uintty * 64 + uinttx;
*dst = *src;
// Increment
dst++;
t += frac;
// tx += xfrac;
// ty += yfrac;
--count;
}
}
void DrawColumn(SDL_Surface* const display, SDL_Surface const* const texTranspose, size_t const dx, size_t const dy1,
size_t const dy2, size_t const tx, size_t const ty1, size_t const ty2)
{
uint32_t* displayPixels = (uint32_t*)display->pixels;
uint32_t* texTPixels = (uint32_t*)texTranspose->pixels;
float const yFrac = float(ty2 - ty1) / float(dy2 - dy1);
float ti = (float)ty1;
for (size_t di = dy1; di < dy2; ++di)
{
*(displayPixels + (display->w) * di + dx) = *(texTPixels + (texTranspose->w) * tx + (size_t)ti);
ti += yFrac;
}
}
void RenderSprite(SDL_Surface* display, SDL_Surface* sprite, float const x, float const z, int const h) noexcept
{
auto const halfWidth = sprite->w / 2;
auto const sCentre = x;
auto const sMin = sCentre - halfWidth;
auto const sMax = sCentre + halfWidth;
auto const d = z;
auto const c = dir.cpos;
// centre_x = s/d * c
// auto const xCentre = (s / d) * c;
auto const hTop = float(sprite->h + h);
auto const hBot = float(h);
int const yTop = static_cast<int>((hTop * c) / d);
int const yBot = static_cast<int>((hBot * c) / d);
int const xMin = static_cast<int>((sMin * c) / d);
int const xMax = static_cast<int>((sMax * c) / d);
int const screenTop = display->h / 2 - yTop;
// int const screenBot = display->h / 2 - yBot;
int const screenXMin = display->w / 2 + xMin;
// int const screenXMax = display->w / 2 + xMax;
// TODO: replace this with custom blitting routine
SDL_Rect finalRect;
finalRect.h = yTop - yBot;
finalRect.w = xMax - xMin;
finalRect.x = screenXMin;
finalRect.y = screenTop;
SDL_BlitScaled(sprite, nullptr, display, &finalRect);
}
void Draw(GameResources* res, uint32_t const frameTime[2])
{
SDL_Surface* display = res->display;
// Render sky
// TODO: sky texture is being rendered flipped possibly. Need to investigate.
for (size_t i = 0; i < (size_t)display->w; ++i)
{
// Treat sky as being projected in a cylinder, with deformation happening
// around the edges of the screen.
// The sky is wrapped around the cylinder 4 times (every 90 degrees the
// image repeats)
// Recall: left side of screen corresponds to +ve
float worldPixelAngle = dir.theta + 180.0f * (std::atan2((display->w / 2.0f) - i, dir.cpos) / M_PI_F);
if (worldPixelAngle < 0.0f)
{
worldPixelAngle += 360.0f;
}
else if (worldPixelAngle > 360.0f)
{ // UGLY, but it works
worldPixelAngle /= 360.0f;
worldPixelAngle -= std::floor(worldPixelAngle);
worldPixelAngle *= 360.0f;
}
// float const mirroredCoord = worldPixelAngle > 180.0f ? (1.0f -
// (worldPixelAngle - 180.0f) / 180.0f) : (worldPixelAngle / 180.0f);
float const finalCoord = (worldPixelAngle / 90.0f) - std::floor(worldPixelAngle / 90.0f);
DrawColumn(display, res->skyTranspose, i, 0, static_cast<size_t>(display->h / 2 + 1),
static_cast<size_t>(finalCoord * float(res->skyTranspose->h)), 0,
static_cast<size_t>(res->skyTranspose->w));
}
// Render floor
for (int i = display->h / 2 + 1; i < display->h; ++i)
{
DrawSpan(display, res->floor, 0, render.xres - 1, i);
}
// Render FPS
static char frameTimeString[128];
if (frameTime[0] != 0 || frameTime[1] != 0)
{
static SDL_Color const colour = {255, 255, 0, 255};
SDL_snprintf(frameTimeString, sizeof(frameTimeString), "Previous frame time: %ums",
frameTime[1] - frameTime[0]);
SDL_Surface* fpsGauge = TTF_RenderText_Blended(res->font, frameTimeString, colour);
SDL_BlitSurface(fpsGauge, nullptr, display, nullptr);
SDL_FreeSurface(fpsGauge);
}
// Render sprites
Eigen::Matrix4f viewInverse = Eigen::Matrix4f::Identity();
viewInverse.topLeftCorner<1, 2>() = dir.n.transpose();
viewInverse.block<1, 2>(1, 0) = dir.p.transpose();
viewInverse.topRightCorner<3, 1>() = viewInverse.topLeftCorner<3, 3>() * (-dir.pos);
// Sort them
// std::vector<std::pair<SDL_Surface*, Eigen::Vector2f>> sprites; // get from entities
std::vector<std::pair<Entity const*, Eigen::Vector4f>,
Eigen::aligned_allocator<std::pair<Entity*, Eigen::Vector4f>>>
sorted;
for (auto const& entity : res->entities)
{
Eigen::Vector4f const viewSpritePos = viewInverse * entity.pos.homogeneous();
sorted.emplace_back(&entity, viewSpritePos);
//
}
// Sort back to front
std::sort(sorted.begin(), sorted.end(),
[](std::pair<Entity const*, Eigen::Vector4f> const& x, std::pair<Entity const*, Eigen::Vector4f>& y) {
return x.second(2) > y.second(2);
});
for (auto const& sprite : sorted)
{
RenderSprite(display, sprite.first->texture, sprite.second(0), sprite.second(1), sprite.second(2));
}
// Doomguy
// Assume doomguy is facing [-1 0]
Eigen::Vector2f doomGuyOrient{-1, 0};
Eigen::Vector2f playerToDoomGuy = Eigen::Vector2f{550, 50} - dir.pos.topRows<2>();
auto const dot = playerToDoomGuy.dot(doomGuyOrient);
auto const det = playerToDoomGuy(0) * doomGuyOrient(1) - playerToDoomGuy(1) * doomGuyOrient(0);
auto angle = std::atan2(det, dot) - (M_PI_F / 8);
/*if (angle < 0)
{
angle += 2 * M_PI_F;
}*/
int spriteSelect = static_cast<int>(4 + std::floor((angle) / (M_PI_F / 4)));
if (spriteSelect >= 8)
{
spriteSelect = 0;
}
if (spriteSelect < 0)
{
spriteSelect = 7;
}
// TODO: figure out the actual transformation
Eigen::Vector4f const doomSpriteViewPos = viewInverse * Eigen::Vector3f{550, 50, 0}.homogeneous();
RenderSprite(display, res->doomGuy[7 - spriteSelect], doomSpriteViewPos(0), doomSpriteViewPos(1),
doomSpriteViewPos(2));
}
void GameLoop(void* const arg)
{
GameResources* const res = static_cast<GameResources* const>(arg);
static uint32_t prevFrameTime[2] = {};
prevFrameTime[0] = prevFrameTime[1];
prevFrameTime[1] = SDL_GetTicks();
if (ProcessEvent(res->windowID) == 0)
{
// FreeGameResources(res); //browser specific: it handles cleanup
emscripten_cancel_main_loop();
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Exiting!\n");
SDL_Quit();
std::exit(0); // Reason for not plain returning: emscripten won't call
// global destructors when main or the main loop exits.
}
MovePlayer(prevFrameTime[1] - prevFrameTime[0]);
Draw(res, prevFrameTime);
SDL_UpdateTexture(res->texture, nullptr, res->display->pixels, res->display->pitch);
// Now draw this directly to the window
SDL_RenderClear(res->renderer);
SDL_RenderCopy(res->renderer, res->texture, nullptr, nullptr);
// And ask the windowing system to redraw the window
SDL_RenderPresent(res->renderer);
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Prev frame time: %u, current
// present: %u", prevFrameTime[1] - prevFrameTime[0], SDL_GetTicks() -
// prevFrameTime[1]);
// MovePlayer(SDL_GetTicks() - prevFrameTime[1]);
// MovePlayer(prevFrameTime[1] - prevFrameTime[0]);
// MovePlayer(16);
}
SDL_Surface* LoadTexture(char const* filename, SDL_PixelFormat const* const dstformat)
{
SDL_Surface* tmp = SDL_LoadBMP(filename);
if (tmp == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load %s!\n", filename);
throw std::runtime_error("Couldn't load texture");
}
SDL_Surface* result = SDL_ConvertSurface(tmp, dstformat, 0);
if (result == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not convert surface for %s!\n", filename);
throw std::runtime_error("Couldn't load texture");
}
SDL_FreeSurface(tmp);
return result;
}
int main(int argc, char* argv[])
{
// VERY IMPORTANT: Ensure SDL2 is initialized
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS) < 0)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "could not initialize sdl2: %s\n", SDL_GetError());
return 1;
}
// VERY IMPORTANT: if using text in your program, ensure SDL2_ttf library is
// initialized
if (TTF_Init() < 0)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "could not initialize SDL2_ttf: %s\n", TTF_GetError());
}
GameResources res;
// This creates the actual window in which graphics are displayed
res.window = SDL_CreateWindow("Floor - SDL2 version", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, render.xres,
render.yres, SDL_WINDOW_SHOWN);
if (res.window == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create window!\n");
return 1;
}
res.windowID = SDL_GetWindowID(res.window);
res.renderer = SDL_CreateRenderer(res.window, -1,
0); // don't force hardware or software
// accel -- let SDL2 choose what's best
if (res.renderer == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "could not create renderer: %s\n", SDL_GetError());
return 1;
}
res.display = SDL_CreateRGBSurfaceWithFormat(0, render.xres, render.yres, 32, SDL_PIXELFORMAT_RGBA8888);
if (res.display == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "could not create surface: %s\n", SDL_GetError());
return 1;
}
res.texture = SDL_CreateTexture(res.renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, render.xres,
render.yres);
if (res.texture == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "could not create texture: %s\n", SDL_GetError());
return 1;
}
// Just in case you need text:
// load iosevka-regular.ttf at a large size into font
res.font = TTF_OpenFont("../assets/iosevka-regular.ttf", 16);
if (res.font == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "TTF_OpenFont: %s\n", TTF_GetError());
return 1;
}
res.floor = LoadTexture("../assets/floor.bmp", res.display->format);
res.skyTranspose = LoadTexture("../assets/sky.bmp", res.display->format);
res.wallTranspose = LoadTexture("../assets/wall.bmp", res.display->format);
res.healthSprite = LoadTexture("../assets/health.bmp", res.display->format);
res.vialSprite = LoadTexture("../assets/vial.bmp", res.display->format);
res.columnSprite = LoadTexture("../assets/column.bmp", res.display->format);
char pathBuf[128];
for (size_t i = 0; i < 8; i++)
{
snprintf(pathBuf, 128, "../assets/playa%zu.bmp", i + 1);
res.doomGuy[i] = LoadTexture(pathBuf, res.display->format);
}
if (SDL_SetSurfaceBlendMode(res.display, SDL_BLENDMODE_BLEND) < 0)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_SetSurfaceBlendMode: %s\n", SDL_GetError());
return 1;
}
// Objects
res.entities.emplace_back("column1", res.columnSprite, Eigen::Vector3f{500, 0, 0});
res.entities.emplace_back("vial", res.vialSprite, Eigen::Vector3f{500, 100, 100});
res.entities.emplace_back("health", res.healthSprite, Eigen::Vector3f{500, 200, 0});
res.entities.emplace_back("column2", res.columnSprite, Eigen::Vector3f{500, 300, 0});
emscripten_set_main_loop_arg(GameLoop, &res, 0, 1);
return 0;
}