Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-sdl2/gui_mouse.c Source File

Freeciv-3.2

Loading...
Searching...
No Matches
gui_mouse.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2006 The Freeciv Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* SDL2 */
19#ifdef SDL2_PLAIN_INCLUDE
20#include <SDL.h>
21#else /* SDL2_PLAIN_INCLUDE */
22#include <SDL2/SDL.h>
23#endif /* SDL2_PLAIN_INCLUDE */
24
25/* utility */
26#include "log.h"
27
28/* client */
29#include "tilespec.h"
30
31/* gui-sdl2 */
32#include "graphics.h"
33#include "mapview.h"
34#include "sprite.h"
35
36#include "gui_mouse.h"
37
38 struct color_cursor {
39 SDL_Surface *cursor;
40 int hot_x;
41 int hot_y;
42};
43
44 SDL_Cursor *fc_cursors[CURSOR_LAST][NUM_CURSOR_FRAMES];
45
46 enum cursor_type mouse_cursor_type = CURSOR_DEFAULT;
47 bool mouse_cursor_changed = FALSE;
48
49 SDL_Cursor *std_cursor = NULL;
50 SDL_Cursor *disabled_cursor = NULL;
51
52 struct color_cursor current_color_cursor;
53
54/**********************************************************************/
57 static SDL_Cursor *SurfaceToCursor(SDL_Surface *image, int hx, int hy)
58{
59 int w, x, y;
60 Uint8 *data, *mask, *d, *m, r, g, b, a;
61 Uint32 color;
62 SDL_Cursor *cursor;
63
64 w = (image->w + 7) / 8;
65 data = (Uint8 *)fc_calloc(1, w * image->h * 2);
66 if (data == NULL) {
67 return NULL;
68 }
69 /* memset(data, 0, w * image->h * 2); */
70 mask = data + w * image->h;
71 lock_surf(image);
72 for (y = 0; y < image->h; y++) {
73 d = data + y * w;
74 m = mask + y * w;
75 for (x = 0; x < image->w; x++) {
76 color = getpixel(image, x, y);
77 SDL_GetRGBA(color, image->format, &r, &g, &b, &a);
78 if (a != 0) {
79 color = (r + g + b) / 3;
80 m[x / 8] |= 128 >> (x & 7);
81 if (color < 128) {
82 d[x / 8] |= 128 >> (x & 7);
83 }
84 }
85 }
86 }
87
88 unlock_surf(image);
89
90 cursor = SDL_CreateCursor(data, mask, w * 8, image->h, hx, hy);
91
92 FC_FREE(data);
93
94 return cursor;
95}
96
97/**********************************************************************/
100 void draw_mouse_cursor(void)
101{
102 int cursor_x = 0;
103 int cursor_y = 0;
104 static SDL_Rect area = {0, 0, 0, 0};
105
106 if (GUI_SDL_OPTION(use_color_cursors)) {
107 /* Restore background */
108 if (area.w != 0) {
109 flush_rect(&area, TRUE);
110 }
111
112 if (current_color_cursor.cursor != NULL) {
113 SDL_GetMouseState(&cursor_x, &cursor_y);
114 area.x = cursor_x - current_color_cursor.hot_x;
115 area.y = cursor_y - current_color_cursor.hot_y;
116 area.w = current_color_cursor.cursor->w;
117 area.h = current_color_cursor.cursor->h;
118
119 /* Show cursor */
120 screen_blit(current_color_cursor.cursor, NULL, &area, 255);
121 /* Update screen */
122 update_main_screen();
123#if 0
124 SDL_UpdateRect(main_data.screen, area.x, area.y, area.w, area.h);
125#endif
126 } else {
127 area = (SDL_Rect){0, 0, 0, 0};
128 }
129 }
130}
131
132/**********************************************************************/
136 void load_cursors(void)
137{
138 enum cursor_type cursor;
139 int frame;
140 SDL_Surface *surf;
141
142 std_cursor = SDL_GetCursor();
143
144 surf = create_surf(1, 1, SDL_SWSURFACE);
145 disabled_cursor = SurfaceToCursor(surf, 0, 0);
146 FREESURFACE(surf);
147
148 for (cursor = 0; cursor < CURSOR_LAST; cursor++) {
149 for (frame = 0; frame < NUM_CURSOR_FRAMES; frame++) {
150 int hot_x, hot_y;
151 struct sprite *sprite
152 = get_cursor_sprite(tileset, cursor, &hot_x, &hot_y, frame);
153
154 surf = GET_SURF(sprite);
155
156 fc_cursors[cursor][frame] = SurfaceToCursor(surf, hot_x, hot_y);
157 }
158 }
159}
160
161/**********************************************************************/
164 void unload_cursors(void)
165{
166 enum cursor_type cursor;
167 int frame;
168
169 for (cursor = 0; cursor < CURSOR_LAST; cursor++) {
170 for (frame = 0; frame < NUM_CURSOR_FRAMES; frame++) {
171 SDL_FreeCursor(fc_cursors[cursor][frame]);
172 }
173 }
174
175 SDL_FreeCursor(std_cursor);
176 SDL_FreeCursor(disabled_cursor);
177}
178
179/**********************************************************************/
182 void animate_mouse_cursor(void)
183{
184 static int cursor_frame = 0;
185
186 if (!mouse_cursor_changed) {
187 return;
188 }
189
190 if (mouse_cursor_type != CURSOR_DEFAULT) {
191 if (!GUI_SDL_OPTION(do_cursor_animation)
192 || (cursor_frame == NUM_CURSOR_FRAMES)) {
193 cursor_frame = 0;
194 }
195
196 if (GUI_SDL_OPTION(use_color_cursors)) {
197 current_color_cursor.cursor
198 = GET_SURF(get_cursor_sprite(tileset,
199 mouse_cursor_type,
200 &current_color_cursor.hot_x,
201 &current_color_cursor.hot_y,
202 cursor_frame));
203 } else {
204 SDL_SetCursor(fc_cursors[mouse_cursor_type][cursor_frame]);
205 }
206
207 cursor_frame++;
208 }
209}
210
211/**********************************************************************/
214 void update_mouse_cursor(enum cursor_type new_cursor_type)
215{
216 if (new_cursor_type == mouse_cursor_type) {
217 return;
218 }
219
220 mouse_cursor_type = new_cursor_type;
221
222 if (mouse_cursor_type == CURSOR_DEFAULT) {
223 SDL_SetCursor(std_cursor);
224 if (GUI_SDL_OPTION(use_color_cursors)) {
225 current_color_cursor.cursor = NULL;
226 }
227 mouse_cursor_changed = FALSE;
228 } else {
229 if (GUI_SDL_OPTION(use_color_cursors)) {
230 SDL_SetCursor(disabled_cursor);
231 }
232 mouse_cursor_changed = TRUE;
233 }
234}
incite_cost
char * incite_cost
Definition comments.c:74
cursor_frame
static int cursor_frame
Definition mapview.c:67
getpixel
Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
Definition graphics.c:430
update_main_screen
void update_main_screen(void)
Definition graphics.c:669
screen_blit
int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:223
main_data
struct sdl2_data main_data
Definition graphics.c:57
create_surf
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
unlock_surf
#define unlock_surf(surf)
Definition graphics.h:344
lock_surf
#define lock_surf(surf)
Definition graphics.h:333
GUI_SDL_OPTION
#define GUI_SDL_OPTION(optname)
Definition gui_main.h:37
fc_cursors
SDL_Cursor * fc_cursors[CURSOR_LAST][NUM_CURSOR_FRAMES]
Definition gui_mouse.c:44
SurfaceToCursor
static SDL_Cursor * SurfaceToCursor(SDL_Surface *image, int hx, int hy)
Definition gui_mouse.c:57
mouse_cursor_type
enum cursor_type mouse_cursor_type
Definition gui_mouse.c:46
animate_mouse_cursor
void animate_mouse_cursor(void)
Definition gui_mouse.c:182
current_color_cursor
struct color_cursor current_color_cursor
Definition gui_mouse.c:52
mouse_cursor_changed
bool mouse_cursor_changed
Definition gui_mouse.c:47
unload_cursors
void unload_cursors(void)
Definition gui_mouse.c:164
disabled_cursor
SDL_Cursor * disabled_cursor
Definition gui_mouse.c:50
std_cursor
SDL_Cursor * std_cursor
Definition gui_mouse.c:49
draw_mouse_cursor
void draw_mouse_cursor(void)
Definition gui_mouse.c:100
update_mouse_cursor
void update_mouse_cursor(enum cursor_type new_cursor_type)
Definition gui_mouse.c:214
load_cursors
void load_cursors(void)
Definition gui_mouse.c:136
flush_rect
void flush_rect(SDL_Rect *rect, bool force_flush)
Definition mapview.c:103
GET_SURF
#define GET_SURF(m_sprite)
Definition sprite.h:29
gui_mouse.h
graphics.h
mapview.h
sprite.h
log.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
mask
struct sprite int int int int struct sprite * mask
Definition sprite_g.h:32
y
struct sprite int int y
Definition sprite_g.h:31
x
struct sprite int x
Definition sprite_g.h:31
color_cursor
Definition gui_mouse.c:38
color_cursor::hot_y
int hot_y
Definition gui_mouse.c:41
color_cursor::cursor
SDL_Surface * cursor
Definition gui_mouse.c:39
color_cursor::hot_x
int hot_x
Definition gui_mouse.c:40
color
Definition colors.h:21
sdl2_data::screen
SDL_Window * screen
Definition graphics.h:209
sprite
Definition sprite.h:22
tileset
Definition tilespec.c:481
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
get_cursor_sprite
struct sprite * get_cursor_sprite(const struct tileset *t, enum cursor_type cursor, int *hot_x, int *hot_y, int frame)
Definition tilespec.c:6964
tilespec.h
NUM_CURSOR_FRAMES
#define NUM_CURSOR_FRAMES
Definition tilespec.h:303
cursor_type
cursor_type
Definition tilespec.h:287
CURSOR_DEFAULT
@ CURSOR_DEFAULT
Definition tilespec.h:300
CURSOR_LAST
@ CURSOR_LAST
Definition tilespec.h:299