Hi All,
I am trying to draw a sprite to the screen. So far, when making small games I have loaded textures and drawn them in a very un-OO way and just used OO techniques for the game mechanics. Now, I wanted to load the textures in to the object and draw them from their own method too. I wrote what I thought would work but the sprite is not being drawn to the screen. The background get drawn fine and there are no compile or runtime errors. Can someone spot what I am doing wrong please?
Headers.h
#ifndef HEADERS_H
#define HEADERS_H
#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
LPDIRECT3D9 d3d; // Pointer to Direct3D interface
LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface
#endif
Customer.h
#include "Headers.h";
class Customer{
private:
LPDIRECT3DTEXTURE9 textureT;
D3DXVECTOR3 position;
D3DXVECTOR3 center;
public:
Customer()
{
D3DXCreateTextureFromFileEx(d3dDevice, "man.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,
D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &textureT);
D3DXVECTOR3 position (10.0f, 10.0f, 0.0f);
D3DXVECTOR3 center (0.0f, 0.0f, 0.0f);
}
void Draw()
{
d3dSprite->Draw(textureT, NULL, ¢er, &position, D3DCOLOR_XRGB(255, 255, 255));
}
};
Main.cpp
/**********************************************
*
* Title: DirectX 9.0 Standard Fullscreen Template
* Author: Chris Ramsey
* Created: November 2012
*
* This program is a standard template for a full screen
* directX 9.0 application.
*
* Any code from here can be used freely by
* anyone for any reason.
*
***********************************************/
#include "Headers.h."
#include "Customer.h"
Customer customer1;
// DirectX 3D Vectors used for sprite positions
D3DXVECTOR3 center, backgroundP;
// Textures
LPDIRECT3DTEXTURE9 backgroundT;
void initDirectX(HWND hWnd); // Initializes Direct3D Graphics
void render(); // Render graphics
void cleanUp(); // Cleans everything up and releases memory
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd; // The handle to the window function
WNDCLASSEX window; // Pointer to window struct
ZeroMemory(&window, sizeof(WNDCLASSEX)); // Clears window class so we can use it
window.cbSize = sizeof(WNDCLASSEX); // Size of window
window.style = CS_HREDRAW | CS_VREDRAW; // Redraws the entire window if a movement or size adjustment changes the height of the client area.
window.lpfnWndProc = WindowProc; // Pointer to the window procedure
window.hInstance = hInstance; // Handle to current instance
window.hCursor = LoadCursor(NULL, IDC_ARROW); // We'll stick with the normal cursor here
window.lpszClassName = "Window"; // Gives the class a name
RegisterClassEx(&window); // Registers the window class
hWnd = CreateWindowEx(NULL,
"Window", // Name of the class
"Space Game", // Title of the window
WS_EX_TOPMOST | WS_POPUP, // Fullscreen
0, 0, // Position of window (0,0 for fullscreen)
SCREEN_WIDTH, SCREEN_HEIGHT, // Screen resolution (Uses global declaration)
NULL, // Parent window (None)
NULL, // Menus (None)
hInstance, // Application handle
NULL); // Set to NULL as we aren't using more than 1 window
ShowWindow(hWnd, nCmdShow); // Display window
initDirectX(hWnd); // Initialises the directX graphics
MSG msg = {0}; // msg holds the Windows events message queue
/************************************** Main game loop *************************************************/
while(TRUE) // Main game loop
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // If messages are waiting
{
TranslateMessage(&msg); // Translates the keystroke messages into the correct format
DispatchMessage(&msg); // Sends message to the windowsProc function
if(msg.message == WM_QUIT) // If the message was a quit message
break; // Breaks out of main game loop
}
else
{
render();
}
}
/*******************************************************************************************************/
// We are out of the main game loop (Due to a WM_QUIT signal)
cleanUp(); // Do some housekeeping before quitting
return msg.wParam; // Return the WM_QUIT message to Windows. End of program
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) // Sorts through messages
{
case WM_DESTROY: // When window has been closed
{
PostQuitMessage(0); // Closes the application
return 0;
}
break;
}
return DefWindowProc (hWnd, // Returns any messages the switch statement didn't pick up
message,
wParam,
lParam);
}
void initDirectX(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // Create DirectX9 interface
D3DPRESENT_PARAMETERS d3dParameters; // Pointer to DirectX9 parameters
ZeroMemory(&d3dParameters, sizeof(d3dParameters)); // Clears the structure so we can use it
d3dParameters.Windowed = FALSE; // Fullscreen
d3dParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Get rid of old framess
d3dParameters.hDeviceWindow = hWnd; // Sets the window to be used by Direct3D
d3dParameters.BackBufferFormat = D3DFMT_X8R8G8B8; // Sets the back buffer format to 32-bit
d3dParameters.BackBufferWidth = SCREEN_WIDTH; // Sets the width of the buffer
d3dParameters.BackBufferHeight = SCREEN_HEIGHT; // Sets the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT, // Creates a device class
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dParameters,
&d3dDevice);
D3DXCreateSprite(d3dDevice, &d3dSprite);
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
D3DXCreateTextureFromFileEx(d3dDevice, "background2.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,
D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &backgroundT);
D3DXVECTOR3 backgroundP(0,0,0);
}
void render()
{
d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10, 0, 32), 1.0f, 0); // Clears the screen to a dark blue
d3dDevice->BeginScene(); // Begins Direct3D scene
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND); // Begin sprite drawing
d3dSprite->Draw(backgroundT, NULL, ¢er, &backgroundP, D3DCOLOR_XRGB(255, 255, 255));
customer1.Draw();
d3dSprite->End(); // End drawing
d3dDevice->EndScene(); // Ends the Direct3D scene
d3dDevice->Present(NULL, NULL, NULL, NULL); // Presents the Direct3D scene (Displays to screen)
}
void cleanUp()
{
d3dDevice->Release(); // Closes the Direct3D device and releases memory
d3d->Release(); // Closes Direct3D and releases memory
}
Thanks