Two quick questions

Started by
27 comments, last by Huzzah 20 years, 8 months ago
First of all let me say that I have been using MS Visual C++ .NET for the last two months and a newbie to C++. But there’s couple of questions that I need help on: 1. How do you convert a String into a series, or array, of chars? My understanding is that a String is made up of ‘wide’ characters, while chars are byte sized. There doesn’t appear to be any way of converting wchar_t to char…or is there? 2. I’m trying to load a bitmap for use as a texture in Opengl using the following code: Bitmap* crap = new Bitmap(S"c:/crap64.bmp"); AUX_RGBImageRec* texture = new AUX_RGBImageRec(); texture->sizeX = crap->get_Width(); texture->sizeY = crap->get_Height(); Console::WriteLine(texture->sizeX); Console::WriteLine(texture->sizeY); AUX_RGBImageRec is a structure from the Glaux.h header file and as far as I can see this structure consists of typedef struct _AUX_RGBImageRec { GLint sizeX, sizeY; unsigned char *data; } AUX_RGBImageRec; I can get the dimensions of “crap” and pass the values into “texture”, but there doesn’t appear to be a data pointer for the Bitmap “crap”. How do you find where the Bitmap image has been loaded into memory?
Advertisement
I''m not sure about the first, but I can answer the second:

You''ve got it something of the wrong way around. GLAux provides functions to load an image (auxLoadDIB, I think) and fills out the structure *there* - you don''t do it yourself.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Ok thnx.
Hey, please forgive me for not posting my own thread, but I can''t as I''m apparently already registered, but I''m not, so I can''t register to post a thread... :/

