vkQuake2 doxygen  1.0 dev
rw_imp.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 */
20 /*
21 ** RW_IMP.C
22 **
23 ** This file contains ALL Win32 specific stuff having to do with the
24 ** software refresh. When a port is being made the following functions
25 ** must be implemented by the port:
26 **
27 ** SWimp_EndFrame
28 ** SWimp_Init
29 ** SWimp_SetPalette
30 ** SWimp_Shutdown
31 */
32 #include "..\ref_soft\r_local.h"
33 #include "rw_win.h"
34 #include "winquake.h"
35 
36 // Console variables that we need to access from this module
37 
39 
40 /*
41 ** VID_CreateWindow
42 */
43 #define WINDOW_CLASS_NAME "Quake 2"
44 
45 void VID_CreateWindow( int width, int height, int stylebits )
46 {
47  WNDCLASS wc;
48  RECT r;
50  int x, y, w, h;
51  int exstyle;
52 
53  vid_xpos = ri.Cvar_Get ("vid_xpos", "0", 0);
54  vid_ypos = ri.Cvar_Get ("vid_ypos", "0", 0);
55  vid_fullscreen = ri.Cvar_Get ("vid_fullscreen", "0", CVAR_ARCHIVE );
56 
57  if ( vid_fullscreen->value )
58  exstyle = WS_EX_TOPMOST;
59  else
60  exstyle = 0;
61 
62  /* Register the frame class */
63  wc.style = 0;
64  wc.lpfnWndProc = (WNDPROC)sww_state.wndproc;
65  wc.cbClsExtra = 0;
66  wc.cbWndExtra = 0;
67  wc.hInstance = sww_state.hInstance;
68  wc.hIcon = 0;
69  wc.hCursor = LoadCursor (NULL,IDC_ARROW);
70  wc.hbrBackground = (void *)COLOR_GRAYTEXT;
71  wc.lpszMenuName = 0;
72  wc.lpszClassName = WINDOW_CLASS_NAME;
73 
74  if (!RegisterClass (&wc) )
75  ri.Sys_Error (ERR_FATAL, "Couldn't register window class");
76 
77  r.left = 0;
78  r.top = 0;
79  r.right = width;
80  r.bottom = height;
81 
82  AdjustWindowRect (&r, stylebits, FALSE);
83 
84  w = r.right - r.left;
85  h = r.bottom - r.top;
86  x = vid_xpos->value;
87  y = vid_ypos->value;
88 
89  sww_state.hWnd = CreateWindowEx (
90  exstyle,
92  "Quake 2 (Software) "CPUSTRING,
93  stylebits,
94  x, y, w, h,
95  NULL,
96  NULL,
98  NULL);
99 
100  if (!sww_state.hWnd)
101  ri.Sys_Error (ERR_FATAL, "Couldn't create window");
102 
103  ShowWindow( sww_state.hWnd, SW_SHOWNORMAL );
104  UpdateWindow( sww_state.hWnd );
105  SetForegroundWindow( sww_state.hWnd );
106  SetFocus( sww_state.hWnd );
107 
108  // let the sound and input subsystems know about the new window
110 }
111 
112 /*
113 ** SWimp_Init
114 **
115 ** This routine is responsible for initializing the implementation
116 ** specific stuff in a software rendering subsystem.
117 */
118 int SWimp_Init( void *hInstance, void *wndProc )
119 {
120  sww_state.hInstance = ( HINSTANCE ) hInstance;
121  sww_state.wndproc = wndProc;
122 
123  return true;
124 }
125 
126 /*
127 ** SWimp_InitGraphics
128 **
129 ** This initializes the software refresh's implementation specific
130 ** graphics subsystem. In the case of Windows it creates DIB surfaces.
131 **
132 ** The necessary width and height parameters are grabbed from
133 ** vid.width and vid.height.
134 */
136 {
137  // free resources in use
138  SWimp_Shutdown ();
139 
140  // create a new window
142 
143  // initialize the appropriate subsystem
144  //qb: nuke ddraw
145 
146  if (!DIB_Init(&vid.buffer, &vid.rowbytes))
147  {
148  vid.buffer = 0;
149  vid.rowbytes = 0;
150 
151  return false;
152  }
153 
154  return true;
155 }
156 
157 /*
158 ** SWimp_EndFrame
159 **
160 ** This does an implementation specific copy from the backbuffer to the
161 ** front buffer. In the Win32 case it uses BitBlt
162 */
163 void SWimp_EndFrame (void)
164 {
165 
166  if (sww_state.palettized)
167  {
168  // holdpal = SelectPalette(hdcScreen, hpalDIB, FALSE);
169  // RealizePalette(hdcScreen);
170  }
171 
172  BitBlt(sww_state.hDC,
173  0, 0,
174  vid.width,
175  vid.height,
177  0, 0,
178  SRCCOPY);
179 
180  if (sww_state.palettized)
181  {
182  // SelectPalette(hdcScreen, holdpal, FALSE);
183  }
184 }
185 
186 /*
187 ** SWimp_SetMode
188 */
189 rserr_t SWimp_SetMode(int *pwidth, int *pheight, int mode, qboolean fullscreen)
190 {
191  const char *win_fs[] = { "W", "FS" };
192  rserr_t retval = rserr_ok;
193 
194  ri.Con_Printf (PRINT_ALL, "setting mode %d:", mode );
195 
196  if ( !ri.Vid_GetModeInfo( pwidth, pheight, mode ) )
197  {
198  ri.Con_Printf( PRINT_ALL, " invalid mode\n" );
199  return rserr_invalid_mode;
200  }
201 
202  ri.Con_Printf(PRINT_ALL, " %d %d %s\n", *pwidth, *pheight, win_fs[fullscreen]);
203 
204  sww_state.initializing = true;
205  if (fullscreen) //qb: get rid of 'fullscreen'.
206  {
207  if (!SWimp_InitGraphics())
208  {
209  ri.Cvar_SetValue("vid_fullscreen", !fullscreen); //qb: added
210  vid_fullscreen->modified = true;
211  if (SWimp_InitGraphics())
212  {
213  // mode is legal but not as fullscreen
214  retval = rserr_invalid_fullscreen;
215  }
216  else
217  {
218  // failed to set a valid mode in windowed mode
219  retval = rserr_unknown;
220  }
221  }
222  }
223  else
224  {
225  // failure to set a valid mode in windowed mode
226  if (!SWimp_InitGraphics())
227  {
228  sww_state.initializing = true;
229  return rserr_unknown;
230  }
231  }
232 
233  sw_state.fullscreen = fullscreen;
234 #if 0
235  if ( retval != rserr_unknown )
236  {
237  if ( retval == rserr_invalid_fullscreen ||
238  ( retval == rserr_ok && !fullscreen ) )
239  {
240  SetWindowLong( sww_state.hWnd, GWL_STYLE, WINDOW_STYLE );
241  }
242  }
243 #endif
244  R_GammaCorrectAndSetPalette( ( const unsigned char * ) d_8to24table );
245  sww_state.initializing = true;
246 
247  return retval;
248 }
249 
250 /*
251 ** SWimp_SetPalette
252 **
253 ** System specific palette setting routine. A NULL palette means
254 ** to use the existing palette. The palette is expected to be in
255 ** a padded 4-byte xRGB format.
256 */
257 void SWimp_SetPalette( const unsigned char *palette )
258 {
259  // MGL - what the fuck was kendall doing here?!
260  // clear screen to black and change palette
261  // for (i=0 ; i<vid.height ; i++)
262  // memset (vid.buffer + i*vid.rowbytes, 0, vid.width);
263 
264  if ( !palette )
265  palette = ( const unsigned char * ) sw_state.currentpalette;
266  DIB_SetPalette((const unsigned char *)palette);
267 }
268 
269 /*
270 ** SWimp_Shutdown
271 **
272 ** System specific graphics subsystem shutdown routine. Destroys DIBs
273 */
274 void SWimp_Shutdown( void )
275 {
276  ri.Con_Printf( PRINT_ALL, "Shutting down SW imp\n" );
277  DIB_Shutdown();
278 
279  if ( sww_state.hWnd )
280  {
281  ri.Con_Printf( PRINT_ALL, "...destroying window\n" );
282  ShowWindow( sww_state.hWnd, SW_SHOWNORMAL ); // prevents leaving empty slots in the taskbar
283  DestroyWindow (sww_state.hWnd);
284  sww_state.hWnd = NULL;
285  UnregisterClass (WINDOW_CLASS_NAME, sww_state.hInstance);
286  }
287 }
288 
289 /*
290 ** SWimp_AppActivate
291 */
293 {
294  if ( active )
295  {
296  if ( sww_state.hWnd )
297  {
298  SetForegroundWindow( sww_state.hWnd );
299  ShowWindow( sww_state.hWnd, SW_RESTORE );
300  }
301  }
302  else
303  {
304  if ( sww_state.hWnd )
305  {
306  if ( sww_state.initializing )
307  return;
308  if ( vid_fullscreen->value )
309  ShowWindow( sww_state.hWnd, SW_MINIMIZE );
310  }
311  }
312 }
313 
314 //===============================================================================
315 
316 
317 /*
318 ================
319 Sys_MakeCodeWriteable
320 ================
321 */
322 void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
323 {
324  DWORD flOldProtect;
325 
326  if (!VirtualProtect((LPVOID)startaddr, length, PAGE_READWRITE, &flOldProtect))
327  ri.Sys_Error(ERR_FATAL, "Protection change failed\n");
328 }
329 
330 /*
331 ** Sys_SetFPCW
332 **
333 ** For reference:
334 **
335 ** 1
336 ** 5 0
337 ** xxxxRRPP.xxxxxxxx
338 **
339 ** PP = 00 = 24-bit single precision
340 ** PP = 01 = reserved
341 ** PP = 10 = 53-bit double precision
342 ** PP = 11 = 64-bit extended precision
343 **
344 ** RR = 00 = round to nearest
345 ** RR = 01 = round down (towards -inf, floor)
346 ** RR = 10 = round up (towards +inf, ceil)
347 ** RR = 11 = round to zero (truncate/towards 0)
348 **
349 */
350 #if !id386
351 void Sys_SetFPCW (void)
352 {
353 }
354 #else
355 unsigned fpu_ceil_cw, fpu_chop_cw, fpu_full_cw, fpu_cw, fpu_pushed_cw;
356 unsigned fpu_sp24_cw, fpu_sp24_ceil_cw;
357 
358 void Sys_SetFPCW( void )
359 {
360  __asm xor eax, eax
361 
362  __asm fnstcw word ptr fpu_cw
363  __asm mov ax, word ptr fpu_cw
364 
365  __asm and ah, 0f0h
366  __asm or ah, 003h ; round to nearest mode, extended precision
367  __asm mov fpu_full_cw, eax
368 
369  __asm and ah, 0f0h
370  __asm or ah, 00fh ; RTZ/truncate/chop mode, extended precision
371  __asm mov fpu_chop_cw, eax
372 
373  __asm and ah, 0f0h
374  __asm or ah, 00bh ; ceil mode, extended precision
375  __asm mov fpu_ceil_cw, eax
376 
377  __asm and ah, 0f0h ; round to nearest, 24-bit single precision
378  __asm mov fpu_sp24_cw, eax
379 
380  __asm and ah, 0f0h ; ceil mode, 24-bit single precision
381  __asm or ah, 008h ;
382  __asm mov fpu_sp24_ceil_cw, eax
383 }
384 #endif
385 
swwstate_t
Definition: rw_win.h:26
vid_fullscreen
cvar_t * vid_fullscreen
Definition: vid_dll.c:47
rserr_t
rserr_t
Definition: r_local.h:103
height
GLsizei height
Definition: qgl_win.c:69
DIB_Init
qboolean DIB_Init(unsigned char **ppbuffer, int *ppitch)
Definition: rw_dib.c:88
DIB_SetPalette
void DIB_SetPalette(const unsigned char *_pal)
Definition: rw_dib.c:269
WINDOW_CLASS_NAME
#define WINDOW_CLASS_NAME
Definition: rw_imp.c:43
rw_win.h
Sys_SetFPCW
void Sys_SetFPCW(void)
Definition: rw_imp.c:351
swwstate_t::initializing
qboolean initializing
Definition: rw_win.h:52
Sys_MakeCodeWriteable
void Sys_MakeCodeWriteable(unsigned long startaddr, unsigned long length)
Definition: rw_imp.c:322
viddef_t::buffer
pixel_t * buffer
Definition: r_local.h:94
R_GammaCorrectAndSetPalette
void R_GammaCorrectAndSetPalette(const unsigned char *pal)
Definition: r_main.c:1257
ri
refimport_t ri
Definition: r_main.c:25
refimport_t::Vid_NewWindow
void(* Vid_NewWindow)(int width, int height)
Definition: ref.h:222
cvar_s::modified
qboolean modified
Definition: q_shared.h:330
refimport_t::Cvar_Get
cvar_t *(* Cvar_Get)(char *name, char *value, int flags)
Definition: ref.h:216
qboolean
qboolean
Definition: q_shared.h:63
x
GLint GLenum GLint x
Definition: qgl_win.c:116
SWimp_AppActivate
void SWimp_AppActivate(qboolean active)
Definition: rw_imp.c:292
winquake.h
SWimp_Shutdown
void SWimp_Shutdown(void)
Definition: rw_imp.c:274
width
GLint GLsizei width
Definition: qgl_win.c:115
cvar_s
Definition: q_shared.h:324
SWimp_SetPalette
void SWimp_SetPalette(const unsigned char *palette)
Definition: rw_imp.c:257
rserr_unknown
@ rserr_unknown
Definition: r_local.h:110
refimport_t::Vid_GetModeInfo
qboolean(* Vid_GetModeInfo)(int *width, int *height, int mode)
Definition: ref.h:220
refimport_t::Con_Printf
void(* Con_Printf)(int print_level, char *str,...)
Definition: ref.h:202
PRINT_ALL
#define PRINT_ALL
Definition: qcommon.h:751
swwstate_t::palettized
qboolean palettized
Definition: rw_win.h:49
SWimp_EndFrame
void SWimp_EndFrame(void)
Definition: rw_imp.c:163
viddef_t::rowbytes
int rowbytes
Definition: r_local.h:97
r
GLdouble GLdouble r
Definition: qgl_win.c:336
viddef_t::width
unsigned width
Definition: vid.h:29
SWimp_InitGraphics
static qboolean SWimp_InitGraphics(void)
Definition: rw_imp.c:135
rserr_invalid_mode
@ rserr_invalid_mode
Definition: r_local.h:108
swwstate_t::hInstance
HINSTANCE hInstance
Definition: rw_win.h:28
refimport_t::Sys_Error
void(* Sys_Error)(int err_level, char *str,...)
Definition: ref.h:194
CVAR_ARCHIVE
#define CVAR_ARCHIVE
Definition: q_shared.h:316
viddef_t::height
unsigned height
Definition: vid.h:29
cvar_s::value
float value
Definition: q_shared.h:331
NULL
#define NULL
Definition: q_shared.h:67
DIB_Shutdown
void DIB_Shutdown(void)
Definition: rw_dib.c:374
SWimp_SetMode
rserr_t SWimp_SetMode(int *pwidth, int *pheight, int mode, qboolean fullscreen)
Definition: rw_imp.c:189
ERR_FATAL
#define ERR_FATAL
Definition: qcommon.h:743
VID_CreateWindow
void VID_CreateWindow(int width, int height, int stylebits)
Definition: rw_imp.c:45
y
GLint y
Definition: qgl_win.c:115
WINDOW_STYLE
#define WINDOW_STYLE
Definition: winquake.h:28
d_8to24table
unsigned d_8to24table[256]
Definition: r_main.c:27
rserr_ok
@ rserr_ok
Definition: r_local.h:105
SWimp_Init
int SWimp_Init(void *hInstance, void *wndProc)
Definition: rw_imp.c:118
swstate_s::fullscreen
qboolean fullscreen
Definition: r_local.h:880
swwstate_t::wndproc
void * wndproc
Definition: rw_win.h:29
vid_xpos
cvar_t * vid_xpos
Definition: vid_dll.c:45
swwstate_t::hdcDIBSection
HDC hdcDIBSection
Definition: rw_win.h:33
DWORD
DWORD
Definition: qgl_win.c:49
w
GLdouble GLdouble GLdouble w
Definition: qgl_win.c:291
FALSE
#define FALSE
Definition: stb_vorbis.c:618
refimport_t::Cvar_SetValue
void(* Cvar_SetValue)(char *name, float value)
Definition: ref.h:218
mode
GLenum mode
Definition: qgl_win.c:113
sww_state
swwstate_t sww_state
Definition: rw_imp.c:38
swwstate_t::hWnd
HWND hWnd
Definition: rw_win.h:31
swwstate_t::hDC
HDC hDC
Definition: rw_win.h:30
vid_ypos
cvar_t * vid_ypos
Definition: vid_dll.c:46
swstate_s::currentpalette
byte currentpalette[1024]
Definition: r_local.h:884
CPUSTRING
#define CPUSTRING
Definition: qcommon.h:86
sw_state
swstate_t sw_state
Definition: r_main.c:49
ax
LEnter16_16 al cl movb ah addl edx movb ch esi ax edx movw ax
Definition: block16.h:11
rserr_invalid_fullscreen
@ rserr_invalid_fullscreen
Definition: r_local.h:107
vid
viddef_t vid
Definition: r_main.c:24