directX 9.0 help with textures and sprite hnadler

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



hi,
im creating a small 2D game using directX9. However, to first ttest it, i
initialised directX and decided to display a texture on screen. To dispally
it i used a sprite handler and a texture object. This is my first time using
directX. For some reason the program will run and window open, but nothing
will be dispalyed even tho i call draw (). I get the following error:

The thread 'Win32 Thread' (0xf2c) has exited with code 0 (0x0).

The code i am using is:

#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>
#pragma warning (disable : 4996) // disable deprecated warning
#include <strsafe.h>
#pragma warning (default : 4996)
#include <dinput.h>

#define KEYDOWN(name, key) (name[key] & 0x80) // the keydown macro

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 D3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 d3dDevice = NULL; // Our rendering device
LPDIRECTINPUT8 directInput = NULL; // collects user input
LPDIRECTINPUTDEVICE8 keyboard = NULL; // the keyboard object
char buffer[256]; // the buffe rthat holds ke strokes

LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 fakeTexture;

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D (HWND hWnd, HINSTANCE hInstance)
{
// Create the D3D object
D3D = Direct3DCreate9 (D3D_SDK_VERSION);

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory (&d3dpp, sizeof (d3dpp));
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.Windowed = TRUE;
d3dpp.hDeviceWindow = hWnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

// Create the D3DDevice
if (FAILED (D3D->CreateDevice (D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3dDevice)))
return E_FAIL;

// create the direct input device
if (FAILED (DirectInput8Create (hInstance,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**) &directInput,
NULL)))
return E_FAIL;

if (FAILED (directInput->CreateDevice (GUID_SysKeyboard, &keyboard, NULL)))
// initialise the keyboard
return E_FAIL;
if (FAILED (keyboard->SetDataFormat (&c_dfDIKeyboard))) // set the input
device
return E_FAIL;
if (FAILED (keyboard->SetCooperativeLevel (hWnd, DISCL_FOREGROUND |
DISCL_NONEXCLUSIVE))) // set the keyboard application sharing values
return E_FAIL;
if (FAILED (keyboard->Acquire ())) // get the input device
return E_FAIL;

return S_OK; // everything intialised ok
} // Init3D

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID cleanUp()
{
if (d3dDevice != NULL)
d3dDevice->Release();

if (D3D != NULL)
D3D->Release();

if (fakeTexture != NULL)
fakeTexture->Release ();
} // CleanUp

VOID checkKeyboard ()
{
keyboard->GetDeviceState (sizeof (buffer), (LPVOID) &buffer);

if (KEYDOWN (buffer, DIK_RIGHT))
{

} // if
if (KEYDOWN (buffer, DIK_LEFT))
{

} // if
if (KEYDOWN (buffer, DIK_UP))
{

} // if
if (KEYDOWN (buffer, DIK_DOWN))
{

} // if
if (KEYDOWN (buffer, DIK_SPACE))
{

} // if
} // checkKeyboard

VOID initTextures ()
{
D3DXCreateSprite (d3dDevice, &sprite);
D3DXCreateTextureFromFile (d3dDevice, L"button.bmp", &fakeTexture);
} // init2D

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render ()
{
// Clear the backbuffer to a blue color
d3dDevice->Clear (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0),
1.0f, 0);
D3DXVECTOR3 pos;

pos.x = 0;
pos.y = 0;
pos.z = 0;

sprite->Begin (D3DXSPRITE_ALPHABLEND);
sprite->Draw (fakeTexture, NULL, NULL, &pos, D3DCOLOR_XRGB (255,255,255));
sprite->End ();
} // Render

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage (0);
break;
}
break;

case WM_CLOSE:
DestroyWindow (hWnd); break;

case WM_DESTROY:
PostQuitMessage (0); break;

default:
return DefWindowProc (hWnd, msg, wParam, lParam); break;
} // switch
return 0;
} // MsgProc

//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE, LPWSTR, INT)
{
// Register the window class
WNDCLASSEX winClass;
HWND hWnd;

MSG msg;
ZeroMemory (&msg, sizeof (msg));

winClass.lpszClassName = L"MyWindowsClass";
winClass.cbSize = sizeof (WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = MsgProc; // callback function
winClass.hInstance = hInstance; // program instance handle
winClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
winClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
winClass.hCursor = LoadCursor (NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); //
background colour
winClass.lpszMenuName = NULL; // no menu
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;

if (RegisterClassEx (&winClass) == 0) // if window class fails to
initialise
return E_FAIL; // exit program

hWnd = CreateWindowEx (NULL, // no variations to window allowed
L"MyWindowsClass", // windows class name
L"Pirate/Ninja/Cowboy/Zombie Game", // windows
title
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // window
properties (on top and always visible
10, // windows x position
10, // windows y position
800, // windows width
600, // windows height
NULL, // not a child window
NULL, // no menu
hInstance, // program instance handler
NULL); // windows creation data (none needed)

// Initialize Direct3D
InitD3D (hWnd, hInstance);

// Show the window
ShowWindow (hWnd, SW_SHOWDEFAULT);
UpdateWindow (hWnd);

initTextures (hWnd);

// Enter the message loop
while (msg.message != WM_QUIT)
{
if (PeekMessage (&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
} // if
Render ();
checkKeyboard ();
} // while

cleanUp ();
UnregisterClass (L"D3D Tutorial", winClass.hInstance);
return 0;
} // wWinMain



.


Quantcast