Reading keys on linux

Started by
16 comments, last by Plasmite 20 years, 10 months ago
Afaik you have to create a display (via SDL_SetVideoMode) to be able to poll events (Here's the link to the corresponding documentation). As I can see you did'nt even initialize SDL.
Add those two lines in the beginning of your program and it shoud work:
int main(int argc, char *argv[]){    SDL_Event event;    int gameover=0;    // Add following two lines    SDL_Init(SDL_INIT_VIDEO);    SDL_SetVideoMode(320, 200, 0, SDL_ANYFORMAT);        while(!gameover)    {      if(SDL_PollEvent(&event))      {          switch(event.type)      {        case SDL_KEYDOWN:          switch(event.key.keysym.sym)          {            case SDLK_ESCAPE:            case SDLK_q:            gameover=1;            break;          } // end switch        break;      } // end switch          } // end if    } // end while  return 0;}



[edited by - baumep on June 9, 2003 6:01:05 AM]
baumep
Advertisement
That''s confusing. I thought displays are for graphics, not for keys. Well, I''ll keep on learning...
Alright, I''m reading keys with SDL now. This is silly, but now I have one window for GL, and one for SDL. If I''m in openGL window, glut functions work but keys don''t. Keys work when I''m in SDL window, but the GL window is the one that''s supposed to be fullscreen. How am I supposed to tell glut functions that they should draw their stuff to the SDL window? (I assume then it could be fullscreen.)

I downloaded one SDL example code from here, that''s supposed to enable me to use openGL, but I don''t understand anything out of it. I don''t want to just start coping it. (nothing would come out of it either).
You just initialize the SDL window with something like

  SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 32, SDL_OPENGL ); 


And then do anything GL. The SDL window will be the context. It works pretty well for me.
I checked some h files, and figured out I should use that SDL_OPENGL flag in SDL_SetVideoMode(). Now, if I use glutCreateWindow() also, it creates me another window (which I don''t want to happen). If I don''t use it, however, other glut functions give me Segmentation Fault error message.

Actually: "Fatal signal: Segmentation Fault (SDL Parachute Deployed)"

That sounds like SDL has done something, but:

screen=SDL_SetVideoMode(1024,768,32,SDL_OPENGL);
printf("A\n");
glutDisplayFunc(&DrawGLScene);
printf("B\n");

results in printing only A before error message. So I guess it''s glut functions not wanting to cooperate.
I realized I can use glVertex3f() and other stuff without glutDisplayFunc() and those other glut...Func things. So, I got rid of Segmentation Faults and have my keys working!!! Finally!!!

I think I''m running out of problems, but there''s one more thing. (I think this is the last one.) The window remains black, as if I would not be drawing anything there.
There was a line "glutSwapBuffers();" after all the drawing. Now when using SDL window I can''t have that there. Should I put there something that would do the same thing with SDL window''s buffers?
SDL_GL_SwapBuffers() should do the trick.
Btw. check out the SDL-Doc page; it should help you a lot with SDL questions.
baumep
Actually I just realized, that I could have checked that SDL_GL_SwapBuffers() from the example code. So, keys and graphics working simultaniusly! Alright.
And hey. These NeHe is a great place. I''ve never received so much reasonable answers to such a long row of questions.

This topic is closed to new replies.

Advertisement