1 #include "LlamaW orks2d.h" 2
3 using namespace llamaworks2d; 4
5 class my_game : public game 6 { 7 public: 8 ATTACH_MESSAGE_MAP; 9 private: 10 }; 11 12 13 CREATE_GAME_OBJECT(my_game); 14 15 START_MESSAGE_MAP(my_game) 16 END_MESSAGE_MAP(my_game)
The file Prog_04_01.cpp begins by including the file LlamaWorks2D.h. Recall from chapter 2 that #include statements are a way of providing the compiler with
the definitions that your program needs to compile. The file LlamaWorks2D.h is a kind of "ultimate include file" in the LlamaWorks2D game engine. All of the files you create for your games must include LlamaWorks2D.h. So you should put the statement you see on line 1 of Listing 4.1 at the beginning of all your C++ source files in your games.
Factoid
Programmers have a couple of different names for files they use with #include statements. They'll refer to them as either include files
or header files.
The statement on line 3 must also be present to use the LlamaWorks2D game engine. Back in chapter 3, you saw that namespaces help group types,
functions, and so forth into related collections. Everything the LlamaWorks2D game engine provides is in the llamaworks2d namespace. The using statement on line
declaration in your program.
The definition of the game class itself begins on line 5. Notice at the end of the line that there's a colon, followed by the C++ keyword public, followed by the
name game. I'll explain exactly what all that means in chapter 6, "Inheritance:
Getting a Lot for a Little." For now, just know that it's required in your game class definition.
The statement on line 8 is a special marker called a macro. What it does is copy a whole bunch of C++ source code into your game class for you. The source code this macro inserts is part of the LlamaWorks2D engine. It adds a
message map to your game class. A message map is a technique for assigning
specific functions to handle player input. Message maps are widely used in all types of programs. (Message maps are presented in more detail in chapter 7, "Game Program Structure.") Every game class must have a message map. It does not matter whether your LlamaWorks2D program actually accepts player input. In fact, the program in Listing 4.1 ignores all player input. Even so, the game class in Listing 4.1 must have the statement shown on line 8 or it will not compile.
In addition to ATTACH_MESSAGE_MAP, LlamaWorks2D also provides you with other
macros that insert a lot of source code into your game. This simplifies your life by saving you from having to write large amounts of code yourself. Line 13 of
Listing 4.1 uses a macro to create an object from your game class. Recall that
classes define what objects contain, but you don't have an object until you declare a variable. The macro on line 13 creates a variable of type my_game.
Warning
If you forget the ATTACH_MESSAGE_MAP; statement in your game class,
the compiler outputs an error telling you that the function
ProcessInputMessageMap() is not a member of your game class. If you see
this error, you know you are missing the ATTACH_MESSAGE_MAP;
statement in your game class.
If you change the name of your game class to something other than my_game,
you need to put that name within the parentheses of the CREATE_GAME_OBJECT()
macro; it has to contain the type name of your game class. In addition, this macro must always appear right after the definition of your game class.
The macros you see on lines 15 and 16 mark the beginning and ending of the game class's message map. As I mentioned, the message map does nothing at this point. However, it must be present for your game to compile.
Warning
If the compiler outputs an error stating that it cannot find the
function CreateGameObject(), you did not put the CREATE_GAME_OBJECT() macro
in your program. If it tells you that it cannot find the function
ProcessInputMessageMap(), you don't have the START_MESSAGE_MAP() macro in
your program. Whenever the compiler tells you that it found the beginning of another function before the end of ProcessInputMessageMap(),
that means you left out the END_MESSAGE_MAP() macro.
Phase 4: Compile and Run the Program
Now comes the easy part. If you've done everything correctly, all you have to do is press F9 or select Compile & Run from the Dev-C++ Execute menu.
Assuming all goes well, you'll see a nice blue window that covers most of your screen. Press Alt+F4 to close it.
That blue window may not seem like much, but it hides a lot that is going behind the scenes. The game engine initialized Windows and OpenGL for you. It sized and positioned to the window, and set its background color. While you gaze into the window's relaxing blueness, the game engine is displaying lots and lots of empty frames per second. It's checking for player input, even
though it doesn't do anything with the input. It sits patiently waiting for you to add your unique and dazzling game elements so that it can display them. Not bad for just 16 lines of code.