IPhone game development tutorial game engine (1)

Source: Internet
Author: User

IPhone gamesDevelopment tutorialGame EngineIs the content to be introduced in this article. First, let's take a look at the details. To solve the problemIPHONECreateGamesWe need to solve a series of small problems first, such as "how to display images" and "how to play sound. Issues related to CreationGame Engine. Just like the human body,Game EngineEach part is different, but it is indispensable. ThereforeGame EngineThis chapter begins with profiling. We will discussGame EngineAll main parts of the image, including the application framework, state machine, and imageEngine, PhysicalEngine, SoundEnginePlayer input andGamesLogic.

Writing a fun game involves a lot of code. It is very necessary to have a good, organized design for the project from the very beginning, rather than adding code everywhere as the progress progresses. Just like building a house, architects draw a blueprint for the whole House and construction workers build it. However, many programmers who are not familiar with game programming will build a part of the house based on the guidance and add room as they learn, which will undoubtedly lead to bad results.

Figure 2-1 functional structure of the Game Engine

Figure 2-1 shows a game engine structure suitable for most games. To understand how all parts of a game engine work together, we can first design the entire game and then create our applications. In the following sections, we will explain each part in Figure 2-1.

Application Framework

Game status Manager

Image Engine

Application Framework

The application framework contains the Code required to make the application work, including creating an application instance and other subsystems in the initial stage. When an application is running, it first creates a framework class and takes over the creation and destruction of state machines, image engines, and sound engines. If our game is so complex that it needs a physical engine, the framework will also manage it.

The framework must adapt to the uniqueness of the platform we choose, including any system events such as shutdown and sleep ), and manage the loading and loading of resources so that other code only needs to be centralized with the game.

Main cycle

The framework provides the main loop, which is the driving force behind all interactive programs. During each iteration of the loop, the program checks and processes the events it receives, runs updates in the game logic, and draws content on the screen as necessary. See Figure 2-2)

Figure 2-2 main cyclic Sequence

The implementation of the main loop depends on the system you use. For a basic console program, it may be a simple while loop that calls various functions:

 
 
  1. while( !finished ) {     
  2.     handle_events();     
  3.     update();     
  4.     render();     
  5.     sleep(20);     
  6. }    

Note the sleep function here. It allows the code to sleep for a short period of time without occupying all the CPU.

Some systems do not want to write user code at all. They use a callback system to force programmers to release their CPU normally. In this way, after the application is executed, the programmer registers some functions for the system to callback in each loop:

 
 
  1. void main(void) {     
  2.     OS_register_event_handler( myEventHandler );     
  3.     OS_register_update_function( myUpdate );     
  4.     OS_register_render_function( myRender );     
  5. }  

Once the program is executed, the functions are called at intervals as needed. The IPHONE is the closest to the following example. You can see it in the next chapter and the iphone sdk.

Game status Manager

A good video game not only has a set of actions to maintain the game: it provides a main menu that allows players to set options and start a new game or continue the previous game; the Group screen displays the names of all the people who work hard to make the game. If your game has no user guide, A help area will prompt users about what they should do.

Any of the above is a game and represents a piece of independent application code snippet. For example, the function and navigation that a user calls on the main menu are completely different from those called on the Group screen, so the program logic is also different. In particular, you may put an image and some menus on the main menu, and wait for the option to be selected, you will describe the name of the game maker on the screen, and wait for the user to enter, change the game status from the production group screen to the main menu. Finally, the status in the game will render the actual game and wait for user input to interact with the game logic.

All the above game statuses are responsible for the tasks of entering the corresponding user, rendering the content to the screen, and providing the corresponding application logic for the game status. You may notice that these tasks come from the main loop discussed earlier, because they are the same task. However, each State implements these tasks in their own way, which is why they should be kept independent. You do not have to look for code in the main menu code to handle events in the game.

State Machine

Status manager is a state machine, which means it tracks the current game status. After the application is executed, the basic status information is created. It then creates information required for various states and destroys the information temporarily stored when it leaves each State.

The state machine maintains the state of a large number of different objects. An obvious status is the main menu of the user's screen status, medium game ). However, if you have an object with artificial intelligence on the screen, the state machine can also be used to manage its "Sleep", "attack", and "death" status.

