Mercurial > sdl-ios-xcode
annotate src/video/SDL_cursor.c @ 1318:f95502c6fc72
Eliminate duplicate modes with different refresh rates
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 09:28:42 +0000 |
parents | c9b51268668f |
children | 450721ad5436 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1296
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1296
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1296
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1296
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1296
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1296
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1296
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
113
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* General cursor handling code for SDL */ | |
24 | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 #include "SDL_mutex.h" | |
30 #include "SDL_error.h" | |
31 #include "SDL_video.h" | |
32 #include "SDL_mouse.h" | |
33 #include "SDL_blit.h" | |
34 #include "SDL_events_c.h" | |
35 #include "SDL_sysvideo.h" | |
36 #include "SDL_sysevents.h" | |
37 #include "SDL_cursor_c.h" | |
38 #include "SDL_pixels_c.h" | |
39 #include "default_cursor.h" | |
40 | |
41 /* These are static for our cursor handling code */ | |
42 volatile int SDL_cursorstate = CURSOR_VISIBLE; | |
43 SDL_Cursor *SDL_cursor = NULL; | |
44 static SDL_Cursor *SDL_defcursor = NULL; | |
45 SDL_mutex *SDL_cursorlock = NULL; | |
46 | |
47 /* Public functions */ | |
48 void SDL_CursorQuit(void) | |
49 { | |
50 if ( SDL_cursor != NULL ) { | |
51 SDL_Cursor *cursor; | |
52 | |
53 SDL_cursorstate &= ~CURSOR_VISIBLE; | |
54 if ( SDL_cursor != SDL_defcursor ) { | |
55 SDL_FreeCursor(SDL_cursor); | |
56 } | |
57 SDL_cursor = NULL; | |
58 if ( SDL_defcursor != NULL ) { | |
59 cursor = SDL_defcursor; | |
60 SDL_defcursor = NULL; | |
61 SDL_FreeCursor(cursor); | |
62 } | |
63 } | |
64 if ( SDL_cursorlock != NULL ) { | |
65 SDL_DestroyMutex(SDL_cursorlock); | |
66 SDL_cursorlock = NULL; | |
67 } | |
68 } | |
69 int SDL_CursorInit(Uint32 multithreaded) | |
70 { | |
71 /* We don't have mouse focus, and the cursor isn't drawn yet */ | |
1140
af8b0f9ac2f4
iPod Linux framebuffer support.
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
72 #ifndef IPOD |
0 | 73 SDL_cursorstate = CURSOR_VISIBLE; |
1140
af8b0f9ac2f4
iPod Linux framebuffer support.
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
74 #endif |
0 | 75 |
76 /* Create the default cursor */ | |
77 if ( SDL_defcursor == NULL ) { | |
78 SDL_defcursor = SDL_CreateCursor(default_cdata, default_cmask, | |
79 DEFAULT_CWIDTH, DEFAULT_CHEIGHT, | |
80 DEFAULT_CHOTX, DEFAULT_CHOTY); | |
81 SDL_SetCursor(SDL_defcursor); | |
82 } | |
83 | |
84 /* Create a lock if necessary */ | |
85 if ( multithreaded ) { | |
86 SDL_cursorlock = SDL_CreateMutex(); | |
87 } | |
88 | |
89 /* That's it! */ | |
90 return(0); | |
91 } | |
92 | |
93 /* Multi-thread support for cursors */ | |
94 #ifndef SDL_LockCursor | |
95 void SDL_LockCursor(void) | |
96 { | |
97 if ( SDL_cursorlock ) { | |
98 SDL_mutexP(SDL_cursorlock); | |
99 } | |
100 } | |
101 #endif | |
102 #ifndef SDL_UnlockCursor | |
103 void SDL_UnlockCursor(void) | |
104 { | |
105 if ( SDL_cursorlock ) { | |
106 SDL_mutexV(SDL_cursorlock); | |
107 } | |
108 } | |
109 #endif | |
110 | |
111 /* Software cursor drawing support */ | |
112 SDL_Cursor * SDL_CreateCursor (Uint8 *data, Uint8 *mask, | |
113 int w, int h, int hot_x, int hot_y) | |
114 { | |
115 SDL_VideoDevice *video = current_video; | |
116 int savelen; | |
117 int i; | |
118 SDL_Cursor *cursor; | |
119 | |
120 /* Make sure the width is a multiple of 8 */ | |
121 w = ((w+7)&~7); | |
122 | |
123 /* Sanity check the hot spot */ | |
124 if ( (hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h) ) { | |
125 SDL_SetError("Cursor hot spot doesn't lie within cursor"); | |
126 return(NULL); | |
127 } | |
128 | |
129 /* Allocate memory for the cursor */ | |
130 cursor = (SDL_Cursor *)malloc(sizeof *cursor); | |
131 if ( cursor == NULL ) { | |
132 SDL_OutOfMemory(); | |
133 return(NULL); | |
134 } | |
135 savelen = (w*4)*h; | |
136 cursor->area.x = 0; | |
137 cursor->area.y = 0; | |
138 cursor->area.w = w; | |
139 cursor->area.h = h; | |
140 cursor->hot_x = hot_x; | |
141 cursor->hot_y = hot_y; | |
142 cursor->data = (Uint8 *)malloc((w/8)*h*2); | |
143 cursor->mask = cursor->data+((w/8)*h); | |
144 cursor->save[0] = (Uint8 *)malloc(savelen*2); | |
145 cursor->save[1] = cursor->save[0] + savelen; | |
146 cursor->wm_cursor = NULL; | |
147 if ( ! cursor->data || ! cursor->save[0] ) { | |
148 SDL_FreeCursor(cursor); | |
149 SDL_OutOfMemory(); | |
150 return(NULL); | |
151 } | |
152 for ( i=((w/8)*h)-1; i>=0; --i ) { | |
153 cursor->data[i] = data[i]; | |
154 cursor->mask[i] = mask[i] | data[i]; | |
155 } | |
156 memset(cursor->save[0], 0, savelen*2); | |
157 | |
158 /* If the window manager gives us a good cursor, we're done! */ | |
159 if ( video->CreateWMCursor ) { | |
160 cursor->wm_cursor = video->CreateWMCursor(video, data, mask, | |
161 w, h, hot_x, hot_y); | |
162 } else { | |
163 cursor->wm_cursor = NULL; | |
164 } | |
165 return(cursor); | |
166 } | |
167 | |
168 /* SDL_SetCursor(NULL) can be used to force the cursor redraw, | |
169 if this is desired for any reason. This is used when setting | |
170 the video mode and when the SDL window gains the mouse focus. | |
171 */ | |
172 void SDL_SetCursor (SDL_Cursor *cursor) | |
173 { | |
174 SDL_VideoDevice *video = current_video; | |
175 SDL_VideoDevice *this = current_video; | |
176 | |
177 /* Make sure that the video subsystem has been initialized */ | |
178 if ( ! video ) { | |
179 return; | |
180 } | |
181 | |
182 /* Prevent the event thread from moving the mouse */ | |
183 SDL_LockCursor(); | |
184 | |
185 /* Set the new cursor */ | |
186 if ( cursor && (cursor != SDL_cursor) ) { | |
187 /* Erase the current mouse position */ | |
188 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) { | |
189 SDL_EraseCursor(SDL_VideoSurface); | |
190 } else if ( video->MoveWMCursor ) { | |
191 /* If the video driver is moving the cursor directly, | |
192 it needs to hide the old cursor before (possibly) | |
193 showing the new one. (But don't erase NULL cursor) | |
194 */ | |
195 if ( SDL_cursor ) { | |
196 video->ShowWMCursor(this, NULL); | |
197 } | |
198 } | |
199 SDL_cursor = cursor; | |
200 } | |
201 | |
202 /* Draw the new mouse cursor */ | |
203 if ( SDL_cursor && (SDL_cursorstate&CURSOR_VISIBLE) ) { | |
204 /* Use window manager cursor if possible */ | |
205 if ( SDL_cursor->wm_cursor && | |
1296 | 206 video->ShowWMCursor(this, SDL_cursor->wm_cursor) ) { |
0 | 207 SDL_cursorstate &= ~CURSOR_USINGSW; |
1296 | 208 } else { |
0 | 209 SDL_cursorstate |= CURSOR_USINGSW; |
210 if ( video->ShowWMCursor ) { | |
211 video->ShowWMCursor(this, NULL); | |
212 } | |
213 { int x, y; | |
214 SDL_GetMouseState(&x, &y); | |
215 SDL_cursor->area.x = (x - SDL_cursor->hot_x); | |
216 SDL_cursor->area.y = (y - SDL_cursor->hot_y); | |
217 } | |
218 SDL_DrawCursor(SDL_VideoSurface); | |
219 } | |
220 } else { | |
221 /* Erase window manager mouse (cursor not visible) */ | |
222 if ( SDL_cursor && (SDL_cursorstate & CURSOR_USINGSW) ) { | |
223 SDL_EraseCursor(SDL_VideoSurface); | |
224 } else { | |
225 if ( video ) { | |
226 video->ShowWMCursor(this, NULL); | |
227 } | |
228 } | |
229 } | |
230 SDL_UnlockCursor(); | |
231 } | |
232 | |
233 SDL_Cursor * SDL_GetCursor (void) | |
234 { | |
235 return(SDL_cursor); | |
236 } | |
237 | |
238 void SDL_FreeCursor (SDL_Cursor *cursor) | |
239 { | |
240 if ( cursor ) { | |
241 if ( cursor == SDL_cursor ) { | |
242 SDL_SetCursor(SDL_defcursor); | |
243 } | |
244 if ( cursor != SDL_defcursor ) { | |
77
1e7e61b9b0f9
Don't crash if freeing a cursor after quit...
Sam Lantinga <slouken@lokigames.com>
parents:
1
diff
changeset
|
245 SDL_VideoDevice *video = current_video; |
1e7e61b9b0f9
Don't crash if freeing a cursor after quit...
Sam Lantinga <slouken@lokigames.com>
parents:
1
diff
changeset
|
246 SDL_VideoDevice *this = current_video; |
1e7e61b9b0f9
Don't crash if freeing a cursor after quit...
Sam Lantinga <slouken@lokigames.com>
parents:
1
diff
changeset
|
247 |
0 | 248 if ( cursor->data ) { |
249 free(cursor->data); | |
250 } | |
251 if ( cursor->save[0] ) { | |
252 free(cursor->save[0]); | |
253 } | |
77
1e7e61b9b0f9
Don't crash if freeing a cursor after quit...
Sam Lantinga <slouken@lokigames.com>
parents:
1
diff
changeset
|
254 if ( video && cursor->wm_cursor ) { |
0 | 255 video->FreeWMCursor(this, cursor->wm_cursor); |
256 } | |
257 free(cursor); | |
258 } | |
259 } | |
260 } | |
261 | |
262 int SDL_ShowCursor (int toggle) | |
263 { | |
264 int showing; | |
265 | |
266 showing = (SDL_cursorstate & CURSOR_VISIBLE); | |
267 if ( toggle >= 0 ) { | |
268 SDL_LockCursor(); | |
269 if ( toggle ) { | |
270 SDL_cursorstate |= CURSOR_VISIBLE; | |
271 } else { | |
272 SDL_cursorstate &= ~CURSOR_VISIBLE; | |
273 } | |
274 SDL_UnlockCursor(); | |
275 if ( (SDL_cursorstate & CURSOR_VISIBLE) != showing ) { | |
276 SDL_VideoDevice *video = current_video; | |
277 SDL_VideoDevice *this = current_video; | |
278 | |
279 SDL_SetCursor(NULL); | |
280 if ( video && video->CheckMouseMode ) { | |
281 video->CheckMouseMode(this); | |
282 } | |
283 } | |
284 } else { | |
285 /* Query current state */ ; | |
286 } | |
287 return(showing ? 1 : 0); | |
288 } | |
289 | |
290 void SDL_WarpMouse (Uint16 x, Uint16 y) | |
291 { | |
292 SDL_VideoDevice *video = current_video; | |
293 SDL_VideoDevice *this = current_video; | |
294 | |
595
591b438ab94a
Don't warp the mouse when a video mode hasn't been set
Sam Lantinga <slouken@libsdl.org>
parents:
527
diff
changeset
|
295 if ( !video || !SDL_PublicSurface ) { |
591b438ab94a
Don't warp the mouse when a video mode hasn't been set
Sam Lantinga <slouken@libsdl.org>
parents:
527
diff
changeset
|
296 SDL_SetError("A video mode must be set before warping mouse"); |
591b438ab94a
Don't warp the mouse when a video mode hasn't been set
Sam Lantinga <slouken@libsdl.org>
parents:
527
diff
changeset
|
297 return; |
591b438ab94a
Don't warp the mouse when a video mode hasn't been set
Sam Lantinga <slouken@libsdl.org>
parents:
527
diff
changeset
|
298 } |
591b438ab94a
Don't warp the mouse when a video mode hasn't been set
Sam Lantinga <slouken@libsdl.org>
parents:
527
diff
changeset
|
299 |
527
5c74ac147358
Fixed mouse warp position bug with offset video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
300 /* If we have an offset video mode, offset the mouse coordinates */ |
1163
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
301 if (this->screen->pitch == 0) { |
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
302 x += this->screen->offset / this->screen->format->BytesPerPixel; |
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
303 y += this->screen->offset; |
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
304 } else { |
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
305 x += (this->screen->offset % this->screen->pitch) / |
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
306 this->screen->format->BytesPerPixel; |
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
307 y += (this->screen->offset / this->screen->pitch); |
96ef83467667
Prevent division-by-zero in WarpMouse if surface's pitch is zero (a GL surface?).
Ryan C. Gordon <icculus@icculus.org>
parents:
1140
diff
changeset
|
308 } |
527
5c74ac147358
Fixed mouse warp position bug with offset video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
309 |
0 | 310 /* This generates a mouse motion event */ |
311 if ( video->WarpWMCursor ) { | |
312 video->WarpWMCursor(this, x, y); | |
313 } else { | |
314 SDL_PrivateMouseMotion(0, 0, x, y); | |
315 } | |
316 } | |
317 | |
318 void SDL_MoveCursor(int x, int y) | |
319 { | |
320 SDL_VideoDevice *video = current_video; | |
321 | |
322 /* Erase and update the current mouse position */ | |
323 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) { | |
324 /* Erase and redraw mouse cursor in new position */ | |
325 SDL_LockCursor(); | |
326 SDL_EraseCursor(SDL_VideoSurface); | |
327 SDL_cursor->area.x = (x - SDL_cursor->hot_x); | |
328 SDL_cursor->area.y = (y - SDL_cursor->hot_y); | |
329 SDL_DrawCursor(SDL_VideoSurface); | |
330 SDL_UnlockCursor(); | |
331 } else if ( video->MoveWMCursor ) { | |
332 video->MoveWMCursor(video, x, y); | |
333 } | |
334 } | |
335 | |
336 /* Keep track of the current cursor colors */ | |
337 static int palette_changed = 1; | |
338 static Uint32 pixels8[2]; | |
339 | |
340 void SDL_CursorPaletteChanged(void) | |
341 { | |
342 palette_changed = 1; | |
343 } | |
344 | |
345 void SDL_MouseRect(SDL_Rect *area) | |
346 { | |
347 int clip_diff; | |
348 | |
349 *area = SDL_cursor->area; | |
350 if ( area->x < 0 ) { | |
351 area->w += area->x; | |
352 area->x = 0; | |
353 } | |
354 if ( area->y < 0 ) { | |
355 area->h += area->y; | |
356 area->y = 0; | |
357 } | |
358 clip_diff = (area->x+area->w)-SDL_VideoSurface->w; | |
359 if ( clip_diff > 0 ) { | |
113
e21ac1dd30f1
Fixed crash if mouse is outside of the screen bounds for some reason
Sam Lantinga <slouken@lokigames.com>
parents:
77
diff
changeset
|
360 area->w = area->w < clip_diff ? 0 : area->w-clip_diff; |
0 | 361 } |
362 clip_diff = (area->y+area->h)-SDL_VideoSurface->h; | |
363 if ( clip_diff > 0 ) { | |
113
e21ac1dd30f1
Fixed crash if mouse is outside of the screen bounds for some reason
Sam Lantinga <slouken@lokigames.com>
parents:
77
diff
changeset
|
364 area->h = area->h < clip_diff ? 0 : area->h-clip_diff; |
0 | 365 } |
366 } | |
367 | |
368 static void SDL_DrawCursorFast(SDL_Surface *screen, SDL_Rect *area) | |
369 { | |
370 const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 }; | |
371 int i, w, h; | |
372 Uint8 *data, datab; | |
373 Uint8 *mask, maskb; | |
374 | |
375 data = SDL_cursor->data + area->y * SDL_cursor->area.w/8; | |
376 mask = SDL_cursor->mask + area->y * SDL_cursor->area.w/8; | |
377 switch (screen->format->BytesPerPixel) { | |
378 | |
379 case 1: { | |
380 Uint8 *dst; | |
381 int dstskip; | |
382 | |
383 if ( palette_changed ) { | |
384 pixels8[0] = SDL_MapRGB(screen->format, 255, 255, 255); | |
385 pixels8[1] = SDL_MapRGB(screen->format, 0, 0, 0); | |
386 palette_changed = 0; | |
387 } | |
388 dst = (Uint8 *)screen->pixels + | |
389 (SDL_cursor->area.y+area->y)*screen->pitch + | |
390 SDL_cursor->area.x; | |
391 dstskip = screen->pitch-area->w; | |
392 | |
393 for ( h=area->h; h; h-- ) { | |
394 for ( w=area->w/8; w; w-- ) { | |
395 maskb = *mask++; | |
396 datab = *data++; | |
397 for ( i=0; i<8; ++i ) { | |
398 if ( maskb & 0x80 ) { | |
399 *dst = pixels8[datab>>7]; | |
400 } | |
401 maskb <<= 1; | |
402 datab <<= 1; | |
403 dst++; | |
404 } | |
405 } | |
406 dst += dstskip; | |
407 } | |
408 } | |
409 break; | |
410 | |
411 case 2: { | |
412 Uint16 *dst; | |
413 int dstskip; | |
414 | |
415 dst = (Uint16 *)screen->pixels + | |
416 (SDL_cursor->area.y+area->y)*screen->pitch/2 + | |
417 SDL_cursor->area.x; | |
418 dstskip = (screen->pitch/2)-area->w; | |
419 | |
420 for ( h=area->h; h; h-- ) { | |
421 for ( w=area->w/8; w; w-- ) { | |
422 maskb = *mask++; | |
423 datab = *data++; | |
424 for ( i=0; i<8; ++i ) { | |
425 if ( maskb & 0x80 ) { | |
426 *dst = pixels[datab>>7]; | |
427 } | |
428 maskb <<= 1; | |
429 datab <<= 1; | |
430 dst++; | |
431 } | |
432 } | |
433 dst += dstskip; | |
434 } | |
435 } | |
436 break; | |
437 | |
438 case 3: { | |
439 Uint8 *dst; | |
440 int dstskip; | |
441 | |
442 dst = (Uint8 *)screen->pixels + | |
443 (SDL_cursor->area.y+area->y)*screen->pitch + | |
444 SDL_cursor->area.x*3; | |
445 dstskip = screen->pitch-area->w*3; | |
446 | |
447 for ( h=area->h; h; h-- ) { | |
448 for ( w=area->w/8; w; w-- ) { | |
449 maskb = *mask++; | |
450 datab = *data++; | |
451 for ( i=0; i<8; ++i ) { | |
452 if ( maskb & 0x80 ) { | |
453 memset(dst,pixels[datab>>7],3); | |
454 } | |
455 maskb <<= 1; | |
456 datab <<= 1; | |
457 dst += 3; | |
458 } | |
459 } | |
460 dst += dstskip; | |
461 } | |
462 } | |
463 break; | |
464 | |
465 case 4: { | |
466 Uint32 *dst; | |
467 int dstskip; | |
468 | |
469 dst = (Uint32 *)screen->pixels + | |
470 (SDL_cursor->area.y+area->y)*screen->pitch/4 + | |
471 SDL_cursor->area.x; | |
472 dstskip = (screen->pitch/4)-area->w; | |
473 | |
474 for ( h=area->h; h; h-- ) { | |
475 for ( w=area->w/8; w; w-- ) { | |
476 maskb = *mask++; | |
477 datab = *data++; | |
478 for ( i=0; i<8; ++i ) { | |
479 if ( maskb & 0x80 ) { | |
480 *dst = pixels[datab>>7]; | |
481 } | |
482 maskb <<= 1; | |
483 datab <<= 1; | |
484 dst++; | |
485 } | |
486 } | |
487 dst += dstskip; | |
488 } | |
489 } | |
490 break; | |
491 } | |
492 } | |
493 | |
494 static void SDL_DrawCursorSlow(SDL_Surface *screen, SDL_Rect *area) | |
495 { | |
496 const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 }; | |
497 int h; | |
498 int x, minx, maxx; | |
499 Uint8 *data, datab = 0; | |
500 Uint8 *mask, maskb = 0; | |
501 Uint8 *dst; | |
502 int dstbpp, dstskip; | |
503 | |
504 data = SDL_cursor->data + area->y * SDL_cursor->area.w/8; | |
505 mask = SDL_cursor->mask + area->y * SDL_cursor->area.w/8; | |
506 dstbpp = screen->format->BytesPerPixel; | |
507 dst = (Uint8 *)screen->pixels + | |
508 (SDL_cursor->area.y+area->y)*screen->pitch + | |
509 SDL_cursor->area.x*dstbpp; | |
510 dstskip = screen->pitch-SDL_cursor->area.w*dstbpp; | |
511 | |
512 minx = area->x; | |
513 maxx = area->x+area->w; | |
514 if ( screen->format->BytesPerPixel == 1 ) { | |
515 if ( palette_changed ) { | |
516 pixels8[0] = SDL_MapRGB(screen->format, 255, 255, 255); | |
517 pixels8[1] = SDL_MapRGB(screen->format, 0, 0, 0); | |
518 palette_changed = 0; | |
519 } | |
520 for ( h=area->h; h; h-- ) { | |
521 for ( x=0; x<SDL_cursor->area.w; ++x ) { | |
522 if ( (x%8) == 0 ) { | |
523 maskb = *mask++; | |
524 datab = *data++; | |
525 } | |
526 if ( (x >= minx) && (x < maxx) ) { | |
527 if ( maskb & 0x80 ) { | |
528 memset(dst, pixels8[datab>>7], dstbpp); | |
529 } | |
530 } | |
531 maskb <<= 1; | |
532 datab <<= 1; | |
533 dst += dstbpp; | |
534 } | |
535 dst += dstskip; | |
536 } | |
537 } else { | |
538 for ( h=area->h; h; h-- ) { | |
539 for ( x=0; x<SDL_cursor->area.w; ++x ) { | |
540 if ( (x%8) == 0 ) { | |
541 maskb = *mask++; | |
542 datab = *data++; | |
543 } | |
544 if ( (x >= minx) && (x < maxx) ) { | |
545 if ( maskb & 0x80 ) { | |
546 memset(dst, pixels[datab>>7], dstbpp); | |
547 } | |
548 } | |
549 maskb <<= 1; | |
550 datab <<= 1; | |
551 dst += dstbpp; | |
552 } | |
553 dst += dstskip; | |
554 } | |
555 } | |
556 } | |
557 | |
558 /* This handles the ugly work of converting the saved cursor background from | |
559 the pixel format of the shadow surface to that of the video surface. | |
560 This is only necessary when blitting from a shadow surface of a different | |
561 pixel format than the video surface, and using a software rendered cursor. | |
562 */ | |
563 static void SDL_ConvertCursorSave(SDL_Surface *screen, int w, int h) | |
564 { | |
565 SDL_BlitInfo info; | |
566 SDL_loblit RunBlit; | |
567 | |
568 /* Make sure we can steal the blit mapping */ | |
569 if ( screen->map->dst != SDL_VideoSurface ) { | |
570 return; | |
571 } | |
572 | |
573 /* Set up the blit information */ | |
574 info.s_pixels = SDL_cursor->save[1]; | |
575 info.s_width = w; | |
576 info.s_height = h; | |
577 info.s_skip = 0; | |
578 info.d_pixels = SDL_cursor->save[0]; | |
579 info.d_width = w; | |
580 info.d_height = h; | |
581 info.d_skip = 0; | |
582 info.aux_data = screen->map->sw_data->aux_data; | |
583 info.src = screen->format; | |
584 info.table = screen->map->table; | |
585 info.dst = SDL_VideoSurface->format; | |
586 RunBlit = screen->map->sw_data->blit; | |
587 | |
588 /* Run the actual software blit */ | |
589 RunBlit(&info); | |
590 } | |
591 | |
592 void SDL_DrawCursorNoLock(SDL_Surface *screen) | |
593 { | |
594 SDL_Rect area; | |
595 | |
596 /* Get the mouse rectangle, clipped to the screen */ | |
597 SDL_MouseRect(&area); | |
598 if ( (area.w == 0) || (area.h == 0) ) { | |
599 return; | |
600 } | |
601 | |
602 /* Copy mouse background */ | |
603 { int w, h, screenbpp; | |
604 Uint8 *src, *dst; | |
605 | |
606 /* Set up the copy pointers */ | |
607 screenbpp = screen->format->BytesPerPixel; | |
608 if ( (screen == SDL_VideoSurface) || | |
609 FORMAT_EQUAL(screen->format, SDL_VideoSurface->format) ) { | |
610 dst = SDL_cursor->save[0]; | |
611 } else { | |
612 dst = SDL_cursor->save[1]; | |
613 } | |
614 src = (Uint8 *)screen->pixels + area.y * screen->pitch + | |
615 area.x * screenbpp; | |
616 | |
617 /* Perform the copy */ | |
618 w = area.w*screenbpp; | |
619 h = area.h; | |
620 while ( h-- ) { | |
621 memcpy(dst, src, w); | |
622 dst += w; | |
623 src += screen->pitch; | |
624 } | |
625 } | |
626 | |
627 /* Draw the mouse cursor */ | |
628 area.x -= SDL_cursor->area.x; | |
629 area.y -= SDL_cursor->area.y; | |
630 if ( (area.x == 0) && (area.w == SDL_cursor->area.w) ) { | |
631 SDL_DrawCursorFast(screen, &area); | |
632 } else { | |
633 SDL_DrawCursorSlow(screen, &area); | |
634 } | |
635 } | |
636 | |
637 void SDL_DrawCursor(SDL_Surface *screen) | |
638 { | |
639 /* Lock the screen if necessary */ | |
640 if ( screen == NULL ) { | |
641 return; | |
642 } | |
643 if ( SDL_MUSTLOCK(screen) ) { | |
644 if ( SDL_LockSurface(screen) < 0 ) { | |
645 return; | |
646 } | |
647 } | |
648 | |
649 SDL_DrawCursorNoLock(screen); | |
650 | |
651 /* Unlock the screen and update if necessary */ | |
652 if ( SDL_MUSTLOCK(screen) ) { | |
653 SDL_UnlockSurface(screen); | |
654 } | |
655 if ( (screen == SDL_VideoSurface) && | |
656 ((screen->flags & SDL_HWSURFACE) != SDL_HWSURFACE) ) { | |
657 SDL_VideoDevice *video = current_video; | |
658 SDL_VideoDevice *this = current_video; | |
659 SDL_Rect area; | |
660 | |
661 SDL_MouseRect(&area); | |
662 | |
663 /* This can be called before a video mode is set */ | |
664 if ( video->UpdateRects ) { | |
665 video->UpdateRects(this, 1, &area); | |
666 } | |
667 } | |
668 } | |
669 | |
670 void SDL_EraseCursorNoLock(SDL_Surface *screen) | |
671 { | |
672 SDL_Rect area; | |
673 | |
674 /* Get the mouse rectangle, clipped to the screen */ | |
675 SDL_MouseRect(&area); | |
676 if ( (area.w == 0) || (area.h == 0) ) { | |
677 return; | |
678 } | |
679 | |
680 /* Copy mouse background */ | |
681 { int w, h, screenbpp; | |
682 Uint8 *src, *dst; | |
683 | |
684 /* Set up the copy pointers */ | |
685 screenbpp = screen->format->BytesPerPixel; | |
686 if ( (screen == SDL_VideoSurface) || | |
687 FORMAT_EQUAL(screen->format, SDL_VideoSurface->format) ) { | |
688 src = SDL_cursor->save[0]; | |
689 } else { | |
690 src = SDL_cursor->save[1]; | |
691 } | |
692 dst = (Uint8 *)screen->pixels + area.y * screen->pitch + | |
693 area.x * screenbpp; | |
694 | |
695 /* Perform the copy */ | |
696 w = area.w*screenbpp; | |
697 h = area.h; | |
698 while ( h-- ) { | |
699 memcpy(dst, src, w); | |
700 src += w; | |
701 dst += screen->pitch; | |
702 } | |
703 | |
704 /* Perform pixel conversion on cursor background */ | |
705 if ( src > SDL_cursor->save[1] ) { | |
706 SDL_ConvertCursorSave(screen, area.w, area.h); | |
707 } | |
708 } | |
709 } | |
710 | |
711 void SDL_EraseCursor(SDL_Surface *screen) | |
712 { | |
713 /* Lock the screen if necessary */ | |
714 if ( screen == NULL ) { | |
715 return; | |
716 } | |
717 if ( SDL_MUSTLOCK(screen) ) { | |
718 if ( SDL_LockSurface(screen) < 0 ) { | |
719 return; | |
720 } | |
721 } | |
722 | |
723 SDL_EraseCursorNoLock(screen); | |
724 | |
725 /* Unlock the screen and update if necessary */ | |
726 if ( SDL_MUSTLOCK(screen) ) { | |
727 SDL_UnlockSurface(screen); | |
728 } | |
729 if ( (screen == SDL_VideoSurface) && | |
730 ((screen->flags & SDL_HWSURFACE) != SDL_HWSURFACE) ) { | |
731 SDL_VideoDevice *video = current_video; | |
732 SDL_VideoDevice *this = current_video; | |
733 SDL_Rect area; | |
734 | |
735 SDL_MouseRect(&area); | |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
736 if ( video->UpdateRects ) { |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
737 video->UpdateRects(this, 1, &area); |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
738 } |
0 | 739 } |
740 } | |
741 | |
742 /* Reset the cursor on video mode change | |
743 FIXME: Keep track of all cursors, and reset them all. | |
744 */ | |
745 void SDL_ResetCursor(void) | |
746 { | |
747 int savelen; | |
748 | |
749 if ( SDL_cursor ) { | |
750 savelen = SDL_cursor->area.w*4*SDL_cursor->area.h; | |
751 SDL_cursor->area.x = 0; | |
752 SDL_cursor->area.y = 0; | |
753 memset(SDL_cursor->save[0], 0, savelen); | |
754 } | |
755 } |