posts - 0,  comments - 0,  trackbacks - 0

The Elements of a 2D/3D Game

Video games are extremely complex pieces of software.This means that you have to learn a new way of programming that's more conducive to real-time applications and simulation, rather than single-line, event-driven, or sequential logic programs that you might be used to. A video game is basically a continuous loop that performs logic and draws an image on the screen—usually at a rate of 30–60 fps (frames per second) or more. This is similar to how a movie is displayed, except that you are making the movie as you go.

Section 1: Initialization
In this section, you perform the standard operations you would for any program, such as memory allocation, resource acquisition, loading data from the disk, and so forth.

Section 2: Enter Game Loop
In this section, the code execution enters into the main game loop. This is where the action begins and continues until the user exits out of the main loop.

Section 3: Retrieve Player Input
In this section, the player’s input is processed and/or buffered for later use in the AI and logic section.

Section 4: Perform AI and Game Logic
This section contains the majority of the game code. The artificial intelligence, physics systems, and general game logic are executed, and the results are used to draw the next frame on the screen.

Section 5: Render Next Frame
In this section, the results of the player's input and the execution of the game AI and logic are used to generate the next frame of animation for the game. This image is usually drawn on an offscreen buffer area, so you can't see it being rendered. Next the image is very quickly copied to the visible display, creating the illusion of animation. In the case of a 3D softwarebased engine, thousands (or millions in some cases) of polygons are rendered that compose the world by a very complex 3D graphics pipeline. In the case of a 3D hardware-accelerated engine based on OpenGL or Direct3D, much of the work is offloaded to the hardware.

Section 6: Synchronize Display
Many computers will speed up or slow down due to the level of complexity that the game is currently exhibiting. For example, if there are 1,000 objects running around on the screen, the CPU is going to have a higher load than if there are only 10 objects. Therefore, the frame rate of the game will vary, which isn’t acceptable. Hence, you must synchronize the game to some maximum frame rate and try and hold it there using timing and/or wait functions. Usually, 30 fps is considered to be minimum frame rate these days, with 60 fps being ideal. Frame rates over 60 fps are hardly an improvement, because your brain has problems processing information any faster that 60 fps—this is a waste, unless you are a Borg.

Section 7: Loop
This section is fairly simple—just loop back to the beginning of the game loop and do it all again.

Section 8: Shutdown
This is the end of the game, meaning that the user has exited the main body or game loop and wants to return to the operating system. However, before doing this, you must release all resources and clean up the system, as you would for any other piece of software. 

In reality, the game loop will in most cases be a FSM (finite state machine) that contains a number of states.

General Game Loop
posted on 2010-03-09 19:08 橘子香水 阅读(79) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理