Friday, October 23, 2015





Introduction to gui programming
using c++

dhia hassen
from university of  gabes (tunisia)
22/10/2015
ABSTRACT
This paper outlines the graphics-user-interface mechanism using C++. It contains explanation of the most common algorithms used for window events handling . The mechanism is flexible, comparatively safe and easy to use, works in a mixed language execution environment, and can be implemented to run efficiently. Two implementation strategies are described in some detail . and can be rewritten using a desired programming language .

1- Introduction
Many  programs are made to do their work in silence behind the running processes , But usually those programs does a background tasks like the child-processes case or a system kernel . A gui need to be appended when the program have to visualize its output , or if it's easier to use with buttons that controls it's state or input boxs that recieves user input dynamicly .
However almost any program can be coded to not use gui , but most of non-programmer's prefers to interract graphically with the program, for this main reason developpers are forced to build this human-machine interface .
Gui's libraries are usually coded as a project component at first , then evaluated each time to handel the current project requirements , wolking through this paper we will build a very bacis c++ gui just as an exemple , to run these codes you will need a c++ compiler , a text-editor and the Allegro library (version 5.0.10) for window creation and managing  .
I choosed to use Allegro (a graphics library and not a gui one) because our main study is the construction a gui from zero and not working with a previously built one .
You can check Allegro manual (on the intenet) if this paper explanation does'nt satisfy your needs .

2 - Gui First Program : Window creation
Let's focus on this few lines  :

#include <stdio.h>                                                         // for later use of printf function
#include <allegro5/allegro.h>                                      //the main allegro header file

int main(int argc, char **argv)                                    //program entry point
{
   ALLEGRO_DISPLAY *display = NULL;              //this pointer will contain our window address

   if(!al_init()) {                                                              // initialize allegro library (load dlls ... )
      fprintf(stderr, "failed to initialize allegro!\n");        // if failed: print a message
      return -1;                                                                 // and exit the program
   }                                                                                  // if not : continue

   display = al_create_display(640, 480);                 // create the display (window)
   if(display == NULL ) {                                                          // if failed :
      fprintf(stderr, "failed to create display!\n");          // print a message
      return -1;                                                           // and exit the program
   }                                                                          // if not contnue

   al_clear_to_color(al_map_rgb(0,0,0));                 //let the window background be black

   al_flip_display();                                                //flip window buffers (to explain later )

   al_rest(3.0);                                                       // wait 3 sec before exit

   al_destroy_display(display);                               // destroy the window (clear memory ...)

   return 0;                                                           // return control to the system
}

Obviously the  code is clear until line nember 12 then i'll explain starting from line nember 13 , let's do that line by line :
13.  display = al_create_display(640, 480);
The Allegro function "al_create_display()" takes two arguments and gives a display (in Allegro terms display means window) ,  the arguments are respectively the display width and height , the function returned value need to be saved in a pointer for later uses , like the case of  "al_flip_display" (line 21), each time "al_create_display" is called a window will appear .
 14.  if(display == NULL )
if "al_create_display()" failed (usually bacause "al_init()" not called or failed or bacause the asked resolution is not supported by the graphics card -only if working on fullscreen mode-) then that function returns a NULL pointer , clearly comparing "display" variable with NULL will tell us know if "al_create_display()" failed or not .
19 .    al_clear_to_color(al_map_rgb(0,0,0));  
To understand what does this line do you need to know the "double buffer" concept , actually any window (by default -not if asked to not be so- ) have two buffers the first is used for drawing and the other is used for displaying , every draw operation affects the drawing buffer but will not take place on the screen because only the display buffer is actually displayed , later the program have to flip both buffers to update the vision , without clearing the draw buffer drawing to the screen will be like painting to a paper , every pixel you put in the draw buffer will stay there and will be visible after the flip operation , then any moving object will be every where on your display.

What "al_clear_to_color()" does is clearing the draw buffer before starting a new draw operation , it waits for a color to clear to , then "al_map_rgb()" is used this function takes three arguments (the red , green and blue components of the color ) and returnes a color that is passed to "al_clear_to_color()" then the display is cleared to that one  .

21 .   al_flip_display();

This line flips the both buffers (the draw buffer is moved to the display buffer) .

 23. al_rest(3.0);
Without this line the program will exit directly before you can see anything .

 25.  al_destroy_display(display);

Before exiting the using thread (program) need to destroy any Allegro resource , "al_destroy_display() " frees the used memory , the display can't be used anymore after this function call  (Allegro manual tells more about this) .

3 - Events Handling
A - Event
Our first program creates a window and exits after 3 seconds , obviously that is not the desired program , a gui program needs to stay active reponding to the user input until an request to exit occurs .
In order to satisfy these needs the term "Event" is made, an event is a data type that contains informations about the computer input devices states . but in gui programming it can be more then that , such as a display close,move,switch ...
Events are normaly generated by the operating system (any thread can create an event) and sent to the program that owns the current selected window , to recieve those events programs need to have an events queue .
B- Events Queue
An "events queue" is a data container ( a queue for specification ) that you can append events to and get events from in the same order of thier construction  (starting from the older reaching the recent one ), programs uses this data type to recieve events from the operating system , that are normaly handled one by one .