This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.d
533 lines (422 loc) · 16.4 KB
/
game.d
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
module game;
import state.all;
import gl.all;
import sdl.all;
import math.all;
import res.shaderpool;
import math.random;
import bitmapfont;
import postprocessing;
import misc.logger;
import res.textures;
import gamecontext;
import sound;
import res.settings;
import state.eye;
import res.bgcolor;
final class Game
{
private
{
// states
State m_currentState, m_lastState;
Splash1State m_splash1state;
MenuState m_mainmenu, m_playmenu, m_settingsMenu, m_graphicsMenu, m_soundMenu;
GameState m_gameState;
GameContext m_context;
// final rendering viewport
box2i m_viewport;
BitmapFont m_font;
ShaderPool m_shaderPool;
bool m_stateChanged;
double m_lastTimeStateChanged, m_lastTimeStateChangedLast;
PostProcessing m_postProcessing;
Texture2D m_mainTexture;
//RenderBuffer m_mainDepthBuffer;
Texture2D m_mainDepthBuffer;
FBO m_mainFBO, m_defaultFBO;
int m_windowWidth, m_windowHeight;
bool m_mouseDown = false;
Textures m_textures;
SoundManager m_soundManager;
double m_stateTransition = 1.0;
void setState(State state)
{
// if in a transition, deny
if (m_stateTransition < 0.99) return;
m_lastState = m_currentState;
m_currentState = state;
if ((m_lastState !is m_currentState) && (m_lastState !is null))
{
m_stateTransition = 0.f;
}
m_stateChanged = true;
}
bool m_terminated = false;
void terminate()
{
m_terminated = true;
}
vec2i m_size;
void createMenus()
{
const float TITLE_SIZE = 1.1f;
const float NORMAL_ITEM_SIZE = 1.0f;
const float SMALL_ITEM_SIZE = 0.7f;
assert(m_context !is null);
MenuItem[] playmenuItems =
[ new MenuItem("Start", new StartAGameAction(), NORMAL_ITEM_SIZE),
new MenuItem("Players: ", new ChangePlayersSettingsAction(), NORMAL_ITEM_SIZE),
// new MenuItem("IA: ", new ChangeIASettingsAction(), NORMAL_ITEM_SIZE),
new MenuItem("Back", null, NORMAL_ITEM_SIZE) ];
m_playmenu = new MenuState("GAME", m_context, playmenuItems);
MenuItem[] graphicsMenuItems =
[ new MenuItem("", new ChangeResolutionSettingsAction(), SMALL_ITEM_SIZE),
new MenuItem("", new ChangeFullscreenSettingsAction(), SMALL_ITEM_SIZE),
new MenuItem("HDR: ", new ChangeHDRSettingsAction(), SMALL_ITEM_SIZE),
new MenuItem("Post-FX: ", new ChangePostProcessingSettingsAction(), SMALL_ITEM_SIZE),
new MenuItem("Gamma: ", new ChangeGammaSettingsAction(), SMALL_ITEM_SIZE),
new MenuItem("Blur: ", new ChangeBlurQualitySettingsAction(), SMALL_ITEM_SIZE),
new MenuItem("", null, NORMAL_ITEM_SIZE) ];
m_graphicsMenu = new MenuState("GRAPHICS", m_context, graphicsMenuItems);
MenuItem[] soundMenuItems =
[ new MenuItem("Vol. FX ", new ChangeFXVolumeSettingsAction(m_soundManager), NORMAL_ITEM_SIZE),
new MenuItem("Vol. Mus. ", new ChangeMusicVolumeSettingsAction(m_soundManager), NORMAL_ITEM_SIZE),
// new MenuItem("Change music", new ChangeMusicAction(m_soundManager), NORMAL_ITEM_SIZE),
new MenuItem("Back", null, NORMAL_ITEM_SIZE) ];
m_soundMenu = new MenuState("SOUND", m_context, soundMenuItems);
MenuItem[] settingsMenuItems =
[ new MenuItem("Graphics", new GotoMenuAction(m_graphicsMenu), NORMAL_ITEM_SIZE),
new MenuItem("Sound", new GotoMenuAction(m_soundMenu), NORMAL_ITEM_SIZE),
//new MenuItem("Controls", null, NORMAL_ITEM_SIZE),
new MenuItem("Back", null, NORMAL_ITEM_SIZE) ];
m_settingsMenu = new MenuState("SETTINGS", m_context, settingsMenuItems);
MenuItem[] m_mainmenuItems =
[ new MenuItem("Play", new GotoMenuAction(m_playmenu), NORMAL_ITEM_SIZE),
new MenuItem("Settings", new GotoMenuAction(m_settingsMenu), NORMAL_ITEM_SIZE),
new MenuItem("Quit", new ExitGameAction(), NORMAL_ITEM_SIZE) ];
m_mainmenu = new MenuState("WORMHOL", m_context, m_mainmenuItems);
m_playmenu.items[$-1].action = new GotoMenuAction(m_mainmenu);
m_settingsMenu.items[$-1].action = new GotoMenuAction(m_mainmenu);
m_graphicsMenu.items[$-1].action = new GotoMenuActionOrRestartApp(m_settingsMenu);
m_soundMenu.items[$-1].action = new GotoMenuAction(m_settingsMenu);
}
// mod = 0 ENTER or click
// mod = 1 LEFT
// mod = 2 RIGHT
void executeMenuAction(MenuAction item, int mod)
{
if (item is null) return;
if (auto a = cast(GotoMenuAction)item)
{
if (mod == 0)
{
m_soundManager.playSound(FX.PLOP);
setState(a.dest);
}
}
else if(auto a = cast(GotoMenuActionOrRestartApp)item)
{
if (mod == 0)
{
m_soundManager.playSound(FX.PLOP);
// restart app if graphics settings have changed
if (graphicsSettingsHaveChanged())
{
mustRestartApp = true;
terminate();
}
else
{
setState(a.dest);
}
}
}
else if (auto a = cast(ExitGameAction)item)
{
if (mod == 0)
{
m_soundManager.playSound(FX.PLOP);
terminate();
}
}
else if (auto a = cast(ChangeValueAction)item)
{
if ((mod == 0) || (mod == 2))
{
m_soundManager.playSound(FX.MENU2);
a.next();
}
if (mod == 1)
{
m_soundManager.playSound(FX.MENU2);
a.previous();
}
} else if (auto a = cast(StartAGameAction)item)
{
if (mod == 0)
{
m_soundManager.playSound(FX.PLOP);
m_gameState.newGame(selectedPlayers, selectedIA);
setState(m_gameState);
}
}
}
void recreateMainTexture(bool hdr)
{
// Texture2D oldMain = m_mainTexture;
info("> create mainTexture");
m_mainTexture = new Texture2D(m_size.x, m_size.y, hdr ? Texture.IFormat.RGBA16F : Texture.IFormat.RGBA8, true, false, false);
m_mainTexture.minFilter = Texture.Filter.LINEAR;
m_mainTexture.magFilter = Texture.Filter.LINEAR;
m_mainTexture.wrapS = Texture.Wrap.CLAMP_TO_EDGE;
m_mainTexture.wrapT = Texture.Wrap.CLAMP_TO_EDGE;
//m_mainTexture.setImage(0, m_size.x, m_size.y, Texture.Format.RGBA, useHDR ? Texture.Type.FLOAT : Texture.Type.UBYTE, null, false);
// m_mainTexture.clear(0, 0.f, 0.f, 0.f, 1.f);
GL.check;
info("< create mainTexture");
info("> create mainDepthBuffer");
// m_mainDepthBuffer = new RenderBuffer(RenderBuffer.Format.DEPTH, m_size.x, m_size.y);
m_mainDepthBuffer = new Texture2D(m_size.x, m_size.y, Texture.IFormat.DEPTH, false, false, false);
info("< create mainDepthBuffer");
info("> create main FBO");
m_mainFBO = new FBO();
m_mainFBO.color[0].setTarget(m_mainTexture, 0);
m_mainFBO.depth.setTarget(m_mainDepthBuffer, 0);
// m_mainFBO.depth.setTarget(m_mainDepthBuffer);
m_mainFBO.setWrite(FBO.Component.COLORS_AND_DEPTH);
m_mainFBO.setDrawBuffers(0);
m_mainFBO.check();
m_mainFBO.use();
GL.clear();
GL.check;
info("< create main FBO");
// if (m_mainTexture !is null) delete m_mainTexture;
// m_mainTexture.generateMipmaps;
}
}
public
{
this(box2i viewport, int windowWidth, int windowHeight)
{
info(format(">Game.this(%s, %s, %s)", viewport, windowWidth, windowHeight));
m_viewport = viewport;
m_windowWidth = windowWidth;
m_windowHeight = windowHeight;
void* getHWND()
{
SDL_SysWMinfo info;
SDL_VERSION(&info.ver);
if (SDL_GetWMInfo(&info))
{
return info.window;
}
else return null;
}
m_soundManager = new SoundManager(getHWND());
m_size = vec2i(viewport.width, viewport.height);
info(format("viewport = %s", viewport));
// load all textures
info(">load Textures");
m_textures = new Textures();
info("<load Textures");
// load all shaders
m_shaderPool = new ShaderPool();
recreateMainTexture(useHDR);
m_defaultFBO = new FBO(true);
info("<create main FBO");
GL.check();
info(">create postProcessing");
m_postProcessing = new PostProcessing(m_defaultFBO, m_shaderPool, m_mainTexture, viewport);
info("<create postProcessing");
// create states
info(">create font");
m_font = new BitmapFont(m_shaderPool, "data/gfx/font.png", "data/gfx/fill.png");
info("<create font");
m_context = new GameContext(m_postProcessing, m_size, m_shaderPool, m_textures, m_font, m_viewport, m_soundManager);
m_context.mousePos = vec2f(-2, 2);
m_context.mouseVel = vec2f(0, 0);
m_splash1state = new Splash1State(m_context);
m_gameState = new GameState(m_context);
createMenus();
setState(m_splash1state);
info("*play music");
info("<Game.this()");
}
~this()
{
info(">Game.~this()");
info("<Game.~this()");
}
bool move(double timeElapsed, double dt)
{
m_soundManager.nextMusicIfNeeded();
if (m_stateChanged)
{
m_lastTimeStateChangedLast = m_lastTimeStateChanged;
m_lastTimeStateChanged = timeElapsed;
m_stateChanged = false;
}
m_stateTransition = mind(1.0, m_stateTransition + dt * 3.0);
if (m_lastState !is null)
{
m_lastState.move(timeElapsed - m_lastTimeStateChangedLast, dt, 1.f - m_stateTransition);
}
m_currentState.move(timeElapsed - m_lastTimeStateChanged, dt, m_stateTransition);
m_context.mouseVel = m_context.mouseVel * exp3(-dt * 4);
return m_terminated;
}
void draw(double timeElapsed)
{
/+
if (useHDRchanged)
{
useHDRchanged = false;
recreateMainTexture(useHDR);
if (m_postProcessing !is null) m_postProcessing.HDRchanged();
}
+/
if (usePostProcessing)
{
m_mainFBO.use();
}
else
{
m_defaultFBO.use;
}
GL.clearColor = getBackGroundColor();
GL.clear(true, true, true);
GL.disable(GL.DEPTH_TEST, GL.CULL_FACE);
GL.enable(GL.ALPHA_TEST);
GL.alphaFunc(GL.GREATER, 0.0038f);
if (m_lastState !is null)
{
m_lastState.draw(timeElapsed - m_lastTimeStateChangedLast, 1.f - m_stateTransition);
}
m_currentState.draw(timeElapsed - m_lastTimeStateChanged, m_stateTransition);
GL.viewport = box2i(0, 0, m_size.x, m_size.y);
if (usePostProcessing)
{
m_postProcessing.render();
}
}
void keyUp(int key, int mod, wchar ch)
{
}
void keyDown(int key, int mod, wchar ch)
{
debug
{
if ((ch == 'p') || (ch == 'P'))
{
usePostProcessing = !usePostProcessing;
}
}
if (auto gs = cast(GameState) m_currentState)
{
if (key == SDLK_ESCAPE)
{
setState(m_playmenu);
}
if ((key == SDLK_RETURN) || (key == SDLK_SPACE))
{
m_soundManager.playSound(FX.PLOP);
m_gameState.newGame(selectedPlayers, selectedIA);
}
}
else if (m_currentState is m_splash1state)
{
setState(m_mainmenu);
}
else if (auto menu = cast(MenuState) m_currentState)
{
if (key == SDLK_UP)
{
menu.previousChoice();
}
else if (key == SDLK_DOWN)
{
menu.nextChoice();
}
else if ((key == SDLK_RETURN) || (key == SDLK_SPACE))
{
MenuItem item = menu.currentItem;
if (item.enabled) executeMenuAction(item.action, 0);
}
else if (key == SDLK_LEFT)
{
MenuItem item = menu.currentItem;
if (item.enabled) executeMenuAction(item.action, 1);
}
else if (key == SDLK_RIGHT)
{
MenuItem item = menu.currentItem;
if (item.enabled) executeMenuAction(item.action, 2);
}
else if (key == SDLK_ESCAPE)
{
MenuItem item = menu.lastItem;
executeMenuAction(item.action, 0);
}
}
}
double ratio()
{
return m_viewport.width / m_viewport.height;
}
void onMouseMove(float x, float y, float dx, float dy)
{
// transform to viewport
double xr = cast(double)m_windowWidth / m_viewport.width;
double yr = cast(double)m_windowHeight / m_viewport.height;
double mx = remapd(x, 0, m_windowWidth, -xr, xr);
double my = remapd(y, 0, m_windowHeight, +yr, -yr);
double mdx = remapd(dx, 0, m_windowWidth, 0, +ratio * xr);
double mdy = remapd(dy, 0, m_windowHeight, 0, -ratio * yr);
m_context.mousePos = vec2f(mx, my);
m_context.mouseVel = m_context.mouseVel + vec2f(mdx, mdy) * 0.03;
float l = m_context.mouseVel.length();
if (l > 0.07f * 0.17f)
{
m_context.mouseVel = m_context.mouseVel * (0.07f * 0.17f / l);
}
if (auto menu = cast(MenuState) m_currentState)
{
for (int i = 0; i < menu.items.length; ++i)
{
box2f r = menu.getItemBounds(i);
if (r.contains(m_context.mousePos))
{
menu.setCurrentChoice(i);
break;
}
}
}
}
void onMouseDown(int button)
{
m_context.mouseDown = true;
// splash 1
if (m_currentState is m_splash1state)
{
setState(m_mainmenu);
}
else if (auto menu = cast(MenuState) m_currentState)
{
for (int i = 0; i < menu.items.length; ++i)
{
box2f r = menu.getItemBounds(i);
if (r.contains(m_context.mousePos))
{
executeMenuAction(menu.items[i].action, 0);
break;
}
}
}
}
void onMouseUp(int button)
{
m_context.mouseDown = false;
}
}
}