Minimalist's C++ OpenGL Programming Template for Windows, Mac OSX, and Linux

1. Introduction

You can download pieces of code that allows you to open an OpenGL-enabled window, take key input, monitor key and mouse status etc.  In the current trend, you need to create a big application framework and then fill the small amount of code.  The file created by the framework gets megabytes when the amount you write is just a few hundred lines.  It's like dispatching a fully loaded F-15E Strike Eagle to kill one rat.  It is often laughable.  On the other hand, the framework you can download here is small.  This framework just opens up a window and let you write a polling-based main loop, just like an old-style programming.  There is nothing wrong with a polling-based programming, which is good enough for simple purposes.

I've been using this framework for verifying my algorithms.  This framework has been derived from the code I wrote for YSFLIGHT, and also I used it in the C++ programming course.  So, I think the code is well battle-tested.  Well, none of my students used Linux framework.  So, Linux framework may still be a little weak.

This framework helps you if you faced, or you are facing, or will probably face one of the following problems.

  • You tried to teach C++ and OpenGL and ended up with spending half of the semester talking about how to open a window.
  • You tried to teach C++ and OpenGL and ended up with spending the rest half of the semester talking about event-driven programming.
  • You tried to teach C++ and OpenGL and mandated programming on Windows, then 6 out of 30 students dropped your course.
  • You tried to teach C++ and OpenGL and mandated programming on Mac OS X, then all but 5 students dropped your course.
  • You tried to teach C++ and OpenGL and mandated programming on Linux, then all but 1 student dropped your course.
  • You tried to teach C++ and OpenGL and used GLUT, ended up with doing nothing spectacular.
  • Your back start itching when you need to create megabytes of framework-generated files just to write a few hundreds of lines of code for verifying your algorithms.
  • You want to try programming for Mac OS X but don't like Objective-C.

I have also included my bitmap-font library in the download package so that you can draw ASCII characters without breaking code portability.  To me, lack of ASCII-text drawing function is only one weakness of OpenGL.  I'm not talking about fancy decorated stylish font.  I just need to draw something readable on the screen, and I don't want to break portability just for that purpose.

Although I have written a reference manual, I suppose it is easier to read some short examples to see how to use my functions. Here is one of the examples included in the package.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#ifdef WIN32
#include <windows.h>

#ifndef MACOSX
#include <GL/gl.h>
#include <GL/glu.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

#include <fssimplewindow.h>

int main(void)
    FsOpenWindow(800,600,0); // 800x600 pixels, useDoubleBuffer=0





        int wid,hei;


        int x1,y1,x2,y2;
        int r,g,b;




    return 0;

Easy enough, isn't it?  Maybe, one thing I need to explain is FsPollDevice() function.  This function internally checks events and stores key states, key strokes, mouse status, etc., in the template's buffer.  Unless you call this function once in the loop, functions like FsInkey(), FsGetMouseState(), and other device-inquiry functions will not return correct value.

The package also includes the same set of functions that works on Windows and Linux. If you stick with the template, your program can be compiled and run on Mac OS X, Windows and Linux.

The condition for using the source codes and programs available on this page is as follows: you take responsibility for any consequence of using the source codes and/or the programs available on this page, and I will not take responsibility.  If you agree with this condition, you can use the source codes available on this page in your program, modify them as necessary, and also bundle the joystick calibrator also available in this web site to your program, regardless of free or commercial, open source or proprietary.  If you do not agree this condition, please do not use anything you download from this web site.  Let me make clear one thing.  Using source code available from this page will NOT obligate you to disclose your source code.

2. Download


3. About Examples

If you learned programming in 1980s, these examples may remind you of good-old days of programming. Some of the examples use my OpenGL bitmap-font functions.  See this page for more details.

The first example is line art. It draws random lines with random colors. It was one of the first programming examples that I saw when I started learning programming.

The next example is "Squash" The ball bounces on the top, left, and right edges of the window, and you need to move your racket by mouse to prevent the ball from falling below the bottom edge of the window.

The third example is "Echo." This shows how you can get characters produced by key strokes. The template has two functions for getting key strokes, FsInkey() and FsInkeyChar(). FsInkey is for getting raw key strokes and FsInkeyChar is for getting characters.

The fourth example has a diamond that follows the mouse cursor. It is for showing how to get mouse state.

The fifth example is a typical OpenGL example, flipping cube. The cube rotates toward the direction of the mouse cursor relative to the center of the window.

The sixth example demonstrates how to check key status. This program is a simple shooting game. FsGetKeyState function will return non-zero if the key specified by the parameter is depressed, or zero if not. You can see fskeycode.h for the keycode used in this template.

4. Function Reference

You can see the function reference from here.  It is also included in the zip file after 2010/12/11 version.

[Update Information]

Fixed misplaced const qualifiers of YsGlWriteStringToSingleBitBitmap and YsGlWriteStringToRGBA8Bitmap

Some bug fixes.  Added function reference.

2010/01/04 [Download this version]
FsInkey and FsGetKeyState functions can detect Alt key.  (In Mac OS, command and option keys)

2009/12/25 [Download this version]
First release.