Author: Movania Muhammad Mobeen

Hello readers, In this article, we will learn how to port the 9th tutorial on Configuring using the ini configuration files in OpenGL 3.3. We saw in the last tutorial how to print fonts in OpenGL 3.3 and above. This article will extend the last tutorial in two ways We will organize the code into definite code blocks to ensure reusability. We will use an ini configuration file to load the application's configuration which includes the screen size and whether to run in fullscreen mode.

Organization of code - GlutFramework

While previously, we only had a single file which becomes more and more complicated as more features are added. Instead, we may organize the code by putting relevant code into separate code units. This allows us to isolate the OpenGL/drawing code from the window management code. We illustrate the changes with a diagram as shown below.

Data flow between different GLUT framework source code units

This diagram gives a snapshot of data communication between different code units. The framework itself is divided into the following functions:

	int FrameworkInit(int *argcp,char *argv[]);
	void FrameworkQuit();
	void FrameworkMainLoop();
	void FrameworkResize (int p_width, int p_height);
	void FrameworkEvents();
	long Framework_GetTicks();
	void FrameworkSwapBuffers();
	void FrameworkAudioPlayWave(char *file);

The function names are self explanatory. The FrameworkInit function does the initialization of the framework. This function is paired with FramworkQuit which does the cleanup. The framework main loop calls the looping function or does the looping itself, checking the events and handling them as needed. The FrameworkResize handler handles the resizing of the client window. This is the typical place where the projection matrix is calculated. The FrameworkEvents function is called when a specific event is to be handled like a mouse or keyboard event. Framework_GetTicks() function returns timing information while FrameworkSwapBuffers function swaps the back and front buffers for double buffered rendering. The FrameworkAudioPlayWave plays the given audio file. Since glut does not support audio playback, this function is left empty.

Loading the ini configuration file

For loading configuration information from the ini file, we will use the ini file parser. The ini file consists of a set of sections each containing one or more sub-sections or key value pair. To use the ini library, we need to include the load_ini.h header file:

	#include "load_ini.h"

This header defines a set of functions for handling the ini files. We start by calling the LoadIni function giving it the name of the ini file to read. Once the file is loaded, we may read the configuration items by calling LoadINI_GetInt function. This function accepts three parameters, the first paramter is the ini filename, the second parameter is the section name and the third paramter is the key. The last paramter is the default value for the key:

	framework_screen_width=LoadINI_GetInt("config.ini","screen settings","width",640);
	framework_screen_height=LoadINI_GetInt("config.ini","screen settings","height",480);

In the above lines of code, we get the screen width and height from the ini file. In case the values are missing, the default values will be returned. Running the code gives us the following output. You may press the 'w','a','s','d' keys along with several other keys to transform the camera and see the result:

Tut inifiles opengl3d3.png


The Source Code of this lesson can be downloaded from the Tutorials Main Page