d3ddev Functions not working in GameEngine!
Here is GameEngine.h:
#include <windows.h>
#include <d3d9.h>
#pragma comment (lib,"d3d9.lib")
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ?1 :0)
#define HR_VERIFY(n)if(FAILED(n)) { MessageBox(NULL, L"Error Occured", L"Error",0); }
#define SAFE_RELEASE(n) {if(n) { (n)->Release(); } }
class GameEngine
{
public:
//Constructor / Destructor
GameEngine(LPCWSTR name, LPCWSTR name2, HINSTANCE hInst,int ShowCmd,bool fullscreen,bool depth,int red,int green,int blue,int swidth,int sheight);//
//windows
int SetUp();//
void ShowMsgBox(LPCWSTR text, LPCWSTR text2);//
HWND getHwnd(){return hwnd; }//
//Graphics
bool returnFullScreen() {return fullscr?true :false; }//
void render_frame();//
private:
//windows
HWND hwnd;//
HINSTANCE hInstance;//
int nShowCmd;//
LPCWSTR className, windowName;//
//graphics
LPDIRECT3D9 d3d;//
LPDIRECT3DDEVICE9 d3ddev;//
int cred, cgreen, cblue;//
bool fullscr;//
bool depthed;//
void initD3D();//
void cleanD3D();//
int width, height;
};
Here is GameEngine.cpp:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void userRender();
void QuitQuery();
GameEngine::GameEngine(LPCWSTR name, LPCWSTR name2, HINSTANCE hInst,int ShowCmd,bool fullscreen,bool depth,int red,int green,int blue,int swidth =640,int sheight =480)
{
className = name;
windowName = name2;
hInstance = hInst;
nShowCmd = ShowCmd;
fullscr = fullscreen;
depthed = depth;
cred = red;
cblue = blue;
cgreen = green;
}
int GameEngine::SetUp()
{
WNDCLASSEX wc;
ZeroMemory(&wc,sizeof(WNDCLASSEX));
wc.cbSize =sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// wc.hbrBackground = (HBRUSH)COLOR_WINDOW; // not needed any more
wc.lpszClassName = className;
RegisterClassEx(&wc);
hwnd = CreateWindowEx(NULL,
className,
windowName,
WS_EX_TOPMOST | WS_POPUP,// fullscreen values
0,0,// the starting x and y positions should be 0
width, height,// set the window to 640 x 480
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nShowCmd);
// set up and initialize Direct3D
initD3D();
// enter the main loop:
MSG msg;
while(TRUE)
{
DWORD starting_point = GetTickCount();
if (PeekMessage(&msg, NULL,0,0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render_frame();
// check the 'escape' key
if(KEY_DOWN(VK_ESCAPE))
PostMessage(hwnd, WM_DESTROY,0,0);
while ((GetTickCount() - starting_point) <25);
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
void GameEngine::initD3D()
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);// create the Direct3D interface
D3DPRESENT_PARAMETERS d3dpp;// create a struct to hold various device information
ZeroMemory(&d3dpp,sizeof(d3dpp));// clear out the struct for use
d3dpp.Windowed =FALSE;// program fullscreen, not windowed
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;// discard old frames
d3dpp.hDeviceWindow = hwnd;// set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;// set the back buffer format to 32-bit
d3dpp.BackBufferWidth = width;// set the width of the buffer
d3dpp.BackBufferHeight = height;// set the height of the buffer
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
}
void GameEngine::cleanD3D()
{
SAFE_RELEASE(d3d);
SAFE_RELEASE(d3ddev);
}
void GameEngine::render_frame()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(cred, cgreen, cblue),1.0f,0);
d3ddev->BeginScene();// begins the 3D scene
userRender();
d3ddev->EndScene();// ends the 3D scene
d3ddev->Present(NULL, NULL, NULL, NULL);// displays the created frame on the screen
}
void GameEngine::ShowMsgBox(LPCWSTR text, LPCWSTR text2)
{
MessageBox(hwnd, text, text2,0);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
QuitQuery();
PostQuitMessage(0);
return0;
}break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
Here is WinMain.cpp:
GameEngine* ge;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
ge =new GameEngine(L"Jeremy", L"Lord", hInstance, nCmdShow,true,false,0,0,0,640,480);
ge->SetUp();
}
void userRender()
{
}
void QuitQuery()
{
}

