Freeciv-3.1
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
39 SDL_Surface *cursor;
40 int hot_x;
41 int hot_y;
42};
43
45
48
49SDL_Cursor *std_cursor = NULL;
50SDL_Cursor *disabled_cursor = NULL;
51
53
54/**********************************************************************/
57static 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/**********************************************************************/
101{
102 int cursor_x = 0;
103 int cursor_y = 0;
104 static SDL_Rect area = {0, 0, 0, 0};
105
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 */
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/**********************************************************************/
136void 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/**********************************************************************/
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/**********************************************************************/
208
209/**********************************************************************/
212void update_mouse_cursor(enum cursor_type new_cursor_type)
213{
214 if (new_cursor_type == mouse_cursor_type) {
215 return;
216 }
217
218 mouse_cursor_type = new_cursor_type;
219
221 SDL_SetCursor(std_cursor);
224 }
226 } else {
228 SDL_SetCursor(disabled_cursor);
229 }
231 }
232}
static int cursor_frame
Definition mapview.c:66
Uint32 getpixel(SDL_Surface *surf, Sint16 x, Sint16 y)
Definition graphics.c:430
void update_main_screen(void)
Definition graphics.c:669
int screen_blit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:223
struct sdl2_data main_data
Definition graphics.c:57
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
#define FREESURFACE(ptr)
Definition graphics.h:322
#define unlock_surf(surf)
Definition graphics.h:344
#define lock_surf(surf)
Definition graphics.h:333
void flush_rect(SDL_Rect *rect, bool force_flush)
Definition mapview.c:103
#define GET_SURF(m_sprite)
Definition sprite.h:29
SDL_Cursor * fc_cursors[CURSOR_LAST][NUM_CURSOR_FRAMES]
Definition gui_mouse.c:44
static SDL_Cursor * SurfaceToCursor(SDL_Surface *image, int hx, int hy)
Definition gui_mouse.c:57
enum cursor_type mouse_cursor_type
Definition gui_mouse.c:46
void animate_mouse_cursor(void)
Definition gui_mouse.c:182
struct color_cursor current_color_cursor
Definition gui_mouse.c:52
bool mouse_cursor_changed
Definition gui_mouse.c:47
void unload_cursors(void)
Definition gui_mouse.c:164
SDL_Cursor * disabled_cursor
Definition gui_mouse.c:50
SDL_Cursor * std_cursor
Definition gui_mouse.c:49
void draw_mouse_cursor(void)
Definition gui_mouse.c:100
void update_mouse_cursor(enum cursor_type new_cursor_type)
Definition gui_mouse.c:212
void load_cursors(void)
Definition gui_mouse.c:136
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
struct client_options gui_options
Definition options.c:71
bool gui_sdl2_do_cursor_animation
Definition options.h:379
bool gui_sdl2_use_color_cursors
Definition options.h:380
SDL_Surface * cursor
Definition gui_mouse.c:39
Definition colors.h:20
SDL_Window * screen
Definition graphics.h:209
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct sprite * get_cursor_sprite(const struct tileset *t, enum cursor_type cursor, int *hot_x, int *hot_y, int frame)
Definition tilespec.c:6656
#define NUM_CURSOR_FRAMES
Definition tilespec.h:300
cursor_type
Definition tilespec.h:285
@ CURSOR_DEFAULT
Definition tilespec.h:297
@ CURSOR_LAST
Definition tilespec.h:296