remove deprecated luminance texture generation
[voxelands-alt.git] / src / graphics / wm_w32.c
blob6c91f328db32a03ae42747d4b932132ef1d295b4
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_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");
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 wm_data.hWnd = CreateWindowEx(
102 dwExStyle,
103 "OpenGL",
104 PACKAGE,
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
115 if (!wm_data.hWnd) {
116 wm_destroy();
117 vlprintf(CN_ERROR "Window Creation Error");
118 return 1;
121 static PIXELFORMATDESCRIPTOR pfd = {
122 sizeof(PIXELFORMATDESCRIPTOR),
124 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
125 PFD_TYPE_RGBA,
143 PFD_MAIN_PLANE,
150 if (!(wm_data.hDC = GetDC(wm_data.hWnd))) {
151 wm_destroy();
152 vlprintf(CN_ERROR "Can't Create A GL Device Context");
153 return 1;
156 if (!(PixelFormat = ChoosePixelFormat(wm_data.hDC,&pfd))) {
157 wm_destroy();
158 vlprintf(CN_ERROR "Can't Find A Suitable PixelFormat");
159 return 1;
162 if (!SetPixelFormat(wm_data.hDC,PixelFormat,&pfd)) {
163 wm_destroy();
164 vlprintf(CN_ERROR "Can't Set The PixelFormat");
165 return 1;
168 /* this should probably be modified so it creates a OpenGL 3.3 context */
169 if (!(wm_data.hRC = wglCreateContext(wm_data.hDC))) {
170 wm_destroy();
171 vlprintf(CN_ERROR "Can't Create A GL Rendering Context");
172 return 1;
175 if (!wglMakeCurrent(wm_data.hDC,wm_data.hRC)) {
176 wm_destroy();
177 vlprintf(CN_ERROR "Can't Activate The GL Rendering Context");
178 return 1;
181 ShowWindow(wm_data.hWnd,SW_SHOW);
182 SetForegroundWindow(wm_data.hWnd);
183 SetFocus(wm_data.hWnd);
185 if (1) {
186 int major;
187 int minor;
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();
197 return 0;
200 /* resize the screen, fullscreen or windowed */
201 int wm_resize()
203 /* MUST be done after a resize, else rendering gets messed up */
204 render3d_set_projection_matrix();
206 return 0;
209 /* flush graphics through and flip buffers */
210 int wm_update()
212 glFlush();
213 /* update the screen */
214 SwapBuffers(wm_data.hDC);
215 return 0;
218 /* destroy the current window */
219 void wm_destroy()
221 if (wm_data.fullscreen)
222 ChangeDisplaySettings(NULL,0);
224 if (wm_data.hRC) {
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");
232 wm_data.hRC = NULL;
235 if (wm_data.hDC && !ReleaseDC(wm_data.hWnd,wm_data.hDC)) {
236 vlprintf(CN_WARN "Release Device Context Failed");
237 wm_data.hDC = NULL;
240 if (wm_data.hWnd && !DestroyWindow(wm_data.hWnd)) {
241 vlprintf(CN_WARN "Could Not Release hWnd");
242 wm_data.hWnd = NULL;
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)
255 return;
257 if (!wm_data.isinit) {
258 wm_data.fullscreen = fs;
259 return;
261 wm_destroy();
262 wm_data.fullscreen = fs;
263 wm_create();
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
271 if (!file) {
272 if (!wm_data.cursor.mat)
273 return;
275 wm_data.cursor.mat = NULL;
276 /* TODO: w32 wm_cursor */
278 return;
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)
288 return;
289 /* TODO: w32 wm_cursor */
290 #endif
293 /* grab mouse */
294 void wm_grab()
296 /* TODO: w32 wm_grab */
299 /* stop grabbing mouse */
300 void wm_ungrab()
302 /* TODO: w32 wm_ungrab */
305 /* set window title */
306 void wm_title(char* title)
308 if (title) {
309 if (wm_data.title)
310 free(wm_data.title);
311 wm_data.title = strdup(title);
313 if (!wm_data.isinit)
314 return;
316 SetWindowText(wm_data.hWnd,wm_data.title);
318 #endif