1 /************************************************************************
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 ************************************************************************/
21 #define _WM_EXPOSE_ALL
26 /* initialise the game window */
34 /* exit the game window */
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
;
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");
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
;
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(
105 dwStyle
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
,
108 WindowRect
.right
-WindowRect
.left
,
109 WindowRect
.bottom
-WindowRect
.top
,
116 vlprintf(CN_ERROR
"Window Creation Error");
120 static PIXELFORMATDESCRIPTOR pfd
= {
121 sizeof(PIXELFORMATDESCRIPTOR
),
123 PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
,
149 if (!(wm_data
.hDC
= GetDC(wm_data
.hWnd
))) {
151 vlprintf(CN_ERROR
"Can't Create A GL Device Context");
155 if (!(PixelFormat
= ChoosePixelFormat(wm_data
.hDC
,&pfd
))) {
157 vlprintf(CN_ERROR
"Can't Find A Suitable PixelFormat");
161 if (!SetPixelFormat(wm_data
.hDC
,PixelFormat
,&pfd
)) {
163 vlprintf(CN_ERROR
"Can't Set The PixelFormat");
167 if (!(wm_data
.hRC
= wglCreateContext(wm_data
.hDC
))) {
169 vlprintf(CN_ERROR
"Can't Create A GL Rendering Context");
173 if (!wglMakeCurrent(wm_data
.hDC
,wm_data
.hRC
)) {
175 vlprintf(CN_ERROR
"Can't Activate The GL Rendering Context");
179 ShowWindow(wm_data
.hWnd
,SW_SHOW
);
180 SetForegroundWindow(wm_data
.hWnd
);
181 SetFocus(wm_data
.hWnd
);
186 /* resize the screen, fullscreen or windowed */
192 /* flush graphics through and flip buffers */
196 /* update the screen */
197 SwapBuffers(wm_data
.hDC
);
201 /* destroy the current window */
204 if (wm_data
.fullscreen
)
205 ChangeDisplaySettings(NULL
,0);
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");
218 if (wm_data
.hDC
&& !ReleaseDC(wm_data
.hWnd
,wm_data
.hDC
)) {
219 vlprintf(CN_WARN
"Release Device Context Failed");
223 if (wm_data
.hWnd
&& !DestroyWindow(wm_data
.hWnd
)) {
224 vlprintf(CN_WARN
"Could Not Release hWnd");
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
)
240 if (!wm_data
.isinit
) {
241 wm_data
.fullscreen
= fs
;
245 wm_data
.fullscreen
= fs
;
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
255 if (!wm_data
.cursor
.mat
)
258 wm_data
.cursor
.mat
= NULL
;
259 /* TODO: w32 wm_cursor */
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
)
272 /* TODO: w32 wm_cursor */
279 /* TODO: w32 wm_grab */
282 /* stop grabbing mouse */
285 /* TODO: w32 wm_ungrab */
288 /* set window title */
289 void wm_title(char* title
)
294 wm_data
.title
= strdup(title
);
299 SetWindowText(wm_data
.hWnd
,wm_data
.title
);