Win32 Class Design

Started by
-1 comments, last by greenvertex 11 years, 8 months ago
Alright I've been mulling over this one for a while now but can't seem to come up with any better solutions. I've created a small class that wraps the Win32 create app/add window functionality to start building my first DX11 game on. Problem is I want to try and support deployment to Win8 eventually so I have:


#define CONTEXT_WINDOW_NAME __TEXT("DXTestBed")
//#define CONTEXT_WINDOW_WIDTH 640
//#define CONTEXT_WINDOW_HEIGHT 480
#if NTDDI_VERSION <= NTDDI_WIN7
#include "Win32Context.h"
#endif


As a sort of "main" file in the project. And for Win32Context.h:


#ifndef _WIN_32_CONTEXT_H
#define _WIN_32_CONTEXT_H
#include <Windows.h>
#include <stdexcept>
#if UNICODE
typedef LPCWSTR pstr;
#else
typedef LPCSTR pstr;
#endif
#ifndef CONTEXT_WINDOW_NAME
#define CONTEXT_WINDOW_NAME __TEXT("")
#endif
#ifndef CONTEXT_WINDOW_WIDTH
#define CONTEXT_WINDOW_WIDTH CW_USEDEFAULT
#endif
#ifndef CONTEXT_WINDOW_HEIGHT
#define CONTEXT_WINDOW_HEIGHT CW_USEDEFAULT
#endif
static LRESULT CALLBACK windowProc(HWND h_window, UINT message, WPARAM w_param, LPARAM l_param) {
PAINTSTRUCT ps;
HDC hdc;
switch(message) {
case WM_PAINT:
hdc = BeginPaint(h_window, &ps);
EndPaint(h_window, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(h_window, message, w_param, l_param);
}
return 0;
}
class Win32Context {
static const pstr CLASS_NAME;
public:
Win32Context(HINSTANCE h_inst, int cmd_show) : h_instance(h_inst) {
WNDCLASSEX wcex;
DWORD wcex_size = sizeof(WNDCLASSEX);
ZeroMemory(&wcex, wcex_size);
wcex.cbSize = wcex_size;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = windowProc;
wcex.hInstance = h_instance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = CLASS_NAME;
if(!RegisterClassEx(&wcex))
throw std::runtime_error("Could not register window class.");
h_window = CreateWindow(
CLASS_NAME, CONTEXT_WINDOW_NAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CONTEXT_WINDOW_WIDTH, CONTEXT_WINDOW_HEIGHT,
NULL, NULL, h_instance, NULL);
if(!h_window)
throw std::runtime_error("Could not create window.");
ShowWindow(h_window, cmd_show);
}
~Win32Context() {}
private:
Win32Context(const Win32Context&);
const Win32Context& operator =(const Win32Context&);
HINSTANCE h_instance;
HWND h_window;
public:
int run() {
MSG msg = {0};
while(WM_QUIT != msg.message) {
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
};
const pstr Win32Context::CLASS_NAME(__TEXT("Win32Context"));
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
try {
Win32Context context(hInstance, nCmdShow);
return context.run();
}
catch(std::exception e) {
MessageBoxA(NULL, e.what(), "Error", MB_OK);
return E_FAIL;
}
}
#endif //_WIN_32_CONTEXT_H


Does any of this seem on the right track? I'm not terribly happy about the required forward defines for CONTEXT_* off the bat; anyone have any better way to approach that? I'm pretty new to Win32 and DX11 programming so the whole architecture side of things is involving a lot of guess work on my part...

This topic is closed to new replies.

Advertisement