C ++ Implementation of a simple game engine core State Machine

Source: Internet
Author: User

This is part of a previous 2D game (unfinished,

The entire state machine engine framework of the game is implemented. Here, I will share my code.

 

As we all know, for a game (or an application), we can split each part of the interface into different states, and then the whole system process is a finite state machine.

 

My job here is to implement the state machine framework and specific scheduling process.

 

Game Main Engine header file

 

/* <Br/> * Copyright 2009-2010 GDE studio <br/> * main game engine <br/> * ============ ================================< br/> * 09/12/12 cgcreate <br/> */</P> <p> # ifndef gde_gameengine_h _ <br/> # define gde_gameengine_h _ </P> <p> # pragma warning (Disable: 4251) <br/> # pragma warning (Disable: 4244) <br/> # pragma warning (Disable: 4018) <br/> # pragma warning (Disable: 4101) <br/> # pragma warning (Disable: 4996) </P> <p> namespace GDE {</P> <p> // Game Engine status <br/> Enum gde_gameenginestatus <br/> {<br/> Empty = 0, // null <br/> scence, // scene and menu rendering <br/> battle, // battle <br/> runningscript // script parsing <br/> }; </P> <p> // main game engine class <br/> class gde_gameengine <br/> {<br/> Public: <br/> gde_gameengine () <br/>: status _ (empty) <br/>, isquit _ (false) <br/>{}</P> <p> ~ Gde_gameengine () {This-> release () ;}</P> <p> void run (); // run </P> <p> PRIVATE: <br/> void Init (); // initialization <br/> void release (); // release </P> <p> void run_scence (); // running scenario <br/> void run_script (); // parsing script <br/> void run_battle (); // running the battlefield </P> <p> PRIVATE: </P> <p> gde_gameenginestatus status _; // engine status </P> <p> bool isquit _; // whether to exit </P> <p> }; // class gde_gameengine </P> <p >}// namespace GDE </P> <p> # endif

 

Core code of implementation

 

Void gde_gameengine: Run () <br/>{< br/> This-> Init (); </P> <p> status _ = scence; <br/> // status _ = battle; </P> <p> while (isquit _ = false) <br/>{< br/> switch (this-> status _) <br/>{< br/> case scence: <br/> run_scence (); <br/> break; <br/> case runningscript: <br/> run_script (); <br/> break; <br/> case battle: <br/> run_battle (); <br/> break; <br/> default: <br/> break; <br/>}< br/>}

 

For each status processor, I define a base class that must implement the State processing process and provide feedback to the game engine for processing results.

As defined below, gde_gameenginehelper

/* <Br/> * Copyright 2009-2010 GDE studio <br/> * game engine-engine auxiliary class group <br/> * ========== ======================================< br/> * 09/12/12 cgcreate <br/> */< /P> <p> # ifndef gde_script_executer_h _ <br/> # define gde_script_executer_h _ </P> <p> # include <string> </P> <p> # include" gde_map/gde_map.h "<br/> # include" gde_gameengine.h "</P> <p> namespace GDE <br/>{</P> <p> // game engine helper <br/> class gde_gameenginehelper <br/>{< br/> public: <Br/> gde_gameenginehelper () {}</P> <p> virtual ~ Gde_gameenginehelper () {}</P> <p>/** <br/> * auxiliary class running <br/> */Param [in] status changes the game engine status <br/> */Param [in] target_filename target file name <br/> */<br/> virtual void run (gde_gameenginestatus & status, STD: string & target_filename) = 0; <br/>}; </P> <p> // script executor <br/> class gde_scriptexecuter: public gde_gameenginehelper <br/>{< br/> Public: <br/> gde_scriptexecuter (STD: String start_file_id ); // start script id </P> <p> virtual void run (gde_gameenginestatus & status, STD: string & target_filename); // execute </P> <p> PRIVATE: <br/> void Dialog (); <br/> void ComboBox (); <br/> void war (); <br/> void item (); <br/> void checkitem (); <br/> void Info (); <br/> void backpic (); <br/> void music (); <br/>}; </P> <p> // scene Renderer <br/> class gde_scencerender: Public gde_gameenginehelper <br/> {<br/> public: <br/> gde_scencerender (STD: String filename); // scenario </P> <p> virtual void run (gde_gameenginestatus & status, STD: string & target_filename ); // execute </P> <p >}; </P> <p> // battlefield manager <br/> class gde_battlemanager: public gde_gameenginehelper <br/>{< br/> Public: <br/> gde_battlemanager (STD: String filename ); // battlefield data file </P> <p> virtual void run (gde_gameenginestatus & status, STD: string & target_filename); // execute </P> <p> PRIVATE: <br/> GDE: Map _; <br/> bool quit_flag _; <br/> }; </P> <p >}</P> <p> # endif

 

In this way, the entire game engine can schedule all states, and the status is fed back to the new State to be scheduled by the engine to implement the entire state machine.

 

Very simple, isn' t it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.