Pulling It Together In The Game Class


This version of Invasion of the Slugwroths is Program 15.1 on the CD. Its game class, called invasion, is surprisingly brief. It makes everything work by initializing the program, processing input, and rendering the Captain on the screen. Listing 15.3 provides the code for the invasion class.

Listing 15.3. The invasion class

 1     class invasion : public game 2     { 3     public: 4         bool OnAppLoad(); 5         bool InitGame(); 6         bool UpdateFrame(); 7         bool RenderFrame(); 8 9         ATTACH_MESSAGE_MAP; 10 11        bool OnKeyDown( 12            keyboard_input_message &theMessage); 13 14        void GetKeyboardInput(); 15 16    private: 17        chloride theCaptain; 18    }; 19 20    CREATE_GAME_OBJECT(invasion); 21 22    START_MESSAGE_MAP(invasion) 23        ON_WMKEYDOWN(OnKeyDown) 24    END_MESSAGE_MAP(invasion) 25 26    bool invasion::OnAppLoad() 27    { 28        bool initOK = true; 29 30        // 31        // Initialize the window parameters. 32        // 33        init_params lw2dInitiParams; 34        lw2dInitiParams.openParams.fullScreenWindow = true; 35        lw2dInitiParams.winParams.screenMode. AddSupportedResolution( 36            LWSR_800X600X32); 37        lw2dInitiParams.winParams.screenMode. AddSupportedResolution( 38            LWSR_800X600X24); 39        lw2dInitiParams.openParams.millisecondsBetweenFrames = 50; 40 41        // This call MUST appear in this function. 42        theApp.InitApp(lw2dInitiParams); 43 44        return (initOK); 45    } 46 47 48    bool invasion::InitGame() 49    { 50 51        bool initOK = true; 52 53 54        initOK = theCaptain.LoadResources(); 55 56        if (initOK==true) 57        { 58            theCaptain.X(400.0); 59            theCaptain.Y(300.0); 60        } 61 62        return (initOK); 63    } 64 65 66    bool invasion::UpdateFrame() 67    { 68        GetKeyboardInput(); 69        bool updateOK = theCaptain.Update(); 70 71        return (updateOK); 72    } 73 74 75    bool invasion::RenderFrame() 76    { 77        bool renderOK = theCaptain.Render(); 78 79        return (renderOK); 80    } 81 82 83    bool invasion::OnKeyDown( 84        keyboard_input_message &theMessage) 85    { 86        switch (theMessage.keyCode) 87        { 88            case KC_ESCAPE: 89            case KC_Q: 90                GameOver(true); 91            break; 92        } 93        return (false); 94    } 95 96 97    void invasion::GetKeyboardInput() 98    { 99        theCaptain.GetKeyboardInput(); 100   } 

The invasion class contains an object of type chloride as its only private data member. As with all game classes in LlamaWorks2D, the invasion class attaches its message map, creates its game object, and then defines its message map.

Tip

Most game programmers buy a video card that supports multiple monitors. That way they can run the game on one and the debugger on the other. I highly recommend this approach if you're serious about writing games.


When this program initializes itself, it runs in full-screen mode. This is how you generally want things when your game runs. However, when you're debugging, it's a good idea to run the program in a window. That way you can switch back and forth between the game and Dev-C++. This is somewhat tedious, but it works reasonably well. To get this program to run in windowed mode, change the value true on line 34 of Listing 15.3 to false.

The game initialization, starting on line 48, calls the chloride::LoadResources() function, which loads all of the bitmaps for Captain Chloride's animations. This function is not shown in this chapter because it's both long and straightforward. To view the code, please see the file Chloride.cpp on the CD in the folder Source\Chapter15\Prog_15_01. After the program loads the Captain's bitmaps, it sets his initial position on the screen on lines 5859.

During each frame, the program calls the invasion::GetKeyboardInput() function on line 68. If you look at the code on lines 97100, you can see that the invasion::GetKeyboardInput() function simply calls the chloride::GetKeyboardInput() function. If this were a multiplayer program, it would also get keyboard input for any additional players.

After the program gets the current keyboard input, it reacts to the input on line 69. As shown previously, the chloride::Update() function moves the Captain based on the player's input.

Because Captain Chloride is the only thing on the screen, the invasion class's Render() function just calls the chloride::Render() function during each frame. The invasion class's OnKeyDown() function, which is used for slower keyboard input, exits the game if the player presses the Escape or Q key.

That's all it takes to get the Captain walking around the screen.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

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