?
Mir war grade langweilig:
Window.hpp:
#ifndef WINDOW_HPP
#define WINDOW_HPP
#include <string>
namespace DXWindow{
class Window
{
public:
Window(HINSTANCE hInstance);
virtual ~Window();
void Create(int width, int height, int depth = 32);
void SetCaption(const wchar_t* caption);
void EnterMainLoop();
inline HWND GetHwnd() const { return m_hwnd; }
inline bool HasFocus()const { return m_isActive; }
private:
void InitDirectX(int width, int height, int depth);
LRESULT WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT __stdcall sWinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void Render();
void ShutDown();
void UpdateLogic();
void OnMouse(int x, int y, int button);
void OnKey(int key, bool down);
void SetFocus(bool state);
protected:
HWND m_hwnd;
HINSTANCE m_hInstance;
bool m_isActive;
static const std::basic_string<wchar_t> m_className;
};
} // namespace
#endif // WINDOW_HPP
Window.cpp:
#include <windows.h>
#include "Window.hpp"
namespace DXWindow
{
const std::basic_string<wchar_t> Window::m_className = L"MYDXWINDOW";
Window::Window(HINSTANCE hInstance)
: m_hInstance(hInstance), m_isActive(true)
{}
Window::~Window()
{}
void Window::Create( int width, int height, int depth )
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = Window::sWinProc;;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = m_hInstance;
wc.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = Window::m_className.c_str();
wc.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION);
if ( !::RegisterClassEx(&wc) )
{
::MessageBox(HWND_DESKTOP, L"RegisterClass() failed!", L"DXWindow", MB_ICONERROR|MB_OK);
return;
}
m_hwnd = ::CreateWindowEx(WS_EX_CLIENTEDGE, Window::m_className.c_str(), L"", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, m_hInstance, NULL);
if ( m_hwnd == NULL )
{
::MessageBox(HWND_DESKTOP, L"CreateWindowEx() failed!", L"DXWindow", MB_ICONERROR|MB_OK);
return;
}
::SetWindowLongPtr(m_hwnd, GWL_USERDATA, reinterpret_cast<long>(this));
::ShowWindow(m_hwnd, true);
::UpdateWindow(m_hwnd);
}
void Window::InitDirectX( int width, int height, int depth )
{
// TODO: DirectX Initalisierung
}
void Window::EnterMainLoop()
{
// Main Loop
MSG msg;
while ( ::GetMessage(&msg, m_hwnd, 0, 0) > 0 )
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
UpdateLogic();
}
}
LRESULT Window::WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch(msg)
{
case WM_CLOSE:
{
::DestroyWindow(m_hwnd);
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
::BeginPaint(hwnd, &ps);
Render();
::EndPaint(m_hwnd, &ps);
return 0;
}
case WM_DESTROY:
{
this->ShutDown();
::PostQuitMessage(0);
return 0;
}
case WM_MOUSEMOVE:
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
this->OnMouse(x, y, wParam);
return 0;
}
case WM_KEYDOWN:
{
this->OnKey(wParam, true);
return 0;
}
case WM_KEYUP:
{
this->OnKey(wParam, false);
return 0;
}
case WM_ACTIVATE:
{
if ( LOWORD(wParam) == 0 )
{
this->SetFocus(false);
}
else
{
this->SetFocus(true);
}
return 0;
}
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//////////////////////////////////////////////////////////////////////////
// Static WindowProc
//////////////////////////////////////////////////////////////////////////
LRESULT __stdcall Window::sWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
Window* ptr = reinterpret_cast<Window*>(GetWindowLong(hwnd, GWL_USERDATA));
return ptr->WindowProc(hwnd, msg, wParam, lParam);
}
//////////////////////////////////////////////////////////////////////////
// Setzt den Text für das Fenster
//////////////////////////////////////////////////////////////////////////
void Window::SetCaption(const wchar_t* caption)
{
::SetWindowText(m_hwnd, caption);
}
void Window::Render()
{
// TODO:
//DirectX Render Code
}
void Window::UpdateLogic()
{
// TODO:
// Hier kommt die Logik rein
}
void Window::OnMouse( int x, int y, int button )
{
// TODO:
// Hier kann man die Maustasten abfragen
}
void Window::OnKey( int key, bool down )
{
// Wenn down == true ist, dann wird die taste gerade gedrückt,
// wenn nicht wurde sie wieder freigegeben.
}
void Window::ShutDown()
{
// TODO:
// Aufräumarbeiten für DirectX hier durchführen
}
void Window::SetFocus(bool state)
{
m_isActive = state;
}
}
WinMain.cpp:
#include <windows.h>
#include "window.hpp"
int __stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
DXWindow::Window win(hInstance);
win.Create(1024,768);
win.SetCaption(L"Hallo Welt!");
win.EnterMainLoop();
}