vkQuake2 doxygen  1.0 dev
rw_dib.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_DIB.C
22 **
23 ** This handles DIB section management under Windows.
24 **
25 */
26 #include "..\ref_soft\r_local.h"
27 #include "rw_win.h"
28 
29 #ifndef _WIN32
30 # error You should not be trying to compile this file on this platform
31 #endif
32 
34 
36 
37 static int s_syspalindices[] =
38 {
39  COLOR_ACTIVEBORDER,
40  COLOR_ACTIVECAPTION,
41  COLOR_APPWORKSPACE,
42  COLOR_BACKGROUND,
43  COLOR_BTNFACE,
44  COLOR_BTNSHADOW,
45  COLOR_BTNTEXT,
46  COLOR_CAPTIONTEXT,
47  COLOR_GRAYTEXT,
48  COLOR_HIGHLIGHT,
49  COLOR_HIGHLIGHTTEXT,
50  COLOR_INACTIVEBORDER,
51 
52  COLOR_INACTIVECAPTION,
53  COLOR_MENU,
54  COLOR_MENUTEXT,
55  COLOR_SCROLLBAR,
56  COLOR_WINDOW,
57  COLOR_WINDOWFRAME,
58  COLOR_WINDOWTEXT
59 };
60 
61 #define NUM_SYS_COLORS ( sizeof( s_syspalindices ) / sizeof( int ) )
62 
64 
65 typedef struct dibinfo
66 {
67  BITMAPINFOHEADER header;
68  RGBQUAD acolors[256];
69 } dibinfo_t;
70 
71 typedef struct
72 {
73  WORD palVersion;
75  PALETTEENTRY palEntries[256];
77 
79 
80 static void DIB_SaveSystemColors( void );
81 static void DIB_RestoreSystemColors( void );
82 
83 /*
84 ** DIB_Init
85 **
86 ** Builds our DIB section
87 */
88 qboolean DIB_Init( unsigned char **ppbuffer, int *ppitch )
89 {
90  dibinfo_t dibheader;
91  BITMAPINFO *pbmiDIB = ( BITMAPINFO * ) &dibheader;
92  DEVMODE gdevmode; //qb: for fullscreen
93  int i;
94 
95  memset( &dibheader, 0, sizeof( dibheader ) );
96 
97  /*
98  ** grab a DC
99  */
100  if ( !sww_state.hDC )
101  {
102  if ( ( sww_state.hDC = GetDC( sww_state.hWnd ) ) == NULL )
103  return false;
104  }
105 
106  /*
107  ** figure out if we're running in an 8-bit display mode
108  */
109  if ( GetDeviceCaps( sww_state.hDC, RASTERCAPS ) & RC_PALETTE )
110  {
111  sww_state.palettized = true;
112 
113  // save system colors
114  if ( !s_systemcolors_saved )
115  {
117  s_systemcolors_saved = true;
118  }
119  }
120  else
121  {
122  sww_state.palettized = false;
123  }
124 
125  vid.width = (int)(vid.width / 4) * 4; //qb: multiple of 4 required for DIB
126 
127  /*
128  ** fill in the BITMAPINFO struct
129  */
130  pbmiDIB->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
131  pbmiDIB->bmiHeader.biWidth = vid.width;
132  pbmiDIB->bmiHeader.biHeight = vid.height;
133  pbmiDIB->bmiHeader.biPlanes = 1;
134  pbmiDIB->bmiHeader.biBitCount = 8;
135  pbmiDIB->bmiHeader.biCompression = BI_RGB;
136  pbmiDIB->bmiHeader.biSizeImage = 0;
137  pbmiDIB->bmiHeader.biXPelsPerMeter = 0;
138  pbmiDIB->bmiHeader.biYPelsPerMeter = 0;
139  pbmiDIB->bmiHeader.biClrUsed = 256;
140  pbmiDIB->bmiHeader.biClrImportant = 256;
141 
142  /*
143  ** fill in the palette
144  */
145  for ( i = 0; i < 256; i++ )
146  {
147  dibheader.acolors[i].rgbRed = ( d_8to24table[i] >> 0 ) & 0xff;
148  dibheader.acolors[i].rgbGreen = ( d_8to24table[i] >> 8 ) & 0xff;
149  dibheader.acolors[i].rgbBlue = ( d_8to24table[i] >> 16 ) & 0xff;
150  }
151 
152  /*
153  ** create the DIB section
154  */
155  sww_state.hDIBSection = CreateDIBSection( sww_state.hDC,
156  pbmiDIB,
157  DIB_RGB_COLORS,
159  NULL,
160  0 );
161 
162  if ( sww_state.hDIBSection == NULL )
163  {
164  ri.Con_Printf( PRINT_ALL, "DIB_Init() - CreateDIBSection failed\n" );
165  goto fail;
166  }
167 
168  if ( pbmiDIB->bmiHeader.biHeight > 0 )
169  {
170  // bottom up
171  *ppbuffer = sww_state.pDIBBase + ( vid.height - 1 ) * vid.width;
172  *ppitch = -vid.width;
173  }
174  else
175  {
176  // top down
177  *ppbuffer = sww_state.pDIBBase;
178  *ppitch = vid.width;
179  }
180 
181  MONITORINFOEX monInfo;
182  memset(&monInfo, 0, sizeof(MONITORINFOEX));
183  monInfo.cbSize = sizeof(MONITORINFOEX);
184  GetMonitorInfo( MonitorFromWindow(sww_state.hWnd, MONITOR_DEFAULTTOPRIMARY), (LPMONITORINFO)&monInfo );
185 
186  ChangeDisplaySettingsEx(monInfo.szDevice, NULL, NULL, 0, NULL);
187 
188  if (vid_fullscreen->value) //qb: fullscreen dib
189  {
190 
191  RECT WindowRect;
192  DWORD WindowStyle, ExWindowStyle;
193 
194  WindowRect.top = monInfo.rcMonitor.top;
195  WindowRect.left = monInfo.rcMonitor.left;
196 
197  WindowRect.right = vid.width;
198  WindowRect.bottom = vid.height;
199  memset(&gdevmode, 0, sizeof(gdevmode));
200  gdevmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
201  gdevmode.dmPelsWidth = vid.width;
202  gdevmode.dmPelsHeight = vid.height;
203  gdevmode.dmSize = sizeof (gdevmode);
204 
205  if ( ChangeDisplaySettingsEx( monInfo.szDevice, &gdevmode, NULL, CDS_FULLSCREEN, NULL ) != DISP_CHANGE_SUCCESSFUL )
206  {
207  //ri.Sys_Error(ERR_FATAL, "Couldn't set fullscreen DIB mode");
208  goto fail; //qb: don't panic
209  }
210 
211  GetMonitorInfo( MonitorFromWindow(sww_state.hWnd, MONITOR_DEFAULTTOPRIMARY), (LPMONITORINFO)&monInfo );
212  if (!SetWindowPos(sww_state.hWnd,
213  NULL,
214  monInfo.rcMonitor.left, monInfo.rcMonitor.top,
215  WindowRect.right,
216  WindowRect.bottom,
217  SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOOWNERZORDER | SWP_NOREPOSITION | SWP_NOZORDER))
218  {
219  //ri.Sys_Error(ERR_FATAL, "Couldn't resize DIB window");
220  goto fail; //qb: don't panic
221  }
222 
223  WindowStyle = WS_POPUP | WS_SYSMENU | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
224  ExWindowStyle = 0;
225 
226  AdjustWindowRectEx(&WindowRect, WindowStyle, FALSE, 0);
227 
228  SetWindowLong(sww_state.hWnd, GWL_STYLE, WindowStyle | WS_VISIBLE);
229  SetWindowLong(sww_state.hWnd, GWL_EXSTYLE, ExWindowStyle);
230  }
231 
232  /*
233  ** clear the DIB memory buffer
234  */
235  memset(sww_state.pDIBBase, 0x00, vid.width * vid.height); //qb: do black, was 0xff
236 
237  if ( ( sww_state.hdcDIBSection = CreateCompatibleDC( sww_state.hDC ) ) == NULL )
238  {
239  ri.Con_Printf( PRINT_ALL, "DIB_Init() - CreateCompatibleDC failed\n" );
240  goto fail;
241  }
243  {
244  ri.Con_Printf( PRINT_ALL, "DIB_Init() - SelectObject failed\n" );
245  goto fail;
246  }
247 
248  return true;
249 
250 fail:
251  DIB_Shutdown();
252  return false;
253 
254 }
255 
256 /*
257 ** DIB_SetPalette
258 **
259 ** Sets the color table in our DIB section, and also sets the system palette
260 ** into an identity mode if we're running in an 8-bit palettized display mode.
261 **
262 ** The palette is expected to be 1024 bytes, in the format:
263 **
264 ** R = offset 0
265 ** G = offset 1
266 ** B = offset 2
267 ** A = offset 3
268 */
269 void DIB_SetPalette( const unsigned char *_pal )
270 {
271  const unsigned char *pal = _pal;
272  LOGPALETTE *pLogPal = ( LOGPALETTE * ) &s_ipal;
273  RGBQUAD colors[256];
274  int i;
275  int ret;
276  HDC hDC = sww_state.hDC;
277 
278  /*
279  ** set the DIB color table
280  */
281  if ( sww_state.hdcDIBSection )
282  {
283  for ( i = 0; i < 256; i++, pal += 4 )
284  {
285  colors[i].rgbRed = pal[0];
286  colors[i].rgbGreen = pal[1];
287  colors[i].rgbBlue = pal[2];
288  colors[i].rgbReserved = 0;
289  }
290 
291  colors[0].rgbRed = 0;
292  colors[0].rgbGreen = 0;
293  colors[0].rgbBlue = 0;
294 
295  colors[255].rgbRed = 0xff;
296  colors[255].rgbGreen = 0xff;
297  colors[255].rgbBlue = 0xff;
298 
299  if ( SetDIBColorTable( sww_state.hdcDIBSection, 0, 256, colors ) == 0 )
300  {
301  ri.Con_Printf( PRINT_ALL, "DIB_SetPalette() - SetDIBColorTable failed\n" );
302  }
303  }
304 
305  /*
306  ** for 8-bit color desktop modes we set up the palette for maximum
307  ** speed by going into an identity palette mode.
308  */
309  if ( sww_state.palettized )
310  {
311  int i;
312  HPALETTE hpalOld;
313 
314  if ( SetSystemPaletteUse( hDC, SYSPAL_NOSTATIC ) == SYSPAL_ERROR )
315  {
316  ri.Sys_Error( ERR_FATAL, "DIB_SetPalette() - SetSystemPaletteUse() failed\n" );
317  }
318 
319  /*
320  ** destroy our old palette
321  */
322  if ( sww_state.hPal )
323  {
324  DeleteObject( sww_state.hPal );
325  sww_state.hPal = 0;
326  }
327 
328  /*
329  ** take up all physical palette entries to flush out anything that's currently
330  ** in the palette
331  */
332  pLogPal->palVersion = 0x300;
333  pLogPal->palNumEntries = 256;
334 
335  for ( i = 0, pal = _pal; i < 256; i++, pal += 4 )
336  {
337  pLogPal->palPalEntry[i].peRed = pal[0];
338  pLogPal->palPalEntry[i].peGreen = pal[1];
339  pLogPal->palPalEntry[i].peBlue = pal[2];
340  pLogPal->palPalEntry[i].peFlags = PC_RESERVED | PC_NOCOLLAPSE;
341  }
342  pLogPal->palPalEntry[0].peRed = 0;
343  pLogPal->palPalEntry[0].peGreen = 0;
344  pLogPal->palPalEntry[0].peBlue = 0;
345  pLogPal->palPalEntry[0].peFlags = 0;
346  pLogPal->palPalEntry[255].peRed = 0xff;
347  pLogPal->palPalEntry[255].peGreen = 0xff;
348  pLogPal->palPalEntry[255].peBlue = 0xff;
349  pLogPal->palPalEntry[255].peFlags = 0;
350 
351  if ( ( sww_state.hPal = CreatePalette( pLogPal ) ) == NULL )
352  {
353  ri.Sys_Error( ERR_FATAL, "DIB_SetPalette() - CreatePalette failed(%x)\n", GetLastError() );
354  }
355 
356  if ( ( hpalOld = SelectPalette( hDC, sww_state.hPal, FALSE ) ) == NULL )
357  {
358  ri.Sys_Error( ERR_FATAL, "DIB_SetPalette() - SelectPalette failed(%x)\n",GetLastError() );
359  }
360 
361  if ( sww_state.hpalOld == NULL )
362  sww_state.hpalOld = hpalOld;
363 
364  if ( ( ret = RealizePalette( hDC ) ) != pLogPal->palNumEntries )
365  {
366  ri.Sys_Error( ERR_FATAL, "DIB_SetPalette() - RealizePalette set %d entries\n", ret );
367  }
368  }
369 }
370 
371 /*
372 ** DIB_Shutdown
373 */
374 void DIB_Shutdown( void )
375 {
378 
379  if ( sww_state.hPal )
380  {
381  DeleteObject( sww_state.hPal );
382  sww_state.hPal = 0;
383  }
384 
385  if ( sww_state.hpalOld )
386  {
387  SelectPalette( sww_state.hDC, sww_state.hpalOld, FALSE );
388  RealizePalette( sww_state.hDC );
390  }
391 
392  if ( sww_state.hdcDIBSection )
393  {
395  DeleteDC( sww_state.hdcDIBSection );
397  }
398 
399  if ( sww_state.hDIBSection )
400  {
401  DeleteObject( sww_state.hDIBSection );
404  }
405 
406  if ( sww_state.hDC )
407  {
408  ReleaseDC( sww_state.hWnd, sww_state.hDC );
409  sww_state.hDC = 0;
410  }
411 }
412 
413 
414 /*
415 ** DIB_Save/RestoreSystemColors
416 */
417 static void DIB_RestoreSystemColors( void )
418 {
419  SetSystemPaletteUse( sww_state.hDC, SYSPAL_STATIC );
421 }
422 
423 static void DIB_SaveSystemColors( void )
424 {
425  int i;
426 
427  for ( i = 0; i < NUM_SYS_COLORS; i++ )
428  s_oldsyscolors[i] = GetSysColor( s_syspalindices[i] );
429 }
vid_fullscreen
cvar_t * vid_fullscreen
Definition: vid_dll.c:47
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
rw_win.h
identitypalette_t::palNumEntries
WORD palNumEntries
Definition: rw_dib.c:74
int
CONST PIXELFORMATDESCRIPTOR int
Definition: qgl_win.c:35
dibinfo_t
struct dibinfo dibinfo_t
s_ipal
static identitypalette_t s_ipal
Definition: rw_dib.c:78
ri
refimport_t ri
Definition: r_main.c:25
qboolean
qboolean
Definition: q_shared.h:63
i
int i
Definition: q_shared.c:305
previously_selected_GDI_obj
static HGDIOBJ previously_selected_GDI_obj
Definition: rw_dib.c:35
swwstate_t::pDIBBase
unsigned char * pDIBBase
Definition: rw_win.h:35
identitypalette_t::palVersion
WORD palVersion
Definition: rw_dib.c:73
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::hPal
HPALETTE hPal
Definition: rw_win.h:37
swwstate_t::palettized
qboolean palettized
Definition: rw_win.h:49
viddef_t::width
unsigned width
Definition: vid.h:29
HDC
HDC(WINAPI *qwglGetCurrentDC)(VOID)
refimport_t::Sys_Error
void(* Sys_Error)(int err_level, char *str,...)
Definition: ref.h:194
viddef_t::height
unsigned height
Definition: vid.h:29
DIB_SaveSystemColors
static void DIB_SaveSystemColors(void)
Definition: rw_dib.c:423
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
DIB_RestoreSystemColors
static void DIB_RestoreSystemColors(void)
Definition: rw_dib.c:417
ERR_FATAL
#define ERR_FATAL
Definition: qcommon.h:743
dibinfo::acolors
RGBQUAD acolors[256]
Definition: rw_dib.c:68
d_8to24table
unsigned d_8to24table[256]
Definition: r_main.c:27
swwstate_t::hpalOld
HPALETTE hpalOld
Definition: rw_win.h:38
swwstate_t::hdcDIBSection
HDC hdcDIBSection
Definition: rw_win.h:33
s_syspalindices
static int s_syspalindices[]
Definition: rw_dib.c:37
DWORD
DWORD
Definition: qgl_win.c:49
dibinfo
Definition: rw_dib.c:65
FALSE
#define FALSE
Definition: stb_vorbis.c:618
dibinfo::header
BITMAPINFOHEADER header
Definition: rw_dib.c:67
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
NUM_SYS_COLORS
#define NUM_SYS_COLORS
Definition: rw_dib.c:61
s_systemcolors_saved
static qboolean s_systemcolors_saved
Definition: rw_dib.c:33
s_oldsyscolors
static int s_oldsyscolors[NUM_SYS_COLORS]
Definition: rw_dib.c:63
swwstate_t::hDIBSection
HBITMAP hDIBSection
Definition: rw_win.h:34
identitypalette_t
Definition: rw_dib.c:71
vid
viddef_t vid
Definition: r_main.c:24