annotate test/checkkeys.c @ 876:9e84d106ec19

(Said Max Horn on the SDL mailing list...) Hi folks, based on Eric Wing's patch, I created the attached patch which fixes the OpenGL coordinate inversion bug in SDL. It works fine over here on 10.3 with Ryan's test program (which I also attached). There is another change in it: I removed the "- 1" in the two lines using CGDisplayPixelsHigh()... while I understand from a logical point of view why they *should* be correct, I checked the actual values computed that way, and they were off-by-one. After removing the " - 1", the returned mouse coordinates are correct. I checked this by moving the mouse to the screen top/bottom in fullscreen mode, BTW. With the change, the proper values 0 and 479 are returned (in 640x480 mode). Sam, you may still want to test on 10.1, it's very simple using Ryan's minimal test code :-) Cheers, Max (Here is the reproduction case for revision history's sake...) /* * To compile: * gcc -o test test.c `sdl-config --cflags` `sdl-config --libs` -framework OpenGL * * --ryan. */ #include <stdio.h> #include "SDL.h" #include "SDL_opengl.h" int main(int argc, char **argv) { Uint32 flags = SDL_OPENGL /* | SDL_FULLSCREEN */; SDL_Surface *screen; SDL_Event event; int done = 0; GLfloat ratio; SDL_Init(SDL_INIT_VIDEO); SDL_ShowCursor(0); if ((argv[1]) && (strcmp(argv[1], "--grab") == 0)) SDL_WM_GrabInput(SDL_GRAB_ON); screen = SDL_SetVideoMode(640, 480, 0, flags); if (!screen) return(42); ratio = ((GLfloat) screen->w) / ((GLfloat) screen->h); glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glClearDepth( 1.0f ); glEnable( GL_DEPTH_TEST ); glDepthFunc( GL_LEQUAL ); glViewport( 0, 0, screen->w, screen->h); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45.0f, ratio, 0.1f, 100.0f ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SDL_GL_SwapBuffers(); // eh, close enough. #define MAX_X 6.12 #define MAX_Y 4.50 while (!done) { int x, y; GLfloat glx, gly; if (!SDL_WaitEvent(&event)) break; switch (event.type) { case SDL_KEYUP: if (event.key.keysym.sym == SDLK_ESCAPE) done = 1; break; } SDL_GetMouseState(&x, &y); glx = ((((GLfloat) x) / ((GLfloat) screen->w)) - 0.5f) * MAX_X; gly = ((((GLfloat) y) / ((GLfloat) screen->h)) - 0.5f) * MAX_Y; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(glx,-gly,-6.0f); glBegin(GL_TRIANGLES); glColor3f(1,0,0); glVertex3f( 0.00f, 0.25f, 0.00f); glColor3f(0,1,0); glVertex3f(-0.25f, -0.25f, 0.00f); glColor3f(0,0,1); glVertex3f( 0.25f, -0.25f, 0.00f); glEnd(); SDL_GL_SwapBuffers(); } SDL_Quit(); return(0); } /* end of test.c ... */
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 22 Mar 2004 09:38:20 +0000
parents 806fcbde0af3
children be9c9c8f6d53
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
2 /* Simple program: Loop, watching keystrokes
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
3 Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4 pump the event loop and catch keystrokes.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
5 */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
6
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
7 #include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
8 #include <stdlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9 #include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 #include "SDL.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
12
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
13 static void print_modifiers(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
14 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
15 int mod;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
16 printf(" modifiers:");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
17 mod = SDL_GetModState();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18 if(!mod) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
19 printf(" (none)");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
20 return;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
21 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
22 if(mod & KMOD_LSHIFT)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23 printf(" LSHIFT");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24 if(mod & KMOD_RSHIFT)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25 printf(" RSHIFT");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26 if(mod & KMOD_LCTRL)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27 printf(" LCTRL");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28 if(mod & KMOD_RCTRL)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 printf(" RCTRL");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30 if(mod & KMOD_LALT)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31 printf(" LALT");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32 if(mod & KMOD_RALT)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
33 printf(" RALT");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
34 if(mod & KMOD_LMETA)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
35 printf(" LMETA");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
36 if(mod & KMOD_RMETA)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
37 printf(" RMETA");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
38 if(mod & KMOD_NUM)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
39 printf(" NUM");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
40 if(mod & KMOD_CAPS)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
41 printf(" CAPS");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
42 if(mod & KMOD_MODE)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
43 printf(" MODE");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
44 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
45
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
46 static void PrintKey(SDL_keysym *sym, int pressed)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
47 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
48 /* Print the keycode, name and state */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
49 if ( sym->sym ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
50 printf("Key %s: %d-%s ", pressed ? "pressed" : "released",
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
51 sym->sym, SDL_GetKeyName(sym->sym));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
52 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
53 printf("Unknown Key (scancode = %d) %s ", sym->scancode,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
54 pressed ? "pressed" : "released");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
55 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
56
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
57 /* Print the translated character, if one exists */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
58 if ( sym->unicode ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
59 /* Is it a control-character? */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
60 if ( sym->unicode < ' ' ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
61 printf(" (^%c)", sym->unicode+'@');
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
62 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
63 #ifdef UNICODE
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
64 printf(" (%c)", sym->unicode);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
65 #else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
66 /* This is a Latin-1 program, so only show 8-bits */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
67 if ( !(sym->unicode & 0xFF00) )
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
68 printf(" (%c)", sym->unicode);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
69 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
70 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
71 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
72 print_modifiers();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
73 printf("\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
74 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
75
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
76 int main(int argc, char *argv[])
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
77 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
78 SDL_Event event;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
79 int done;
473
30466f501b77 Added a -fullscreen flag
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
80 Uint32 videoflags;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
81
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
82 /* Initialize SDL */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
83 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
84 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
85 exit(1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
86 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
87 atexit(SDL_Quit);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
88
473
30466f501b77 Added a -fullscreen flag
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
89 videoflags = SDL_SWSURFACE;
493
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
90 while( argc > 1 ) {
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
91 --argc;
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
92 if ( argv[argc] && !strcmp(argv[argc], "-fullscreen") ) {
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
93 videoflags |= SDL_FULLSCREEN;
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
94 } else {
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
95 fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
96 exit(1);
806fcbde0af3 Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents: 473
diff changeset
97 }
473
30466f501b77 Added a -fullscreen flag
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
98 }
30466f501b77 Added a -fullscreen flag
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
99
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
100 /* Set 640x480 video mode */
473
30466f501b77 Added a -fullscreen flag
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
101 if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
102 fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
103 SDL_GetError());
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
104 exit(2);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
105 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
106
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
107 /* Enable UNICODE translation for keyboard input */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
108 SDL_EnableUNICODE(1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
109
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
110 /* Enable auto repeat for keyboard input */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
111 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
112 SDL_DEFAULT_REPEAT_INTERVAL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
113
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
114 /* Watch keystrokes */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
115 done = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
116 while ( !done ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
117 /* Check for events */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
118 SDL_WaitEvent(&event);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
119 switch (event.type) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
120 case SDL_KEYDOWN:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
121 PrintKey(&event.key.keysym, 1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
122 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
123 case SDL_KEYUP:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
124 PrintKey(&event.key.keysym, 0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
125 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
126 case SDL_MOUSEBUTTONDOWN:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
127 /* Any button press quits the app... */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
128 case SDL_QUIT:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
129 done = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
130 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
131 default:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
132 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
133 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
134 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
135 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
136 }