-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.h
More file actions
88 lines (83 loc) · 2.99 KB
/
Window.h
File metadata and controls
88 lines (83 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#include "SmflmWin.h" // always put wrapper headers first to override windows macros
#include "ErrorHandling/SmflmException.h"
#include "ErrorHandling/WindowExceptionMacros.h"
#include "Input/Keyboard.h"
#include "Input/Mouse.h"
#include "Graphics/Graphics.h"
#include <optional>
#include <string>
#include <memory>
#include <sstream>
class Window
{
public:
class HrException : public SmflmException
{
using SmflmException::SmflmException;
public:
HrException(int line, const char* file, HRESULT hr) noexcept; // includes windows error code HRESULT
const char* what() const noexcept override;
virtual const char* GetType() const noexcept;
static std::string TranslateErrorCode(HRESULT hr) noexcept; // Translates HRESULT into error string
HRESULT GetErrorCode() const noexcept;
std::string GetErrorString() const noexcept;
private:
HRESULT hr;
};
class NoGfxException : public HrException
{
public:
using HrException::HrException;
const char* GetType() const noexcept override;
};
private:
// singleton for registering/cleanup of window class
class WindowClass
{
public: // noexcept keywords insures no exceptions are thrown, so compiler doesn't add exception handling
static const char* GetName() noexcept;
static HINSTANCE GetInstance() noexcept;
private:
WindowClass() noexcept;
~WindowClass();
WindowClass(const WindowClass&) = delete; // enforce singleton by destroying new instances
static constexpr const char* wndClassName = "SmartFlame's Dx3D Engine";
static WindowClass wndClass;
HINSTANCE hInst;
};
public:
Window(int width, int height, const char* name);
~Window();
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
void SetTitle(const std::string& title);
void EnableCursor() noexcept;
void DisableCursor() noexcept;
void RecenterCursor() noexcept;
static std::optional<int> ProcessMessages(); // c++17 feature allows returning int or nullopt if no message
Graphics& Gfx();
private:
void ShowCursor() noexcept;
void HideCursor() noexcept;
void FreeCursor() noexcept;
void ConfineCursor() noexcept;
POINT GetClientCenter() const noexcept;
void SetCursorToClientCenter() noexcept;
void EnableImGuiMouse() noexcept;
void DisableImGuiMouse() noexcept;
bool IsCursorEnabled() const noexcept;
static LRESULT CALLBACK HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept; // static function so that winapi can register as callback procedure without class pointer
static LRESULT CALLBACK HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
LRESULT HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept; // little hack needed to pass member function to static function
public:
Keyboard kbd; // accessible keyboard object for key input handling
Mouse mouse; // mouse object as well
private:
int width;
int height;
bool cursorEnabled = true;
HWND hWnd;
std::unique_ptr<Graphics> pGfx;
std::vector<char> rawBuffer; // buffer for raw input (for mouse)
};