Anyway, I''m new to DirectX, hence being in the Beginners forum. I used to know OpenGL, but it seems to have faded from my brain. Okay, I did the NeXe tutorial on creating a D3D window (http://nexe.gamedev.net/tutorials/ViewTutorial.asp?tutorialfile=Pages/Tutorial2.myxml), and can understand it... But I found this code in the Tutorials folder, in the C++ folder, in the Samples folder, in the DXSDK folder... I can''t understand it very well, and want to know (hopefully somebody will be kind enough to tell me. ^_^) the differences between the two. The DX SDK one is most likely going to be much better.

Thanks.

NEXE D3D WINDOW CODE:
#include <windows.h> // Windows header file#include <d3d8.h> // Direct3D header fileconst int WINDOW_WIDTH = 640; // We plug this in whereve we need the window widthconst int WINDOW_HEIGHT = 480; // Ditto except for heightconst int WINDOW_X = 0;  // And X location.const int WINDOW_Y = 0;  // And Y location. const char *WINDOW_TITLE="NeXe Tutorial 2"; // And titleconst char *WINDOW_CLASS_NAME="NeXe Tutorial"; // And class name.HINSTANCE g_hInst; // Handle to the instance of our programHWND g_hWnd; // Handle to the windowbool g_bFullscreen; // Boolean to hold whether or not it''s in full screenIDirect3D8 *g_pDirect3D; // Pointer to Direct3DIDirect3DDevice8 *g_pDevice; // Pointer to the Direct3D deviceHRESULT g_hr; // Variable to hold function call resultsbool Direct3DInit() // Function to initialize all of our Direct3D goodness{	g_pDirect3D=Direct3DCreate8(D3D_SDK_VERSION); // Create Direct3D	if (g_pDirect3D==NULL) // Check if it succeeded, if it didn''t, return failure		return false;	D3DDISPLAYMODE displayMode; // Structure to hold display mode info, like the resolution	if (g_bFullscreen==false) // If it''s not in fullscreen, we fill that structure with the current display mode	{		g_hr=g_pDirect3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &displayMode); // Get the current display mode on he default display adapter		if (FAILED(g_hr)) // If the above call failed, return failure			return false;	}	else	{		displayMode.Width=WINDOW_WIDTH; // Set the screen width to the window width		displayMode.Height=WINDOW_HEIGHT; // Set the screen height to the window height		displayMode.RefreshRate=0; // Set the refresh rate to default		displayMode.Format=D3DFMT_R5G6B5; // Set the color format to 565 16 bit	}	D3DPRESENT_PARAMETERS presentParameters; // Used to explain to Direct3D how it will present things on the screen	memset(&presentParameters, 0, sizeof(D3DPRESENT_PARAMETERS)); // We need to fill it with 0''s first	if (g_bFullscreen==false) // If the user answered "no" when we asked whether they''d like to use fullscreen	{ 		presentParameters.Windowed   = TRUE; // Then set windowed to true	}	else // Otherwise	{		presentParameters.Windowed   = FALSE; // Set windowed to false	}	presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Tells Direct3D to present the graphics to the screen the quickest way possible. We don''t care if it saves the data after or not. 	presentParameters.BackBufferFormat = displayMode.Format; // The color format	presentParameters.BackBufferWidth = displayMode.Width; // The back buffer width	presentParameters.BackBufferHeight = displayMode.Height; // The back buffer height	// Create the device	g_hr=g_pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING,                                  &presentParameters, &g_pDevice ); 	if (FAILED(g_hr)) // If it failed, return failure		return false;	return true;}void DrawScene(){	// Clear the screen	g_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0,0), 1.0f, 0 );	// Make it so we can draw stuff	g_pDevice->BeginScene();	// Make it so we can''t draw stuff	g_pDevice->EndScene();	// Put the back buffer onto the screen	g_pDevice->Present(NULL, NULL, NULL, NULL);}void Direct3DRelease(){	// Release the device	if (g_pDevice)		g_pDevice->Release();	g_pDevice=NULL;	// Release the Direct3D object	if (g_pDirect3D)		g_pDirect3D->Release();	g_pDirect3D=NULL;}// Windows message processing functionLRESULT CALLBACK WndProc(HWND wpHWnd, 						    UINT msg,                             WPARAM wParam,                             LPARAM lParam){	switch(msg)	{			case WM_DESTROY: 		{ 			PostQuitMessage(0);			return 0;		} break;		default:break; 	} 	return DefWindowProc(wpHWnd, msg, wParam, lParam);} int WINAPI WinMain(	HINSTANCE hInstance, 					HINSTANCE hPrevInstance, 					LPSTR lpCmdLine, 					int nCmdShow) {	WNDCLASSEX winClass; 	MSG msg;		// Set our global HINSTANCE to the one we get passed	g_hInst=hInstance;	// Setup and register the window class	winClass.cbSize         = sizeof(WNDCLASSEX); 	winClass.style			= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;	winClass.lpfnWndProc	= WndProc; 	winClass.cbClsExtra		= 0;	winClass.cbWndExtra		= 0; 	winClass.hInstance		= g_hInst; 	winClass.hIcon			= LoadIcon(NULL, IDI_APPLICATION); 	winClass.hCursor		= LoadCursor(NULL, IDC_ARROW); 	winClass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH); 	winClass.lpszMenuName	= NULL; 	winClass.lpszClassName	= WINDOW_CLASS_NAME; 	winClass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);	if (!RegisterClassEx(&winClass)) 		return 0;	// Ask the user whether or not they want to run in fullscreen	if (MessageBox(NULL, "Would you like to run in fullscreen?", "Fullscreen?", MB_YESNO)==IDYES)		g_bFullscreen=true;	else 		g_bFullscreen=false;	if (g_bFullscreen==false)	{		// Create a normal window with a border, a caption, and an X button		g_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,  							 WINDOW_CLASS_NAME,     							 WINDOW_TITLE, 							 WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE, 					 		 WINDOW_X, WINDOW_Y ,							 WINDOW_WIDTH, WINDOW_HEIGHT,							 NULL,							 NULL,							 g_hInst,  							 NULL);		}	else	{		// Create a fullscreen window, one that doesn''t have anything in it		g_hWnd = CreateWindowEx(NULL,  							 WINDOW_CLASS_NAME,     							 WINDOW_TITLE, 							 WS_POPUP | WS_VISIBLE, 					 		 WINDOW_X, WINDOW_Y ,							 WINDOW_WIDTH, WINDOW_HEIGHT,							 NULL,							 NULL,							 g_hInst,  							 NULL);					}	// Make sure the window was created properly. If it wasn''t, quit.		if (!g_hWnd) 		return 0; 			// Try to initialize the Direct3D parts of our program. If it fails, make sure everything they might have been created gets released.	if (Direct3DInit()==false)	{		Direct3DRelease();		return 0;	}	while(1)	{		// Windows message stuff		while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))		{ 		    TranslateMessage(&msg);		    DispatchMessage(&msg); 		    if (msg.message == WM_QUIT)			break;		}				if (msg.message==WM_QUIT)			break;		// Quit if the users presses escape		if (GetAsyncKeyState(VK_ESCAPE))			PostQuitMessage(0);		// Draw the scene each frame				DrawScene();	}	// Release Direct3D	Direct3DRelease();	return 0;}


DXSDK D3D WINDOW CODE:
//-----------------------------------------------------------------------------// File: CreateDevice.cpp//// Desc: This is the first tutorial for using Direct3D. In this tutorial, all//       we are doing is creating a Direct3D device and using it to clear the//       window.//// Copyright (c) Microsoft Corporation. All rights reserved.//-----------------------------------------------------------------------------#include <d3d9.h>//-----------------------------------------------------------------------------// Global variables//-----------------------------------------------------------------------------LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDeviceLPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device//-----------------------------------------------------------------------------// Name: InitD3D()// Desc: Initializes Direct3D//-----------------------------------------------------------------------------HRESULT InitD3D( HWND hWnd ){    // Create the D3D object, which is needed to create the D3DDevice.    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )        return E_FAIL;    // Set up the structure used to create the D3DDevice. Most parameters are    // zeroed out. We set Windowed to TRUE, since we want to do D3D in a    // window, and then set the SwapEffect to "discard", which is the most    // efficient method of presenting the back buffer to the display.  And     // we request a back buffer format that matches the current desktop display     // format.    D3DPRESENT_PARAMETERS d3dpp;     ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    // Create the Direct3D device. Here we are using the default adapter (most    // systems only have one, unless they have multiple graphics hardware cards    // installed) and requesting the HAL (which is saying we want the hardware    // device rather than a software one). Software vertex processing is     // specified since we know it will work on all cards. On cards that support     // hardware vertex processing, though, we would see a big performance gain     // by specifying hardware vertex processing.    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                                      &d3dpp, &g_pd3dDevice ) ) )    {        return E_FAIL;    }    // Device state would normally be set here    return S_OK;}//-----------------------------------------------------------------------------// Name: Cleanup()// Desc: Releases all previously initialized objects//-----------------------------------------------------------------------------VOID Cleanup(){    if( g_pd3dDevice != NULL)         g_pd3dDevice->Release();    if( g_pD3D != NULL)        g_pD3D->Release();}//-----------------------------------------------------------------------------// Name: Render()// Desc: Draws the scene//-----------------------------------------------------------------------------VOID Render(){    if( NULL == g_pd3dDevice )        return;    // Clear the backbuffer to a blue color    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );        // Begin the scene    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )    {        // Rendering of scene objects can happen here            // End the scene        g_pd3dDevice->EndScene();    }    // Present the backbuffer contents to the display    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}//-----------------------------------------------------------------------------// Name: MsgProc()// Desc: The window''s message handler//-----------------------------------------------------------------------------LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {        case WM_DESTROY:            Cleanup();            PostQuitMessage( 0 );            return 0;        case WM_PAINT:            Render();            ValidateRect( hWnd, NULL );            return 0;    }    return DefWindowProc( hWnd, msg, wParam, lParam );}//-----------------------------------------------------------------------------// Name: WinMain()// Desc: The application''s entry point//-----------------------------------------------------------------------------INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    // Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,                      "D3D Tutorial", NULL };    RegisterClassEx( &wc );    // Create the application''s window    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice",                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,                              GetDesktopWindow(), NULL, wc.hInstance, NULL );    // Initialize Direct3D    if( SUCCEEDED( InitD3D( hWnd ) ) )    {         // Show the window        ShowWindow( hWnd, SW_SHOWDEFAULT );        UpdateWindow( hWnd );        // Enter the message loop        MSG msg;         while( GetMessage( &msg, NULL, 0, 0 ) )        {            TranslateMessage( &msg );            DispatchMessage( &msg );        }    }    UnregisterClass( "D3D Tutorial", wc.hInstance );    return 0;}
Well I most say that DXSDK have a much more organized code then nexe got. But one thing that nexes code does that DXSDK doesnt is to ask you if you want to show it in fullscreen. DXSDK show everything in windows mode. And DXSDK have ofcurso more checking to se so everything works well!

My english sucks I know so dont say anything about it ok?
Mullvad
Ok then, thanks.

Ack lol, I just compiled my window code, and I got 2 errors...

c:\directx stuff\test workspace\test project\window.cpp(22) : error C2146: syntax error : missing '';'' before identifier ''bool''
c:\directx stuff\test workspace\test project\window.cpp(22) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

TEST Project.exe - 2 error(s), 0 warning(s)

I don''t see any missing semicolons...
Heh, I''m going blind.
Anyway, I fixed it, and now I got some different errors... I modified the code slightly, mostly changing ''8'' to ''9'' in the DX functions.

Here''s my code (using modified nexe code):
////////////////////////////////////////////////////// window.cpp - Initialises the D3D window////////////////////////////////////////////////////////////////////////////////// Includes#include <windows.h>	// Windows header file.#include <d3d9.h>		// Direct3D header file.////////////////////////////// Definesconst int WINDOW_WIDTH = 640;	// The window width.const int WINDOW_HEIGHT = 480;	// The window height.const int WINDOW_X = 0;			// The window''s X location.const int WINDOW_Y = 0;			// The window''s Y location.const char *WINDOW_TITLE = "Game Window";		// The window''s title.const char *WINDOW_CLASS_NAME = "Game Window";	// The window''s class name.HINSTANCE g_hInst;				// The handle to this instance of the program.HWND g_hWnd;						// The handle to the window.bool g_bFullscreen;	// A boolean to hold whether or not the window is in fullscreen mode.IDirect3D9 *g_pDirect3D;			// The pointer to Direct3D.IDirect3DDevice9 *g_pDevice;	// The pointer to the Direct3D device.HRESULT g_hr;					// A variable to hold function call results.////////////////////////////// Functions// A function to initialise Direct3D.bool Direct3DInit(){	g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);	// Create Direct3D.	if(g_pDirect3D == NULL)							// If it failed, return failure.	{	return false;	}	D3DDISPLAYMODE displayMode;	// A structure to hold display mode info (eg. Resolution).	if(g_bFullscreen == false)	// If not in fullscreen, fill the structure with the current display mode.	{		// Get the current display mode on the default display adaptor.		g_hr = g_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);		if(FAILED(g_hr))		// If the above call failed, return failure.		{	return false;	}	}	else	{		displayMode.Width = WINDOW_WIDTH;	// Set the screen width to the window width.		displayMode.Height = WINDOW_HEIGHT;	// Set the screen height to the window height.		displayMode.RefreshRate = 0;		// Set the refresh rate to the default.		displayMode.Format = D3DFMT_R5G6B5;	// Set the colour format to 565 16 bit.	}	D3DPRESENT_PARAMETERS presentParameters;// Tells D3D how to present things on the screen.	memset(&presentParameters, 0, sizeof(D3DPRESENT_PARAMETERS));	// Fill it with 0s first.	if(g_bFullscreen == false)	// If the user answered "no" to the fullscreen message box...	{		presentParameters.Windowed = TRUE;	// ...then set windowed to true.	}	else						// If the user answered "yes" to the fullscreen message box...	{		presentParameters.Windowed = FALSE;	// ...then set windowed to false.	}	// Tells Direct3D to present the graphics to the screen in the quickest way possible.	presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;	presentParameters.BackBufferFormat = displayMode.Format;	// The colour format.	presentParameters.BackBufferWidth = displayMode.Width;		// The back buffer width.	presentParameters.BackBufferHeight = displayMode.Height;	// The back buffer height.	// Create the device.	g_hr = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,									D3DDEVTYPE_HAL,									g_hWnd,									D3DCREATE_SOFTWARE_VERTEXPROCESSING,									&presentParameters,									&g_pDevice);	if(FAILED(g_hr))			// If it failed, return failure.	{	return false;	}	return true;				// The function was successful.}// A function that is called every frame, and draws the scene.void DrawScene(){	// Clear the screen.	g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);	// Begin the scene (allow us to draw).	g_pDevice->BeginScene();	// End the scene.	g_pDevice->EndScene();	// Put the back buffer on to the screen.	g_pDevice->Present(NULL, NULL, NULL, NULL);}// A function that releases/deletes what was done with Direct3DInit().void Direct3DRelease(){	// Release the device.	if(g_pDevice)	{	g_pDevice->Release();	}	g_pDevice = NULL;	// Release the Direct3D object.	if(g_pDirect3D)	{	g_pDirect3D->Release();	}	g_pDirect3D = NULL;}// Windows message processing function.LRESULT CALLBACK WndProc(HWND wpHWnd, UINT msg, WPARAM wParam, LPARAM lParam){	switch(msg)	{		case WM_DESTROY:		{			PostQuitMessage(0);			return 0;		} break;		default: break;	}	return DefWindowProc(wpHWnd, msg, wParam, lParam);}// Main function.int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIntance, LPSTR lpCmdLine, int nCmdShow){	WNDCLASSEX winClass;	MSG msg;	// Set our global HINSTANCE to the one we get passed.	g_hInst = hInstance;	// Setup and register the window class.	winClass.cbSize = sizeof(WNDCLASSEX); 	winClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;	winClass.lpfnWndProc = WndProc; 	winClass.cbClsExtra = 0;	winClass.cbWndExtra = 0; 	winClass.hInstance = g_hInst; 	winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 	winClass.hCursor = LoadCursor(NULL, IDC_ARROW); 	winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 	winClass.lpszMenuName = NULL; 	winClass.lpszClassName = WINDOW_CLASS_NAME; 	winClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);	if(!RegisterClassEx(&winClass))	{	return 0;	}	// Ask the user if they want to run the program in fullscreen mode.	if(MessageBox(NULL, "Would you like to run in fullscreen mode?", "Fullscreen mode?",		MB_YESNO) == IDYES)	{	g_bFullscreen = true;	}else{	g_bFullscreen = false;	}	if(g_bFullscreen == false)	{		// Create a normal window with a border, a caption, and an X button.		g_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,							 WINDOW_CLASS_NAME,							 WINDOW_TITLE,							 WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE,					 		 WINDOW_X, WINDOW_Y,							 WINDOW_WIDTH, WINDOW_HEIGHT,							 NULL,							 NULL,							 g_hInst,							 NULL);	}	else	{		// Create a fullscreen window without a border, a caption, or any buttons.		g_hWnd = CreateWindowEx(NULL,							 WINDOW_CLASS_NAME,							 WINDOW_TITLE,							 WS_POPUP | WS_VISIBLE,					 		 WINDOW_X, WINDOW_Y,							 WINDOW_WIDTH, WINDOW_HEIGHT,							 NULL,							 NULL,							 g_hInst,							 NULL);	}	// Make sure the window was created properly. If it wasn''t, quit the program.	if(g_hWnd)	{	return 0;	}	// Initialise Direct3D. If it fails, release anything that may have been created.	if(Direct3DInit() == false)	{		Direct3DRelease();		return 0;	}	while(1)	{		// Windows message checks...		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{		    TranslateMessage(&msg);		    DispatchMessage(&msg);		    if (msg.message == WM_QUIT)			break;		}		if (msg.message == WM_QUIT)		{	break;	}		// Quit if the user presses the escape button...		if (GetAsyncKeyState(VK_ESCAPE))		{	PostQuitMessage(0);	}		// Draw the scene (each frame)...		DrawScene();	}	// We''re out of the loop... Quit the program.	// Release Direct3D...	Direct3DRelease();	return 0;}

And here are my errors:

window.obj : error LNK2001: unresolved external symbol _Direct3DCreate9@4
Debug/TEST Project.exe : fatal error LNK1120: 1 unresolved externals

What''s it mean!?
Thanks.
ARRR! This is really annoying! :''(

I fixed it - I forgot to link in one of the D3D libs lol. But now, when I run it (finally ), it brings up the messagebox, and whatever button I click on, yes or no, they both just flash up the window in the top left corner of the screen, about half the size of the screen, and then vanishes straight away. If you blink, you miss it, literally.
Sorry, I fixed it... I missed out the ! when checking if it was created properly lol. It still crashes in fullscreen mode, but that''s because my card doesn''t support it.
AHHHHHHH!!! >.< I did the next NeXe tutorial...creating a triangle... I get an error(God, I hate errors... ), and I don''t have a clue what I''m supposed to do about it... Help!

The error:
C:\DirectX stuff\TEST Workspace\TEST Project\window.cpp(153) : error C2664: ''SetVertexShader'' : cannot convert parameter 1 from ''const unsigned long'' to ''struct IDirect3DVertexShader9 *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

TEST Project.exe - 1 error(s), 0 warning(s)

The code(the SetVertexShader line - line 153, is in the DrawScene function):
////////////////////////////////////////////////////// window.cpp - Initialises the D3D window////////////////////////////////////////////////////////////////////////////////// Includes#include <windows.h>	// Windows header file.#include <d3d9.h>		// Direct3D header file.#include <d3dx9.h>		// Direct3D header file.////////////////////////////// Structure and Class Definitions// Vertex Class...class CVertex{public:	// Coordinates...	float fX, fY, fZ;	// Colour...	DWORD dwColor;	// A method to simplify initialisation...	void Create(float fp_fX, float fp_fY, float fp_fZ, float fp_fR, float fp_fG, float fp_fB)	{		fX = fp_fX;		fY = fp_fY;		fZ = fp_fZ;		dwColor = D3DCOLOR_XRGB((BYTE)(fp_fR*255), (BYTE)(fp_fG*255), (BYTE)(fp_fB*255));	}};// CVertex''s FVF (Flexible Vertex Format)... Diffuse colour, and XYZ...const DWORD D3DFVF_CVertex = D3DFVF_XYZ | D3DFVF_DIFFUSE;////////////////////////////// Other Global Defines and Declarations// Initialisation contants...const int WINDOW_WIDTH = 640;	// The window width.const int WINDOW_HEIGHT = 480;	// The window height.const int WINDOW_X = 0;			// The window''s X location.const int WINDOW_Y = 0;			// The window''s Y location.const char *WINDOW_TITLE = "Game Window";		// The window''s title.const char *WINDOW_CLASS_NAME = "Game Window";	// The window''s class name.// Windows handles...HINSTANCE g_hInst;				// The handle to this instance of the program.HWND g_hWnd;						// The handle to the window.// Fullscreen flag...bool g_bFullscreen;	// A boolean to hold whether or not the window is in fullscreen mode.// Direct3D pointers...IDirect3D9 *g_pDirect3D;			// The pointer to Direct3D.IDirect3DDevice9 *g_pDevice;	// The pointer to the Direct3D device.// Direct3D call result variable...HRESULT g_hr;					// A variable to hold function call results.// An array of vertices for a triangle...CVertex vtxTriangle[3];////////////////////////////// Functions// A function to initialise Direct3D.bool Direct3DInit(){	g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);	// Create Direct3D.	if(g_pDirect3D == NULL)							// If it failed, return failure.	{	return false;	}	D3DDISPLAYMODE displayMode;	// A structure to hold display mode info (eg. Resolution).	if(g_bFullscreen == false)	// If not in fullscreen, fill the structure with the current display mode.	{		// Get the current display mode on the default display adaptor.		g_hr = g_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);		if(FAILED(g_hr))		// If the above call failed, return failure.		{	return false;	}	}	else	{		displayMode.Width = WINDOW_WIDTH;	// Set the screen width to the window width.		displayMode.Height = WINDOW_HEIGHT;	// Set the screen height to the window height.		displayMode.RefreshRate = 0;		// Set the refresh rate to the default.		displayMode.Format = D3DFMT_R5G6B5;	// Set the colour format to 565 16 bit.	}	D3DPRESENT_PARAMETERS presentParameters;// Tells D3D how to present things on the screen.	memset(&presentParameters, 0, sizeof(D3DPRESENT_PARAMETERS));	// Fill it with 0s first.	if(g_bFullscreen == false)	// If the user answered "no" to the fullscreen message box...	{		presentParameters.Windowed = TRUE;	// ...then set windowed to true.	}	else						// If the user answered "yes" to the fullscreen message box...	{		presentParameters.Windowed = FALSE;	// ...then set windowed to false.	}	// Tells Direct3D to present the graphics to the screen in the quickest way possible.	presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;	presentParameters.BackBufferFormat = displayMode.Format;	// The colour format.	presentParameters.BackBufferWidth = displayMode.Width;		// The back buffer width.	presentParameters.BackBufferHeight = displayMode.Height;	// The back buffer height.	// Create the device...	g_hr = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,									D3DDEVTYPE_HAL,									g_hWnd,									D3DCREATE_SOFTWARE_VERTEXPROCESSING,									&presentParameters,									&g_pDevice);	if(FAILED(g_hr))			// If it failed, return failure.	{	return false;	}	// Matrix code...	D3DXMATRIX mat;		// Projection Matrix.	D3DXMatrixPerspectiveFovLH(&mat, D3DX_PI/6, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 100.0);	g_pDevice->SetTransform(D3DTS_PROJECTION, &(D3DMATRIX)mat);		// World Matrix.	D3DXMatrixIdentity(&mat);	g_pDevice->SetTransform(D3DTS_WORLD, &(D3DMATRIX)mat);		// View Matrix.	D3DXMatrixTranslation(&mat, 0, 0, 10.0);	g_pDevice->SetTransform(D3DTS_VIEW, &(D3DMATRIX)mat);	// Setup triangle vertices..	vtxTriangle[0].Create(0, 0, 0, 1, 0, 0);	vtxTriangle[1].Create(0, 1, 0, 0, 1, 0);	vtxTriangle[2].Create(1, 0, 0, 0, 0, 1);	// Render States...	// Enable drawing of triangles that face away from the camera...	g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);	// Disable lighting...	g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);	return true;				// The function was successful.}// A function that is called every frame, and draws the scene.void DrawScene(){	// Clear the screen.	g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);	// Begin the scene (allow us to draw).	g_pDevice->BeginScene();	// Tell Direct3D how to process the vertices...	g_pDevice->SetVertexShader(D3DFVF_CVertex);	// Draw the triangle...	g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vtxTriangle, sizeof(CVertex));	// End the scene.	g_pDevice->EndScene();	// Put the back buffer on to the screen.	g_pDevice->Present(NULL, NULL, NULL, NULL);}// A function that releases/deletes what was done with Direct3DInit().void Direct3DRelease(){	// Release the device.	if(g_pDevice)	{	g_pDevice->Release();	}	g_pDevice = NULL;	// Release the Direct3D object.	if(g_pDirect3D)	{	g_pDirect3D->Release();	}	g_pDirect3D = NULL;}// Windows message processing function.LRESULT CALLBACK WndProc(HWND wpHWnd, UINT msg, WPARAM wParam, LPARAM lParam){	switch(msg)	{		case WM_DESTROY:		{			PostQuitMessage(0);			return 0;		} break;		default: break;	}	return DefWindowProc(wpHWnd, msg, wParam, lParam);}// Main function.int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIntance, LPSTR lpCmdLine, int nCmdShow){	WNDCLASSEX winClass;	MSG msg;	// Set our global HINSTANCE to the one we get passed.	g_hInst = hInstance;	// Setup and register the window class.	winClass.cbSize			= sizeof(WNDCLASSEX); 	winClass.style			= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;	winClass.lpfnWndProc	= WndProc; 	winClass.cbClsExtra		= 0;	winClass.cbWndExtra		= 0; 	winClass.hInstance		= g_hInst; 	winClass.hIcon			= LoadIcon(NULL, IDI_APPLICATION); 	winClass.hCursor		= LoadCursor(NULL, IDC_ARROW); 	winClass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH); 	winClass.lpszMenuName	= NULL; 	winClass.lpszClassName	= WINDOW_CLASS_NAME; 	winClass.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);	if(!RegisterClassEx(&winClass))	{	return 0;	}	// Ask the user if they want to run the program in fullscreen mode.	if(MessageBox(NULL, "Would you like to run in fullscreen mode?", "Fullscreen mode?",		MB_YESNO) == IDYES)	{	g_bFullscreen = true;	}else{	g_bFullscreen = false;	}	if(g_bFullscreen == false)	{		// Create a normal window with a border, a caption, and an X button.		g_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,							 WINDOW_CLASS_NAME,							 WINDOW_TITLE,							 WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE,					 		 WINDOW_X, WINDOW_Y,							 WINDOW_WIDTH, WINDOW_HEIGHT,							 NULL,							 NULL,							 g_hInst,							 NULL);	}	else	{		// Create a fullscreen window without a border, a caption, or any buttons.		g_hWnd = CreateWindowEx(NULL,							 WINDOW_CLASS_NAME,							 WINDOW_TITLE,							 WS_POPUP | WS_VISIBLE,					 		 WINDOW_X, WINDOW_Y,							 WINDOW_WIDTH, WINDOW_HEIGHT,							 NULL,							 NULL,							 g_hInst,							 NULL);	}	// Make sure the window was created properly. If it wasn''t, quit the program.	if(!g_hWnd)	{	return 0;	}	// Initialise Direct3D. If it fails, release anything that may have been created.	if(Direct3DInit() == false)	{		Direct3DRelease();		return 0;	}	while(1)	{		// Windows message checks...		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{		    TranslateMessage(&msg);		    DispatchMessage(&msg);		    if (msg.message == WM_QUIT)			break;		}		if (msg.message == WM_QUIT)		{	break;	}		// Quit if the user presses the escape button...		if (GetAsyncKeyState(VK_ESCAPE))		{	PostQuitMessage(0);	}		// Draw the scene (each frame)...		DrawScene();	}	// We''re out of the loop... Quit the program.	// Release Direct3D...	Direct3DRelease();	return 0;}

This topic is closed to new replies.

Advertisement