Sign in to follow this  
Ran

DirectXTutorial.com Help

Recommended Posts

Hello all! I'm following the tutorial on DirectXTutorial.com and have ran into a snag. I can't get a sprite to display despite my best efforts. Instead of showing anything, my program just displays a black screen ( or whatever other color I set) and no sprite. Any idea what might be causing it? There is no error message. I can post the code if need be. thanks!

Share this post


Link to post
Share on other sites
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>


#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

//Global

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;

LPD3DXSPRITE d3dspt; //D3D Sprite interface

LPDIRECT3DTEXTURE9 sprite; //sprite pointer



void initD3D(HWND hWnd);
void render_frame(int); //render a frame
void cleanD3D(void);

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// the handle for the window, filled by a function
HWND hWnd;
// this struct holds information for the window class
WNDCLASSEX wc;

int color = 1;

// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));

// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass1";

// register the window class
RegisterClassEx(&wc);

// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
"WindowClass1", // name of the window class
"Our First DirectX Program", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
600, // width of the window
480, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL

// display the window on the screen
ShowWindow(hWnd, nCmdShow);

// enter the main loop:
initD3D(hWnd);
// this struct holds Windows event messages
MSG msg;


//New code: peek message routine.
while(TRUE)
{

DWORD starting_point = GetTickCount(); // Timer start


if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Check to see if we are quitting
if (msg.message == WM_QUIT)
break;

//Otherwise lets translate and move on
TranslateMessage(&msg);
DispatchMessage(&msg);
}


//Life goes on here
//if (color < 200) color++;
//else
// color = 1;



render_frame(color);
//Wait the timer
while((GetTickCount() - starting_point) < 25);




}
cleanD3D();
// return this part of the WM_QUIT message to Windows (or return 0)
return msg.wParam;
}


//Functions and stuff



// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch(message)
{
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
}

// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}


void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);


D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd; // Set our window to the Direct3D interface

d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

//create the sprite object
D3DXCreateSprite(d3ddev, &d3dspt);
D3DXCreateTextureFromFile(d3ddev, "W.BMP", &sprite);

return;
}

void render_frame(int color)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

// do stuff

d3dspt->Begin(NULL); //Sprite engine requires a seperate start/stop
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f); //Center at the upper left
D3DXVECTOR3 position(10.0f, 10.0f, 0.0f);
d3dspt->Draw(sprite, NULL, ¢er, &position, D3DCOLOR_XRGB(255, 0, 0));


d3dspt->End();

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);

return;
}

void cleanD3D(void)
{
sprite->Release();
d3ddev->Release(); //close the device
d3d->Release(); // close the Directx interface

return;
}

Share this post


Link to post
Share on other sites
I don't see anything different from your and that tutorial except for the color value passed in the sprites draw function. A value of 0xFFFFFFFF, or D3DCOLOR_XRGB(255,255,255) as the tutorial notes, would maintain the sprites original texture color/alpha.

Share this post


Link to post
Share on other sites
You need to proofread your variables, why did you put ¢er in,
instead of center?


D3DXVECTOR3 center(0.0f, 0.0f, 0.0f); //Center at the upper left
D3DXVECTOR3 position(10.0f, 10.0f, 0.0f);
d3dspt->Draw(sprite, NULL, ¢er, &position, D3DCOLOR_XRGB(255, 0, 0));



whereas, it should be:


D3DXVECTOR3 center(0.0f, 0.0f, 0.0f); //Center at the upper left
D3DXVECTOR3 position(10.0f, 10.0f, 0.0f);
d3dspt->Draw(sprite, NULL, &center, &position, D3DCOLOR_XRGB(255, 0, 0));

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this