-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpenGW.cpp
More file actions
310 lines (239 loc) · 7.93 KB
/
OpenGW.cpp
File metadata and controls
310 lines (239 loc) · 7.93 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
#include "controls.hpp"
#include "game.hpp"
#include "scene.hpp"
#include "settings.hpp"
#include "blur.hpp"
#include "sincos.hpp"
#include <SDL3/SDL_opengl.h>
#ifdef __APPLE__
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#endif
#include <SDL3/SDL.h>
#include <cstdio>
#include <memory>
// declare image buffers
#ifdef __amigaos4__
// MiniGL / Warp3D requires power of two size
static constexpr int blurBufferWidth = 256;
static constexpr int blurBufferHeight = 256;
#else
static constexpr int blurBufferWidth = 500;
static constexpr int blurBufferHeight = 250;
#endif
static std::vector<ColorRGB> blurBuffer(blurBufferWidth * blurBufferHeight);
static SDL_Window* window;
static SDL_GLContext context;
static void OGLCreate();
static void OGLDestroy();
static void OGLSize(int cx, int cy);
std::unique_ptr<scene> oglScene;
static bool oglInited = false;
// our OpenGL texture handles
static unsigned int texOffscreen;
static void createOffscreens();
static void drawOffscreens();
static void run();
#define CONTEXT_PRIMARY 0
#define CONTEXT_GLOW 1
static int mWidth, mHeight;
static Uint32 lastTime;
static Uint32 fpsTime;
static int frameCount;
static int fps;
const bool* keyboardState;
static bool handleEvents()
{
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_EVENT_QUIT:
printf("Quit\n");
return false;
case SDL_EVENT_WINDOW_RESIZED:
// printf("%d %d\n", e.window.data1, e.window.data2);
OGLSize(e.window.data1, e.window.data2);
break;
case SDL_EVENT_GAMEPAD_ADDED:
theGame->mControls->handleGamepadAdded(e.cdevice.which);
break;
case SDL_EVENT_GAMEPAD_REMOVED:
theGame->mControls->handleGamepadRemoved(e.cdevice.which);
break;
}
}
keyboardState = SDL_GetKeyboardState(nullptr);
return true;
}
int main(int /*argc*/, char** /*argv*/)
{
printf("SDL_Init\n");
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD)) {
printf("SDL_Init failed: %s\n", SDL_GetError());
return 0;
}
Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
if (0) {
flags |= SDL_WINDOW_FULLSCREEN;
}
window = SDL_CreateWindow("OpenGL SDL3",
settings::get().displayWidth, settings::get().displayHeight, flags);
if (window) {
srand(SDL_GetTicks());
make_sin_cos_tables();
oglScene = std::make_unique<scene>();
OGLCreate();
run();
OGLDestroy();
oglScene.reset();
SDL_DestroyWindow(window);
} else {
printf("Failed to create window: %s\n", SDL_GetError());
}
printf("SDL_Quit\n");
SDL_Quit();
return 0;
}
static void OGLCreate()
{
context = SDL_GL_CreateContext(window);
if (context == nullptr) {
printf("SDL_GL_CreateContext failed: %s\n", SDL_GetError());
}
OGLSize(settings::get().displayWidth, settings::get().displayHeight);
// Do stuff with the context here if needed...
createOffscreens();
if (!SDL_GL_SetSwapInterval(0)) {
printf("SDL_GL_SetSwapInterval failed: %s\n", SDL_GetError());
}
if (!SDL_GL_MakeCurrent(window, context)) {
printf("SDL_GL_MakeCurrent failed: %s\n", SDL_GetError());
}
oglInited = true;
}
static void OGLDestroy()
{
oglInited = false;
SDL_GL_MakeCurrent(nullptr, nullptr);
SDL_GL_DestroyContext(context);
}
static void OGLSize(int cx, int cy)
{
oglScene->size(cx, cy);
mWidth = cx;
mHeight = cy;
}
static void createOffscreens()
{
std::vector<char> colorBits(blurBufferWidth * blurBufferHeight * 3);
// texture creation..
glGenTextures(1, &texOffscreen);
glBindTexture(GL_TEXTURE_2D, texOffscreen);
glTexImage2D(GL_TEXTURE_2D, 0, 3, blurBufferWidth, blurBufferHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, colorBits.data());
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, blurBufferWidth, blurBufferHeight, GL_RGB, GL_UNSIGNED_BYTE, colorBits.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
}
static void drawOffscreens()
{
int viewport[4];
glGetIntegerv(GL_VIEWPORT, (int*)viewport);
if (settings::get().mEnableGlow) {
// Draw to the blur texture
{
glViewport(0, 0, blurBufferWidth, blurBufferHeight);
oglScene->draw(scene::RENDERPASS_BLUR);
// Transfer image to the blur texture
glBindTexture(GL_TEXTURE_2D, texOffscreen);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, blurBufferWidth, blurBufferHeight, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
// Draw the scene normally
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
oglScene->draw(scene::RENDERPASS_PRIMARY);
if (settings::get().mEnableGlow) {
////////////////////////////////////////////////
// Do blur
// Bind the blur texture and copy the screen bits to it
glBindTexture(GL_TEXTURE_2D, texOffscreen);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, blurBuffer.data());
const int blurRadius = (game::mGameMode == game::GAMEMODE_ATTRACT || game::mGameMode == game::GAMEMODE_CREDITED) ? 8 : 4;
superFastBlur(blurBuffer.data(), blurBufferWidth, blurBufferHeight, blurRadius);
superFastBlur(blurBuffer.data(), blurBufferWidth, blurBufferHeight, blurRadius);
// Bind the blur result back to our texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, blurBufferWidth, blurBufferHeight, GL_RGB, GL_UNSIGNED_BYTE, blurBuffer.data());
////////////////////////////////////////////////
// Draw the blur texture on top of the existing scene
// Glowy blending effect
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
if (game::mGameMode == game::GAMEMODE_ATTRACT || game::mGameMode == game::GAMEMODE_CREDITED)
glColor4f(1, 1, 1, 1);
else
glColor4f(1, 1, 1, 1);
// Draw it on the screen
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0);
glVertex2d(-1.0, -1.0);
glTexCoord2d(1.0, 0.0);
glVertex2d(1.0, -1.0);
glTexCoord2d(1.0, 1.0);
glVertex2d(1.0, 1.0);
glTexCoord2d(0.0, 1.0);
glVertex2d(-1.0, 1.0);
if (game::mGameMode == game::GAMEMODE_ATTRACT || game::mGameMode == game::GAMEMODE_CREDITED) {
glTexCoord2d(0.0, 0.0);
glVertex2d(-1.0, -1.0);
glTexCoord2d(1.0, 0.0);
glVertex2d(1.0, -1.0);
glTexCoord2d(1.0, 1.0);
glVertex2d(1.0, 1.0);
glTexCoord2d(0.0, 1.0);
glVertex2d(-1.0, 1.0);
}
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
}
static void updateFps(Uint32 now)
{
++frameCount;
lastTime = now;
if ((now - fpsTime) > 1000) {
fpsTime = now;
fps = (fps + frameCount) / 2;
frameCount = 0;
char buf[32];
snprintf(buf, sizeof(buf), "OpenGW SDL3 - FPS %d", fps);
SDL_SetWindowTitle(window, buf);
}
}
static void run()
{
if (!oglInited)
return;
constexpr Uint32 logicRate = 60;
constexpr Uint32 logicPeriod = 1000 / logicRate;
Uint32 lastLogicUpdate = SDL_GetTicks();
bool running = true;
while (running) {
const Uint32 now = SDL_GetTicks();
while ((now - lastLogicUpdate) > logicPeriod) {
lastLogicUpdate += logicPeriod;
if (!handleEvents()) {
running = false;
}
oglScene->run();
}
drawOffscreens();
SDL_GL_SwapWindow(window);
updateFps(now);
}
}