Skip to content
June 17, 2011 / Nathan

2D Game Development with OpenGL – Part 1

In this tutorial I will be demonstrating a “quick and easy” way to make a 2D game using OpenGL and a programming language, no engines involved! To follow this tutorial you are expected to have at least a bit of experience using OpenGL and the language you want to use, even if you have only ever gotten as far as making a window and a triangle! I will be using C++ throughout the creation of the game and will be writing this whilst developing the software. I’ll be using Ubuntu 11.04 with the proprietary ATI driver as the platform but it shouldn’t make much difference, the window creation code might be the only difference if the platform being used does not have GLFW.

The whole development process will be split up into 4 stages, each should reach significant milestones leading up to the project’s completion. I’m not going to bother with a design stage, for now the game is going to consist of basic shapes with solid colour. This can be changed later on in development to actual graphics will little effort anyway.

Development: Stage 1

 Creating a Window

Hopefully you have GLFW installed somewhere if you are going to use C++ to follow this tutorial. If not, and you are using Ubuntu (maybe Debian, but I don’t know):

sudo apt-get install libglfw-dev

Once all of the dependencies are installed, we can create the first two files which will be used in the development process. These files are Main.cpp and Makefile. Main.cpp contains the main() method which will be called when the program is loaded and will initialise the GLFW window, OpenGL and start the main game loop. Makefile is used for making compiling easier on Linux so if you are on windows or using an IDE, you probably won’t want/need one.

Makefile

The contents of the Makefile file are as follows:

all:
        g++ Main.cpp -o Game -lGL -lglfw

SIMPLES! Right? It just calls g++ and compiles Main.cpp into Game and links the two libraries GL and glfw usually found in /usr/lib/gl(fw).o.

Main.cpp

The entry point to the program. It contains the main function which sets up the glfw window and starts the game loop, ready to render objects. The following code does all of the above and is a simple framework on which a game or graphical application can be developed upon.

#include <iostream>
#include <ctime>

#include <GL/glfw.h>

void render();
void update();

/**
    - sleep function -
    Cross platform sleep implementation
*/
void _sleep(double ms)
{
    double st = clock();
    if (ms <= 0)
        ms = 10;
    while (clock() < (st + ms));
}

/**
    - main function -
    Program entry point
*/
int main(int argc, char** argv)
{
    std::cout << "Loading Tank Game v1 (Noz3001.wordpress.com)" << std::endl;
    glfwInit(); // Initialise GLFW

    // Create glfw window
    if (glfwOpenWindow(800, 600, 5, 6, 5, 0, 8, 0, GLFW_WINDOW) != GL_TRUE)
        std::cout << "Error creating window!" << std::endl;

    // Set window title
    glfwSetWindowTitle("Tank Game");

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Start main game loop
    // This calls the functions which update objects and render them to screen
    while (true)
    {
        update();
        render();

        glfwSwapBuffers(); // Switch buffers (double buffering)
        _sleep(10); // Let a bit of CPU time for other processes
    }    

    return 0;
}

/**
    - Render function -
    Used to draw objects to the screen.
*/
void render()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Colour to clear the scene
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // TODO: Draw stuff here!    
}

/**
    - Update function -
    Updates objects. States, locations, etc
*/
void update()
{
    // TODO: Update objects here
}

You should now have two files saved and ready to be compiled! Read through the code if you want, it is all pretty self explanatory. It just creates the window and then goes into an infinite loop, ready to render and update!

To build this on Linux, just type make into a terminal and then run it with ./Game. On windows, do whatever you have to to build it :D .

Once ran, you should be presented with a window like this:

Game Window

Blank window ready for rendering

It may look a bit bland, but this is the base on which we will create a fully functional game! You can test that OpenGL is actually doing something by changing the code on line 63:

glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Colour to clear the scene

The parameters this function takes are Red value, Green value, Blue value and Alpha value. By changing the first parameter to 1, the window should be red instead of black.

In part 2, we will create the player object!

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.