-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
cursor.cpp
304 lines (247 loc) · 7.02 KB
/
cursor.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright (C) 2003 - 2015 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Support for different cursors-shapes.
*/
#include "cursor.hpp"
#include "game_preferences.hpp"
#include "image.hpp"
#include "preferences_display.hpp"
#include "sdl/rect.hpp"
#include "video.hpp"
#include <iostream>
static bool use_color_cursors()
{
#ifdef __APPLE__
// Color cursors on OS X are known to be unusable, so don't use them ever.
// See bug #18112.
return false;
#else
return game_config::editor == false && preferences::use_color_cursors();
#endif
}
static SDL_Cursor* create_cursor(surface surf)
{
const surface nsurf(make_neutral_surface(surf));
if(nsurf == NULL) {
return NULL;
}
// The width must be a multiple of 8 (SDL requirement)
#ifdef __APPLE__
size_t cursor_width = 16;
#else
size_t cursor_width = nsurf->w;
if((cursor_width%8) != 0) {
cursor_width += 8 - (cursor_width%8);
}
#endif
std::vector<Uint8> data((cursor_width*nsurf->h)/8,0);
std::vector<Uint8> mask(data.size(),0);
// See http://sdldoc.csn.ul.ie/sdlcreatecursor.php for documentation
// on the format that data has to be in to pass to SDL_CreateCursor
const_surface_lock lock(nsurf);
const Uint32* const pixels = lock.pixels();
for(int y = 0; y != nsurf->h; ++y) {
for(int x = 0; x != nsurf->w; ++x) {
if (static_cast<size_t>(x) < cursor_width) {
Uint8 r,g,b,a;
SDL_GetRGBA(pixels[y*nsurf->w + x],nsurf->format,&r,&g,&b,&a);
const size_t index = y*cursor_width + x;
const size_t shift = 7 - (index % 8);
const Uint8 trans = (a < 128 ? 0 : 1) << shift;
const Uint8 black = (trans == 0 || (r+g + b) / 3 > 128 ? 0 : 1) << shift;
data[index/8] |= black;
mask[index/8] |= trans;
}
}
}
return SDL_CreateCursor(&data[0],&mask[0],cursor_width,nsurf->h,0,0);
}
namespace {
SDL_Cursor* cache[cursor::NUM_CURSORS] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
// This array must have members corresponding to cursor::CURSOR_TYPE enum members
// Apple need 16x16 b&w cursors
#ifdef __APPLE__
const std::string bw_images[cursor::NUM_CURSORS] = { "normal.png", "wait-alt.png", "move.png", "attack.png", "select.png", "move_drag_alt.png" , "attack_drag_alt.png", "no_cursor.png"};
#else
const std::string bw_images[cursor::NUM_CURSORS] = { "normal.png", "wait.png", "move.png", "attack.png", "select.png", "move_drag.png", "attack_drag.png", "no_cursor.png"};
#endif
const std::string color_images[cursor::NUM_CURSORS] = { "normal.png", "wait.png", "move.png", "attack.png", "select.png", "move_drag.png", "attack_drag.png", ""};
// Position of the hotspot of the cursor, from the normal topleft
const int shift_x[cursor::NUM_CURSORS] = {0, 0, 0, 0, 0, 2, 3, 0};
const int shift_y[cursor::NUM_CURSORS] = {0, 0, 0, 0, 0, 20, 22, 0};
cursor::CURSOR_TYPE current_cursor = cursor::NORMAL;
int cursor_x = -1, cursor_y = -1;
surface cursor_buf = NULL;
bool have_focus = true;
bool color_ready = false;
}
static SDL_Cursor* get_cursor(cursor::CURSOR_TYPE type)
{
if(cache[type] == NULL) {
static const std::string prefix = "cursors-bw/";
const surface surf(image::get_image(prefix + bw_images[type]));
cache[type] = create_cursor(surf);
}
return cache[type];
}
static void clear_cache()
{
for(size_t n = 0; n != cursor::NUM_CURSORS; ++n) {
if(cache[n] != NULL) {
SDL_FreeCursor(cache[n]);
cache[n] = NULL;
}
}
if(cursor_buf != NULL) {
cursor_buf = NULL;
}
}
namespace cursor
{
manager::manager()
{
SDL_ShowCursor(SDL_ENABLE);
set();
}
manager::~manager()
{
clear_cache();
SDL_ShowCursor(SDL_ENABLE);
}
void set(CURSOR_TYPE type)
{
// Change only if it's a valid cursor
if (type != NUM_CURSORS) {
current_cursor = type;
} else if (current_cursor == NUM_CURSORS) {
// Except if the current one is also invalid.
// In this case, change to a valid one.
current_cursor = NORMAL;
}
const CURSOR_TYPE new_cursor = use_color_cursors() && color_ready ? cursor::NO_CURSOR : current_cursor;
SDL_Cursor * cursor_image = get_cursor(new_cursor);
// Causes problem on Mac:
//if (cursor_image != NULL && cursor_image != SDL_GetCursor())
SDL_SetCursor(cursor_image);
SDL_ShowCursor(SDL_ENABLE);
}
void set_dragging(bool drag)
{
switch(current_cursor) {
case MOVE:
if (drag) cursor::set(MOVE_DRAG);
break;
case ATTACK:
if (drag) cursor::set(ATTACK_DRAG);
break;
case MOVE_DRAG:
if (!drag) cursor::set(MOVE);
break;
case ATTACK_DRAG:
if (!drag) cursor::set(ATTACK);
break;
default:
break;
}
}
CURSOR_TYPE get()
{
return current_cursor;
}
void set_focus(bool focus)
{
have_focus = focus;
if (focus==false) {
color_ready = false;
set();
}
}
setter::setter(CURSOR_TYPE type) : old_(current_cursor)
{
set(type);
}
setter::~setter()
{
set(old_);
}
void draw(surface screen)
{
if(use_color_cursors() == false) {
return;
}
if(current_cursor == NUM_CURSORS) {
current_cursor = NORMAL;
}
if(have_focus == false) {
cursor_buf = NULL;
return;
}
if (!color_ready) {
// Display start to draw cursor
// so it can now display color cursor
color_ready = true;
// Reset the cursor to be sure that we hide the b&w
set();
}
/** @todo FIXME: don't parse the file path every time */
const surface surf(image::get_image("cursors/" + color_images[current_cursor]));
if(surf == NULL) {
// Fall back to b&w cursors
std::cerr << "could not load color cursors. Falling back to hardware cursors\n";
preferences::set_color_cursors(false);
return;
}
if(cursor_buf != NULL && (cursor_buf->w != surf->w || cursor_buf->h != surf->h)) {
cursor_buf = NULL;
}
if(cursor_buf == NULL) {
cursor_buf = create_compatible_surface(surf);
if(cursor_buf == NULL) {
std::cerr << "Could not allocate surface for mouse cursor\n";
return;
}
}
int new_cursor_x, new_cursor_y;
SDL_GetMouseState(&new_cursor_x,&new_cursor_y);
const bool must_update = new_cursor_x != cursor_x || new_cursor_y != cursor_y;
cursor_x = new_cursor_x;
cursor_y = new_cursor_y;
// Save the screen area where the cursor is being drawn onto the back buffer
SDL_Rect area = sdl::create_rect(cursor_x - shift_x[current_cursor]
, cursor_y - shift_y[current_cursor]
, surf->w
, surf->h);
sdl_blit(screen,&area,cursor_buf,NULL);
// Blit the surface
sdl_blit(surf,NULL,screen,&area);
if(must_update) {
update_rect(area);
}
}
void undraw(surface screen)
{
if(use_color_cursors() == false) {
return;
}
if(cursor_buf == NULL) {
return;
}
SDL_Rect area = sdl::create_rect(cursor_x - shift_x[current_cursor]
, cursor_y - shift_y[current_cursor]
, cursor_buf->w
, cursor_buf->h);
sdl_blit(cursor_buf,NULL,screen,&area);
update_rect(area);
}
} // end namespace cursor