Advertisement

code troubles

Started by January 01, 2003 02:14 AM
6 comments, last by Sabonis 21 years, 10 months ago

  // 

////////////////////////////////////////////

// Chapter 8, Example 1

// Name: Direct3D Intialization

// Author: Peter Walsh

// 1st Dec 2000

//

// 

//


#define WIN32_LEAN_AND_MEAN

// The main windows include file

#include <windows.h>
#include <mmsystem.h>

#include <d3d8.h>
#include <d3dx8.h>

#include "MustangII.h"

int GameInit();
int GameLoop();
int GameShutdown();
int Render();

#define RENDER_GDI 2 //Render with the GDI
#define RENDER_D3D 1 // Render with Direct 3D
#define RENDER_BOTH 0 //Render with both


int g_RenderMode = 0;
LPDIRECT3D8 g_pD3D = 0;
LPDIRECT3DDEVICE8 g_pDevice = 0;
LPDIRECT3DSURFACE8 g_pBackSurface = 0;
LPDIRECT3DSURFACE8 g_pD3DBitmapSurf = 0;
HBITMAP g_hGDIBitmap = 0;
HDC g_hGDIBitmapDC = 0;
HWND g_hWndMain;

// The window procedure to handle events

long CALLBACK WndProc( HWND hWnd, UINT uMessage, 
                                WPARAM wParam, LPARAM lParam )
{

	// Switch the windows message to figure out what it is

	switch( uMessage )
	{
		case WM_CREATE:	// The CreateWindow() function was just called

		{
			// One Time Initialization

			return 0;
		}

		case WM_PAINT:	// The window needs to be redrawn

		{
			ValidateRect( hWnd, NULL );

			return 0;
		}
		
		case WM_DESTROY:	// The window is about to be closed

		{
			// Our main window is closing.  This means we want our app to exit.

			// Tell windows to put a WM_QUIT message in our message queue

			PostQuitMessage( 0 );
			return 0;
		}
		case WM_KEYDOWN:
			{
				switch( wParam )
				{
				case VK_SPACE:
					{
					g_RenderMode++;
					if( g_RenderMode > 2)
						g_RenderMode = 0;
					break;
					}
				default:
					break;
				}
			return 0 ;
	
			}
		default:			// Some other message

		{
			// Let windows handle this message

			return DefWindowProc( hWnd, uMessage, wParam, lParam );
		}
	}
}

// The windows entry point. The application will start executing here

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                     PSTR pstrCmdLine, int iCmdShow )
{
	HWND hWnd;		// The handle to our main window

	MSG msg;		// The message windows is sending us

	WNDCLASSEX wc;	// The window class used to create our window


	// The name of our class and also the title to our window

	static char strAppName[] = "Direct3D Initialization FullScreen";

	// Fill in the window class with the attributes for our main window


	// The size of this struture in bytes

	wc.cbSize			= sizeof( WNDCLASSEX );
	
	//  The style of the window.

	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	// Useless information.  Just set to zero.

	wc.cbClsExtra		= 0;
	// Useless information.  Just set to zero.

	wc.cbWndExtra		= 0;
	// The name of our event handler

	wc.lpfnWndProc		= WndProc;
	// A handle to the applications instance

	wc.hInstance		= hInstance;
	// The handle to the brush to use for the window background

	wc.hbrBackground	= (HBRUSH)GetStockObject( DKGRAY_BRUSH );
	// A handle to the icon to use for the window

	wc.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
	// A handle to a smaller version of the apps icon

	wc.hIconSm			= LoadIcon( NULL, IDI_APPLICATION );
	// A handle to the cursor to use while the mouse is over our window

	wc.hCursor			= LoadCursor( NULL, IDC_CROSS );
	// A handle to the resource to use as our menu

	wc.lpszMenuName		= NULL;
	// The human readable name for this class

	wc.lpszClassName	= strAppName;

	// Register the class with windows

	RegisterClassEx( &wc );

	// Create the window based on the previous class

	hWnd = CreateWindowEx( WS_EX_TOPMOST,	// Advanced style settings

	            strAppName,			// The name of the class

				strAppName,			// The window caption

				WS_OVERLAPPEDWINDOW,// The window style

			   	CW_USEDEFAULT,		// The initial x position 

                CW_USEDEFAULT,		// The initial y position

			   	512, 512,			// The intiial width / height

				NULL,				// Handle to parent window						

				NULL,				// Handle to the menu

				hInstance,			// Handle to the apps instance

				NULL );				// Advanced context


	g_hWndMain = hWnd;

	// Display the window we just created

	ShowWindow( hWnd, iCmdShow );
	// Draw the window contents for the first time

	UpdateWindow( hWnd );

	if( FAILED( GameInit() ) )
	{
		SetError( "Initialization Failed" );
		GameShutdown();
		return E_FAIL;
	}

	// Start the message loop

	while( TRUE )
	{
		// Check if a message is waiting for processing

		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			// Check if the message is to quit the application

			if( msg.message == WM_QUIT )
				// Exit the message loop

				break;
			
			// Change the format of certain messages

			TranslateMessage( &msg );
			// Pass the message to WndProc() for processing

			DispatchMessage( &msg );
		}
		else
		{
			GameLoop();
		}
	}
	
	GameShutdown();

	// Return control to windows with the exit code

	return msg.wParam;
}


