-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor.cc
More file actions
270 lines (237 loc) · 8.63 KB
/
cursor.cc
File metadata and controls
270 lines (237 loc) · 8.63 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "cursor.hh"
#include <tuple>
#include <cmath>
#include <vector>
namespace SDL {
namespace _implementation {
namespace cursor {
Pixel char_to_pixel(const char c) {
switch (c) {
case 'X':
return Pixel::X;
case 'O':
case '.':
return Pixel::O;
case 'Z':
return Pixel::Z;
default:
return Pixel::_;
}
}
std::vector<std::string> split(const std::string s) {
std::vector<std::string> out;
std::string current = "";
for (const char c : s)
if (c == '\n') {
out.push_back(current);
current = "";
} else
current += c;
out.push_back(current);
return out;
}
}
}
template <typename T>
Vector2S get_size(const T& container) {
size_t width = 0;
for (const auto& i : container) {
size_t local_width = i.size();
if (local_width > width)
width = local_width;
}
if (width % 8 != 0) // fix the width to be a multiple of 8.
width = ((width >> 3) + 1) << 3;
size_t height = container.size();
if (height % 8 != 0) // fix the height to be a multiple of 8
height = ((height >> 3) + 1) << 3;
// Are the same restrictions valid for the height of the cursor as for the width?
return {width, height};
}
template <typename T, typename U, typename V>
auto line_walker(T& iterator, U& sub_iterator, const V& container) {
return [=](bool first) mutable -> bool {
if (!first) {
++ iterator;
if (iterator == container.end())
return true;
else {
sub_iterator = iterator->begin();
return false;
}
} else {
return iterator == container.end();
}
};
}
template <typename T, typename U>
SDL_Cursor* build_cursor(const Vector2S s, const Vector2S c, T next_line, U next_element) {
// U has the form std::pair<bool, Pixel>(),
// where the first entry is true if the iterator has iterated throug all elements
// T only returns if there is a next line or not
Uint8 data[size_t(x(s) * y(s) / 8)];
Uint8 mask[size_t(x(s) * y(s) / 8)];
size_t index = -1; // equals max_value(size_t), but gets wrapped when incremented.
// col is 0 at first, so there is no problem.
bool done = next_line(true);
while (!done) {
size_t col = 0;
bool line_done;
Pixel i;
std::tie(line_done, i) = next_element(true);
while (!line_done) {
if (col % 8 != 0) { // we write the next bit in the current byte
data[index] <<= 1;
mask[index] <<= 1;
} else { // we start the next byte
++ index;
data[index] = mask[index] = 0;
}
++ col;
switch (i) {
case Pixel::_:
break;
case Pixel::X:
data[index] += 1; // sets the last bit to one.
case Pixel::O:
mask[index] += 1;
break;
case Pixel::Z:
data[index] += 1;
break;
}
std::tie(line_done, i) = next_element(false);
}
for (; col < x(s); ++ col)
if (col % 8 != 0) {
data[index] <<= 1;
mask[index] <<= 1;
} else {
++ index;
data[index] = mask[index] = 0;
}
done = next_line(false);
}
return SDL_CreateCursor(data, mask, x(s), y(s), x(c), y(c));
}
template <typename T>
SDL_Cursor* string_list_to_cursor(const T& l, const Vector2S c) {
auto current_line = l.begin();
auto current_element = current_line->begin();
return build_cursor(
get_size(l),
c,
line_walker(current_line, current_element, l),
[&](bool first) -> std::pair<bool, Pixel> {
if (!first)
++ current_element;
if (current_element == current_line->end())
return {true, Pixel::_};
else {
return {false, _implementation::cursor::char_to_pixel(*current_element)};
}
}
);
}
namespace cursor_pixels {
Pixel _ = Pixel::_;
Pixel X = Pixel::X;
Pixel O = Pixel::O;
Pixel Z = Pixel::Z;
}
Cursor::Cursor() {
this->cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
this->has_control = true;
}
Cursor::Cursor(Cursor&& other) {
this->cursor = other.cursor;
other.has_control = false;
this->has_control = true;
}
Cursor::Cursor(
const std::initializer_list<std::initializer_list<Pixel>> l,
const Vector2S c
) {
auto current_line = l.begin();
auto current_element = current_line->begin();
this->cursor = build_cursor(
get_size(l),
c,
line_walker(current_line, current_element, l),
[&](bool first) -> std::pair<bool, Pixel> {
if (!first)
++ current_element;
if (current_element == current_line->end())
return {true, Pixel::_};
else
return {false, *current_element};
}
);
this->has_control = true;
}
Cursor::Cursor(
const std::initializer_list<std::string> l,
const Vector2S c
) {
this->cursor = string_list_to_cursor(l, c);
this->has_control = true;
}
Cursor::Cursor(
const std::string s,
size_t w,
const Vector2S c
) {
auto current_element = s.begin();
if (w % 8 != 0) // fix the width to be a multiple of 8
w = ((w >> 3) + 1) << 3;
size_t h = std::ceil(s.size() / w);
if (h % 8 != 0) // fix the height to be a multiple of 8
h = ((h >> 3) + 1) << 3;
// Are the same restrictions valid for the height of the cursor as for the width?
this->cursor = build_cursor(
Vector2S {w, h},
c,
[&](bool first) -> bool {
return current_element == s.end();
},
[&](bool first) -> std::pair<bool, Pixel> {
if (current_element == s.end())
return {true, Pixel::_};
else {
Pixel out = _implementation::cursor::char_to_pixel(*current_element);
++ current_element;
return {false, out};
}
}
);
this->has_control = true;
}
Cursor::Cursor(const std::string s, const Vector2S c) {
this->cursor = string_list_to_cursor(_implementation::cursor::split(s), c);
this->has_control = true;
}
Cursor::Cursor(Surface& s, Vector2S c) {
this->cursor = SDL_CreateColorCursor(s.sdl_surface, x(c), y(c));
this->has_control = true;
}
Cursor::Cursor(const SDL_SystemCursor c) {
this->cursor = SDL_CreateSystemCursor(c);
this->has_control = true;
}
Cursor::~Cursor() {
if (this->has_control) // We don't want to free cursors that were moved.
SDL_FreeCursor(this->cursor);
}
Cursor Cursor::pointer = SDL_SYSTEM_CURSOR_ARROW;
Cursor Cursor::i_beam = SDL_SYSTEM_CURSOR_IBEAM;
Cursor Cursor::wait = SDL_SYSTEM_CURSOR_WAIT;
Cursor Cursor::crosshair = SDL_SYSTEM_CURSOR_CROSSHAIR;
Cursor Cursor::wait_and_pointer = SDL_SYSTEM_CURSOR_WAITARROW;
Cursor Cursor::arrow_nw_se = SDL_SYSTEM_CURSOR_SIZENWSE;
Cursor Cursor::arrow_ne_sw = SDL_SYSTEM_CURSOR_SIZENESW;
Cursor Cursor::arrow_horizontal = SDL_SYSTEM_CURSOR_SIZEWE;
Cursor Cursor::arrow_vertical = SDL_SYSTEM_CURSOR_SIZENS;
Cursor Cursor::arrow_cross = SDL_SYSTEM_CURSOR_SIZEALL;
Cursor Cursor::slashed_circle = SDL_SYSTEM_CURSOR_NO;
Cursor Cursor::hand = SDL_SYSTEM_CURSOR_HAND;
}