Hardcheese
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm

How do you pick up keyboard input with OpenGLES?

Sat Sep 01, 2012 4:06 pm

I've read a lot trawling the internet about how this could work but nothing really that conclusive.

I understand you can use EGL and SDL, but being a bit of a novice graphics programmer I can't really implement it without an example to give me something to get started from. Could someone please point me in the direction of an example or at least something with a little more detail on the subject.

I have already managed to get the graphics I want rendered on the screen all I need is something to pick up key and mouse events.

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 7:35 am

Once you have a basic SDL context (you need to create an SDL Window for mouse and keyboard functions but not an OpenGL Context use EGL for that) you just need to process the SDL event loop

http://www.libsdl.org/intro.en/usingevents.html

Here is some basic code from one of my (non OpenGL demos)

Code: Select all

{
   SDL_Init( SDL_INIT_VIDEO );

   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
      SDL_HWSURFACE | SDL_DOUBLEBUF );
   SDL_WM_SetCaption( "Invaders Demo", 0 );


  int xPos=50;
  int yPos=50;
  int xVel=0, yVel=0;
  SDL_Event event;
  bool gameRunning = true;

   while (gameRunning)
   {
     while ( SDL_PollEvent(&event) )
     {
       switch (event.type)
       {

         case SDL_QUIT : gameRunning = false; break;


         case SDL_KEYDOWN:

          switch( event.key.keysym.sym )
          {
          case SDLK_ESCAPE :  gameRunning = false; break;
          case SDLK_LEFT: xVel = -SPEED; break;
          case SDLK_RIGHT: xVel = SPEED; break;
          case SDLK_UP: yVel = -SPEED; break;
          case SDLK_DOWN: yVel = SPEED; break;
          case SDLK_SPACE : recording ^= true; break;
          default : break;
          }

        break;
        case SDL_KEYUP :
         switch( event.key.keysym.sym )
         {

          case SDLK_LEFT:
            if( xVel < 0 )
              xVel = 0;
          break;
          case SDLK_RIGHT:
            if( xVel > 0 )
              xVel = 0;
          break;
          case SDLK_UP:
            if( yVel < 0 )
              yVel = 0;
          break;
          case SDLK_DOWN:
            if( yVel > 0 )
              yVel = 0;
          break;
          default : break;
         }

      }
    }
   xPos+=xVel;
   yPos+=yVel;
   // check against screen
   if(xPos>WINDOW_WIDTH-64)
     xPos=WINDOW_WIDTH-64;
   else if(xPos<0)
     xPos=0;
   if(yPos>WINDOW_HEIGHT-64)
     yPos=WINDOW_HEIGHT-64;
   else if(yPos<0)
     yPos=0;



   }

   SDL_FreeSurface(bitmap);

   SDL_Quit();

   return 0;
}

For a more complex example look at some of my pingl demos here http://code.google.com/p/pingl/

Jon

Hardcheese
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 9:43 am

Thanks Jon.

So do you create one SDL window and one EGL window leaving the SDL window blank or do you somehow integrate them so that your graphics are drawn in the SDL window?

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 9:54 am

I create both, First create a small SDL window, which is not used but the creation of the window actually enables the mouse and key subsystem.

Then do the EGL stuff, here is some of the code I use

Code: Select all

void exitfunc()
{
delete win;
SDL_Quit();
bcm_host_deinit();
}

int main()
{
  bcm_host_init();
	atexit(exitfunc);

	if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
	{
      std::cerr<<"Unable to init SDL: "<<SDL_GetError()<<"\n";
      exit(EXIT_FAILURE);
  }


	SDL_Surface* myVideoSurface = SDL_SetVideoMode(0,0, 32,  SDL_SWSURFACE);
// Print out some information about the video surface
  if (myVideoSurface != NULL) 
  {
    std::cout << "The current video surface bits per pixel is " << (int)myVideoSurface->format->BitsPerPixel << std::endl;
  }
	
	// now create EGL window and do GL stuff

}
I've started looking at the SDL 2.0 source to see how easy it would be to integrate the EGL stuff into the existing OpenGLES sub-systems but not done that much yet, will post once I get a chance to do more investigation.

Jon

Hardcheese
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 10:18 am

I had tried something similar to this before but the creation of the loop to check events interferes with the creation of the EGL window.

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 10:25 am

can you post the code?

Hardcheese
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 10:45 am

I can't really post the code now but it is because the game hangs on the loop checking for events so doesn't get to the creation of the EGL window before you exit the SDL window and therefore exit the loop. I hope that makes sense.

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 11:05 am

In my example above, I first create the SDL window, then the EGL window, only once both have been done do I process the SDL Event loop.

Usually the process I use is to do all initial OpenGL setup (load textures, models etc) then go into the event loop, where I process the keys / mouse, update the animation then redraw.

Something like this

Image

Hardcheese
Posts: 36
Joined: Sat Sep 01, 2012 12:25 pm

Re: How do you pick up keyboard input with OpenGLES?

Mon Sep 03, 2012 11:15 am

That solved it! I just had to make a few modifications to the example I was using to create the EGL window. Many thanks for your help. :D

Return to “OpenGLES”