I am creating a window wrapper class in c++ but I cannot seem to fix this bug. it seems that when my window process callback gets called it is not retrieving the this pointer I inputted for the extra information when creating the window. I did a little bit of debugging to find out that when I reinterpret it to a Window* it is NULL and every variable is uninitialized. here is Window.h
#pragma once
#include <Windows.h>
#include <string>
class Window
{
private:
class WindowClass
{
private:
static WindowClass wndClass;
WindowClass();
HINSTANCE hInst;
const char* className = "Sadie Game Engine";
WindowClass(WindowClass&) = delete;
WindowClass operator=(WindowClass&) = delete;
~WindowClass();
public:
static WindowClass& getInstance();
HINSTANCE getHInstance();
const char* getClassName();
};
private:
const char* windowTitle;
int width, height;
int windowX, windowY;
HWND windowHandle;
public:
bool keyStates[256];
Window(int width, int height, int x, int y, const char* title);
void setWindowTitle(std::string str);
static LRESULT CALLBACK handleMsgCallBack(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT handleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
~Window();
};
and here is Window.cpp
#include "Window.h"
///
/// WindowClass
///
Window::WindowClass Window::WindowClass::wndClass;
Window::WindowClass::WindowClass()
{
hInst = GetModuleHandleA(nullptr);
WNDCLASSEXA wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = handleMsgCallBack;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.lpszClassName = className;
wc.hIcon = nullptr;
wc.hIconSm = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
RegisterClassExA(&wc);
}
Window::WindowClass::~WindowClass()
{
UnregisterClassA(className, hInst);
}
Window::WindowClass& Window::WindowClass::getInstance()
{
return wndClass;
}
HINSTANCE Window::WindowClass::getHInstance()
{
return hInst;
}
const char* Window::WindowClass::getClassName()
{
return className;
}
///
/// Window
///
Window::Window(int _width, int _height, int x, int y, const char* title)
{
width = _width;
height = _height;
windowX = x;
windowY = y;
windowTitle = title;
RECT wr;
wr.left = 100;
wr.right = width + wr.left;
wr.top = 100;
wr.bottom = height + wr.top;
AdjustWindowRect(&wr, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, FALSE);
windowHandle = CreateWindowExA(0l, WindowClass::getInstance().getClassName(),
windowTitle, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
windowX, windowY, width, height, nullptr, nullptr, WindowClass::getInstance().getHInstance(), this);
ShowWindow(windowHandle, SW_SHOW);
}
void Window::setWindowTitle(std::string str)
{
SetWindowTextA(windowHandle, str.c_str());
}
LRESULT Window::handleMsgCallBack(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Window* const windowPtr = reinterpret_cast<Window*>(GetWindowLongPtrA(hWnd, GWLP_USERDATA));
return windowPtr->handleMsg(hWnd, msg, wParam, lParam);
}
LRESULT Window::handleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
keyStates[wParam] = true;
break;
case WM_SYSKEYUP:
case WM_KEYUP:
keyStates[wParam] = false;
break;
case WM_CHAR:
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProcA(hWnd, msg, wParam, lParam);
}
Window::~Window()
{
DestroyWindow(windowHandle);
}
Any help would be appreciated.