int GameInit()
{
	
	HRESULT r = 0;	// Holds return values


	
	// Acquire a pointer to IDirect3D8

	g_pD3D = Direct3DCreate8( D3D_SDK_VERSION );
	if( g_pD3D == NULL )
	{
		SetError( "Could not create IDirect3D8 object" );
		return E_FAIL;
	}
	//Create the device


	r = InitDirect3DDevice( g_hWndMain, 640, 480, FALSE, D3DFMT_X8R8G8B8, g_pD3D, &g_pDevice );
	
	if(FAILED(r))
	{
		SetError("Initialization of the device failed");
	return E_FAIL;
	}

	g_pDevice->Clear(0,0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f,0);

	//----------GDI CODE-------------

	//Create a Direct3D-compatible DC to hold the GDI bitmap

	g_hGDIBitmapDC = CreateD3DCompatibleDC(128, 128, &g_hGDIBitmap);
	//Create a temporary DC to hold the bitmap as it is loaded off disk

	HDC hTempDC = CreateCompatibleDC(0);
	//Load the image from disk

	HBITMAP hTempBitmap = (HBITMAP)LoadImage(0,"CGMD3D.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	// Select the image into the temp DC

	SelectObject(hTempDC,hTempBitmap);
	//Copy the image to the Direct 3D compatible DC

	BitBlt(g_hGDIBitmapDC, 0, 0, 128, 128, hTempDC, 0, 0, SRCCOPY);
	//Delete the temporary bitmap

	DeleteObject(hTempBitmap);
	//Delete the temporary device context

	DeleteDC(hTempDC);

	//---------DIRECT3D CODE---------

	//Get a pointer the the back buffer and save it in a global variable

	r = g_pDevice->GetBackBuffer( 0 , D3DBACKBUFFER_TYPE_MONO, &g_pBackSurface);
	if(FAILED(r))
	{
		SetError("Could not get back buffer");
		return E_FAIL;
	}
	//create a surface to hold the Direct3D Bitmap

	g_pDevice->CreateImageSurface(128, 128, D3DFMT_X8R8G8B8, &g_pD3DBitmapSurf);
	//Load the image from the disk to the surface

	D3DXLoadSurfaceFromFile( g_pD3DBitmapSurf, NULL, NULL, "CGM.BMP", NULL, D3DX_FILTER_NONE, 0, NULL);

	srand(GetTickCount());
	
	return S_OK;
}


int GameLoop()
{
	Render();

	if( GetAsyncKeyState( VK_ESCAPE ) )
		PostQuitMessage(0);
	
	return S_OK;
}

LPDIRECT3DSURFACE8 g_pBackSurf = 0;

int Render()
{
	HRESULT r = 0;
	if(!g_pDevice)
	{
		SetError("Cannot render because there is no device");
		return E_FAIL;
	}
	//return if the device is not ready;

	r = ValidateDevice();
	if(FAILED(r))
	{
		return E_FAIL;
	}

	//Source size of the D3D Image

	RECT D3DSrcRect = { 0,0,128,128};
	//Destination point for the D3DImage

	POINT D3DDestPoint = { rand() % (g_DeviceWidth - 128), rand() % (g_DeviceHeight - 128)};
	//Destination point for the GDI image

	POINT GDIDestPoint = {rand() % (g_DeviceWidth - 128), rand() % (g_DeviceHeight - 128)};
	//copy the GDI device context if that render mode is active

	if( g_RenderMode == RENDER_GDI || g_RenderMode == RENDER_BOTH)
		CopyDCToSurface(g_pBackSurface, &GDIDestPoint, g_hGDIBitmapDC, g_hGDIBitmap, 0, -1);
	//copy the D3D surface if that render mode is active

	if(g_RenderMode == RENDER_D3D || g_RenderMode == RENDER_BOTH)
		g_pDevice->CopyRects( g_pD3DBitmapSurf, &D3DSrcRect, 1, g_pBackSurface, &D3DDestPoint);
	//Present the back buffer to the primary surface


	g_pDevice->Present( NULL, NULL, NULL, NULL);
	return S_OK;
}



int GameShutdown()
{
	//Delete the DC and bitmap for the GDI image

	DeleteD3DCompatibleDC(g_hGDIBitmapDC, g_hGDIBitmap);
	//Release the pointer back to the surface

	if( g_pBackSurf)
		g_pBackSurface->Release();
	//Release the surface holding the D3D image

	if(g_pD3DBitmapSurf)
		g_pD3DBitmapSurf->Release();

	// Release the pointer to IDirect3DDevice8

	if( g_pDevice )
		g_pDevice->Release();
	
	// Release the pointer to IDirect3D8

	if( g_pD3D )
		g_pD3D->Release();

	return S_OK;
}

</source>
this is the code in my source file
<source> // ZenEngine.h

// Engine code for Zen of Direct3D Game Programming

// (C) Peter Walsh 2000, 2001





D3DPRESENT_PARAMETERS g_SavedPresParams;

int g_DeviceWidth = 0;
int g_DeviceHeight = 0;

// Output an error to the debug window

void SetError( char* String )
{
	OutputDebugString( "ERROR: " );
	OutputDebugString( String );
	OutputDebugString( "\n" );
}

/**************************************************************
***************************************************************

				Direct3D Initialization Code

***************************************************************
**************************************************************/

// Initializes the Direct3D device

int InitDirect3DDevice( HWND hWndTarget, int Width, int Height, BOOL bWindowed, 
							D3DFORMAT FullScreenFormat, LPDIRECT3D8 pD3D, 
							LPDIRECT3DDEVICE8* ppDevice )
{
	// Structure to hold information about the rendering method

	D3DPRESENT_PARAMETERS d3dpp;
	// Structure to hold information about the current display mode

	D3DDISPLAYMODE d3ddm;

	HRESULT r = 0;
	
	if( *ppDevice )
		(*ppDevice)->Release();

	// Initialize the structure to 0

	ZeroMemory( &d3dpp, sizeof( D3DPRESENT_PARAMETERS ) );

	// Get the settings for the current display mode

	r = pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );
	if( FAILED( r ) )
	{
		SetError( "Could not get display adapter information" );
		return E_FAIL;
	}
	
	// The width of the back buffer in pixels

	d3dpp.BackBufferWidth = Width;
	// The height of the buffer in pixels

	d3dpp.BackBufferHeight = Height;
	// The format of the back buffer

	d3dpp.BackBufferFormat = bWindowed ? d3ddm.Format : FullScreenFormat;

	// The number of back buffers

	d3dpp.BackBufferCount = 1;

	// The type of multisampling

	d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
	// The swap effect

	d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
	
	// The handle to the window that we want to render to 

	d3dpp.hDeviceWindow = hWndTarget;	
	// Windowed or fullscreen

	d3dpp.Windowed = bWindowed;

	// Let Direct3D manage the depth buffer

	d3dpp.EnableAutoDepthStencil = TRUE;
	// Set the depth buffer format to 16 bits

	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	
	// Use the default refresh rate available

	d3dpp.FullScreen_RefreshRateInHz = 0;
	
	// Present the information as fast as possible

	d3dpp.FullScreen_PresentationInterval = bWindowed ? 0 : D3DPRESENT_INTERVAL_IMMEDIATE;
	// Allow the back buffer to be accessed for 2D work

	d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;

	// Acquire a pointer to IDirect3DDevice8

	r = pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWndTarget,
							D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
							&d3dpp, ppDevice );
	if( FAILED( r ) )

	{
		SetError( "Could not create the render device" );
		return E_FAIL;
	}

	// Save global copies of the device dimensions

	g_DeviceHeight = Height;
	g_DeviceWidth = Width;

	// Save a copy of the pres params for use in device validation later

	g_SavedPresParams = d3dpp;

	return S_OK;
}




