Mercurial > sdl-ios-xcode
annotate test/testbitmap.c @ 1192:54aa9aa32327
To: sdl@libsdl.org
From: Christian Walther <cwalther@gmx.ch>
Date: Fri, 18 Nov 2005 23:39:02 +0100
Subject: [SDL] Mouse position bugs on Mac OS X
The attached patch fixes a few bugs in SDL related to the mouse position
in windowed mode on Mac OS X, reproduced using the attached minimal test
program - at least here on 10.3.9, with SDL CVS from today. Could anyone
test whether the bugs exist and are fixed by the patch on 10.2 and 10.4?
1. When using OpenGL, the vertical mouse positions obtained through
events or SDL_GetMouseState() are off by one.
2. When using OpenGL, SDL_WarpMouse() inverts the y coordinate.
3. Clicks on the topmost pixel row of the window are not recognized.
1 and 2 do not occur in non-OpenGL mode, while 3 does. All three only
occur in windowed mode, not in fullscreen.
The cause for 1 and 3 is that in Cocoa, "the location of the mouse"
seems to be defined as "the location of the top left corner of the mouse
pointer's hot pixel" (this is not documented, it's just what I found out
here), which together with the fact that Cocoa's usual y coordinates
start at the bottom and increase upwards means that the y coordinate of
the mouse runs from 1 to h, not from 0 to h-1, in a window of height h.
If it does work on 10.2 and 10.4 (I'll try to test it as soon as I can,
but at the moment all I have at hand is 10.3.9), can this be applied to
the CVS?
-Christian
To: sdl@libsdl.org
From: Christian Walther <cwalther@gmx.ch>
Date: Mon, 28 Nov 2005 10:41:51 +0100
Subject: [SDL] Re: Mouse position bugs on Mac OS X
I wrote:
> I'll try to test it as soon as I can, but at the moment all I have at hand is 10.3.9
So, here are the results of my tests (with patched and unpatched
frameworks compiled with Xcode 1.5 (gcc 3.3) on 10.3.9):
On 10.1.5, my test program doesn't run because of "Undefined symbols:
SDL undefined reference to _CGMainDisplayID expected to be defined in
Carbon". I guess not supporting 10.1 was a deliberate decision then and
that's OK with me.
On 10.2.8, 10.3.9, and 10.4.0, the bugs exist as described in my
original post and are fixed by my patch. That is, there is no difference
between pre/post 10.3 and the patched version works correctly in all
combinations of GL/non-GL and windowed/fullscreen.
I therefore recommend the patch for inclusion.
-Christian
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 28 Nov 2005 13:58:26 +0000 |
parents | be9c9c8f6d53 |
children | 782fd950bd46 c121d94672cb |
rev | line source |
---|---|
0 | 1 |
2 /* Simple program: Test bitmap blits */ | |
3 | |
4 #include <stdio.h> | |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 | |
8 #include "SDL.h" | |
9 #include "picture.xbm" | |
10 | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
11 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
12 static void quit(int rc) |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
13 { |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
14 SDL_Quit(); |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
15 exit(rc); |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
16 } |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
17 |
0 | 18 SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits) |
19 { | |
20 SDL_Surface *bitmap; | |
21 Uint8 *line; | |
22 | |
23 /* Allocate the bitmap */ | |
24 bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0); | |
25 if ( bitmap == NULL ) { | |
26 fprintf(stderr, "Couldn't allocate bitmap: %s\n", | |
27 SDL_GetError()); | |
28 return(NULL); | |
29 } | |
30 | |
31 /* Copy the pixels */ | |
32 line = (Uint8 *)bitmap->pixels; | |
33 w = (w+7)/8; | |
34 while ( h-- ) { | |
35 memcpy(line, bits, w); | |
36 /* X11 Bitmap images have the bits reversed */ | |
37 { int i, j; Uint8 *buf, byte; | |
38 for ( buf=line, i=0; i<w; ++i, ++buf ) { | |
39 byte = *buf; | |
40 *buf = 0; | |
41 for ( j=7; j>=0; --j ) { | |
42 *buf |= (byte&0x01)<<j; | |
43 byte >>= 1; | |
44 } | |
45 } | |
46 } | |
47 line += bitmap->pitch; | |
48 bits += w; | |
49 } | |
50 return(bitmap); | |
51 } | |
52 | |
53 int main(int argc, char *argv[]) | |
54 { | |
55 SDL_Surface *screen; | |
56 SDL_Surface *bitmap; | |
57 Uint8 video_bpp; | |
58 Uint32 videoflags; | |
59 Uint8 *buffer; | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
60 int i, k, done; |
0 | 61 SDL_Event event; |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
62 Uint16 *buffer16; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
63 Uint16 color; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
64 Uint8 gradient; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
65 SDL_Color palette[256]; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
66 |
0 | 67 |
68 /* Initialize SDL */ | |
69 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
70 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
71 return(1); |
0 | 72 } |
73 | |
74 video_bpp = 0; | |
75 videoflags = SDL_SWSURFACE; | |
76 while ( argc > 1 ) { | |
77 --argc; | |
78 if ( strcmp(argv[argc-1], "-bpp") == 0 ) { | |
79 video_bpp = atoi(argv[argc]); | |
80 --argc; | |
81 } else | |
82 if ( strcmp(argv[argc], "-warp") == 0 ) { | |
83 videoflags |= SDL_HWPALETTE; | |
84 } else | |
85 if ( strcmp(argv[argc], "-hw") == 0 ) { | |
86 videoflags |= SDL_HWSURFACE; | |
87 } else | |
88 if ( strcmp(argv[argc], "-fullscreen") == 0 ) { | |
89 videoflags |= SDL_FULLSCREEN; | |
90 } else { | |
91 fprintf(stderr, | |
92 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", | |
93 argv[0]); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
94 quit(1); |
0 | 95 } |
96 } | |
97 | |
98 /* Set 640x480 video mode */ | |
99 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { | |
100 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", | |
101 video_bpp, SDL_GetError()); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
102 quit(2); |
0 | 103 } |
104 | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
105 if (video_bpp==8) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
106 /* Set a gray colormap, reverse order from white to black */ |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
107 for ( i=0; i<256; ++i ) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
108 palette[i].r = 255-i; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
109 palette[i].g = 255-i; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
110 palette[i].b = 255-i; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
111 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
112 SDL_SetColors(screen, palette, 0, 256); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
113 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
114 |
0 | 115 /* Set the surface pixels and refresh! */ |
116 if ( SDL_LockSurface(screen) < 0 ) { | |
117 fprintf(stderr, "Couldn't lock the display surface: %s\n", | |
118 SDL_GetError()); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
119 quit(2); |
0 | 120 } |
121 buffer=(Uint8 *)screen->pixels; | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
122 if (screen->format->BytesPerPixel!=2) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
123 for ( i=0; i<screen->h; ++i ) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
124 memset(buffer,(i*255)/screen->h, screen->pitch); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
125 buffer += screen->pitch; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
126 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
127 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
128 else |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
129 { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
130 for ( i=0; i<screen->h; ++i ) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
131 gradient=((i*255)/screen->h); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
132 color = SDL_MapRGB(screen->format, gradient, gradient, gradient); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
133 buffer16=(Uint16*)buffer; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
134 for (k=0; k<screen->w; k++) |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
135 { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
136 *(buffer16+k)=color; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
137 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
138 buffer += screen->pitch; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
139 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
140 } |
0 | 141 SDL_UnlockSurface(screen); |
142 SDL_UpdateRect(screen, 0, 0, 0, 0); | |
143 | |
144 /* Load the bitmap */ | |
145 bitmap = LoadXBM(screen, picture_width, picture_height, | |
146 (Uint8 *)picture_bits); | |
147 if ( bitmap == NULL ) { | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
148 quit(1); |
0 | 149 } |
150 | |
151 /* Wait for a keystroke */ | |
152 done = 0; | |
153 while ( !done ) { | |
154 /* Check for events */ | |
155 while ( SDL_PollEvent(&event) ) { | |
156 switch (event.type) { | |
157 case SDL_MOUSEBUTTONDOWN: { | |
158 SDL_Rect dst; | |
159 | |
160 dst.x = event.button.x - bitmap->w/2; | |
161 dst.y = event.button.y - bitmap->h/2; | |
162 dst.w = bitmap->w; | |
163 dst.h = bitmap->h; | |
164 SDL_BlitSurface(bitmap, NULL, | |
165 screen, &dst); | |
166 SDL_UpdateRects(screen,1,&dst); | |
167 } | |
168 break; | |
169 case SDL_KEYDOWN: | |
170 /* Any key press quits the app... */ | |
171 done = 1; | |
172 break; | |
173 case SDL_QUIT: | |
174 done = 1; | |
175 break; | |
176 default: | |
177 break; | |
178 } | |
179 } | |
180 } | |
181 SDL_FreeSurface(bitmap); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
182 SDL_Quit(); |
0 | 183 return(0); |
184 } |