Browse Source

win32 platform

Henrique Santiago 10 years ago
parent
commit
2e2c71ecc7

+ 1
- 1
src/framework/engine.cpp View File

@@ -50,7 +50,7 @@ void Engine::init()
50 50
     int height = g_config.getInteger("height");
51 51
 
52 52
     // create the window
53
-    Platform::createWindow(width, height, 550, 450);
53
+    Platform::createWindow(0, 0, width, height, 550, 450, false);
54 54
     Platform::setWindowTitle(APP_NAME);
55 55
     Platform::setVsync();
56 56
 

+ 1
- 1
src/framework/platform.h View File

@@ -40,7 +40,7 @@ namespace Platform
40 40
     /// Sleep in current thread
41 41
     void sleep(unsigned long miliseconds);
42 42
 
43
-    bool createWindow(int width, int height, int minWidth, int minHeight);
43
+    bool createWindow(int x, int y, int width, int height, int minWidth, int minHeight, bool maximized);
44 44
     void destroyWindow();
45 45
     void showWindow();
46 46
     void setWindowTitle(const char *title);

+ 339
- 0
src/framework/win32platform.cpp View File

@@ -1 +1,340 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
1 23
 
24
+#include "platform.h"
25
+#include "engine.h"
26
+
27
+#include <windows.h>
28
+
29
+LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
30
+
31
+struct Win32PlatformPrivate {
32
+    HWND window;
33
+    HINSTANCE instance;
34
+    HDC hdc;
35
+    HGLRC hrc;
36
+
37
+    int x, y;
38
+    int width, height;
39
+    int minWidth, minHeight;
40
+    bool maximized;
41
+} win32;
42
+
43
+void Platform::init()
44
+{
45
+    win32.instance = GetModuleHandle(NULL);
46
+
47
+    WNDCLASS wc;
48
+    wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
49
+    wc.lpfnWndProc		= (WNDPROC)WndProc;			// WndProc Handles Messages
50
+    wc.cbClsExtra		= 0;					// No Extra Window Data
51
+    wc.cbWndExtra		= 0;					// No Extra Window Data
52
+    wc.hInstance		= win32.instance;			// Set The Instance
53
+    wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);		// Load The Default Icon
54
+    wc.hCursor			= LoadCursor(NULL, IDC_ARROW);		// Load The Arrow Pointer
55
+    wc.hbrBackground            = NULL;					// No Background Required For GL
56
+    wc.lpszMenuName		= NULL;					// We Don't Want A Menu
57
+    wc.lpszClassName            = L"OTClient";		// Set The Class Name
58
+
59
+    if(!RegisterClass(&wc))
60
+        fatal("Failed to register the window class.");
61
+}
62
+
63
+void Platform::terminate()
64
+{
65
+    if(win32.window) {
66
+        destroyWindow();
67
+        win32.window = NULL;
68
+    }
69
+
70
+    if(win32.instance) {
71
+        if(!UnregisterClass(L"OTClient", win32.instance))
72
+            error("Unregister class failed.");
73
+
74
+        win32.instance = NULL;
75
+    }
76
+}
77
+
78
+void Platform::poll()
79
+{
80
+    MSG msg;
81
+    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
82
+        TranslateMessage(&msg);
83
+        DispatchMessage(&msg);
84
+    }
85
+}
86
+
87
+unsigned long Platform::getTicks()
88
+{
89
+    return GetTickCount();
90
+}
91
+
92
+void Platform::sleep(unsigned long miliseconds)
93
+{
94
+    Sleep(miliseconds);
95
+}
96
+
97
+bool Platform::createWindow(int x, int y, int width, int height, int minWidth, int minHeight, bool maximized)
98
+{
99
+    DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
100
+    DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
101
+
102
+    win32.x = x;
103
+    win32.y = y;
104
+    win32.width = width;
105
+    win32.height = height;
106
+    win32.minWidth = minWidth;
107
+    win32.minHeight = minHeight;
108
+    win32.maximized = maximized;
109
+
110
+    //AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);
111
+
112
+    win32.window = CreateWindowEx(dwExStyle,		// Extended Style For The Window
113
+                                  L"OTClient",		// Class Name
114
+                                  L"OTClient",          // Window Title
115
+                                  dwStyle,		// Required Window Style
116
+                                  x,                    // Window X Position
117
+                                  y,                    // Window Y Position
118
+                                  width,                // Calculate Window Width
119
+                                  height,               // Calculate Window Height
120
+                                  NULL,			// No Parent Window
121
+                                  NULL,			// No Menu
122
+                                  win32.instance,	// Instance
123
+                                  NULL);
124
+
125
+    if(!win32.window) {
126
+        terminate();
127
+        fatal("Window creation error.");
128
+        return false;
129
+    }
130
+
131
+    GLuint pixelFormat;
132
+    static PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
133
+    {
134
+        sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
135
+        1,							// Version Number
136
+        PFD_DRAW_TO_WINDOW |					// Format Must Support Window
137
+        PFD_SUPPORT_OPENGL |					// Format Must Support OpenGL
138
+        PFD_DOUBLEBUFFER,					// Must Support Double Buffering
139
+        PFD_TYPE_RGBA,						// Request An RGBA Format
140
+        32,							// Select Our Color Depth
141
+        0, 0, 0, 0, 0, 0,					// Color Bits Ignored
142
+        0,							// No Alpha Buffer
143
+        0,							// Shift Bit Ignored
144
+        0,							// No Accumulation Buffer
145
+        0, 0, 0, 0,						// Accumulation Bits Ignored
146
+        16,							// 16Bit Z-Buffer (Depth Buffer)
147
+        0,							// No Stencil Buffer
148
+        0,							// No Auxiliary Buffer
149
+        PFD_MAIN_PLANE,						// Main Drawing Layer
150
+        0,							// Reserved
151
+        0, 0, 0							// Layer Masks Ignored
152
+    };
153
+
154
+    if(!(win32.hdc = GetDC(win32.window))) {
155
+        terminate();
156
+        fatal("Can't Create A GL Device Context.");
157
+        return false;
158
+    }
159
+
160
+    if(!(pixelFormat = ChoosePixelFormat(win32.hdc, &pfd))) {
161
+        terminate();
162
+        fatal("Can't Find A Suitable PixelFormat.");
163
+        return false;
164
+    }
165
+
166
+    if(!SetPixelFormat(win32.hdc, pixelFormat, &pfd)) {
167
+        terminate();
168
+        fatal("Can't Set The PixelFormat.");
169
+        return false;
170
+    }
171
+
172
+    if(!(win32.hrc = wglCreateContext(win32.hdc))) {
173
+        terminate();
174
+        fatal("Can't Create A GL Rendering Context.");
175
+        return false;
176
+    }
177
+
178
+    if(!wglMakeCurrent(win32.hdc, win32.hrc)) {
179
+        terminate();
180
+        fatal("Can't Activate The GL Rendering Context.");
181
+        return false;
182
+    }
183
+
184
+    return true;
185
+}
186
+
187
+void Platform::destroyWindow()
188
+{
189
+    if(win32.hrc) {
190
+        if(!wglMakeCurrent(NULL, NULL))
191
+            error("Release Of DC And RC Failed.");
192
+
193
+        if(!wglDeleteContext(win32.hrc))
194
+            error("Release Rendering Context Failed.");
195
+
196
+        win32.hrc = NULL;
197
+    }
198
+
199
+    if(win32.hdc) {
200
+        if(!ReleaseDC(win32.window, win32.hdc))
201
+            error("Release Device Context Failed.");
202
+
203
+        win32.hdc = NULL;
204
+    }
205
+
206
+    if(win32.window) {
207
+        if(!DestroyWindow(win32.window))
208
+            error("Destroy window failed.");
209
+
210
+        win32.window = NULL;
211
+    }
212
+}
213
+
214
+void Platform::showWindow()
215
+{
216
+    if(win32.maximized)
217
+        ShowWindow(win32.window, SW_MAXIMIZE);
218
+    else
219
+        ShowWindow(win32.window, SW_SHOW);
220
+}
221
+
222
+void Platform::setWindowTitle(const char *title)
223
+{
224
+    // TODO
225
+    SetWindowText(win32.window, (LPCWSTR)title);
226
+}
227
+
228
+void *Platform::getExtensionProcAddress(const char *ext)
229
+{
230
+    return (void*)wglGetProcAddress(ext);
231
+}
232
+
233
+bool Platform::isExtensionSupported(const char *ext)
234
+{
235
+    // TODO
236
+    const char *exts = NULL;//glXQueryExtensionsString(x11.display, DefaultScreen(x11.display));
237
+    if(strstr(exts, ext))
238
+        return true;
239
+    return true;
240
+}
241
+
242
+void Platform::showMouseCursor()
243
+{
244
+    // TODO
245
+    ShowCursor(false);
246
+    /*XUndefineCursor(x11.display, x11.window);
247
+    if(x11.cursor != None) {
248
+        XFreeCursor(x11.display, x11.cursor);
249
+        x11.cursor = None;
250
+    }*/
251
+}
252
+
253
+void Platform::setVsync(bool enable)
254
+{
255
+    typedef GLint (*glSwapIntervalProc)(GLint);
256
+    glSwapIntervalProc glSwapInterval = NULL;
257
+
258
+    if(isExtensionSupported("GLX_MESA_swap_control"))
259
+        glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalMESA");
260
+    else if(isExtensionSupported("GLX_SGI_swap_control"))
261
+        glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalSGI");
262
+
263
+    if(glSwapInterval)
264
+        glSwapInterval(enable ? 1 : 0);
265
+}
266
+
267
+void Platform::swapBuffers()
268
+{
269
+    SwapBuffers(win32.hdc);
270
+}
271
+
272
+int Platform::getWindowWidth()
273
+{
274
+    return win32.width;
275
+}
276
+
277
+int Platform::getWindowHeight()
278
+{
279
+    return win32.height;
280
+}
281
+
282
+const char *Platform::getAppUserDir()
283
+{
284
+    // TODO
285
+    /*std::stringstream sdir;
286
+    sdir << PHYSFS_getUserDir() << "/." << APP_NAME << "/";
287
+    if((mkdir(sdir.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) && (errno != EEXIST))
288
+        error("Couldn't create directory for saving configuration file. (%s)", sdir.str().c_str());
289
+    return sdir.str().c_str();*/
290
+    return "lol";
291
+}
292
+
293
+LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
294
+{
295
+    switch(uMsg)
296
+    {
297
+    case WM_CLOSE:
298
+        {
299
+            g_engine.onClose();
300
+            break;
301
+        }
302
+    case WM_GETMINMAXINFO:
303
+        {
304
+            MINMAXINFO *minMax = (MINMAXINFO*)lParam;
305
+            minMax->ptMinTrackSize.x = win32.minWidth;
306
+            minMax->ptMinTrackSize.y = win32.minHeight;
307
+            break;
308
+        }
309
+    case WM_MOVING:
310
+    case WM_SIZING:
311
+        {
312
+            RECT *rect = (RECT*)lParam;
313
+            win32.x = rect->left;
314
+            win32.y = rect->top;
315
+            win32.width = rect->right - rect->left;
316
+            win32.height = rect->bottom - rect->top;
317
+            break;
318
+        }
319
+    case WM_SIZE:
320
+        {
321
+            switch(wParam)
322
+            {
323
+            case SIZE_MAXIMIZED:
324
+                win32.maximized = true;
325
+                break;
326
+            case SIZE_RESTORED:
327
+                win32.maximized = false;
328
+                break;
329
+            }
330
+
331
+            g_engine.onResize(LOWORD(lParam), HIWORD(lParam));
332
+            break;
333
+        }
334
+    default:
335
+        {
336
+            return DefWindowProc(hWnd,uMsg,wParam,lParam);
337
+        }
338
+    }
339
+    return 0;
340
+}

