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