wm pt1
[voxelands-alt.git] / src / graphics / wm_w32.c
blob21e9174f8bd2f9306748ba330dc87ddb8ef29d9a
1 /************************************************************************
2 * wm_x11.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #define _WM_EXPOSE_ALL
22 #include "wm.h"
24 #ifdef WIN32
26 /* initialise the game window */
27 int wm_init()
29 wm_data.isinit = 1;
31 return wm_create();
34 /* exit the game window */
35 void wm_exit()
37 wm_destroy();
40 /* create a window */
41 int wm_create()
43 GLuint PixelFormat;
44 WNDCLASS wc;
45 DWORD dwExStyle;
46 DWORD dwStyle;
47 RECT WindowRect;
49 wm_data.hDC = NULL;
50 wm_data.hRC = NULL;
51 wm_data.hWnd = NULL;
53 WindowRect.left = (long)0;
54 WindowRect.right = (long)wm_data.size.width;
55 WindowRect.top = (long)0;
56 WindowRect.bottom=(long)wm_data.size.height;
58 wm_data.hInstance = GetModuleHandle(NULL);
59 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
60 wc.lpfnWndProc = (WNDPROC)WndProc;
61 wc.cbClsExtra = 0;
62 wc.cbWndExtra = 0;
63 wc.hInstance = wm_data.hInstance;
64 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
65 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
66 wc.hbrBackground = NULL;
67 wc.lpszMenuName = NULL;
68 wc.lpszClassName = "OpenGL";
70 if (!RegisterClass(&wc)) {
71 vlprintf(CN_ERROR "Failed To Register The Window Class");
72 return 1;
75 if (wm_data.fullscreen) {
76 DEVMODE dmScreenSettings;
77 memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
78 dmScreenSettings.dmSize = sizeof(dmScreenSettings);
79 dmScreenSettings.dmPelsWidth = wm_data.size.width;
80 dmScreenSettings.dmPelsHeight = wm_data.size.height;
81 dmScreenSettings.dmBitsPerPel = 24;
82 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
84 /* if fullscreen fails, switch to windowed mode */
85 if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
86 vlprintf(CN_WARN "Requested Fullscreen Mode Is Not Supported");
87 wm_data.fullscreen = 0;
91 if (wm_data.fullscreen) {
92 dwExStyle = WS_EX_APPWINDOW;
93 dwStyle = WS_POPUP;
94 }else{
95 dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
96 dwStyle = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU);
99 AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
101 if (!(wm_data.hWnd = CreateWindowEx(
102 dwExStyle,
103 "OpenGL",
104 rtg_game_name,
105 dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
108 WindowRect.right-WindowRect.left,
109 WindowRect.bottom-WindowRect.top,
110 NULL,
111 NULL,
112 wm_data.hInstance,
113 NULL
114 ))) {
115 wm_destroy();
116 vlprintf(CN_ERROR "Window Creation Error");
117 return 1;
120 static PIXELFORMATDESCRIPTOR pfd = {
121 sizeof(PIXELFORMATDESCRIPTOR),
123 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
124 PFD_TYPE_RGBA,
142 PFD_MAIN_PLANE,
149 if (!(wm_data.hDC = GetDC(wm_data.hWnd))) {
150 wm_destroy();
151 vlprintf(CN_ERROR "Can't Create A GL Device Context");
152 return 1;
155 if (!(PixelFormat = ChoosePixelFormat(wm_data.hDC,&pfd))) {
156 wm_destroy();
157 vlprintf(CN_ERROR "Can't Find A Suitable PixelFormat");
158 return 1;
161 if (!SetPixelFormat(wm_data.hDC,PixelFormat,&pfd)) {
162 wm_destroy();
163 vlprintf(CN_ERROR "Can't Set The PixelFormat");
164 return 1;
167 if (!(wm_data.hRC = wglCreateContext(wm_data.hDC))) {
168 wm_destroy();
169 vlprintf(CN_ERROR "Can't Create A GL Rendering Context");
170 return 1;
173 if (!wglMakeCurrent(wm_data.hDC,wm_data.hRC)) {
174 wm_destroy();
175 vlprintf(CN_ERROR "Can't Activate The GL Rendering Context");
176 return 1;
179 ShowWindow(wm_data.hWnd,SW_SHOW);
180 SetForegroundWindow(wm_data.hWnd);
181 SetFocus(wm_data.hWnd);
183 return 0;
186 /* resize the screen, fullscreen or windowed */
187 int wm_resize()
189 return 0;
192 /* flush graphics through and flip buffers */
193 int wm_update()
195 glFlush();
196 /* update the screen */
197 SwapBuffers(wm_data.hDC);
198 return 0;
201 /* destroy the current window */
202 void wm_destroy()
204 if (wm_data.fullscreen)
205 ChangeDisplaySettings(NULL,0);
207 if (wm_data.hRC) {
208 if (!wglMakeCurrent(NULL,NULL)) {
209 vlprintf(CN_WARN "Release Of DC And RC Failed");
212 if (!wglDeleteContext(wm_data.hRC)) {
213 vlprintf(CN_WARN "Release Rendering Context Failed");
215 wm_data.hRC = NULL;
218 if (wm_data.hDC && !ReleaseDC(wm_data.hWnd,wm_data.hDC)) {
219 vlprintf(CN_WARN "Release Device Context Failed");
220 wm_data.hDC = NULL;
223 if (wm_data.hWnd && !DestroyWindow(wm_data.hWnd)) {
224 vlprintf(CN_WARN "Could Not Release hWnd");
225 wm_data.hWnd = NULL;
228 if (!UnregisterClass("OpenGL",wm_data.hInstance)) {
229 vlprintf(CN_WARN "Could Not Unregister Class");
230 wm_data.hInstance = NULL;
234 /* set fullscreen on/off */
235 void wm_toggle_fullscreen(int fs)
237 if (fs == wm_data.fullscreen)
238 return;
240 if (!wm_data.isinit) {
241 wm_data.fullscreen = fs;
242 return;
244 wm_destroy();
245 wm_data.fullscreen = fs;
246 wm_create();
249 /* use file as a cursor texture */
250 void wm_cursor(char* file, int width, int height, int offset_x, int offset_y)
252 /* TODO: once the rest of the graphics are in, do it */
253 #ifdef DONT_DO_IT_FOR_FUCKS_SAKE
254 if (!file) {
255 if (!wm_data.cursor.mat)
256 return;
258 wm_data.cursor.mat = NULL;
259 /* TODO: w32 wm_cursor */
261 return;
264 wm_data.cursor.mat = mat_from_image(file);
265 wm_data.cursor.w = width;
266 wm_data.cursor.h = height;
267 wm_data.cursor.x = offset_x;
268 wm_data.cursor.y = offset_y;
270 if (!wm_data.cursor.mat)
271 return;
272 /* TODO: w32 wm_cursor */
273 #endif
276 /* grab mouse */
277 void wm_grab()
279 /* TODO: w32 wm_grab */
282 /* stop grabbing mouse */
283 void wm_ungrab()
285 /* TODO: w32 wm_ungrab */
288 /* set window title */
289 void wm_title(char* title)
291 if (title) {
292 if (wm_data.title)
293 free(wm_data.title);
294 wm_data.title = strdup(title);
296 if (!wm_data.isinit)
297 return;
299 SetWindowText(wm_data.hWnd,wm_data.title);
301 #endif