我正在关注Youtube的2D DirectX图形编程教程。该项目包含3个文件。它包含一个main.cpp文件,用于创建窗口,图形类和图形标题。当代码尝试在图形类中创建D2D1工厂时,我收到Unhandled Exception错误。我在Windows Vista上使用Visual Studio 10。以下是Visual Studio 10中输出窗口的3个文件和输出:
main.cpp中:
includeKey
graphics.h中:
#include <Windows.h>
#include <d2d1.h>
#include "graphics.h"
Graphics *graphics;
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
if(uMsg == WM_DESTROY) { PostQuitMessage(0); return 0; }
return DefWindowProc(hwnd, uMsg, wParam, lParam);
if(uMsg == WM_PAINT)
{
graphics->BeginDraw();
graphics->ClearScrean(0.0f, 0.0f, 0.5f);
graphics->DrawCircle(100, 100, 50, 1.0f, 0.0, 0.0, 1.0);
graphics->EndDraw();
}
}
int WINAPI wWinMain(
HINSTANCE hInstance,
HINSTANCE precInstance,
LPWSTR cmd,
int nCmdShow)
{
WNDCLASSEX windowclass;
ZeroMemory(&windowclass, sizeof(WNDCLASSEX));
windowclass.cbSize = sizeof(WNDCLASSEX);
windowclass.hbrBackground = (HBRUSH) COLOR_WINDOW;
windowclass.hInstance = hInstance;
windowclass.lpfnWndProc = WindowProc;
windowclass.lpszClassName = "MainWindow";
windowclass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&windowclass);
RECT rect = { 0, 0, 800, 600 };
AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, false,
WS
HWND windowhandle = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
"MainWindow",
"DirectX",
WS_OVERLAPPEDWINDOW,
100,
100,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
hInstance,
0);
if(!windowhandle) { return -1; }
if(!graphics->Init(windowhandle))
{
delete graphics;
return -1;
}
ShowWindow(windowhandle, nCmdShow);
MSG message;
while(GetMessage(&message, NULL, 0, 0))
{
DispatchMessage(&message);
}
delete graphics;
return 0;
}
Graphics.cpp:
#pragma once
#include <Windows.h>
#include <d2d1.h>
class Graphics
{
ID2D1Factory* factory;
ID2D1HwndRenderTarget* renderTarget;
public:
Graphics();
~Graphics();
bool Init(HWND windowhandle);
void BeginDraw() { renderTarget->BeginDraw(); }
void EndDraw() { renderTarget->EndDraw(); }
void ClearScrean(float r, float g, float b);
void DrawCircle(float x,
float y, float radius, float r, floatg,float b, float a);
};
调试输出:
#include "Graphics.h"
Graphics::Graphics()
{
factory = NULL;
renderTarget = NULL;
}
Graphics::~Graphics()
{
if(factory) factory->Release();
if(renderTarget) renderTarget->Release();
}
bool Graphics::Init(HWND windowHandle)
{
HRESULT res = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&factory);
if(res != S_OK) { return false; }
RECT rect;
GetClientRect(windowHandle, &rect);
res = factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
windowHandle,
D2D1::SizeU(rect.right, rect.bottom)),
&renderTarget);
if(res != S_OK) { return false; }
return true;
}
void Graphics::ClearScrean(float r, float g, float b)
{
renderTarget->Clear(D2D1::ColorF(r, g, b));
}
void Graphics::DrawCircle(float x, float y, float radius,
float r, float g, float b, float a)
{
ID2D1SolidColorBrush* brush;
renderTarget->CreateSolidColorBrush(D2D1::ColorF(r, g, b, a), &brush);
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(x, y),
radius, radius), brush, 3.0f);
brush->Release();
}
答案 0 :(得分:0)
您正在访问未初始化的指针:
Graphics *graphics;
//...
if(!graphics->Init(windowhandle)) // <-- CRASH
{...}
要解决此问题,请使用new:
分配内存Graphics *graphics;
graphics = new Graphics;
if(!graphics->Init(windowhandle))
{...}