What is the correct game status manager structure? Let's look at some state machines and decide which one is best for us.

There are many ways to implement the state machine. The most basic is a simple switch statement:

 
 
  1. class StateManager {     
  2.     void main_loop() {     
  3.         switch(myState) {     
  4.         case STATE_01:     
  5.             state01_handle_event();     
  6.             state01_update();     
  7.             state01_render;     
  8.             break;     
  9.         case STATE_02:     
  10.             state02_handle_event();     
  11.             state02_update();     
  12.             state02_render;     
  13.             break;     
  14.         case STATE_03:     
  15.             state03_handle_event();     
  16.             state03_update();     
  17.             state03_render;     
  18.             break;     
  19.         }     
  20.     }     
  21. }; 

All you need to do when changing the state is to change the value of the myState variable and return it to the beginning of the loop. However, as you can see, when we add more and more States, the code block will become larger and larger. What's worse, in order to make the program run as expected, we need to execute the entire task block when the program enters or leaves a certain State to initialize specific variables in this state, load new resources) and release the resources loaded in the previous state. In this simple switch statement, we need to add more blocks and ensure that no one is missed.

The above are some simple repetitive work, but our status manager needs a better solution. The following is a better implementation method:

 
 
  1. class StateManager {     
  2.     //the function pointer:     
  3.     void (*m_stateHandleEventFPTR) (void);     
  4.     void (*m_stateUpdateFPTR)(void);     
  5.     void (*m_stateRenderFPTR)(void);     
  6.     void main_loop() {     
  7.         stateHandleEventFPTR();     
  8.         m_stateUpdateFPTR();     
  9.         m_stateRenderFPTR();     
  10.     }     
  11.     void change_state(  void (*newHandleEventFPTR)(void),     
  12.                     void (*newUpdateFPTR)(void),     
  13.                     void (*newRenderFPTR)(void)     
  14.     ) {     
  15.         m_stateHandleEventFPTR = newHandleEventFPTR;     
  16.         m_stateUpdateFPTR = newUpdateFPTR;     
  17.         m_stateRenderFPTR = newRenderFPTR     
  18.     }     
  19. };    

Now, even if we process more states, the main loop is small enough and simple. However, this solution still does not help us solve the initialization and release status. Because each game status not only contains code but also its own resources, it is more appropriate to consider the game status as the attribute of the object. Therefore, we will look at the implementation of object-oriented OOP.

First, we create a class that represents the game status:

 
 
  1. class GameState     
  2. {     
  3.     GameState();        //constructor     
  4.     virtual ~GameState();    //destructor     
  5.     virtual void Handle_Event();     
  6.     virtual void Update();     
  7.     virtual void Render();     
  8. };  

Next, we change our status manager to use this class:

 
 
  1. class StateManager {     
  2.     GameState* m_state;     
  3.     void main_loop() {     
  4.         m_state->Handle_Event();     
  5.         m_state->Update();     
  6.         m_state->Render();     
  7.     }     
  8.     void change_state( GameState* newState ) {     
  9.         delete m_state;     
  10.         m_state = newState;     
  11.     }     
  12. };    

Finally, we create a class that specifies the specific game status:

 
 
  1. class State_MainMenu : public GameState     
  2. {     
  3.     int m_currMenuOption;     
  4.     State_MainMenu();     
  5.     ~State_MainMenu();     
  6.     void Handle_Event();     
  7.     void Update();     
  8.     void Render();     
  9. };    

WhenGamesWhen the status is expressed as a classGamesState can store its unique variables in this class. This class can also load any resources in its constructor and release these resources in the destructor.

Moreover, this system maintains a good organizational structure for our code, because we needGamesThe status code is placed in each file. If you are looking for the main menu code, you only need to open the State_MainMenu class. In addition, the OOP solution makes code easier to reuse. This seems to be the most suitable for our needs, so we decided to use it as our status manager.

Summary:IPhone gamesDevelopment tutorialGame EngineI hope this article will help you. Want to learn moreGame EngineFor more information, see the following articles:

IPhone game development tutorial Game Engine 2)

IPhone game development tutorial Game Engine 3)

IPhone game development tutorial game engine 4)

IPhone game development tutorial game engine 5)

IPhone game development tutorial Game Engine 6)

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.