//******************************************************************************

//****************************2D RENDERING CODE*********************************

//******************************************************************************




//Set a single pixel to a color


void SetPixel32( int x, int y, DWORD Color, int Pitch, DWORD* pData)
{
	if( x > g_DeviceWidth || x < 0)
		return;
	if(y > g_DeviceHeight || y < 0)
		return;

	pData[ ((Pitch/4) * y) + x ] = Color;
}

//Old slow way of drawing a rectangle

void Rectangle32( D3DRECT* pRect, DWORD Color, int Pitch, DWORD* pData )
{
	int y1 = pRect->y1;
	int y2 = pRect->y2;
	int x1 = pRect->x1;
	int x2 = pRect->x2;

	int Pitch4 = Pitch / 4;
	
	DWORD Offset = y1 * Pitch4 + x1;

	for( int y = y1 ; y < y2 ; y++ ) 
	{
		for( int x = x1 ; x < x2 ; x++ )
		{
			pData[ Offset + x ] = Color;			
		}

		Offset += Pitch4;
	}

}
//New good fast way to draw a rectangle

void Rectangle32Fast( D3DRECT* pRect, DWORD Color, LPDIRECT3DDEVICE8 pDevice)
{

	pDevice->Clear(1,pRect,D3DCLEAR_TARGET, Color, 0.0f, 0);

}
//Loading a bitmap to the primary surface

int LoadBitmapToSurface( char* PathName, LPDIRECT3DSURFACE8* ppSurface, LPDIRECT3DDEVICE8 pDevice)
{
	HRESULT r;
	HBITMAP hBitmap;
	BITMAP Bitmap;

	hBitmap = (HBITMAP)LoadImage(NULL, PathName, IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);

	if(hBitmap == NULL)
	{
		SetError("Unable to load Bitmap");
		return E_FAIL;
	}

	GetObject( hBitmap, sizeof(BITMAP),&Bitmap);
	DeleteObject(hBitmap);

	r= pDevice->CreateImageSurface(Bitmap.bmWidth, Bitmap.bmHeight, D3DFMT_X8R8G8B8, ppSurface);

	if(FAILED(r))
	{
		SetError("Unable to create Surface for bitmap to load");
		return E_FAIL;
	}

	r= D3DXLoadSurfaceFromFile(*ppSurface, NULL, NULL, PathName, NULL, D3DX_FILTER_NONE, 0, NULL);

	if(FAILED(r))
	{
		SetError("Unable to load the file to surface");
		return E_FAIL;
	}


	return S_OK;
}


//Create a compatible Device context from GDI for use with DX surfaces


HDC CreateD3DCompatibleDC( int Width, int Height, HBITMAP* phDibSection)
{

	//Create a new DC that is compatible with the current display mode

	HDC hDC = CreateCompatibleDC(0);
	//Temp pointer to the dib data

	void* pDibSection = 0;

	//Structure to hold information about the bitmap

	BITMAPINFO bi;
	ZeroMemory(&bi, sizeof(BITMAPINFO));
	//Size of the stucture in bytes

	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	//the width of the new bitmap

	bi.bmiHeader.biWidth = Width;
	//the height of the new bitmap ( negative for topdown image )

	bi.bmiHeader.biHeight = -Height;
	//Obsolete  - -Just set to 1

	bi.bmiHeader.biPlanes = 1;
	// the bit depth of the surface

	bi.bmiHeader.biBitCount = 32;
	//the compression format. BI_RGB indicates none

	bi.bmiHeader.biCompression = BI_RGB;


	//Create a new DIB

	HBITMAP hDibSection = CreateDIBSection( hDC, &bi, DIB_RGB_COLORS, &pDibSection, NULL, NULL);

	//Select the bitmap into the new DC

	SelectObject(hDC, hDibSection);

	//Update the pointer to the handle to the bitmap

	*phDibSection = hDibSection;

	//Return the handle to the device context

	return hDC;
}

//Delete the bitmap from memory


void DeleteD3DCompatibleDC( HDC hDC, HBITMAP hDibSection)
{

	// Delete the bitmap

	DeleteObject( hDibSection);
	//Delete the DC

	DeleteDC(hDC);
}

