-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
154 lines (136 loc) · 3.87 KB
/
Copy pathmain.cpp
File metadata and controls
154 lines (136 loc) · 3.87 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
#include <SDL2/SDL.h>
#undef main
#include <Scene/AntiAliasing.h>
#include <Scene/ColorShowdown.h>
#include <Scene/ExampleScene1.h>
#include <Scene/ExampleScene2.h>
#include <Scene/Raytracing.h>
#include <Scene/Raytracing2.h>
#include <Scene/Reflection.h>
#include <Scene/Scene.h>
#include <Scene/Texture.h>
#include <thread>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define WIDTH 400
#define HEIGHT 400
#define LATENCY_MIN 16.6f// 60 FPS
#define COLOR_SHOWDOWN 1
#define RAY_TRACING 2
#define RAY_TRACING2 3
#define REFLECTION 4
#define TEXTURE 5
#define ANTI_ALIASING 6
#define SCENE1 7
#define SCENE2 8
#define ACTIVE 4
#if ACTIVE == 6
#undef WIDTH
#undef HEIGHT
#define WIDTH 800
#define HEIGHT 800
#endif
class Screen {
private:
int running;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
Scene *scene;
public:
Screen() : running(0) {
int textureWidth;
int textureHeight;
int channels;
unsigned char *img;
switch (ACTIVE) {
case COLOR_SHOWDOWN:
scene = new ColorShowdown();
return;
case RAY_TRACING:
scene = new Raytracing(WIDTH, HEIGHT);
return;
case RAY_TRACING2:
scene = new Raytracing2(WIDTH, HEIGHT);
return;
case REFLECTION:
scene = new Reflection(WIDTH, HEIGHT);
return;
case TEXTURE:
img = stbi_load("../lava.bmp", &textureHeight, &textureWidth, &channels, 0);
scene = new Texture(WIDTH, HEIGHT, img, textureWidth, textureHeight,channels);
return;
case ANTI_ALIASING:
scene = new AntiAliasing(WIDTH, HEIGHT);
return;
case SCENE1:
scene = new ExampleScene1(WIDTH, HEIGHT);
return;
case SCENE2:
scene = new ExampleScene2(WIDTH, HEIGHT);
return;
}
}
~Screen() { this->stop(); }
void start() {
if (SDL_Init(SDL_INIT_EVERYTHING)) {
return;
}
if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, SDL_WINDOW_SHOWN, &window,
&renderer)) {
return;
}
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
//std::thread drawThread(&Screen::drawLoop, this);
std::thread tickThread(&Screen::tickLoop, this);
SDL_Event event;
this->running = 1;
drawLoop();
tickThread.detach();
}
void tickLoop() {
while (running) {
tick();
// Reduce CPU usage
SDL_Delay(10);
}
}
void drawLoop() {
auto lastTicks = SDL_GetTicks();
while (running) {
draw();
auto newTicks = SDL_GetTicks();
double latency = (double) newTicks - (double) lastTicks;
if (latency < LATENCY_MIN) {
// Reduce GPU usage
SDL_Delay(LATENCY_MIN - latency);
}
lastTicks = SDL_GetTicks();
}
}
void stop() {
if (nullptr != renderer) {
SDL_DestroyRenderer(renderer);
renderer = nullptr;
}
if (nullptr != window) {
SDL_DestroyWindow(window);
window = nullptr;
}
SDL_Quit();
}
void tick() { scene->GetPixels(); }
void draw() {
if (scene->pixels == nullptr) return;
SDL_RenderClear(renderer);
SDL_UpdateTexture(texture, nullptr, scene->pixels, WIDTH * sizeof(int));
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
};
int main() {
Screen screen;
screen.start();
return 0;
}