Hi all
I have two classes here, one to display a window and one to initialize directx and draw on that window. Here is my code for both classes and the main.cpp file
Display.h
#pragma once
#include <Windows.h>
class Display
{
private:
const char *mWindowTitle;
WNDCLASSEX mWndClass;
HWND mHwnd;
HINSTANCE mHinstance;
unsigned int mWidth;
unsigned int mHeight;
static Display* mInstance;
static bool mInstanceFlag;
Display(void); public:
~Display(void);
bool InitializeWindow(bool fullscrn);
void SetWindowProperties(int width, int height,char* windowtitle, WNDPROC messagefunc, HINSTANCE hinst);
HWND GetWindowHandle();
static Display* GetDisplayInstance();
long GetDisplayHeight();
long GetDisplayWidth();
};
Display.cpp
#include "display.h"
bool Display::mInstanceFlag = false;
Display* Display::mInstance = NULL;
Display::Display()
{
}
void Display::SetWindowProperties(int width, int height,char *windowtitle, WNDPROC messagefunc, HINSTANCE hinst)
{
mWindowTitle = windowtitle;
mWidth = width;
mHeight = height;
mHwnd = NULL;
mHinstance = hinst;
mWndClass.cbSize = sizeof(WNDCLASSEX);
mWndClass.style = 0; mWndClass.lpfnWndProc = messagefunc;
mWndClass.cbClsExtra = 0;
mWndClass.cbWndExtra = 0;
mWndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
mWndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
mWndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
mWndClass.lpszClassName = "EngineGameWindow";
mWndClass.lpszMenuName = NULL;
mWndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
mWndClass.hInstance = mHinstance;
}
Display::~Display(void)
{
mInstanceFlag = false;
}
bool Display::InitializeWindow(bool fullscrn)
{
if(!RegisterClassEx(&mWndClass))
MessageBox(NULL,"Sorry could not register window class","Window Registration Error", MB_ICONEXCLAMATION | MB_OK);
if(fullscrn)
{
mHeight = GetSystemMetrics(SM_CYSCREEN);
mWidth = GetSystemMetrics(SM_CXSCREEN);
mHwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"EngineGameWindow", mWindowTitle,
WS_POPUP,0,0,mWidth, mHeight,NULL,NULL,mHinstance,NULL);
}
else
{
mHwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"EngineGameWindow", mWindowTitle,
WS_OVERLAPPEDWINDOW,GetSystemMetrics(SM_CXSCREEN)/4,
GetSystemMetrics(SM_CYSCREEN)/8,mWidth, mHeight,NULL,NULL,mHinstance,NULL);
}
if(!mHwnd)
{
MessageBox(NULL,"Window not created","Window Creation Error",MB_ICONEXCLAMATION | MB_OK);
return false;
}
ShowWindow(mHwnd, SW_SHOW);
UpdateWindow(mHwnd);
return true;
}
HWND Display::GetWindowHandle()
{
return mHwnd;
}
long Display::GetDisplayWidth()
{
return mWidth;
}
long Display::GetDisplayHeight()
{
return mHeight;
}
Display* Display::GetDisplayInstance()
{
if(!mInstanceFlag)
mInstanceFlag = true;
mInstance = new Display();
return mInstance;
}
Directx9api.h
#pragma once
#include "igraphicsapi.h"
#include "display.h"
class Directx9Api
{
private:
Display *displayobject;
LPDIRECT3D9 mDirectx9Obj;
D3DPRESENT_PARAMETERS mDirectx9Presentation;
IDirect3DDevice9 *mDirectx9Device;
static Directx9Api* mInstance;
static bool mInstanceIsSet;
Directx9Api(void);
public:
~Directx9Api(void); bool InitializeApi(HWND hwnd);
static Directx9Api* GetInstance();
LPDIRECT3D9 GetDirectx9Obj(); IDirect3DDevice9* GetDirectxDevice();
bool IsDeviceLost();
void DrawTest();
};
Directx9api.cpp
#include "directx9api.h"
bool Directx9Api::mInstanceIsSet = false;
Directx9Api* Directx9Api::mInstance = NULL;
Directx9Api::Directx9Api(void)
{
}
Directx9Api::~Directx9Api(void)
{
}
bool Directx9Api::InitializeApi(HWND hwnd)
{
mDirectx9Device = NULL;
displayobject = Display::GetDisplayInstance();
mDirectx9Obj = Direct3DCreate9(D3D_SDK_VERSION);
if(!mDirectx9Obj)
MessageBox(0,"Error no directx 9",0,0);
ZeroMemory(&mDirectx9Presentation, sizeof(mDirectx9Presentation));
mDirectx9Presentation.Windowed = true;
mDirectx9Presentation.BackBufferCount = 1;
mDirectx9Presentation.SwapEffect = D3DSWAPEFFECT_DISCARD;
mDirectx9Presentation.hDeviceWindow = hwnd;
mDirectx9Presentation.MultiSampleType = D3DMULTISAMPLE_NONE;
mDirectx9Presentation.BackBufferFormat = D3DFMT_UNKNOWN;
mDirectx9Presentation.MultiSampleQuality = 0;
mDirectx9Presentation.BackBufferHeight = displayobject->GetDisplayHeight();
mDirectx9Presentation.BackBufferWidth = displayobject->GetDisplayWidth();
mDirectx9Presentation.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
mDirectx9Presentation.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
mDirectx9Presentation.EnableAutoDepthStencil = true;
mDirectx9Presentation.AutoDepthStencilFormat = D3DFMT_D24S8;
mDirectx9Presentation.Flags = 0;
if(mDirectx9Obj->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&mDirectx9Presentation, &mDirectx9Device)== NULL)
{
return false;
}
}
Directx9Api* Directx9Api::GetInstance()
{
if(!mInstanceIsSet)
mInstanceIsSet = true;
mInstance = new Directx9Api();
return mInstance;
}
LPDIRECT3D9 Directx9Api::GetDirectx9Obj()
{
return mDirectx9Obj;
}
IDirect3DDevice9* Directx9Api::GetDirectxDevice()
{
return mDirectx9Device;
}
bool Directx9Api::IsDeviceLost()
{
HRESULT hr = mDirectx9Device->TestCooperativeLevel();
return true;
}
void Directx9Api::DrawTest()
{
mDirectx9Device->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
mDirectx9Device->BeginScene();
mDirectx9Device->EndScene();
mDirectx9Device->Present(0,0,0,0);
}
Main.cpp
#include <Windows.h>
#include <tchar.h>
#include <iostream>
#include "display.h"
#include "directx9api.h"
using namespace std;
Display *display = Display::GetDisplayInstance();
Directx9Api *dx = Directx9Api::GetInstance();
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
if(msg == WM_CHAR && wparam == VK_ESCAPE)
{
PostQuitMessage(0);
return 0;
}
switch(msg)
{
case WM_CLOSE: DestroyWindow(hwnd);
break;
case WM_DESTROY: PostQuitMessage(0);
break;
case WM_PAINT:
ValidateRect(hwnd, 0);
break;
default:
return DefWindowProc(hwnd,msg,wparam,lparam);
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int main(HINSTANCE hinst, HINSTANCE hPrevinstance, LPSTR lpcmdline, int ncmdshow)
{
MSG msg ={0};
display->SetWindowProperties(800,600,"Engine",WndProc,hinst);
display->InitializeWindow(false);
if(!dx->InitializeApi(display->GetWindowHandle()))
{
MessageBox(0,"Directx 9 was not initialized",0,0);
return 0;
}
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
cout << "Is running" << endl;
dx->DrawTest();
}
}
return 0;
}
Everytime i debug the above code i get an exception saying First chance exception at [some address]. Access voilation at 0x0000000 and a yellow cursor points to
mDirectx9Device->Clear() inside Directx9Api::DrawTest()
I don't Know what the problem is. I have run out of ideas can anyone help. I really want to learn graphics programming with directx.
Thanks.