HRESULT CopyDCToSurface( LPDIRECT3DSURFACE8 pDestSurf, POINT* pDestPoint, HDC hDCSource, HBITMAP hDibSection, RECT* pSrcRect, COLORREF ColorKey)
{

	HRESULT r = 0;
	//The source rectangle

	RECT SourceRect;
	//The destination origin point

	POINT DestPoint;

	//Holds Information about the bitmap

	DIBSECTION DibSection;

	//Holds information abou the surface when locked

	D3DLOCKED_RECT LockedRect;
	//Get information about the bitmap in the DC

	GetObject( hDibSection, sizeof(DIBSECTION), &DibSection);
	int SrcTotalWidth = DibSection.dsBm.bmWidth;
	int SrcTotalHeight = DibSection.dsBm.bmHeight;


	//if no source rectangle was specified then this indicates that the entire map in the DC

	//is to be copied

	if( !pSrcRect )
		SetRect( &SourceRect, 0, 0, SrcTotalWidth, SrcTotalHeight);
	else
		SourceRect = *(pSrcRect);
	//if no destination point was specified then the origin is set to (0,0)

	if( !pDestPoint )
		DestPoint.x = DestPoint.y = 0;
		else
		DestPoint = *(pDestPoint);
	//return failure if a valid destination surface was not specified

	if( !pDestSurf )
		return E_FAIL;
	//return failure if a valid DC source was not specified

	if( !hDCSource )
		return E_FAIL;
	//Lock the source surface

	r = pDestSurf->LockRect(&LockedRect, 0, 0);
	if(FAILED(r))
	{
		SetError("Unable to lock the surface for GDI transfer");
		return E_FAIL;
	}
	//Conver the source and destination data pointers to DWORD(32 bit) Values

	DWORD* pSrcData = (DWORD*)(DibSection.dsBm.bmBits);
	DWORD* pDestData = (DWORD*)(LockedRect.pBits);
	// Convert the pitch to a 32-bit value

	int Pitch32 = LockedRect.Pitch/4;

	//Compute the dimensions for the copy

	int SrcHeight = SourceRect.bottom - SourceRect.top;
	int SrcWidth = SourceRect.right - SourceRect.left;
	// compute the index into memory

	DWORD SrcOffset = SourceRect.top * SrcTotalWidth + SourceRect.left;
	DWORD DestOffset = DestPoint.y * Pitch32 + DestPoint.x;
	//If not using a color key then a faster copy can be done

	if( ColorKey == -1 )
	{
		//Loop for each row in the image

		for( int y= 0 ; y < SrcHeight; y++)
		{
			//Copy this line of the image

			memcpy( (void*)&(pDestData[ DestOffset ] ), (void*)&(pSrcData [ SrcOffset ] ), SrcWidth*4);
			//increase the destination pointer by the pitch

			DestOffset+=Pitch32;
			//incerase the source pointer by the total width

			SrcOffset +=SrcTotalWidth;
		}
	}
	else //a color key was specified

	{
		//Loop for each row in the image

		for( int y = 0; y < SrcHeight; y++)
		{
			//Loop for each column

			for(int x = 0; x < SrcWidth; x++)
			{
				//If the source pixel is not the same as the color key

				if( pSrcData[SrcOffset] != ColorKey)
					//then copy the pixel to the destination

					pDestData[DestOffset] = pSrcData[SrcOffset];
				//Move to the next pixel in the source


				SrcOffset++;
				//Move to the next pixel in the destination

				DestOffset++;
			}
		}
	}
	//Unlock the surface

	pDestSurf->UnlockRect();
	//return success

	return S_OK;
}

//Check for a valid device

HRESULT ValidateDevice()
{
	HRESULT r = 0;
	//test the current state of the device

	r = g_pDevice->TestCooperativeLevel();
	if(FAILED(r))
	{

		//if the device is lost then return failure

		if(r == D3DERR_DEVICELOST )
			return E_FAIL;
		//if the device is ready to be reset then attempt to do so

		if(r==D3DERR_DEVICENOTRESET)
		{
			g_pBackSurface->Release();
			//reset the device

			r = g_pDevice->Reset( &g_SavedPresParams);
			if(FAILED(r))
			{
				//if the device was not reset then exit

				SetError("Could not reset device");
				return E_FAIL;
			}
			//Reaquire a pointer to the new back buffer

			r = g_pDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &g_pBackSurface);
			if(FAILED(r))
			{
				SetError("Unable to reaquire the back buffer");
				PostQuitMessage(0);
				return E_FAIL;
			}

			g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 0.0f,0);
			RestoreGraphics();
		}
	}

 return S_OK;
}

HRESULT RestoreGraphics()
{
	return S_OK;
}

   
This is the code in my header file it gives me 18 errors, all having something to do with ->Clear(), ->Release() type functions that say they must have a pointer to a class structure or something... major newb here, so pls help thanks "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true... [edited by - Run_The_Shadows on January 1, 2003 5:26:13 AM]
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Exact error messages? Line numbers? Actual relevant code? Reasonably formatted code? A compilable project? Anything?

Help us help you. Unless someone has had this problem or has the book CD/source handy, you''re probably not gonna get much. ''Course there may be some poor sadist who decides to wade through that..

Best of luck,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

Advertisement
It compiles. Get it here and have fun.

Later,
ZE.

P.S. Just ask if you don''t understand what I did, and in the future, freakin'' make sure things are declared before you use them.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

