forked from azzuriel/TerrainProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Win.cpp
224 lines (188 loc) · 4.37 KB
/
Win.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*-----------------------------------------------------------------------------
Win.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Create the main window and make it go.
-----------------------------------------------------------------------------*/
#include "precompiled.h"
#define CONSOLE_SIZE 30000
#define CONSOLE_PAGE 1000
#define MOUSE_MOVEMENT 0.4f
#define MAX_STRING 128
static HWND hwnd;
static HINSTANCE module;
static int width;
static int height;
static bool lmb;
static bool rmb;
static bool mouse_forced;
static vec2 mouse_pos;
static vec2 select_pos;
/*
====================
CenterCursor
====================
*/
void CenterCursor()
{
int center_x;
int center_y;
RECT rect;
SetCursor( NULL );
mouse_forced = true;
GetWindowRect( hwnd, &rect );
center_x = rect.left + ( rect.right - rect.left ) / 2;
center_y = rect.top + ( rect.bottom - rect.top ) / 2;
SetCursorPos( center_x, center_y );
}
/*
====================
MoveCursor
====================
*/
void MoveCursor( int x, int y )
{
int center_x;
int center_y;
RECT rect;
SetCursor( NULL );
mouse_forced = true;
GetWindowRect( hwnd, &rect );
center_x = rect.left + x;
center_y = rect.top + y;
SetCursorPos( center_x, center_y );
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
RECT r;
float delta_x, delta_y;
vec2 p;
switch( message ) {
case WM_MOVE:
GetClientRect( hwnd, &r );
height = r.bottom - r.top;
width = r.right - r.left;
break;
case WM_LBUTTONDOWN:
lmb = true;
SetCapture( hwnd );
break;
case WM_RBUTTONDOWN:
rmb = true;
SetCapture( hwnd );
break;
case WM_LBUTTONUP:
lmb = false;
if( !rmb ) {
ReleaseCapture();
MoveCursor( static_cast<int>(select_pos.x), static_cast<int>(select_pos.y) );
}
break;
case WM_RBUTTONUP:
rmb = false;
if( !lmb ) {
ReleaseCapture();
MoveCursor( static_cast<int>(select_pos.x), static_cast<int>(select_pos.y) );
}
break;
case WM_MOUSEMOVE:
p.x = LOWORD( lParam ); // horizontal position of cursor
p.y = HIWORD( lParam ); // vertical position of cursor
if( !mouse_forced && !lmb && !rmb ) {
select_pos = p;
}
if( mouse_forced ) {
mouse_forced = false;
} else if( rmb || lmb ) {
CenterCursor();
delta_x = ( float )( mouse_pos.x - p.x ) / MOUSE_MOVEMENT;
delta_y = ( float )( mouse_pos.y - p.y ) / MOUSE_MOVEMENT;
if( rmb && lmb ) {
CameraSelectionYaw( delta_x );
CameraSelectionZoom( -delta_y );
} else if( rmb ) {
CameraPan( delta_x );
CameraForward( delta_y );
} else if( lmb ) {
CameraSelectionYaw( delta_x );
CameraSelectionPitch( delta_y );
}
}
mouse_pos = p;
break;
case WM_CLOSE:
AppQuit();
return 0;
}
return DefWindowProc( hWnd, message, wParam, lParam );
}
/*
====================
WinPopup
Show popup window with message
====================
*/
void WinPopup( char* message, ... )
{
va_list marker;
char buf[1024];
va_start( marker, message );
vsprintf_s( buf, message, marker );
va_end( marker );
MessageBox( NULL, buf, APP_TITLE.c_str(), MB_ICONSTOP | MB_OK | MB_TASKMODAL );
}
int WinWidth( void )
{
return width;
}
void WinMousePosition( vec2& v )
{
v.x = select_pos.x;
v.y = select_pos.y;
}
int WinHeight( void )
{
return height;
}
void WinTerm( void )
{
DestroyWindow( hwnd );
}
HWND WinHwnd( void )
{
return hwnd;
}
/*
====================
WinInit
Create a Win32 API window
====================
*/
bool WinInit( void )
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = ( WNDPROC )WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = AppInstance();
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = ( HBRUSH )( COLOR_BTNFACE + 1 );
wcex.lpszMenuName = NULL;
wcex.lpszClassName = APP_TITLE.c_str();
wcex.hIconSm = NULL;
if( !RegisterClassEx( &wcex ) ) {
WinPopup( "Cannot create window class" );
return false;
}
if( !( hwnd = CreateWindowEx( 0, APP_TITLE.c_str(), APP_TITLE.c_str(), WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, 0, 544, 640, NULL, NULL, AppInstance(), NULL ) ) ) {
WinPopup( "Cannot create window" );
return false;
}
ShowWindow( hwnd, SW_SHOW );
UpdateWindow( hwnd );
return true;
}