Programming Pong in C and OpenGL – Part III

Sorry for the delay in this post, but here’s Part III.

For reference, you probably want to start out with Part’s II and I here and here.

Last we left off I said I’d get into some of the basic mechanics of GLUT and OpenGL, so let’s hit the ground running.

GLUT

GLUT, or the Graphics Library Utility Toolkit, is a cross-platform C library for generating windows and handling IO events. Basically, GLUT was written so you can get to learning OpenGL very quickly without having to spend enormous amounts of time learning how to handle mouse or keyboard actions in your current OS, or worse, learn how to setup a simple GUI just so you can see your work. On top of making these tasks simpler, GLUT is, as I mentioned, cross-platform. This means that you can take your code and recompile it on another OS as long as you have the appropriate GLUT library referenced in your IDE. There are currently versions of GLUT created for Linux, OS X, Windows, and probably even more operating systems.

So how do we create a simple window?

Here’s a snippet of code for how to do just that!

int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);

glutCreateWindow(“Tim GL”);

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardup);
glutIdleFunc(idle);

glutMainLoop();
return EXIT_SUCCESS;
}

You start off with your regular main function in C. Then you call the glutInit function and pass it is passed any parameters you may have passed to main. For this tutorial you won’t be needing any, so don’t worry about it. If you need to find out more, check out the GLUT Documentation.

Next, you pass the glutInitDisplayMode() function a “logically OR-ed” value of GLUT_RGBA, GLUT_DOUBLE, and GLUT_DEPTH. If you don’t know what logically OR-ed means, just think of it as smooshing all these flags together to set the display mode with all of these properties. (If you want an in-depth explanation of this, just ask and I’ll put up a post about it!).

After this, we set the pixel dimensions (size) of the window to 500×500 pixels with the line “glutInitWindowSize(500,500);”. We next set the window’s title bar with “glutCreateWindow(”Tim GL”);” You can set either of these last two functions to whatever value you wish!

Next, is the pretty interesting part. And to understand it you need to understand a little bit about Call-Back functions. Call-Back functions are functions that hold a pointer to another function and call it when some event happens. (This is also why Call-Back functions are usually regarded as “Event-Driven Programming”.) To explain this it’s best to show you by example. I’ll start with the glutKeyboardFunc(keyboard). Whenever a key is pressed down, we have set glutKeyboardFunc() to call ANOTHER function called keyboard that WE have defined. The function is not show here, but we will get to that in the next post! Suffice it to say, our keyboard function will handle certain things like “what to do when the up arrow is pressed” in our Pong clone.

It is important to understand that we can’t just write a function that constantly checks the keyboard like this

while(TRUE) {

whatIsTheKeyboardDoing();

}

because we’d be in an infinite loop and never be able to do anything else. The Call-Back function allows us to be “called back” whenever someone presses the keyboard.

Now that we’ve explained that, the rest of the functions are easy to describe:

  • glutDisplayFunc() is called most of the time and is the function that paints our screen with whatever we tell it to with OpenGl
  • glutReshapeFunc() is what is called whenever the user resizes the window (In case we need to do anything special if the user all of a sudden changes the screen size on us.)
  • glutKeyboardFuncUp() is a tricky one, and it’s what is called whenever a key is “depressed”, or let go. This is a subtle point, but it is useful to know when someone presses a key and let’s go of one. Without this, you wouldn’t be able to tell when someone was holding a key.
  • glutIdleFunc() this is the function that is called whenever there is nothing to do!

So that’s it! That’s about all you need to know for GLUT for now! Check out the next post for OpenGL.

Share this Post!
  • Digg
  • Reddit
  • del.icio.us
  • Facebook
  • Technorati
  • Propeller
  • NewsVine
  • Google Bookmarks
  • StumbleUpon
  • Ma.gnolia
  • YahooMyWeb
  • Netvouz
  • blogmarks

Tags: , , , , ,

2 Responses to “Programming Pong in C and OpenGL – Part III”

  1. Wall Hooks says:

    `:: that seems to be a great topic, i really love it ‘”"

  2. Our goal is to make research as easy as possible for you! Discover the research!…

    [...]Programming Pong in C and OpenGL – Part III « Last Stop[...]…

Leave a Reply