forked from sysprog21/raycaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
133 lines (122 loc) · 4.53 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
#include <SDL.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include "game.h"
#include "raycaster.h"
#include "raycaster_fixed.h"
#include "raycaster_float.h"
#include "renderer.h"
using namespace std;
static void DrawBuffer(SDL_Renderer *sdlRenderer,
SDL_Texture *sdlTexture,
uint32_t *fb,
int dx)
{
int pitch = 0;
void *pixelsPtr;
if (SDL_LockTexture(sdlTexture, NULL, &pixelsPtr, &pitch)) {
throw runtime_error("Unable to lock texture");
}
memcpy(pixelsPtr, fb, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(uint32_t));
SDL_UnlockTexture(sdlTexture);
SDL_Rect r;
r.x = dx * SCREEN_SCALE;
r.y = 0;
r.w = SCREEN_WIDTH * SCREEN_SCALE;
r.h = SCREEN_HEIGHT * SCREEN_SCALE;
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &r);
}
static bool ProcessEvent(const SDL_Event &event,
int *moveDirection,
int *rotateDirection)
{
if (event.type == SDL_QUIT) {
return true;
} else if ((event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) &&
event.key.repeat == 0) {
auto k = event.key;
auto p = event.type == SDL_KEYDOWN;
switch (k.keysym.sym) {
case SDLK_ESCAPE:
return true;
break;
case SDLK_UP:
*moveDirection = p ? 1 : 0;
break;
case SDLK_DOWN:
*moveDirection = p ? -1 : 0;
break;
case SDLK_LEFT:
*rotateDirection = p ? -1 : 0;
break;
case SDLK_RIGHT:
*rotateDirection = p ? 1 : 0;
break;
default:
break;
}
}
return false;
}
int main(int argc, char *args[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
} else {
SDL_Window *sdlWindow =
SDL_CreateWindow("RayCaster [fixed-point vs. floating-point]",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_SCALE * (SCREEN_WIDTH * 2 + 1),
SCREEN_SCALE * SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (sdlWindow == NULL) {
printf("Window could not be created! SDL_Error: %s\n",
SDL_GetError());
} else {
Game game;
RayCasterFloat floatCaster;
Renderer floatRenderer(&floatCaster);
uint32_t floatBuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
RayCasterFixed fixedCaster;
Renderer fixedRenderer(&fixedCaster);
uint32_t fixedBuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
int moveDirection = 0;
int rotateDirection = 0;
bool isExiting = false;
const static auto tickFrequency = SDL_GetPerformanceFrequency();
auto tickCounter = SDL_GetPerformanceCounter();
SDL_Event event;
SDL_Renderer *sdlRenderer =
SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture *fixedTexture = SDL_CreateTexture(
sdlRenderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_Texture *floatTexture = SDL_CreateTexture(
sdlRenderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
while (!isExiting) {
floatRenderer.TraceFrame(&game, floatBuffer);
fixedRenderer.TraceFrame(&game, fixedBuffer);
DrawBuffer(sdlRenderer, fixedTexture, fixedBuffer, 0);
DrawBuffer(sdlRenderer, floatTexture, floatBuffer,
SCREEN_WIDTH + 1);
SDL_RenderPresent(sdlRenderer);
if (SDL_PollEvent(&event)) {
isExiting =
ProcessEvent(event, &moveDirection, &rotateDirection);
}
const auto nextCounter = SDL_GetPerformanceCounter();
const auto seconds = (nextCounter - tickCounter) /
static_cast<float>(tickFrequency);
tickCounter = nextCounter;
game.Move(moveDirection, rotateDirection, seconds);
}
SDL_DestroyTexture(floatTexture);
SDL_DestroyTexture(fixedTexture);
SDL_DestroyRenderer(sdlRenderer);
SDL_DestroyWindow(sdlWindow);
}
}
SDL_Quit();
return 0;
}