Create a Game Engine: Part III GLUT

Let’s continue with our game engine and in case you forgot from the previous tutorial, we are breaking the main.cpp file into a few modules to handle them more easily. Finally, in this part, we take the code from our main.cpp and put it in a class which uses the structures we created in Part II to initialize OpenGL.


Sorry for the ugly red pencil drawing circle ūüôā


So first let’s create Init_GLUT.h and Init_GLUT.cpp in the same Init folder. So far you project folders should be like this:


Init Folder

Now let’s see the Init_GLUT.h class:

#pragma once
#include "ContextInfo.h"
#include "FrameBufferInfo.h"
#include "WindowInfo.h"
#include <iostream>
#include "Init_GLEW.h"

namespace Core {
   namespace Init{//two namespaces

       class Init_GLUT{

       public:             //use the structures from Part II
         static void init(const Core::WindowInfo& window,
                          const Core::ContextInfo& context,
                          const Core::FramebufferInfo& framebufferInfo);

          static void run();//called from outside
          static void close();

          void enterFullscreen();
          void exitFullscreen();

         //used to print info about GL
         static void printOpenGLInfo(const Core::WindowInfo& windowInfo,
                                     const Core::ContextInfo& context);
            static void idleCallback(void);
            static void displayCallback(void);
            static void reshapeCallback(int width, int height);
            static void closeCallback();

Now let’s see¬†Init_GLUT.cpp. This file is a lot bigger so I will break it here in a few parts. Let’s begin with init method which is the most important one¬†here:

#include "Init_GLUT.h"

using namespace Core::Init;

void Init_GLUT::init(const Core::WindowInfo& windowInfo,
                     const Core::ContextInfo& contextInfo,
                     const Core::FramebufferInfo& framebufferInfo)
   //we need to create these fake arguments
  int fakeargc = 1;
  char *fakeargv[] = { "fake", NULL };
  glutInit(&fakeargc, fakeargv);

  if (contextInfo.core)
       //As I said in part II, version doesn't matter
       // in Compatibility mode

   //these functions were called in the old main.cpp
   //Now we use info from the structures
  glutInitWindowSize(windowInfo.width, windowInfo.height);


   std::cout << "GLUT:initialized" << std::endl;
   //these callbacks are used for rendering

  //init GLEW, this can be called in main.cpp


  //our method to display some info. Needs contextInfo and windowinfo
  printOpenGLInfo(windowInfo, contextInfo);


//starts the rendering Loop
void Init_GLUT::run()
   std::cout << "GLUT:\t Start Running " << std::endl;

First we are creating fake arguments for¬†glutInit function, maybe in the future we have to initialize this from other parts of the program and we don’t have this arguments with us. However you can pass them as parameters if you want.

After that we set the GL context. As I said in the previous tutorial, if we set the¬†GL¬†context to Compatibility mode we can’t use a lower version. This works only if you are in a Core context mode. After that we set the position of the window, the size and the display mode. These functions were copied from our old main.cpp file.

Then we create the OpenGL window based on values from:¬†WindowInfo, ContextInfo and¬†FramebufferInfo¬†parameters. If you try to call those functions (glutInitWindowSize,¬† glutInitContextProfile, glutInitDisplayMode, etc) ¬†after¬†glutCreateWindow they won’t affect the GL window with anything. Now after¬†glutCreateWindow is up, we can initialize GLEW and set the proper callbacks declared as static to match the arguments required by FreeGLUT’s functions.

As you can see I added two more callbacks:

  • IdleCallback – called when¬†events are not being received.
  • Reshape – triggered when the window is reshaped.

In the end we would like to print some information about Context in console so we call our method printOpenGLInfo.

The run method is used to start the loop by calling glutMainLoop(). We call this method in main.cpp after initialization

Now let’s look over the callbacks. For the moment we are not using them to draw anything. In the next part we create a Scene Manager and use these functions.

void Init_GLUT::close()
   std::cout << "GLUT:\t Finished" << std::endl;

void Init_GLUT::idleCallback(void)
   //do nothing, just redisplay

void Init_GLUT::displayCallback()
   glClearColor(0.0, 0.0, 0.0, 1);

void Init_GLUT::reshapeCallback(int width, int height)


void Init_GLUT::closeCallback()

void Init_GLUT::enterFullscreen()

void Init_GLUT::exitFullscreen()

And finally we have the printOpenGLInfo() which prints some information about the program:

void Init_GLUT::printOpenGLInfo(const Core::WindowInfo& windowInfo,
                                const Core::ContextInfo& contextInfo){

 const unsigned char* renderer = glGetString(GL_RENDERER);
 const unsigned char* vendor = glGetString(GL_VENDOR);
 const unsigned char* version = glGetString(GL_VERSION);

 std::cout << "******************************************************               ************************" << std::endl;
 std::cout << "GLUT:Initialise" << std::endl;
 std::cout << "GLUT:\tVendor : " << vendor << std::endl;
 std::cout << "GLUT:\tRenderer : " << renderer << std::endl;
 std::cout << "GLUT:\tOpenGl version: " << version << std::endl;

Actually I printed more information(check the source code), but to keep things simple these are the most important things.

Now let’s take a look at¬†the beautiful small¬†main.cpp ūüôā

#pragma once
#include "Core\Init\Init_GLUT.h"
using namespace Core;
using namespace Init;
int main(int argc, char **argv)
  WindowInfo window(std::string("in2gpu OpenGL Beginner Tutorial "),
                         400, 200,//position
                         800, 600, //size

  ContextInfo context(4, 5, true);
  FramebufferInfo frameBufferInfo(true, true, true, true);
  Init_GLUT::init(window, context, frameBufferInfo);


  return 0;

That’s all, you should end up with a black screen for now. In the next part we are going to create a Scene Manager.¬†By the way, this class will suffer some modifications later on.

The result

The result

Source code :1_Setting_OpenGL_GLUT

blog comments powered by Disqus