[ Previous | Next | Contents | Glossary | Home | Search ]
GL3.2 Version 4.1 for AIX: Programming Concepts

Hello World Example Program

The first GL program opens a window on the screen, and prints the message Hello, World! inside it. Create a hello.c file and enter the following text:

#include <gl/gl.h>
main ()
   prefsize (200, 100);
   winopen ("HI THERE");
   color (BLACK);
   color (GREEN);
   cmov2 (50, 50);
   charstr ("Hello, World!");
   sleep (5);

To compile and link this program, enter the following AIX command:

cc hello.c -o hello -lgl

To run the program, enter the following at the command prompt:


Depending on the configuration of your X server (as controlled by the .Xdefaults and .mwmrc files), either the window is displayed immediately on the screen or a rubber band is displayed. If a rubber band is displayed, you can place it at any location; to display the window, press the left mouse button. To learn more about customizing the X server for GL applications, see "Understanding Windows and Input Control".

The first line of the hello.c program, #include <gl/gl.h> , includes constant, type, and function declarations needed for all GL programs. For instance, it defines the preprocessor tokens BLACK and GREEN. This line should be included in every GL program.

The prefsize subroutine communicates to the window manager the suggested size of the window that is created with the winopen subroutine. The prefsize subroutine does not actually create the window; it only specifies the window size preferences. In this case, the preference is a window that is 200 pixels wide and 100 pixels high. You can also control other window properties, such as the preferred position or the window title. To learn more about creating and managing windows, see "Understanding Windows and Input Control".

The color subroutine sets the current color. Everything you draw is is displayed in the current color until you change that color. The color subroutine itself does not draw anything. To learn more about setting attributes, see "Setting Drawing Attributes". To learn more about setting colors and frame buffer modes (an advanced topic), see "Understanding the Frame Buffer".

The clear subroutine clears the entire window to the current color. Because the program line immediately preceding the clear subroutine call sets the current color to black, the window is cleared to black.

The cmov2 subroutine sets the current character position. Every character you draw is displayed at the current character position until you change that position. In this example, the current character position is set to 50 pixels up and 50 pixels to the right of the lower left-hand corner of the window.

The charstr subroutine actually draws the text Hello, World! . The text is drawn with the current color, which is now green. To learn more about drawing text (such as choosing fonts), see "Creating Text Characters". To learn about drawing lines and polygons, see "Drawing with the Graphics Library".

The sleep subroutine is an AIX system call. In this example, the sleep subroutine prompts the system to do nothing for 5 seconds. After 5 seconds, the sleep subroutine returns and the program exits. When a GL program exits, any windows that it has created disappear. Without the sleep call, the window would be created, would flash to black, with some green text, and would then disappear immediately. You can replace the sleep call with anything that will keep the program running: for instance, an infinite loop. But when the program exits, the window definitely disappears.

[ Previous | Next | Contents | Glossary | Home | Search ]