Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
#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); //ctor
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

C++
#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; //Style of the class
	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;
	}

	//Show the window and update the window
	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

C++
#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); // ctor

public:
	~Directx9Api(void); // dtor
	bool InitializeApi(HWND hwnd); 
	static Directx9Api* GetInstance(); 
	LPDIRECT3D9 GetDirectx9Obj(); //
	IDirect3DDevice9* GetDirectxDevice(); 
	bool IsDeviceLost();
	void DrawTest();
};



Directx9api.cpp

C++
#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)
	{
		//MessageBox(0,"Directx device not created",0,0);
		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

C++
#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: //when the window is to be closed
		//close the window 
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY: // When the entire window class is destroyed
		// 
		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;
	}

	//Step 3 the Message loop
	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.
Posted
Updated 24-Feb-15 10:20am
v2
Comments
[no name] 24-Feb-15 16:27pm    
Also exception without Debugger? I know very strange question. But I'm using bcb (borland) and often I face Problems _only while_ debugging. No comment....
Member 10809885 24-Feb-15 17:16pm    
used the local windos debugger in vs2012 and i got that exception. I could not post a screenshot. sorry
SoMad 24-Feb-15 16:47pm    
Is this your actual code? I don't see a return true in InitializeApi().
Member 10809885 24-Feb-15 17:17pm    
@SoMad sorry i had to edit the code a bit so it looked more readable.

1 solution

your test at
mDirectx9Obj->CreateDevice
is wrong - you're testing for NULL - that call returns an HR, so i'm guessing it's failing, you're not catching it

which would give you a NULL device, which would cause an exception first time you use it - say, at Clear()
 
Share this answer
 
Comments
Member 10809885 24-Feb-15 18:41pm    
@barneyman I checked for failure like this and i got the message box telling directx 9 had failed from the main.cpp file. Am i still doing something wrong.

HRESULT hr = mDirectx9Obj->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&mDirectx9Presentation, &mDirectx9Device);
if(FAILED(hr))
{
//MessageBox(0,"Directx device not created",0,0);
return false;
}
return true;
Member 10809885 2-Mar-15 10:47am    
Thanks very much. After trying so many times so many times it turns out an HRESULT of 1 is failed and 0 is success
barneyman 2-Mar-15 20:41pm    
no, an HRESULT of 1 is S_FALSE, 0 is S_OK
a -ve hresult is failure


Member 10809885 12-Mar-15 23:53pm    
oh sorry thats what i meant to say...thanks
barneyman 24-Feb-15 19:17pm    
you'll have to be more explicit - by 'failed' do you mean your DX initialisation is failing, or you're still getting an exception?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900