this is in my cpp file

   // ////////////////////////////////////////////// Chapter 8, Example 1// Name: Direct3D Intialization// Author: Peter Walsh// 1st Dec 2000//// //#define WIN32_LEAN_AND_MEAN// The main windows include file#include <windows.h>#include <mmsystem.h>#include <d3d8.h>#include <d3dx8.h>#include "MustangII.h"int GameInit();int GameLoop();int GameShutdown();int Render();#define RENDER_GDI 2 //Render with the GDI#define RENDER_D3D 1 // Render with Direct 3D#define RENDER_BOTH 0 //Render with bothextern int g_RenderMode = 0;extern LPDIRECT3D8 g_pD3D = 0;extern LPDIRECT3DDEVICE8 g_pDevice = 0;extern LPDIRECT3DSURFACE8 g_pBackSurface = 0;extern LPDIRECT3DSURFACE8 g_pD3DBitmapSurf = 0;extern HBITMAP g_hGDIBitmap = 0;extern HDC g_hGDIBitmapDC = 0;extern HWND g_hWndMain;// The window procedure to handle eventslong CALLBACK WndProc( HWND hWnd, UINT uMessage,                                 WPARAM wParam, LPARAM lParam ){	// Switch the windows message to figure out what it is	switch( uMessage )	{		case WM_CREATE:	// The CreateWindow() function was just called		{			// One Time Initialization			return 0;		}		case WM_PAINT:	// The window needs to be redrawn		{			ValidateRect( hWnd, NULL );			return 0;		}				case WM_DESTROY:	// The window is about to be closed		{			// Our main window is closing.  This means we want our app to exit.			// Tell windows to put a WM_QUIT message in our message queue			PostQuitMessage( 0 );			return 0;		}		case WM_KEYDOWN:			{				switch( wParam )				{				case VK_SPACE:					{					g_RenderMode++;					if( g_RenderMode > 2)						g_RenderMode = 0;					break;					}				default:					break;				}			return 0 ;				}		default:			// Some other message		{			// Let windows handle this message			return DefWindowProc( hWnd, uMessage, wParam, lParam );		}	}}// The windows entry point. The application will start executing hereint WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,                                     PSTR pstrCmdLine, int iCmdShow ){	HWND hWnd;		// The handle to our main window	MSG msg;		// The message windows is sending us	WNDCLASSEX wc;	// The window class used to create our window	// The name of our class and also the title to our window	static char strAppName[] = "Direct3D Initialization FullScreen";	// Fill in the window class with the attributes for our main window	// The size of this struture in bytes	wc.cbSize			= sizeof( WNDCLASSEX );		//  The style of the window.	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Useless information.  Just set to zero.	wc.cbClsExtra		= 0;	// Useless information.  Just set to zero.	wc.cbWndExtra		= 0;	// The name of our event handler	wc.lpfnWndProc		= WndProc;	// A handle to the applications instance	wc.hInstance		= hInstance;	// The handle to the brush to use for the window background	wc.hbrBackground	= (HBRUSH)GetStockObject( DKGRAY_BRUSH );	// A handle to the icon to use for the window	wc.hIcon			= LoadIcon( NULL, IDI_APPLICATION );	// A handle to a smaller version of the apps icon	wc.hIconSm			= LoadIcon( NULL, IDI_APPLICATION );	// A handle to the cursor to use while the mouse is over our window	wc.hCursor			= LoadCursor( NULL, IDC_CROSS );	// A handle to the resource to use as our menu	wc.lpszMenuName		= NULL;	// The human readable name for this class	wc.lpszClassName	= strAppName;	// Register the class with windows	RegisterClassEx( &wc );	// Create the window based on the previous class	hWnd = CreateWindowEx( WS_EX_TOPMOST,	// Advanced style settings	            strAppName,			// The name of the class				strAppName,			// The window caption				WS_OVERLAPPEDWINDOW,// The window style			   	CW_USEDEFAULT,		// The initial x position                 CW_USEDEFAULT,		// The initial y position			   	512, 512,			// The intiial width / height				NULL,				// Handle to parent window										NULL,				// Handle to the menu				hInstance,			// Handle to the apps instance				NULL );				// Advanced context	g_hWndMain = hWnd;	// Display the window we just created	ShowWindow( hWnd, iCmdShow );	// Draw the window contents for the first time	UpdateWindow( hWnd );	if( FAILED( GameInit() ) )	{		SetError( "Initialization Failed" );		GameShutdown();		return E_FAIL;	}	// Start the message loop	while( TRUE )	{		// Check if a message is waiting for processing		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )		{			// Check if the message is to quit the application			if( msg.message == WM_QUIT )				// Exit the message loop				break;						// Change the format of certain messages			TranslateMessage( &msg );			// Pass the message to WndProc() for processing			DispatchMessage( &msg );		}		else		{			GameLoop();		}	}		GameShutdown();	// Return control to windows with the exit code	return msg.wParam;}int GameInit(){		HRESULT r = 0;	// Holds return values		// Acquire a pointer to IDirect3D8	g_pD3D = Direct3DCreate8( D3D_SDK_VERSION );	if( g_pD3D == NULL )	{		SetError( "Could not create IDirect3D8 object" );		return E_FAIL;	}	//Create the device	r = InitDirect3DDevice( g_hWndMain, 640, 480, FALSE, D3DFMT_X8R8G8B8, g_pD3D, &g_pDevice );		if(FAILED(r))	{		SetError("Initialization of the device failed");	return E_FAIL;	}	g_pDevice->Clear(0,0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f,0);	//----------GDI CODE-------------	//Create a Direct3D-compatible DC to hold the GDI bitmap	g_hGDIBitmapDC = CreateD3DCompatibleDC(128, 128, &g_hGDIBitmap);	//Create a temporary DC to hold the bitmap as it is loaded off disk	HDC hTempDC = CreateCompatibleDC(0);	//Load the image from disk	HBITMAP hTempBitmap = (HBITMAP)LoadImage(0,"CGMD3D.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);	// Select the image into the temp DC	SelectObject(hTempDC,hTempBitmap);	//Copy the image to the Direct 3D compatible DC	BitBlt(g_hGDIBitmapDC, 0, 0, 128, 128, hTempDC, 0, 0, SRCCOPY);	//Delete the temporary bitmap	DeleteObject(hTempBitmap);	//Delete the temporary device context	DeleteDC(hTempDC);	//---------DIRECT3D CODE---------	//Get a pointer the the back buffer and save it in a global variable	r = g_pDevice->GetBackBuffer( 0 , D3DBACKBUFFER_TYPE_MONO, &g_pBackSurface);	if(FAILED(r))	{		SetError("Could not get back buffer");		return E_FAIL;	}	//create a surface to hold the Direct3D Bitmap	g_pDevice->CreateImageSurface(128, 128, D3DFMT_X8R8G8B8, &g_pD3DBitmapSurf);	//Load the image from the disk to the surface	D3DXLoadSurfaceFromFile( g_pD3DBitmapSurf, NULL, NULL, "CGM.BMP", NULL, D3DX_FILTER_NONE, 0, NULL);	srand(GetTickCount());		return S_OK;}int GameLoop(){	Render();	if( GetAsyncKeyState( VK_ESCAPE ) )		PostQuitMessage(0);		return S_OK;}LPDIRECT3DSURFACE8 g_pBackSurf = 0;int Render(){	HRESULT r = 0;	if(!g_pDevice)	{		SetError("Cannot render because there is no device");		return E_FAIL;	}	//return if the device is not ready;	r = ValidateDevice();	if(FAILED(r))	{		return E_FAIL;	}	//Source size of the D3D Image	RECT D3DSrcRect = { 0,0,128,128};	//Destination point for the D3DImage	POINT D3DDestPoint = { rand() % (g_DeviceWidth - 128), rand() % (g_DeviceHeight - 128)};	//Destination point for the GDI image	POINT GDIDestPoint = {rand() % (g_DeviceWidth - 128), rand() % (g_DeviceHeight - 128)};	//copy the GDI device context if that render mode is active	if( g_RenderMode == RENDER_GDI || g_RenderMode == RENDER_BOTH)		CopyDCToSurface(g_pBackSurface, &GDIDestPoint, g_hGDIBitmapDC, g_hGDIBitmap, 0, -1);	//copy the D3D surface if that render mode is active	if(g_RenderMode == RENDER_D3D || g_RenderMode == RENDER_BOTH)		g_pDevice->CopyRects( g_pD3DBitmapSurf, &D3DSrcRect, 1, g_pBackSurface, &D3DDestPoint);	//Present the back buffer to the primary surface	g_pDevice->Present( NULL, NULL, NULL, NULL);	return S_OK;}int GameShutdown(){	//Delete the DC and bitmap for the GDI image	DeleteD3DCompatibleDC(g_hGDIBitmapDC, g_hGDIBitmap);	//Release the pointer back to the surface	if( g_pBackSurf)		g_pBackSurface->Release();	//Release the surface holding the D3D image	if(g_pD3DBitmapSurf)		g_pD3DBitmapSurf->Release();	// Release the pointer to IDirect3DDevice8	if( g_pDevice )		g_pDevice->Release();		// Release the pointer to IDirect3D8	if( g_pD3D )		g_pD3D->Release();	return S_OK;}  

