⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rw_ddraw.c

📁 Quake 2 Source code for students by Theerthan You can also download from idsoftwares.com
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
/*
** RW_DDRAW.C
**
** This handles DirecTDraw management under Windows.
*/
#ifndef _WIN32
#  error You should not be compiling this file on this platform
#endif

#include <float.h>

#include "..\ref_soft\r_local.h"
#define INITGUID
#include "rw_win.h"

static const char *DDrawError( int code );

/*
** DDRAW_Init
**
** Builds our DDRAW stuff
*/
qboolean DDRAW_Init( unsigned char **ppbuffer, int *ppitch )
{
	HRESULT ddrval;
	DDSURFACEDESC ddsd;
	DDSCAPS ddscaps;
	PALETTEENTRY palentries[256];
	int i;
	extern cvar_t *sw_allow_modex;

	HRESULT (WINAPI *QDirectDrawCreate)( GUID FAR *lpGUID, LPDIRECTDRAW FAR * lplpDDRAW, IUnknown FAR * pUnkOuter );

ri.Con_Printf( PRINT_ALL, "Initializing DirectDraw\n");


	for ( i = 0; i < 256; i++ )
	{
		palentries[i].peRed		= ( d_8to24table[i] >> 0  ) & 0xff;
		palentries[i].peGreen	= ( d_8to24table[i] >> 8  ) & 0xff;
		palentries[i].peBlue	= ( d_8to24table[i] >> 16 ) & 0xff;
	}

	/*
	** load DLL and fetch pointer to entry point
	*/
	if ( !sww_state.hinstDDRAW )
	{
		ri.Con_Printf( PRINT_ALL, "...loading DDRAW.DLL: ");
		if ( ( sww_state.hinstDDRAW = LoadLibrary( "ddraw.dll" ) ) == NULL )
		{
			ri.Con_Printf( PRINT_ALL, "failed\n" );
			goto fail;
		}
		ri.Con_Printf( PRINT_ALL, "ok\n" );
	}

	if ( ( QDirectDrawCreate = ( HRESULT (WINAPI *)( GUID FAR *, LPDIRECTDRAW FAR *, IUnknown FAR * ) ) GetProcAddress( sww_state.hinstDDRAW, "DirectDrawCreate" ) ) == NULL )
	{
		ri.Con_Printf( PRINT_ALL, "*** DirectDrawCreate == NULL ***\n" );
		goto fail;
	}

	/*
	** create the direct draw object
	*/
	ri.Con_Printf( PRINT_ALL, "...creating DirectDraw object: ");
	if ( ( ddrval = QDirectDrawCreate( NULL, &sww_state.lpDirectDraw, NULL ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n", DDrawError( ddrval ) );
		goto fail;
	}
	ri.Con_Printf( PRINT_ALL, "ok\n" );

	/*
	** see if linear modes exist first
	*/
	sww_state.modex = false;

	ri.Con_Printf( PRINT_ALL, "...setting exclusive mode: ");
	if ( ( ddrval = sww_state.lpDirectDraw->lpVtbl->SetCooperativeLevel( sww_state.lpDirectDraw, 
																		 sww_state.hWnd,
																		 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n",DDrawError (ddrval) );
		goto fail;
	}
	ri.Con_Printf( PRINT_ALL, "ok\n" );

	/*
	** try changing the display mode normally
	*/
	ri.Con_Printf( PRINT_ALL, "...finding display mode\n" );
	ri.Con_Printf( PRINT_ALL, "...setting linear mode: " );
	if ( ( ddrval = sww_state.lpDirectDraw->lpVtbl->SetDisplayMode( sww_state.lpDirectDraw, vid.width, vid.height, 8 ) ) == DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "ok\n" );
	}
	/*
	** if no linear mode found, go for modex if we're trying 320x240
	*/
	else if ( ( sw_mode->value == 0 ) && sw_allow_modex->value )
	{
		ri.Con_Printf( PRINT_ALL, "failed\n" );
		ri.Con_Printf( PRINT_ALL, "...attempting ModeX 320x240: ");

		/*
		** reset to normal cooperative level
		*/
		sww_state.lpDirectDraw->lpVtbl->SetCooperativeLevel( sww_state.lpDirectDraw, 
															 sww_state.hWnd,
															 DDSCL_NORMAL );

		/*															 
		** set exclusive mode
		*/
		if ( ( ddrval = sww_state.lpDirectDraw->lpVtbl->SetCooperativeLevel( sww_state.lpDirectDraw, 
																			 sww_state.hWnd,
																			 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_NOWINDOWCHANGES | DDSCL_ALLOWMODEX ) ) != DD_OK )
		{
			ri.Con_Printf( PRINT_ALL, "failed SCL - %s\n",DDrawError (ddrval) );
			goto fail;
		}

		/*
		** change our display mode
		*/
		if ( ( ddrval = sww_state.lpDirectDraw->lpVtbl->SetDisplayMode( sww_state.lpDirectDraw, vid.width, vid.height, 8 ) ) != DD_OK )
		{
			ri.Con_Printf( PRINT_ALL, "failed SDM - %s\n", DDrawError( ddrval ) );
			goto fail;
		}
		ri.Con_Printf( PRINT_ALL, "ok\n" );

		sww_state.modex = true;
	}
	else
	{
		ri.Con_Printf( PRINT_ALL, "failed\n" );
		goto fail;
	}

	/*
	** create our front buffer
	*/
	memset( &ddsd, 0, sizeof( ddsd ) );
	ddsd.dwSize = sizeof( ddsd );
	ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
	ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
	ddsd.dwBackBufferCount = 1;

	ri.Con_Printf( PRINT_ALL, "...creating front buffer: ");
	if ( ( ddrval = sww_state.lpDirectDraw->lpVtbl->CreateSurface( sww_state.lpDirectDraw, &ddsd, &sww_state.lpddsFrontBuffer, NULL ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n", DDrawError( ddrval ) );
		goto fail;
	}
	ri.Con_Printf( PRINT_ALL, "ok\n" );

	/*
	** see if we're a ModeX mode
	*/
	sww_state.lpddsFrontBuffer->lpVtbl->GetCaps( sww_state.lpddsFrontBuffer, &ddscaps );
	if ( ddscaps.dwCaps & DDSCAPS_MODEX )
		ri.Con_Printf( PRINT_ALL, "...using ModeX\n" );

	/*
	** create our back buffer
	*/
	ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;

	ri.Con_Printf( PRINT_ALL, "...creating back buffer: " );
	if ( ( ddrval = sww_state.lpddsFrontBuffer->lpVtbl->GetAttachedSurface( sww_state.lpddsFrontBuffer, &ddsd.ddsCaps, &sww_state.lpddsBackBuffer ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n", DDrawError( ddrval ) );
		goto fail;
	}
	ri.Con_Printf( PRINT_ALL, "ok\n" );

	/*
	** create our rendering buffer
	*/
	memset( &ddsd, 0, sizeof( ddsd ) );
	ddsd.dwSize = sizeof( ddsd );
	ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
	ddsd.dwHeight = vid.height;
	ddsd.dwWidth = vid.width;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;

	ri.Con_Printf( PRINT_ALL, "...creating offscreen buffer: " );
	if ( ( ddrval = sww_state.lpDirectDraw->lpVtbl->CreateSurface( sww_state.lpDirectDraw, &ddsd, &sww_state.lpddsOffScreenBuffer, NULL ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n", DDrawError( ddrval ) );
		goto fail;
	}
	ri.Con_Printf( PRINT_ALL, "ok\n" );

	/*
	** create our DIRECTDRAWPALETTE
	*/
	ri.Con_Printf( PRINT_ALL, "...creating palette: " );
	if ( ( ddrval = sww_state.lpDirectDraw->lpVtbl->CreatePalette( sww_state.lpDirectDraw,
														DDPCAPS_8BIT | DDPCAPS_ALLOW256,
														palentries,
														&sww_state.lpddpPalette,
														NULL ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n", DDrawError( ddrval ) );
		goto fail;
	}
	ri.Con_Printf( PRINT_ALL, "ok\n" );

	ri.Con_Printf( PRINT_ALL, "...setting palette: " );
	if ( ( ddrval = sww_state.lpddsFrontBuffer->lpVtbl->SetPalette( sww_state.lpddsFrontBuffer,
														 sww_state.lpddpPalette ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n", DDrawError( ddrval ) );
		goto fail;
	}
	ri.Con_Printf( PRINT_ALL, "ok\n" );

	DDRAW_SetPalette( ( const unsigned char * ) sw_state.currentpalette );

	/*
	** lock the back buffer
	*/
	memset( &ddsd, 0, sizeof( ddsd ) );
	ddsd.dwSize = sizeof( ddsd );
	
ri.Con_Printf( PRINT_ALL, "...locking backbuffer: " );
	if ( ( ddrval = sww_state.lpddsOffScreenBuffer->lpVtbl->Lock( sww_state.lpddsOffScreenBuffer, NULL, &ddsd, DDLOCK_WAIT, NULL ) ) != DD_OK )
	{
		ri.Con_Printf( PRINT_ALL, "failed - %s\n", DDrawError( ddrval ) );
		goto fail;
	}
ri.Con_Printf( PRINT_ALL, "ok\n" );

	*ppbuffer = ddsd.lpSurface;
	*ppitch   = ddsd.lPitch;

	for ( i = 0; i < vid.height; i++ )
	{
		memset( *ppbuffer + i * *ppitch, 0, *ppitch );
	}

	sww_state.palettized = true;

	return true;
fail:
	ri.Con_Printf( PRINT_ALL, "*** DDraw init failure ***\n" );

	DDRAW_Shutdown();
	return false;
}

/*
** DDRAW_SetPalette
**
** Sets the color table in our DIB section, and also sets the system palette
** into an identity mode if we're running in an 8-bit palettized display mode.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -