Directx 9渲染问题 [英] Directx 9 Rendering Problem

查看:73
本文介绍了Directx 9渲染问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我这里有两个类,一个用于显示窗口,另一个用于初始化directx并在该窗口上绘制。这是我的类和main.cpp文件的代码



Display.h



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); //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






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; //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





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

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







Directx9api.cpp






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)
	{
		//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





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: //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;
}





每次调试上面的代码我都会得到一个异常,说[某地址]的第一次机会异常。在0x0000000处访问语音并且黄色光标指向

mDirectx9Device->在Directx9Api :: DrawTest()中的Clear()





我不知道问题是什么。任何人都可以提供帮助,我的想法已经用完。我真的很想学习使用directx进行图形编程。



谢谢。



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.

推荐答案

你的测试
mDirectx9Obj->CreateDevice

是错误的 - 你正在测试NULL - 该调用返回一个HR,所以我猜它失败了,你没有抓住它



会给你一个NULL设备,这会在你第一次使用它时引起异常 - 例如,在Clear()

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()


这篇关于Directx 9渲染问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