15.4 First ClanLib Application


15.4 First ClanLib Application

The remaining sections of this chapter examine the first ClanLib application in detail, although it is important to note that in this chapter and subsequent ones we explore and examine many techniques and ideas that apply to games in general, regardless of whether ClanLib or some other library is used.

15.4.1 Include Files

The first step to creating a ClanLib application is to create a new, blank project. Once created, the compiler needs to be notified which libraries are to be included in the project, and this is achieved by using the #include preprocessor directive, as seen in previous chapters. Specifically, for ClanLib, a number of files need to be included. Consider the following code:

      #include <ClanLib/core.h>      #include <ClanLib/display.h>      #include <ClanLib/gl.h>      #include <ClanLib/application.h> 

15.4.2 Creating an Application Class

So how should a ClanLib application begin? The ClanLib library has been structured so it handles program initialization in the same way the main function handled initialization. When using ClanLib, a programmer is expected to control and represent the lifetime of a program by creating a derived (descendant) class from a CL_ClanApplication class, which is part of the ClanLib library. It should be created as the application begins and destroyed as the application ends. Furthermore, it has a virtual main method that a programmer is expected to override in the descendant class. This is to define what happens when an application begins. This method can be seen to correspond to a main function. Consequently, if left empty, the program will begin and then immediately exit afterward, so something must occur here. Consider the following class:

      //Main App Class      class cClanLibApp : public CL_ClanApplication {      public:         cClanLibApp();         ~cClanLibApp();         virtual int main(int, char **);      }app; 
Note 

Creating an application class to control the flow of an application is common practice in most computer games as well as in other software.

15.4.3 Overriding the Main Method

At program startup, a ClanLib application class is created. As stated, the main method of this class is where program execution effectively begins. Programmers are expected to override this method in their descendant application class, and it is here program initialization should occur. Briefly, in this function we must:

  1. Create and initialize game objects

  2. Create a game window

  3. Create a message loop

15.4.4 Initializing Game Objects

One of the first steps of a ClanLib application is to initialize some of its core classes. These crucial classes, such as display, ensure the smooth running of any ClanLib application. These classes will be the first to be created and the last to be destroyed. These objects are used later to draw images to the screen. Consider the following code:

      //Main App Class      class cClanLibApp : public CL_ClanApplication {      public:         cClanLibApp();         ~cClanLibApp();         virtual int main(int, char **)         {            CL_SetupCore setup_core;            CL_SetupDisplay setup_display;            CL_SetupGL setup_gl;            CL_SetupSound setup_sound;            return 0;         }      }app; 

15.4.5 Initializing Game Window

Every game needs a window to display itself in, whether it is a full-screen window, a smaller size window, or a variable sized window. To create and maintain a window, ClanLib uses the CL_DisplayWindow class. This actually shows the application in a window. Consider the following class constructor for CL_Display-Window and the list below that explains its arguments.

      CL_DisplayWindow(const std::string& title, int width, int height, bool      start_fullscreen = false, bool allow_resize = false, int flipping_buffers =      2); 

  • const std::string& title

    Name of the window

  • int width

    Width in pixels of the window

  • int height

    Height in pixels of the window

  • bool start_fullscreen

    True or false whether the application should be windowed or full screen

  • bool allow_resize

    True or false whether the window, if not full screen, can be resized

  • int flipping_buffers

    Refers to the number of flipping buffers. This can be left at 2.

The following code is a sample application class to create a window.

      //Main App Class      class cClanLibApp : public CL_ClanApplication {      public:         cClanLibApp();         ~cClanLibApp();         virtual int main(int, char **)         {            CL_SetupCore setup_core;            CL_SetupDisplay setup_display;            CL_SetupGL setup_gl;            CL_SetupSound setup_sound;            CL_DisplayWindow window("Test", 640, 480);            return 0;         }      }app; 




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net