+ 3
- 3
src/framework/x11platform.cpp View File

@@ -448,7 +448,7 @@ void Platform::sleep(unsigned long miliseconds)
448 448
     nanosleep(&tv, NULL);
449 449
 }
450 450
 
451
-bool Platform::createWindow(int width, int height, int minWidth, int minHeight)
451
+bool Platform::createWindow(int x, int y, int width, int height, int minWidth, int minHeight, bool maximized)
452 452
 {
453 453
     static int attrList[] = {
454 454
         GLX_USE_GL,
@@ -483,8 +483,8 @@ bool Platform::createWindow(int width, int height, int minWidth, int minHeight)
483 483
                          StructureNotifyMask | FocusChangeMask;
484 484
 
485 485
     // calculate center position
486
-    int x = (XDisplayHeight(x11.display, DefaultScreen(x11.display)) - width) / 2;
487
-    int y = (XDisplayHeight(x11.display, DefaultScreen(x11.display)) - height) / 2;
486
+    //x = (XDisplayWidth(x11.display, DefaultScreen(x11.display)) - width) / 2;
487
+    //y = (XDisplayHeight(x11.display, DefaultScreen(x11.display)) - height) / 2;
488 488
 
489 489
     // create the window
490 490
     x11.window = XCreateWindow(x11.display,

Loading…
Cancel
Save