Jump to content
  • Advertisement
Sign in to follow this  

GLUT Wrapper

This topic is 2850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I decided one day that I should wrap my GLUT functionality into a class. I then found out this is a LOT more confusing than I ever thought it would be. After looking at many things on the internet I have it working just about with the use of lots of static things which obviously is not ideal, however as long as its all simple and reusable and I can reduce copy and pasting to about 3 lines instead of large chunks of code then I am happy(ish).

The problem I have now is that the update function is within the GLUT Wrapper class and I don't really want to change that file for every different project. Is there a way I can pass a pointer to a function into the initialise of the class so it uses an update function specified in the main instead of in the class???? I would also want to do this for my draw and keyboard read functions.

Here's some code (it doesn't look very pretty but this GLUT malarkey seems like a lot more hassle then I first thought.)


#pragma once
#include "StandardIncludes.h"
#include "FPSCounter.h"

static float windowHeight = 0;
static float windowWidth = 0;

class GLUTWrapper
GLUTWrapper(int argWindowWidth, int argWindowHeight);

void Initialise(int argc, char **argv);

static void WinReshapeFunc(GLint w, GLint h);
void Cleanup();
static void HandleKeyPress(unsigned char key, int x, int y);

static void Draw();
static void Update(int value);

static void ProcessMousePassiveMotion(int x, int y);
static void ProcessMouse(int button, int state, int x, int y);

static int GetWindowWidth() { return windowWidth; }
static void SetWindowWidth(float value) { windowWidth = value; }
static int GetWindowHeight() { return windowHeight; }
static void SetWindowHeight(float value) { windowHeight = value; }

Vector3f cameraPosition;
Vector3f cameraPositionSet;

Vector3f lookAtPosition;

float elapsedTime;
float prevElapsedTime;

Vector2f mousePosition;
//FPSCounter* fpsCount;

int FPS;


#include "GLUTWrapper.h"
#include <ctime>

GLUTWrapper::GLUTWrapper(int argWindowWidth, int argWindowHeight)
gravityActive = true;

elapsedTime = 0;
prevElapsedTime = 0;
const int FPS = 60;

windowWidth = argWindowWidth; windowHeight = argWindowHeight;
//fpsCount = new FPSCount();


void GLUTWrapper::Initialise(int argc, char **argv)

//glutSetCursor(GLUT_CURSOR_NONE); //hide the cursor
glEnable ( GL_LIGHTING ) ; // Enable the lighting

/* initialize random seed: */
srand ( time(0) );

glShadeModel(GL_SMOOTH); //uses GL_SMOOTH or GL_FLAT

// initialise the glut library
glutInit(&argc, argv);

// set up the initial display mode
// need both double buffering and z-buffering

// set the initial window position
glutInitWindowPosition(0, 0);
// set the initial window size
glutInitWindowSize(windowWidth, windowHeight);
// create and name the window
// reshape callback for current window
// set display callback for current window
glutMouseFunc(ProcessMouse); //Process mouse clicks
glutPassiveMotionFunc(ProcessMousePassiveMotion); //Process mouse movement

glutTimerFunc(25, Update, 0);

// enter the glut event processing loop


void GLUTWrapper::WinReshapeFunc(GLint w, GLint h)
windowWidth = (float)w;
windowHeight = (float)h;
glViewport(0, 0, w, h);
//glOrtho(0, windowWidth, 0, windowHeight, 1, 1000);
gluPerspective(45.0, windowWidth / windowHeight, 1.0, 1000.0);

void GLUTWrapper::Cleanup()
void GLUTWrapper::HandleKeyPress(unsigned char key, int x, int y)
switch (key)
case 27: //Escape key


void GLUTWrapper::Draw()
// clear the back buffer

gluPerspective(45.0, windowWidth / windowHeight, 1.0, 1500.0);

//gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
// lookAtPosition.x, lookAtPosition.y, 0,
// //(windowWidth / (currentLevel->GetMazeWidth() * 0.5f)), (windowHeight / (currentLevel->GetMazeHeight() * 0.5f)), 0.0,
// 0.0, 1.0, 0.0);

// swap the buffers of the current window

void GLUTWrapper::Update(int value)
glutTimerFunc(25, Update, 0);

void GLUTWrapper::ProcessMousePassiveMotion(int x, int y)

void GLUTWrapper::ProcessMouse(int button, int state, int x, int y)
// if a mouse button is down
if (state == GLUT_DOWN)
if (button == GLUT_LEFT_BUTTON) //If the left button is down
if (button == GLUT_RIGHT_BUTTON) //If the right button is down


Many thanks!

Share this post

Link to post
Share on other sites
Good morning.

To answer your question first, yes you can pass a pointer to a function as a parameter of a function. You could then use the function pointer to call the function in the sameway as you would call the function. If you're unfamiliar with pointers to functions then you may want to do some googling on the subject. There are many more resources on the web that explain them much more completely than I could in a minute here.

You could try:
or possibly

Having said all that, are you sure that is what you really want to do? C++ supports virtual functions and inheritance which can be used to do nearly the same thing.

And lastly, is there a particular reason that you defined the windowHeight and windowWidth as static global variables?

(EDIT: Spelling)

[Edited by - JasonR on December 27, 2010 9:09:20 AM]

Share this post

Link to post
Share on other sites
Many thanks for the reply JasonR,

1st - thanks for the articles on function pointers, very interesting.

2nd - You are right I think overriding functions is the way like is done in this article:

I made the window width and height global because it was giving me link errors when I tried to make them static member variables and I couldn't figure out why at all.

To tell the truth all this hassle has persuaded me to look into other options instead of GLUT as it just seems to be complication after complication so I'm now looking into GLFW as I think it will be worth it in the long run.

Thanks :-)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!