-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.h
More file actions
246 lines (187 loc) · 5.87 KB
/
Copy pathUtil.h
File metadata and controls
246 lines (187 loc) · 5.87 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#pragma once
#include <stdio.h>
#include <Windows.h>
#include "windowsx.h"
#include <string>
#include <vector>
#include <map>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <fstream>
class NonCopyable {
private:
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
public:
NonCopyable() = default;
~NonCopyable() = default;
};
enum class MouseButton {
NA = 0,
Left = 1 << 0, // 1, 0001
Middle = 1 << 1, // 2, 0010
Right = 1 << 2, // 4, 0100
};
inline constexpr MouseButton operator& (MouseButton x, MouseButton y) {
return static_cast<MouseButton>(static_cast<int>(x) & static_cast<int>(y));
}
inline constexpr MouseButton operator| (MouseButton x, MouseButton y) {
return static_cast<MouseButton>(static_cast<int>(x) | static_cast<int>(y));
}
inline constexpr MouseButton operator^(MouseButton x, MouseButton y) {
return static_cast<MouseButton> (static_cast<int>(x) ^ static_cast<int>(y));
}
inline constexpr MouseButton operator~(MouseButton x) {
return static_cast<MouseButton>(~static_cast<int>(x));
}
inline MouseButton& operator&=(MouseButton& x, MouseButton y) {
x = x & y;
return x;
}
inline MouseButton& operator|=(MouseButton& x, MouseButton y) {
x = x | y;
return x;
}
inline MouseButton& operator^=(MouseButton& x, MouseButton y) {
x = x ^ y;
return x;
}
enum class MouseEventType {
None = 0,
Up,
Down,
Move,
};
class MouseEvent {
public:
using Type = MouseEventType;
using Button = MouseButton;
POINT pos;
Type eventType = Type::None;
Button button = Button::NA;
Button buttonState = Button::NA;
bool isUp() const { return eventType == Type::Up; }
bool isDown() const { return eventType == Type::Down; }
bool isMove() const { return eventType == Type::Move; }
bool isLButton() const { return button == Button::Left; }
bool isMButton() const { return button == Button::Middle; }
bool isRButton() const { return button == Button::Right; }
};
class BackBuffer : NonCopyable {
HBITMAP _bitmap;
HBRUSH _bgColor = NULL;
HDC _dc = NULL;
int _w = 0;
int _h = 0;
public:
~BackBuffer(){ destroy(); }
void create(HWND hWnd_) {
destroy();
HDC wndDC = GetDC(hWnd_);
_dc = CreateCompatibleDC(wndDC);
RECT rc;
GetClientRect(hWnd_, &rc);
_h = rc.bottom - rc.top;
_w = rc.right - rc.left;
_bitmap = CreateCompatibleBitmap(wndDC, _w, _h); //https://stackoverflow.com/questions/7134465/the-result-of-createcompatibledc-only-has-two-colors
SelectObject(_dc, _bitmap);
_bgColor = (HBRUSH)GetStockObject(WHITE_BRUSH);
ReleaseDC(hWnd_, wndDC);
}
HDC dc() const { return _dc; }
void destroy() {
if (_dc) {
DeleteDC(_dc);
_dc = NULL;
}
if (_bitmap) {
DeleteObject(_bitmap);
_bitmap = NULL;
}
}
void clear() {
RECT rc{ 0, 0, _w, _h };
FillRect(_dc, &rc, _bgColor);
}
void draw(HDC hdc, int x = 0, int y = 0) {
BitBlt(hdc, x, y, _w, _h, _dc, 0, 0, SRCCOPY);
}
};
inline void writeInt(std::ofstream& f, int v) { f.write((char*)&v, sizeof(v)); } // worst implementation, problem: alignment; different cpu archi i.e. little/big endian?
inline void writeString(std::ofstream& f, const char* str) { f.write(str, strlen(str)); }
inline void readString(std::ifstream& f, char* buff, int nChar) {
assert(!f.eof());
f.read(buff, nChar);
}
inline void readInt(std::ifstream& f, int& v) {
assert(!f.eof());
f.read((char*)&v, sizeof(v));
}
class Vector2D {
public:
double x = 0;
double y = 0;
Vector2D(double _x = 0, double _y = 0) : x(_x), y(_y) { }
Vector2D(const Vector2D& v) { //copy
x = v.x;
y = v.y;
}
~Vector2D() { x = 0; y = 0; }
double distance(const Vector2D& p) const {
return (p - *this).length();
}
double length() const {
return sqrt(x * x + y * y);
}
Vector2D unitVector() const {
return *this / length();
}
double dotProduct(const Vector2D& p) const {
return x * p.x + y * p.y;
}
Vector2D project(const Vector2D& v) const {
Vector2D u = v.unitVector();
double projectLen = dotProduct(u);
return u * projectLen;
}
inline void operator=(const Vector2D& p) { *this = Vector2D(p); } //recursive here!!
inline Vector2D operator+(const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); }
inline Vector2D operator-(const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
inline Vector2D operator*(const Vector2D& v) const { return Vector2D(x * v.x, y * v.y); }
inline Vector2D operator/(const Vector2D& v) const { return Vector2D(x / v.x, y / v.y); }
inline void operator+=(const Vector2D& v) { x += v.x; y += v.y; }
inline void operator-=(const Vector2D& v) { x -= v.x; y -= v.y; }
inline void operator*=(const Vector2D& v) { x *= v.x; y *= v.y; }
inline void operator/=(const Vector2D& v) { x /= v.x; y /= v.y; }
inline Vector2D operator*(int val) const { return Vector2D(x * val, y * val); }
inline Vector2D operator/(int val) const { return Vector2D(x / val, y / val); }
inline void operator*=(int val) { x *= val; y *= val; }
inline void operator/=(int val) { x /= val; y /= val; }
inline Vector2D operator*(float val) const { return Vector2D(x * val, y * val); }
inline Vector2D operator/(float val) const { return Vector2D(x / val, y / val); }
inline Vector2D operator*(double val) const { return Vector2D(x * val, y * val); }
inline Vector2D operator/(double val) const { return Vector2D(x / val, y / val); }
inline bool operator!=(const Vector2D& v) const { return v.x != x || v.y != y; }
inline bool operator==(const Vector2D& v) const { return !operator!=(v); }
};
inline std::wstring my_getCurrentDirectory() {
wchar_t buff[MAX_PATH];
GetCurrentDirectory(MAX_PATH, buff);
std::wstring r = buff;
return r;
};
template<class T>
inline void my_bzero(T& s) {
memset(&s, 0, sizeof(s));
}
inline int getClientWidth(HWND hWnd) {
RECT r;
GetClientRect(hWnd, &r);
return r.right - r.left;
}
inline int getClientHeight(HWND hWnd) {
RECT r;
GetClientRect(hWnd, &r);
return r.bottom - r.top;
}