this is whats in my .h file

  // ZenEngine.h// Engine code for Zen of Direct3D Game Programming// (C) Peter Walsh 2000, 2001D3DPRESENT_PARAMETERS g_SavedPresParams;int g_DeviceWidth = 0;int g_DeviceHeight = 0;// Output an error to the debug windowvoid SetError( char* String ){	OutputDebugString( "ERROR: " );	OutputDebugString( String );	OutputDebugString( "\n" );}/*****************************************************************************************************************************				Direct3D Initialization Code*****************************************************************************************************************************/// Initializes the Direct3D deviceint InitDirect3DDevice( HWND hWndTarget, int Width, int Height, BOOL bWindowed, 							D3DFORMAT FullScreenFormat, LPDIRECT3D8 pD3D, 							LPDIRECT3DDEVICE8* ppDevice ){	// Structure to hold information about the rendering method	D3DPRESENT_PARAMETERS d3dpp;	// Structure to hold information about the current display mode	D3DDISPLAYMODE d3ddm;	HRESULT r = 0;		if( *ppDevice )		(*ppDevice)->Release();	// Initialize the structure to 0	ZeroMemory( &d3dpp, sizeof( D3DPRESENT_PARAMETERS ) );	// Get the settings for the current display mode	r = pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );	if( FAILED( r ) )	{		SetError( "Could not get display adapter information" );		return E_FAIL;	}		// The width of the back buffer in pixels	d3dpp.BackBufferWidth = Width;	// The height of the buffer in pixels	d3dpp.BackBufferHeight = Height;	// The format of the back buffer	d3dpp.BackBufferFormat = bWindowed ? d3ddm.Format : FullScreenFormat;	// The number of back buffers	d3dpp.BackBufferCount = 1;	// The type of multisampling	d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;	// The swap effect	d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;		// The handle to the window that we want to render to 	d3dpp.hDeviceWindow = hWndTarget;		// Windowed or fullscreen	d3dpp.Windowed = bWindowed;	// Let Direct3D manage the depth buffer	d3dpp.EnableAutoDepthStencil = TRUE;	// Set the depth buffer format to 16 bits	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;		// Use the default refresh rate available	d3dpp.FullScreen_RefreshRateInHz = 0;		// Present the information as fast as possible	d3dpp.FullScreen_PresentationInterval = bWindowed ? 0 : D3DPRESENT_INTERVAL_IMMEDIATE;	// Allow the back buffer to be accessed for 2D work	d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;	// Acquire a pointer to IDirect3DDevice8	r = pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWndTarget,							D3DCREATE_SOFTWARE_VERTEXPROCESSING, 							&d3dpp, ppDevice );	if( FAILED( r ) )	{		SetError( "Could not create the render device" );		return E_FAIL;	}	// Save global copies of the device dimensions	g_DeviceHeight = Height;	g_DeviceWidth = Width;	// Save a copy of the pres params for use in device validation later	g_SavedPresParams = d3dpp;	return S_OK;}//******************************************************************************//****************************2D RENDERING CODE*********************************//******************************************************************************//Set a single pixel to a colorvoid SetPixel32( int x, int y, DWORD Color, int Pitch, DWORD* pData){	if( x > g_DeviceWidth || x < 0)		return;	if(y > g_DeviceHeight || y < 0)		return;	pData[ ((Pitch/4) * y) + x ] = Color;}//Old slow way of drawing a rectanglevoid Rectangle32( D3DRECT* pRect, DWORD Color, int Pitch, DWORD* pData ){	int y1 = pRect->y1;	int y2 = pRect->y2;	int x1 = pRect->x1;	int x2 = pRect->x2;	int Pitch4 = Pitch / 4;		DWORD Offset = y1 * Pitch4 + x1;	for( int y = y1 ; y < y2 ; y++ ) 	{		for( int x = x1 ; x < x2 ; x++ )		{			pData[ Offset + x ] = Color;					}		Offset += Pitch4;	}}//New good fast way to draw a rectanglevoid Rectangle32Fast( D3DRECT* pRect, DWORD Color, LPDIRECT3DDEVICE8 pDevice){	pDevice->Clear(1,pRect,D3DCLEAR_TARGET, Color, 0.0f, 0);}//Loading a bitmap to the primary surfaceint LoadBitmapToSurface( char* PathName, LPDIRECT3DSURFACE8* ppSurface, LPDIRECT3DDEVICE8 pDevice){	HRESULT r;	HBITMAP hBitmap;	BITMAP Bitmap;	hBitmap = (HBITMAP)LoadImage(NULL, PathName, IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);	if(hBitmap == NULL)	{		SetError("Unable to load Bitmap");		return E_FAIL;	}	GetObject( hBitmap, sizeof(BITMAP),&Bitmap);	DeleteObject(hBitmap);	r= pDevice->CreateImageSurface(Bitmap.bmWidth, Bitmap.bmHeight, D3DFMT_X8R8G8B8, ppSurface);	if(FAILED(r))	{		SetError("Unable to create Surface for bitmap to load");		return E_FAIL;	}	r= D3DXLoadSurfaceFromFile(*ppSurface, NULL, NULL, PathName, NULL, D3DX_FILTER_NONE, 0, NULL);	if(FAILED(r))	{		SetError("Unable to load the file to surface");		return E_FAIL;	}	return S_OK;}//Create a compatible Device context from GDI for use with DX surfacesHDC CreateD3DCompatibleDC( int Width, int Height, HBITMAP* phDibSection){	//Create a new DC that is compatible with the current display mode	HDC hDC = CreateCompatibleDC(0);	//Temp pointer to the dib data	void* pDibSection = 0;	//Structure to hold information about the bitmap	BITMAPINFO bi;	ZeroMemory(&bi, sizeof(BITMAPINFO));	//Size of the stucture in bytes	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);	//the width of the new bitmap	bi.bmiHeader.biWidth = Width;	//the height of the new bitmap ( negative for topdown image )	bi.bmiHeader.biHeight = -Height;	//Obsolete  - -Just set to 1	bi.bmiHeader.biPlanes = 1;	// the bit depth of the surface	bi.bmiHeader.biBitCount = 32;	//the compression format. BI_RGB indicates none	bi.bmiHeader.biCompression = BI_RGB;	//Create a new DIB	HBITMAP hDibSection = CreateDIBSection( hDC, &bi, DIB_RGB_COLORS, &pDibSection, NULL, NULL);	//Select the bitmap into the new DC	SelectObject(hDC, hDibSection);	//Update the pointer to the handle to the bitmap	*phDibSection = hDibSection;	//Return the handle to the device context	return hDC;}//Delete the bitmap from memoryvoid DeleteD3DCompatibleDC( HDC hDC, HBITMAP hDibSection){	// Delete the bitmap	DeleteObject( hDibSection);	//Delete the DC	DeleteDC(hDC);}HRESULT CopyDCToSurface( LPDIRECT3DSURFACE8 pDestSurf, POINT* pDestPoint, HDC hDCSource, HBITMAP hDibSection, RECT* pSrcRect, COLORREF ColorKey){	HRESULT r = 0;	//The source rectangle	RECT SourceRect;	//The destination origin point	POINT DestPoint;	//Holds Information about the bitmap	DIBSECTION DibSection;	//Holds information abou the surface when locked	D3DLOCKED_RECT LockedRect;	//Get information about the bitmap in the DC	GetObject( hDibSection, sizeof(DIBSECTION), &DibSection);	int SrcTotalWidth = DibSection.dsBm.bmWidth;	int SrcTotalHeight = DibSection.dsBm.bmHeight;	//if no source rectangle was specified then this indicates that the entire map in the DC	//is to be copied	if( !pSrcRect )		SetRect( &SourceRect, 0, 0, SrcTotalWidth, SrcTotalHeight);	else		SourceRect = *(pSrcRect);	//if no destination point was specified then the origin is set to (0,0)	if( !pDestPoint )		DestPoint.x = DestPoint.y = 0;		else		DestPoint = *(pDestPoint);	//return failure if a valid destination surface was not specified	if( !pDestSurf )		return E_FAIL;	//return failure if a valid DC source was not specified	if( !hDCSource )		return E_FAIL;	//Lock the source surface	r = pDestSurf->LockRect(&LockedRect, 0, 0);	if(FAILED(r))	{		SetError("Unable to lock the surface for GDI transfer");		return E_FAIL;	}	//Conver the source and destination data pointers to DWORD(32 bit) Values	DWORD* pSrcData = (DWORD*)(DibSection.dsBm.bmBits);	DWORD* pDestData = (DWORD*)(LockedRect.pBits);	// Convert the pitch to a 32-bit value	int Pitch32 = LockedRect.Pitch/4;	//Compute the dimensions for the copy	int SrcHeight = SourceRect.bottom - SourceRect.top;	int SrcWidth = SourceRect.right - SourceRect.left;	// compute the index into memory	DWORD SrcOffset = SourceRect.top * SrcTotalWidth + SourceRect.left;	DWORD DestOffset = DestPoint.y * Pitch32 + DestPoint.x;	//If not using a color key then a faster copy can be done	if( ColorKey == -1 )	{		//Loop for each row in the image		for( int y= 0 ; y < SrcHeight; y++)		{			//Copy this line of the image			memcpy( (void*)&(pDestData[ DestOffset ] ), (void*)&(pSrcData [ SrcOffset ] ), SrcWidth*4);			//increase the destination pointer by the pitch			DestOffset+=Pitch32;			//incerase the source pointer by the total width			SrcOffset +=SrcTotalWidth;		}	}	else //a color key was specified	{		//Loop for each row in the image		for( int y = 0; y < SrcHeight; y++)		{			//Loop for each column			for(int x = 0; x < SrcWidth; x++)			{				//If the source pixel is not the same as the color key				if( pSrcData[SrcOffset] != ColorKey)					//then copy the pixel to the destination					pDestData[DestOffset] = pSrcData[SrcOffset];				//Move to the next pixel in the source				SrcOffset++;				//Move to the next pixel in the destination				DestOffset++;			}		}	}	//Unlock the surface	pDestSurf->UnlockRect();	//return success	return S_OK;}//Check for a valid deviceHRESULT ValidateDevice(){	HRESULT r = 0;	//test the current state of the device	r = g_pDevice->TestCooperativeLevel();	if(FAILED(r))	{		//if the device is lost then return failure		if(r == D3DERR_DEVICELOST )			return E_FAIL;		//if the device is ready to be reset then attempt to do so		if(r==D3DERR_DEVICENOTRESET)		{			g_pBackSurface->Release();			//reset the device			r = g_pDevice->Reset( &g_SavedPresParams);			if(FAILED(r))			{				//if the device was not reset then exit				SetError("Could not reset device");				return E_FAIL;			}			//Reaquire a pointer to the new back buffer			r = g_pDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &g_pBackSurface);			if(FAILED(r))			{				SetError("Unable to reaquire the back buffer");				PostQuitMessage(0);				return E_FAIL;			}			g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 0.0f,0);			RestoreGraphics();		}	} return S_OK;}HRESULT RestoreGraphics(){	return S_OK;}  

