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_APPLICATION
);
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 wm_data
.hWnd
= CreateWindowEx(
105 dwStyle
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
,
108 WindowRect
.right
-WindowRect
.left
,
109 WindowRect
.bottom
-WindowRect
.top
,
117 vlprintf(CN_ERROR
"Window Creation Error");
121 static PIXELFORMATDESCRIPTOR pfd
= {
122 sizeof(PIXELFORMATDESCRIPTOR
),
124 PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
,
150 if (!(wm_data
.hDC
= GetDC(wm_data
.hWnd
))) {
152 vlprintf(CN_ERROR
"Can't Create A GL Device Context");
156 if (!(PixelFormat
= ChoosePixelFormat(wm_data
.hDC
,&pfd
))) {
158 vlprintf(CN_ERROR
"Can't Find A Suitable PixelFormat");
162 if (!SetPixelFormat(wm_data
.hDC
,PixelFormat
,&pfd
)) {
164 vlprintf(CN_ERROR
"Can't Set The PixelFormat");
168 /* this should probably be modified so it creates a OpenGL 3.3 context */
169 if (!(wm_data
.hRC
= wglCreateContext(wm_data
.hDC
))) {
171 vlprintf(CN_ERROR
"Can't Create A GL Rendering Context");
175 if (!wglMakeCurrent(wm_data
.hDC
,wm_data
.hRC
)) {
177 vlprintf(CN_ERROR
"Can't Activate The GL Rendering Context");
181 ShowWindow(wm_data
.hWnd
,SW_SHOW
);
182 SetForegroundWindow(wm_data
.hWnd
);
183 SetFocus(wm_data
.hWnd
);
188 glGetIntegerv(GL_MAJOR_VERSION
, &major
);
189 glGetIntegerv(GL_MINOR_VERSION
, &minor
);
191 vlprintf(CN_INFO
"Direct Rendering: OpenGL %d.%d",major
,minor
);
194 /* MUST be done, else rendering gets messed up */
195 render3d_set_projection_matrix();
200 /* resize the screen, fullscreen or windowed */
203 /* MUST be done after a resize, else rendering gets messed up */
204 render3d_set_projection_matrix();
209 /* flush graphics through and flip buffers */
213 /* update the screen */
214 SwapBuffers(wm_data
.hDC
);
218 /* destroy the current window */
221 if (wm_data
.fullscreen
)
222 ChangeDisplaySettings(NULL
,0);
225 if (!wglMakeCurrent(NULL
,NULL
)) {
226 vlprintf(CN_WARN
"Release Of DC And RC Failed");
229 if (!wglDeleteContext(wm_data
.hRC
)) {
230 vlprintf(CN_WARN
"Release Rendering Context Failed");
235 if (wm_data
.hDC
&& !ReleaseDC(wm_data
.hWnd
,wm_data
.hDC
)) {
236 vlprintf(CN_WARN
"Release Device Context Failed");
240 if (wm_data
.hWnd
&& !DestroyWindow(wm_data
.hWnd
)) {
241 vlprintf(CN_WARN
"Could Not Release hWnd");
245 if (!UnregisterClass("OpenGL",wm_data
.hInstance
)) {
246 vlprintf(CN_WARN
"Could Not Unregister Class");
247 wm_data
.hInstance
= NULL
;
251 /* set fullscreen on/off */
252 void wm_toggle_fullscreen(int fs
)
254 if (fs
== wm_data
.fullscreen
)
257 if (!wm_data
.isinit
) {
258 wm_data
.fullscreen
= fs
;
262 wm_data
.fullscreen
= fs
;
266 /* use file as a cursor texture */
267 void wm_cursor(char* file
, int width
, int height
, int offset_x
, int offset_y
)
269 /* TODO: once the rest of the graphics are in, do it */
270 #ifdef DONT_DO_IT_FOR_FUCKS_SAKE
272 if (!wm_data
.cursor
.mat
)
275 wm_data
.cursor
.mat
= NULL
;
276 /* TODO: w32 wm_cursor */
281 wm_data
.cursor
.mat
= mat_from_image(file
);
282 wm_data
.cursor
.w
= width
;
283 wm_data
.cursor
.h
= height
;
284 wm_data
.cursor
.x
= offset_x
;
285 wm_data
.cursor
.y
= offset_y
;
287 if (!wm_data
.cursor
.mat
)
289 /* TODO: w32 wm_cursor */
296 /* TODO: w32 wm_grab */
299 /* stop grabbing mouse */
302 /* TODO: w32 wm_ungrab */
305 /* set window title */
306 void wm_title(char* title
)
311 wm_data
.title
= strdup(title
);
316 SetWindowText(wm_data
.hWnd
,wm_data
.title
);