000
03.09.2007, 16:02 Uhr
~TheN00b
Gast
|
Moin Leute,
hab mir vor ein paar monaten ein Buch über Spieleprogrammierung mit DirectX gekauft.
Im Buch wird eine Klasse beschrieben, die mehrere Direct3D Funktionen zusammenfasst.
Leider ist diese Klasse in mehrere Module aufgeteilt. Ich möchte den Quelltext aber in einem
Modul schreiben. Leider funktioniert das nicht, denn es kommen tausende Fehlermeldungen.
Der Quelltext mit der Klasse lautet wiefolgt:
C++: |
#include <d3d9.h> #include <d3dx9.h> #include <dxerr9.h>
HWND CreateMainWindow(HINSTANCE hInstance);
LRESULT CALLBACK MessageHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
HWND hWnd = 0;
const int SCR_WIDTH = 800; const int SCR_HEIGHT = 600;
CDirect3D Direct3D;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { hWnd = CreateMainWindow(hInstance);
if(0 == hWnd) { MessageBox(0, "Fenster konnte nicht erzeugt werden", "Fehler", MB_OK); return -1; }
if(!Direct3D.Init(hWnd)) { return -1; }
Direct3D.SetClearColor(D3DCOLOR_XRGB(0, 0, 0xFF));
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
return 0; }
HWND CreateMainWindow(HINSTANCE hInstance) { WNDCLASSEX wndClass = { sizeof(WNDCLASSEX), CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW, MessageHandler, 0, 0, hInstance, LoadIcon(NULL, IDI_WINLOGO), LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), NULL, "WindowClass", LoadIcon(NULL, IDI_WINLOGO) };
RegisterClassEx(&wndClass);
return CreateWindowEx(NULL, "WindowClass", "Direct3D Class", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 50, 50, SCR_WIDTH, SCR_HEIGHT, NULL, NULL, hInstance, NULL); }
LRESULT CALLBACK MessageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { D3DCOLOR ClearColor = D3DCOLOR_XRGB(0, 0, 0xFF);
switch(msg) { case WM_DESTROY: PostQuitMessage(0); return 0; break;
case WM_KEYDOWN: switch(wParam) { case VK_ESCAPE: DestroyWindow(hWnd); break; } break;
case WM_PAINT:
Direct3D.BeginScene();
Direct3D.DrawText("Hallo Direct3D", 100, 100, D3DCOLOR_XRGB(0xFF, 0, 0));
Direct3D.EndScene();
break; }
return DefWindowProc(hwnd, msg, wParam, lParam); }
class CDirect3D { public:
CDirect3D(); virtual ~CDirect3D();
BOOL Init(HWND hWnd, BOOL bWindowed = TRUE);
void SetClearColor(D3DCOLOR Color);
void DrawText(LPCSTR Text, int x, int y, D3DCOLOR TextColor);
void BeginScene(void); void EndScene(void);
protected:
void CreateFont(void); void CleanUp(void);
D3DCOLOR m_ClearColor;
LPDIRECT3D9 m_lpD3D; LPDIRECT3DDEVICE9 m_lpD3DDevice; LPD3DXFONT m_lpD3DFont; };
CDirect3D::CDirect3D() { m_lpD3D = NULL; m_lpD3DDevice = NULL; m_lpD3DFont = NULL; }
CDirect3D::~CDirect3D() { CleanUp(); }
BOOL CDirect3D::Init(HWND hWnd, BOOL bWindowed) { m_lpD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(NULL == m_lpD3D) { return FALSE; }
D3DPRESENT_PARAMETERS PParams; ZeroMemory(&PParams, sizeof(PParams));
PParams.SwapEffect = D3DSWAPEFFECT_DISCARD; PParams.hDeviceWindow = hWnd; PParams.Windowed = bWindowed;
PParams.BackBufferWidth = SCR_WIDTH; PParams.BackBufferHeight = SCR_HEIGHT; PParams.BackBufferFormat = D3DFMT_A8R8G8B8;
HRESULT hr;
if(FAILED(hr = m_lpD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PParams, &m_lpD3DDevice))) { const char* Err = DXGetErrorDescription9(hr);
DXTRACE_MSG(Err);
return FALSE; }
CreateFont();
return TRUE; }
void CDirect3D::SetClearColor(D3DCOLOR Color) { m_ClearColor = Color; }
void CDirect3D::DrawText(LPCSTR Text, int x, int y, D3DCOLOR TextColor) { RECT r = { x, y, 0, 0 };
m_lpD3DFont->DrawText(NULL, Text, -1, &r, DT_CALCRECT, TextColor);
m_lpD3DFont->DrawText(NULL, Text, -1, &r, DT_CENTER, TextColor); }
void CDirect3D::BeginScene(void) { m_lpD3DDevice->Clear(0, 0, D3DCLEAR_TARGET, m_ClearColor, 0, 0);
m_lpD3DDevice->BeginScene(); }
void CDirect3D::EndScene(void) { m_lpD3DDevice->EndScene();
m_lpD3DDevice->Present(0, 0, 0, 0); }
void CDirect3D::CreateFont(void) { D3DXFONT_DESC FontDesc = { 0 };
strcpy(FontDesc.FaceName, "Arial");
D3DXCreateFontIndirect(m_lpD3DDevice, &FontDesc, &m_lpD3DFont); }
void CDirect3D::CleanUp(void) { if(NULL != m_lpD3DFont) { m_lpD3DFont->Release(); m_lpD3DFont = NULL; }
if(NULL != m_lpD3DDevice) { m_lpD3DDevice->Release(); m_lpD3DDevice = NULL; }
if(NULL != m_lpD3D) { m_lpD3D->Release(); m_lpD3D = NULL; } }
|
Ich benutze übrigens Code::Blocks als IDE. Bitte helft mir
Mgf, TheNoob |