these are my errors

  c:\chris\programming1\thunderbolt\mustangii.h(349) : error C2065: ''g_pDevice'' : undeclared identifierc:\chris\programming1\thunderbolt\mustangii.h(349) : error C2227: left of ''->TestCooperativeLevel'' must point to class/struct/unionc:\chris\programming1\thunderbolt\mustangii.h(359) : error C2065: ''g_pBackSurface'' : undeclared identifierc:\chris\programming1\thunderbolt\mustangii.h(359) : error C2227: left of ''->Release'' must point to class/struct/unionc:\chris\programming1\thunderbolt\mustangii.h(361) : error C2227: left of ''->Reset'' must point to class/struct/unionc:\chris\programming1\thunderbolt\mustangii.h(369) : error C2227: left of ''->GetBackBuffer'' must point to class/struct/unionc:\chris\programming1\thunderbolt\mustangii.h(377) : error C2227: left of ''->Clear'' must point to class/struct/unionc:\chris\programming1\thunderbolt\mustangii.h(378) : error C2065: ''RestoreGraphics'' : undeclared identifierc:\chris\programming1\thunderbolt\mustangii.h(386) : error C2373: ''RestoreGraphics'' : redefinition; different type modifiersC:\Chris\Programming1\Thunderbolt\Main.cpp(33) : error C2040: ''g_pDevice'' : ''struct IDirect3DDevice8 *'' differs in levels of indirection from ''int''C:\Chris\Programming1\Thunderbolt\Main.cpp(34) : error C2040: ''g_pBackSurface'' : ''struct IDirect3DSurface8 *'' differs in levels of indirection from ''int''C:\Chris\Programming1\Thunderbolt\Main.cpp(214) : error C2227: left of ''->Clear'' must point to class/struct/unionC:\Chris\Programming1\Thunderbolt\Main.cpp(234) : error C2227: left of ''->GetBackBuffer'' must point to class/struct/unionC:\Chris\Programming1\Thunderbolt\Main.cpp(241) : error C2227: left of ''->CreateImageSurface'' must point to class/struct/unionC:\Chris\Programming1\Thunderbolt\Main.cpp(289) : error C2227: left of ''->CopyRects'' must point to class/struct/unionC:\Chris\Programming1\Thunderbolt\Main.cpp(292) : error C2227: left of ''->Present'' must point to class/struct/unionC:\Chris\Programming1\Thunderbolt\Main.cpp(304) : error C2227: left of ''->Release'' must point to class/struct/unionC:\Chris\Programming1\Thunderbolt\Main.cpp(311) : error C2227: left of ''->Release'' must point to class/struct/unionError executing cl.exe.  


i do not wish to slam it all together... i want to keep functions in the header file and calls in the cpp file to keep everything neat... im not sure if im even making sense its so late
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Begone.

[twitter]warrenm[/twitter]

sorry if i bothered you too much, elixir im sorry... i figured i declared everything in the wrong spot is all thank you very much for your help.
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Advertisement
We all have bad days, forgive him...

.lick
I''m brash because I care for you and the community. It gets me a bad reputation sometimes, but please don''t take it personally. It''s important to use all the resources available to you to their full, since the resources of the community (knowledge and, much more importantly, time) are precious.

I figured that by telling you "freakin'' make sure things are declared before you use them," you''d realize what you did wrong - you failed to declare your variables before they were used, and that does carry across translation units (thus the use of extern). I apologize if you didn''t already have that knowledge, but I don''t apologize if you did. Some things I said were rude, granted, but sometimes harshness is one of few ways to drill information in.

So, once again, don''t take anything I said as personal or insulting; it''s all there to help you.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement