Visual C++ and Direct2D

  • Thread starter Thread starter franco395
  • Start date Start date
F

franco395

Guest
Hello people, I recently started working with direct2D and I am creating a 2D minigame but I have a series of problems, when executing there is an error with the pointer "this" in the RenderWindow method and the truth is that I do not know what to do. They could help at least or correct it. I would appreciate it. Thank you.


#include "framework.h"
#include "GameProject1.h"
#include <d2d1.h>
#include <d2d1helper.h>

class App {
public:
App(HINSTANCE Instance);
void RegisterAppWindowClass();
void InitApp();
void RunMessageLoop();
void RenderWindow();
private:
HINSTANCE AppInstance;
HWND AppWindow;
WNDCLASSEXW wcex;
MSG message;
ID2D1Factory* ID2D1FactoryPtr;
ID2D1HwndRenderTarget* ID2D1HwndRenderTargetPtr;
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
};

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,_In_opt_ HINSTANCE hPrevInstance,_In_ LPWSTR lpCmdLine,_In_ int nCmdShow) {
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
//CoInitialize(NULL);
App AppObj = App(hInstance);
AppObj.RegisterAppWindowClass();
AppObj.InitApp();
AppObj.RunMessageLoop();
CoUninitialize();
return 0;
}

App::App(HINSTANCE Instance) {
AppInstance = Instance;
AppWindow = nullptr;
wcex = { 0 };
message = { 0 };
ID2D1FactoryPtr = nullptr;
ID2D1HwndRenderTargetPtr = nullptr;
}

void App::RegisterAppWindowClass() {
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEXW);
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hbrBackground = nullptr;
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wcex.hIcon = LoadIconW(AppInstance, MAKEINTRESOURCEW(IDI_GAMEPROJECT1));
wcex.hIconSm = LoadIconW(AppInstance, MAKEINTRESOURCEW(IDI_SMALL));
wcex.hInstance = AppInstance;
wcex.lpfnWndProc = App::WndProc;
wcex.lpszClassName = L"APP_CLASS";
wcex.lpszMenuName = 0;
wcex.style = CS_DBLCLKS;
RegisterClassExW(&wcex);
}

void App::InitApp() {
D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_SINGLE_THREADED, &ID2D1FactoryPtr);
AppWindow = CreateWindowExW(0, L"APP_CLASS", L"GameProject1", WS_OVERLAPPEDWINDOW xor WS_THICKFRAME, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, nullptr, nullptr, AppInstance, this);
ShowWindow(AppWindow, SW_SHOWDEFAULT);
ID2D1FactoryPtr->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(AppWindow, D2D1::SizeU(800, 600)), &ID2D1HwndRenderTargetPtr);
}

void App::RunMessageLoop() {
while (GetMessageW(&message, nullptr, 0, 0)) {
TranslateMessage(&message);
DispatchMessageW(&message);
}
}

void App::RenderWindow() {
ID2D1HwndRenderTargetPtr->BeginDraw();
ID2D1HwndRenderTargetPtr->DrawLine(D2D1::Point2F(10,10),D2D1::Point2F(100,100),nullptr);
ID2D1HwndRenderTargetPtr->EndDraw();
}

LRESULT CALLBACK App::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT Lresult = 0;

if (message == WM_CREATE) {
LPCREATESTRUCTW lpCreateStructW = (CREATESTRUCTW*)lParam;
App* AppPtr_ = static_cast<App*>(lpCreateStructW->lpCreateParams);
SetWindowLongW(hWnd, GWLP_USERDATA, PtrToLong(AppPtr_));
Lresult = 0;
}
else {
App* AppPtr__ = reinterpret_cast<App*>(static_cast<LONG_PTR>(GetWindowLongW(hWnd, GWLP_USERDATA)));
bool WasHandled = false;

if (AppPtr__ != nullptr) {
switch (message) {
case WM_DESTROY:
WasHandled = true;
Lresult = 0;
PostQuitMessage(0);
break;
case WM_PAINT:
Lresult = 0;
AppPtr__->RenderWindow();
break;
}
}

if (!WasHandled)
{
Lresult = DefWindowProcW(hWnd, message, wParam, lParam);
}
}

return Lresult;
}

Continue reading...
 
Back
Top