/
window.d
294 lines (210 loc) · 6.04 KB
/
window.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
/*
* window.d
*
* This module has the structure that is kept with a Window class.
*
* Author: Dave Wilkinson
* Originated: July 22th, 2009
*
*/
module platform.vars.window;
import binding.win32.windef;
import binding.win32.winnt;
import binding.win32.winbase;
import binding.win32.wingdi;
import binding.win32.winuser;
import core.definitions;
import core.string;
import core.unicode;
import gui.window;
import binding.opengl.gl;
import binding.opengl.glu;
import opengl.window;
import synch.thread;
import scaffold.opengl;
struct WindowPlatformVars {
HWND hWnd;
HDC windhDC;
int hoverTimerSet;
int doubleClickTimerSet;
int doubleClickAmount;
int doubleClickX;
int doubleClickY;
HBRUSH brsh;
HPEN pen;
HFONT fnt;
uint oldX;
uint oldY;
int oldWidth;
int oldHeight;
ulong oldStyle;
ulong oldExStyle;
WindowState oldState;
bool infullscreen;
bool supress_WM_SIZE;
bool supress_WM_SIZE_state;
bool supress_WM_MOVE;
Thread msgThread;
uint istyle;
uint iexstyle;
HWND parenthWnd;
void* userData;
string oldTitle;
Window windowClass;
// ----
// resolution of the timer
// reset the game timer
struct _TimeInfo
{
long timeLast=0;
long timeCur=0;
double timeFactor = 0.0;
double timeAverages[25] = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0];
int timeAvgCount = 0;
int timeAvgIndex = 0;
int timeLast_tick = 0;
}
// pointer to the info structure (in the stack space of the thread... SIGH)
_TimeInfo* tmInfo;
// ----
// Deeply routed logic for windows in a file that doesn't need it
void msgLoop(bool pleaseStop) {
if (pleaseStop) {
return;
}
wstring oldTitleW = Unicode.toUtf16(oldTitle.dup);
oldTitleW ~='\0';
while(hWnd is null) {
hWnd = CreateWindowExW(iexstyle, "djehutyApp\0"w.ptr,oldTitleW.ptr, cast(DWORD)(istyle | WS_CLIPSIBLINGS),
oldX, oldY, oldWidth, oldHeight, null,
cast(HMENU) null, null, cast(void*)userData);
}
// create the window's view object
windowClass.onInitialize();
// call the onAdd() event
windowClass.onAdd();
MSG msg;
while (GetMessageW(&msg, cast(HWND) hWnd, 0, 0)) {
TranslateMessage(&msg);
if (msg.message == WM_DESTROY || msg.message == 0) {
break;
}
DispatchMessageW(&msg);
}
}
void gameLoopCallResize() {
GLWindow glWindow = cast(GLWindow)windowClass;
glWindow.onDraw(getDeltaTime());
// Now, we can swap the buffers
SwapBuffers(windhDC);
}
void initTime() {
long perfFreq;
// initialize query performance timer
QueryPerformanceFrequency(&perfFreq);
tmInfo.timeFactor = cast(double)1.0 / cast(double)perfFreq;
QueryPerformanceCounter(&tmInfo.timeLast);
// initialize tick counter
tmInfo.timeLast_tick = GetTickCount();
}
double getDeltaTime() {
QueryPerformanceCounter(&tmInfo.timeCur);
double timeSpan = cast(double)(tmInfo.timeCur - tmInfo.timeLast) * tmInfo.timeFactor;
tmInfo.timeLast = tmInfo.timeCur;
tmInfo.timeAverages[tmInfo.timeAvgIndex] = timeSpan;
tmInfo.timeAvgIndex++;
if (tmInfo.timeAvgCount != 25)
{
tmInfo.timeAvgCount++;
}
if (tmInfo.timeAvgIndex == tmInfo.timeAvgCount)
{
tmInfo.timeAvgIndex = 0;
}
timeSpan = tmInfo.timeAverages[0];
int i;
for (i=1; i<tmInfo.timeAvgCount; i++)
{
timeSpan += tmInfo.timeAverages[i];
}
timeSpan /= tmInfo.timeAvgCount;
//*/
return timeSpan;
}
void gameLoop(bool pleaseStop)
{
if (pleaseStop)
{
return;
}
wstring oldTitleW = Unicode.toUtf16(oldTitle.dup);
oldTitleW ~='\0';
hWnd = CreateWindowExW(0, "djehutyApp\0"w.ptr,oldTitleW.ptr, istyle ,
oldX, oldY, oldWidth, oldHeight, null,
cast(HMENU) null, null, cast(void*)userData);
assert(hWnd);
// Create GL context
HGLRC hRC;
GLuint PixelFormat;
if ((windhDC=GetDC(hWnd)) == null) // Did We Get A Device Context?
{
MessageBoxW(null,"Can't Create A GL Device Context.\0"w.ptr,"ERROR\0"w.ptr,MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
if ((PixelFormat=ChoosePixelFormat(windhDC,&pfd)) == 0) // Did Windows Find A Matching Pixel Format?
{
MessageBoxW(null,"Can't Find A Suitable PixelFormat.\0"w.ptr,"ERROR\0"w.ptr,MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
if(SetPixelFormat(windhDC,PixelFormat,&pfd) == 0) // Are We Able To Set The Pixel Format?
{
MessageBoxW(null,"Can't Set The PixelFormat.\0"w.ptr,"ERROR\0"w.ptr,MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
if ((hRC=wglCreateContext(windhDC)) == null) // Are We Able To Get A Rendering Context?
{
MessageBoxW(null,"Can't Create A GL Rendering Context.\0"w.ptr,"ERROR\0"w.ptr,MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
if(wglMakeCurrent(windhDC,hRC) == 0) // Try To Activate The Rendering Context
{
MessageBoxW(null,"Can't Activate The GL Rendering Context.\0"w.ptr,"ERROR\0"w.ptr,MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
// create the window's view object
windowClass.onInitialize();
// call the onAdd() event
windowClass.onAdd();
_TimeInfo tInfo;
// O M G... I hate doing this:
// pointer to the stack allocated array for doing thread stuffs without screwing the pooch somewhere else
// if I don't have this, I cannot destroy the thread cleanly? I don't know why.
tmInfo = &tInfo;
initTime();
GLWindow glWindow = cast(GLWindow)windowClass;
MSG msg;
for (;;)
{
if ( PeekMessageW(&msg, cast(HWND)hWnd,0,0,PM_REMOVE) != 0 )
{
if (msg.message==WM_QUIT)
{
//WM_QUIT
break;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
else
{
// Calculate the delta time for this window
// It is based upon typical game timer techniques
// This will need work to support multiple GL windows
// For now, one is supported
glWindow.onDraw(getDeltaTime());
// Now, we can swap the buffers
SwapBuffers(windhDC);
}
}
}
}