Mercurial > sdl-ios-xcode
annotate test/testgamma.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 /* Bring up a window and manipulate the gamma on it */ | |
3 | |
4 #include <stdlib.h> | |
5 #include <stdio.h> | |
6 #include <string.h> | |
7 #include <math.h> | |
8 | |
9 #include "SDL.h" | |
10 | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
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:
0
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:
0
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:
0
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:
0
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:
0
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:
0
diff
changeset
|
17 |
0 | 18 /* Turn a normal gamma value into an appropriate gamma ramp */ |
19 void CalculateGamma(double gamma, Uint16 *ramp) | |
20 { | |
21 int i, value; | |
22 | |
23 gamma = 1.0 / gamma; | |
24 for ( i=0; i<256; ++i ) { | |
25 value = (int)(pow((double)i/256.0, gamma)*65535.0 + 0.5); | |
26 if ( value > 65535 ) { | |
27 value = 65535; | |
28 } | |
29 ramp[i] = (Uint16)value; | |
30 } | |
31 } | |
32 | |
33 /* This can be used as a general routine for all of the test programs */ | |
34 int get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 *flags) | |
35 { | |
36 int i; | |
37 | |
38 *w = 640; | |
39 *h = 480; | |
40 *bpp = 0; | |
41 *flags = SDL_SWSURFACE; | |
42 | |
43 for ( i=1; argv[i]; ++i ) { | |
44 if ( strcmp(argv[i], "-width") == 0 ) { | |
45 if ( argv[i+1] ) { | |
46 *w = atoi(argv[++i]); | |
47 } | |
48 } else | |
49 if ( strcmp(argv[i], "-height") == 0 ) { | |
50 if ( argv[i+1] ) { | |
51 *h = atoi(argv[++i]); | |
52 } | |
53 } else | |
54 if ( strcmp(argv[i], "-bpp") == 0 ) { | |
55 if ( argv[i+1] ) { | |
56 *bpp = atoi(argv[++i]); | |
57 } | |
58 } else | |
59 if ( strcmp(argv[i], "-fullscreen") == 0 ) { | |
60 *flags |= SDL_FULLSCREEN; | |
61 } else | |
62 if ( strcmp(argv[i], "-hw") == 0 ) { | |
63 *flags |= SDL_HWSURFACE; | |
64 } else | |
65 if ( strcmp(argv[i], "-hwpalette") == 0 ) { | |
66 *flags |= SDL_HWPALETTE; | |
67 } else | |
68 break; | |
69 } | |
70 return i; | |
71 } | |
72 | |
73 int main(int argc, char *argv[]) | |
74 { | |
75 SDL_Surface *screen; | |
76 SDL_Surface *image; | |
77 float gamma; | |
78 int i; | |
79 int w, h, bpp; | |
80 Uint32 flags; | |
81 Uint16 ramp[256]; | |
82 Uint16 red_ramp[256]; | |
83 Uint32 then, timeout; | |
84 | |
85 /* Check command line arguments */ | |
86 argv += get_video_args(argv, &w, &h, &bpp, &flags); | |
87 | |
88 /* Initialize SDL */ | |
89 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
90 fprintf(stderr, | |
91 "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:
0
diff
changeset
|
92 return(1); |
0 | 93 } |
94 | |
95 /* Initialize the display, always use hardware palette */ | |
96 screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE); | |
97 if ( screen == NULL ) { | |
98 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", | |
99 w, h, 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:
0
diff
changeset
|
100 quit(1); |
0 | 101 } |
102 | |
103 /* Set the window manager title bar */ | |
104 SDL_WM_SetCaption("SDL gamma test", "testgamma"); | |
105 | |
106 /* Set the desired gamma, if any */ | |
107 gamma = 1.0f; | |
108 if ( *argv ) { | |
109 gamma = (float)atof(*argv); | |
110 } | |
111 if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) { | |
112 fprintf(stderr, "Unable to set gamma: %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:
0
diff
changeset
|
113 quit(1); |
0 | 114 } |
115 | |
116 #if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */ | |
117 /* See what gamma was actually set */ | |
118 float real[3]; | |
119 if ( SDL_GetGamma(&real[0], &real[1], &real[2]) < 0 ) { | |
120 printf("Couldn't get gamma: %s\n", SDL_GetError()); | |
121 } else { | |
122 printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n", | |
123 real[0], real[1], real[2]); | |
124 } | |
125 #endif | |
126 | |
127 /* Do all the drawing work */ | |
128 image = SDL_LoadBMP("sample.bmp"); | |
129 if ( image ) { | |
130 SDL_Rect dst; | |
131 | |
132 dst.x = (screen->w - image->w)/2; | |
133 dst.y = (screen->h - image->h)/2; | |
134 dst.w = image->w; | |
135 dst.h = image->h; | |
136 SDL_BlitSurface(image, NULL, screen, &dst); | |
137 SDL_UpdateRects(screen, 1, &dst); | |
138 } | |
139 | |
140 /* Wait a bit, handling events */ | |
141 then = SDL_GetTicks(); | |
142 timeout = (5*1000); | |
143 while ( (SDL_GetTicks()-then) < timeout ) { | |
144 SDL_Event event; | |
145 | |
146 while ( SDL_PollEvent(&event) ) { | |
147 switch (event.type) { | |
148 case SDL_QUIT: /* Quit now */ | |
149 timeout = 0; | |
150 break; | |
151 case SDL_KEYDOWN: | |
152 switch (event.key.keysym.sym) { | |
153 case SDLK_SPACE: /* Go longer.. */ | |
154 timeout += (5*1000); | |
155 break; | |
156 case SDLK_UP: | |
157 gamma += 0.2f; | |
158 SDL_SetGamma(gamma, gamma, gamma); | |
159 break; | |
160 case SDLK_DOWN: | |
161 gamma -= 0.2f; | |
162 SDL_SetGamma(gamma, gamma, gamma); | |
163 break; | |
164 case SDLK_ESCAPE: | |
165 timeout = 0; | |
166 break; | |
167 default: | |
168 break; | |
169 } | |
170 break; | |
171 } | |
172 } | |
173 } | |
174 | |
175 /* Perform a gamma flash to red using color ramps */ | |
176 while ( gamma < 10.0 ) { | |
177 /* Increase the red gamma and decrease everything else... */ | |
178 gamma += 0.1f; | |
179 CalculateGamma(gamma, red_ramp); | |
180 CalculateGamma(1.0/gamma, ramp); | |
181 SDL_SetGammaRamp(red_ramp, ramp, ramp); | |
182 } | |
183 /* Finish completely red */ | |
184 memset(red_ramp, 255, sizeof(red_ramp)); | |
185 memset(ramp, 0, sizeof(ramp)); | |
186 SDL_SetGammaRamp(red_ramp, ramp, ramp); | |
187 | |
188 /* Now fade out to black */ | |
189 for ( i=(red_ramp[0] >> 8); i >= 0; --i ) { | |
190 memset(red_ramp, i, sizeof(red_ramp)); | |
191 SDL_SetGammaRamp(red_ramp, NULL, NULL); | |
192 } | |
193 SDL_Delay(1*1000); | |
194 | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
195 SDL_Quit(); |
0 | 196 return(0); |
197 } |