forked from p0nce/Wormhol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wormholprogram.d
170 lines (130 loc) · 4.34 KB
/
wormholprogram.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
module wormholprogram;
import sdl.all;
import misc.all;
import math.all;
import gl.all;
import game;
import misc.logger;
final class WormholProgram : SDLApp
{
private
{
box2i m_view;
Game m_game;
}
public
{
this(int asked_width, int asked_height, double ratio, bool fullscreen, int fsaa)
{
info(">WormholProgram.this");
FSAA aa = void;
if (fsaa == 2) aa = FSAA.FSAA2X;
else if (fsaa == 4) aa = FSAA.FSAA4X;
else if (fsaa == 8) aa = FSAA.FSAA8X;
else if (fsaa == 16) aa = FSAA.FSAA16X;
else aa = FSAA.OFF;
super(asked_width, asked_height, fullscreen, false, "Wormhol", "data/icon.bmp", aa, 0, OpenGLVersion.Version20, true);
title = "Wormhol";
GL.check();
SDL_ShowCursor(SDL_DISABLE);
if (abs(ratio) < 0.0001) // auto ratio
{
ratio = cast(double)width / height;
if (ratio < 5.0 / 4.0) ratio = 5.0 / 4.0;
if (ratio > 16.0 / 9.0) ratio = 16.0 / 9.0;
}
// adjust viewport according to ratio
m_view = box2i(0, 0, width, height).subRectWithRatio(ratio);
GL.check();
GL.disable(GL.DEPTH_TEST, GL.LINE_SMOOTH, GL.POLYGON_SMOOTH, GL.POINT_SMOOTH, GL.BLEND,
GL.FOG, GL.LIGHTING, GL.NORMALIZE, GL.STENCIL_TEST, GL.CULL_FACE,
GL.AUTO_NORMAL);
GL.hint(GL.PERSPECTIVE_CORRECTION_HINT, GL.NICEST);
//GL.hint(GL.GENERATE_MIPMAP_HINT, GL.NICEST);
GL.check();
GL.clampColors(false, false, false);
GL.check();
GL.clearColor = vec4f(0.0,0.0,0.0,1.0);
GL.clear(true, true, true);
GL.depthFunc(GL.LEQUAL);
m_game = new Game(m_view, width, height);
//GL.hint(GL.GENERATE_MIPMAP_HINT, GL.FASTEST);
info("<WormholProgram.this");
}
~this()
{
info(">WormholProgram.~this");
delete m_game;
info("<WormholProgram.~this");
}
override void onRender(double elapsedTime)
{
//GL.viewport = m_view;
m_game.draw(elapsedTime);
}
override void onMove(double elapsedTime, double dt)
{
if (m_game.move(elapsedTime, dt))
{
terminate();
}
}
override void onKeyUp(int key, int mod, wchar ch)
{
m_game.keyUp(key, mod, ch);
}
override void onFrameRateChanged(float frameRate)
{
string s = format("Wormhol | FPS = %s | joyaxis = %s %s %s %s",
frameRate, axis(0), axis(1), axis(2), axis(3));
debug
{
title = s;
}
// info(s);
}
override void onKeyDown(int key, int mod, wchar ch)
{
m_game.keyDown(key, mod, ch);
}
override void onMouseMove(int x, int y, int dx, int dy)
{
m_game.onMouseMove(x, y, dx, dy);
}
override void onMouseDown(int button)
{
button &= (SDL_BUTTON_LMASK | SDL_BUTTON_MMASK | SDL_BUTTON_RMASK);
if (button != 0) m_game.onMouseDown(button);
}
override void onMouseUp(int button)
{
button &= (SDL_BUTTON_LMASK | SDL_BUTTON_MMASK | SDL_BUTTON_RMASK);
if (button != 0) m_game.onMouseUp(button);
}
override void onReshape(int width, int height)
{
}
// GAME LOOP
void run()
{
m_frameCounter.reset();
bool firstFrame = true;
while (! m_finished)
{
firstFrame = false;
final double time = m_frameCounter.elapsedTime;
double dt = m_frameCounter.deltaTime;
processEvents();
onMove(time, dt);
if (!firstFrame) swapBuffers();
firstFrame = false;
if (m_frameCounter.tick())
{
onFrameRateChanged(m_frameCounter.frameRate);
}
onRender(time);
}
processEvents();
}
}
}