-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.cpp
More file actions
147 lines (130 loc) · 3.34 KB
/
device.cpp
File metadata and controls
147 lines (130 loc) · 3.34 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Author: Antony Lulciuc
* Description: Defines process of creating a Client Window
*/
#include "device.h"
namespace
{
// Pointer to device
Device* g_pDevice = NULL;
}
// Window Procedure Handler
LRESULT CALLBACK WndProc(HWND _hWnd, UINT _uiMessage, WPARAM _wParam, LPARAM _lParam)
{
return (g_pDevice->wndProc(_hWnd, _uiMessage, _wParam, _lParam));
}
// Constructor
Device::Device(HINSTANCE _hInstance, LPCTSTR _lpszDeviceName,
UINT _uiWindowWidth, UINT _uiWindowHeight)
{
m_hInstance = _hInstance;
m_uiClientWidth = _uiWindowWidth;
m_uiClientHeight = _uiWindowHeight;
m_strDeviceName = _lpszDeviceName;
g_pDevice = this;
}
Device::~Device(void)
{
m_hMain = 0;
}
// Initialize device
int Device::init(void)
{
// Register Device
if (!registerDevice())
return (1);
// Create Device,
// NOTE: When creating a window (i.e. a window) it must be registered first
if (!createDevice())
return (2);
return (0);
}
// Method that handles window events
LRESULT Device::wndProc(HWND _hWnd, UINT _uiMessage, WPARAM _wParam, LPARAM _lParam)
{
// What type of message is it?
switch (_uiMessage)
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
// Handles mouse clicks
onMouseDown(_hWnd, _wParam, GET_X_LPARAM(_lParam), GET_Y_LPARAM(_lParam));
return (0);
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
// Handles mouse click release
onMouseUp(_hWnd, _wParam, GET_X_LPARAM(_lParam), GET_Y_LPARAM(_lParam));
return (0);
case WM_MOUSEMOVE:
// Handles mouse movement
onMouseMove(_hWnd, _wParam, GET_X_LPARAM(_lParam), GET_Y_LPARAM(_lParam));
return (0);
case WM_SIZE:
// Resizes winodw buffers
resize();
return (0);
case WM_KEYDOWN:
// Handle keyboard press
return (0);
case WM_DESTROY:
// Window termination message
PostQuitMessage(0);
return (0);
}
// Return default message (i.e. unhandled message valid)
return (DefWindowProc(_hWnd, _uiMessage, _wParam, _lParam));
}
// Create device
bool Device::createDevice(void)
{
m_hMain = CreateWindow(m_strDeviceName.c_str(),
m_strDeviceName.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
m_uiClientWidth,
m_uiClientHeight,
0,
0,
m_hInstance,
NULL);
// If HANDLE returned equals NULL or 0 it failed to create the window
if (!m_hMain)
{
MessageBox(NULL, TEXT("ERROR-Failed to create main device!"),
TEXT("Creation Error!"), MB_ICONERROR | MB_OK);
return (false);
}
// Update Display
ShowWindow(m_hMain, SW_SHOWNORMAL);
UpdateWindow(m_hMain);
return (true);
}
// Register device
bool Device::registerDevice(void)
{
// Window Registration information
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpfnWndProc = WndProc;
wc.hInstance = m_hInstance;
wc.hbrBackground = static_cast<HBRUSH>(CreateSolidBrush(RGB(255.0f, 255.0f, 255.0f)));
wc.hCursor = LoadIcon(m_hInstance, IDC_ARROW);
wc.hIcon = LoadIcon(m_hInstance, IDI_APPLICATION);
wc.lpszClassName = m_strDeviceName.c_str();
wc.lpszMenuName = NULL;
// If failed t register the device
if (!RegisterClass(&wc))
{
MessageBox(NULL, TEXT("ERROR-Failed to register main device!"),
TEXT("Registry Error!"), MB_ICONERROR | MB_OK);
return (false);
}
// Store registration information for later use (i.e. create child windows)
m_wcWndClass = wc;
return (true);
}