#include <d3d9.h>
#include <d3dx9.h>
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture
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;
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
// Turn off culling
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
// Turn on the zbuffer
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
return S_OK;
}
VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if( g_pD3D != NULL)
g_pD3D->Release();
}
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 );
}
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 );
}
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
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
}
UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}
HRESULT InitGeometry()
{
// Use D3DX to create a texture from a file based image
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "banana.bmp", &g_pTexture ) ) )
{
// If texture is not in current folder, try parent folder
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "..\\banana.bmp", &g_pTexture ) ) )
{
MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
return E_FAIL;
}
}
return S_OK;
}
need help: weird compile error on my code
Hi I this is my first time coding in DX. I got this piece of codes in the tutorials and connected them. What happened is when I compiled it, it diplays this errors:
--------------------Configuration: dx9 - Win32 Debug--------------------
Compiling...
main.cpp
C:\projects\dx9\main.cpp(166) : error C2065: 'InitGeometry' : undeclared identifier
C:\projects\dx9\main.cpp(193) : error C2373: 'InitGeometry' : redefinition; different type modifiers
Error executing cl.exe.
main.obj - 2 error(s), 0 warning(s)
I know it's sounds simple to most of you here but I'm just starting out. A little help means a lot to me. Thanks to all of you!
[Edited by - Makoy on November 13, 2004 8:54:49 AM]
It looks like you called InitGeometry() (line 166) before it's actually defined (line 193). C++ requires that you define a symbol before you use it. Try moving the definition of InitGeometry() to the start of the file, or make a declaration for it.
yes! it works fine now but, there's one more problem my friend. Why is it that my texture doesn't display on the window?
Look at your Render() function. You aren't actually rendering anything in between BeginScene() and EndScene(). If you don't know how to draw primitive to the screen, I'd recommend checking out some of the tutorials in the DirectX SDK (it looks like you've already found the first one [smile]). They cover a lot of basic concepts.
If you don't like the SDK tutorials, try some of the sites listed here. DrukenHyena and Andy Pike are among the best.
If you don't like the SDK tutorials, try some of the sites listed here. DrukenHyena and Andy Pike are among the best.
yeah I did a quick check last night and found out that it's actually my render() function. I tried adding g_pd3Device->drawscene but it produces a compile error. Can I call ID3DXSprite interface within my render() function? BTW thank you for replying circlesoft :)
Yes, you need to call ID3DXSprite::Draw() inside your Render() function. This should be done in between your g_pd3dDevice->BeginScene() and g_pd3dDevice->EndScene() calls.
Quote:
Yes, you need to call ID3DXSprite::Draw() inside your Render() function. This should be done in between your g_pd3dDevice->BeginScene() and g_pd3dDevice->EndScene() calls.
Hi circlesoft! Since I'm new to this DX9 thing, can you please give me a clue. Thanks :)
Quote:Original post by MakoyQuote:
Yes, you need to call ID3DXSprite::Draw() inside your Render() function. This should be done in between your g_pd3dDevice->BeginScene() and g_pd3dDevice->EndScene() calls.
Hi circlesoft! Since I'm new to this DX9 thing, can you please give me a clue. Thanks :)
Sure. I don't have a compiler or the DX docs with me right now, so this may not compile perfectly (it should be fundamentally correct, though). Note that I am referencing the DirectX 9.0c October SDK.
/* Global Declarations */LPD3DXSPRITE d3dSprite = NULL; // This is our sprite interfaceHRESULT InitD3D(){ // ... // All of your code you have here // Create our sprite interface if( FAILED( D3DXCreateSprite( g_pd3dDevice, &d3dSprite ) ) ) { return E_FAIL; }}void Render(){ // ... // All of your code previous to begin scene here // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Rendering of scene objects can happen here // Begin sprite d3dSprite->Begin( 0 ); // Draw our texture at position (100,100) D3DXVECTOR3 screenPos = D3DXVECTOR3( 100.0f, 100.0f, 0.0f ); // Specify NULL for 2nd and 3rd parameters to draw // the entire image at (100,100). // Specify 0xFFFFFFFF for the modulation color to retain // the original color d3dSprite->Draw( g_pTexture, NULL, NULL, &screenPos, 0xFFFFFFFF ); // End the scene g_pd3dDevice->EndScene(); } // ... // All of your code after end scene here}void Cleanup(){ // ... // All of your other code here d3dSprite->Release(); d3dSprite = NULL;}
And that should just about do it [smile]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement