Starting from 3D Game Engine

Source: Internet
Author: User

Game cycle:
Initialization: perform some basic initialization operations, such as memory allocation, resource acquisition, and data loading from the disk.
Enter the game cycle: Enter the main game cycle, the user continues to execute the action, know to exit the main cycle.
Read player input: Process player input, or store it in a cache for AI and game logic.
Execute AI and game logic: This part is the main part of the game code. It will execute AI, physical systems, and general system logic and draw the next frame on the screen based on the results.
Rendering the next frame: first draw the next frame of the Game Image in the off-frequency offscreen cache, and then perform a series of rendering. In a 3D software-based engine, a complex 3D graphic assembly line is used to render countless polygon in the world. In a 3D hardware acceleration Engine Based on OpenGL or direct3d, most of the work is undertaken by hardware.
Synchronous Display: controls the Fixed Frame Rate. 30 frames are the minimum value that can be received, and 60 frames are the maximum value. Generally, you can use some timer functions or wait functions to synchronize the game to a certain maximum frame rate.
Loop.




A large game is a super-high-performance computer program. for efficiency, the key code section does not use advanced APIs. In general, you must write everything related to the internal loop of the game code, otherwise the game will not reach the required speed and performance. For example, using the memset () function, this function is quite fast, but in fact it is used to fill in a single byte. Generally, it uses a memory structure consisting of 4 or 8 bytes of basic data types, therefore, it usually uses four-byte filling or more, and the efficiency will also be improved. For example, the following implementation of a 4-byte write function is an inline assembly language function, further improving the efficiency:

Inline void memset_quad (void * DEST, uint data, int count) {// 4-byte 32-bit _ ASM {mov EDI, DEST; MoV ECx, count; MoV eax, data; rep stosd;} // The Assembly Code greatly improves the efficiency of some time-critical code parts and memory-critical code parts. You should study Assembly later .}


This code is a 32-bit assembly code. Currently, 64-bit assembly code should be used for efficiency.
The API in DirectX is trustworthy. DirectX is compiled as a high-performance API, which ensures the efficiency and generally avoids the use of advanced functions of Win32 API.


Notes for some tips:
Back up your work. It is easy to lock the system by writing game code, so it is necessary to back up generations.
When you start a game project. Well organized. Use a reasonable file name and directory name in the project, use consistent variable naming rules, and use a separate directory to store graphics and sound data as much as possible, instead of putting all content in one directory.
Use inline functions. Use inline to avoid function calls and speed up the program. This will increase the program, but the speed is more important for the game.
Use 32-bit variables instead of 8-bit or 16-bit variables as much as possible, because the current CPU is 64-bit instead of 32-bit, and for data smaller than 64-bit, high-speed cache and other memory addressing operations will slow down the speed.
Do not be afraid to use global variables: many games do not use parameters in functions that have strict time requirements. Instead, they simply use global parameters. The output and stack of parameters will cause high consumption. Global variables must use good naming rules and be safe.
Programming is performed in the form of a reduced instruction set computer using the Reduced Instruction Set (the complex operations of the computer system are implemented by the hardware to the software, and the system is more efficient) in this case, do not write complex statements. Because it takes more time to process these complex statements in the CPU, multiple simple statements are used instead of complex statements.
Multiple bitwise operations are used to replace multiplication and division, as well as to skillfully use XOR.
Compile efficient algorithms. Although compilation can speed up the O (n) algorithm, it has no effect on O (N * n). The most important thing is to start with the algorithm, to improve efficiency.
Do not optimize the code During code writing. In code writing, you can't help but want to optimize the previous code. But this is a waste of time. Instead of distracting code unrelated to the current task, it is better to finish the current task faster, then proceed with careful optimization.
Use C ++ with caution. In particular, multi-inheritance in C ++.
If you find that the current route is very difficult, stop immediately, back up, and find a way to bypass the difficulties. Making a mistake and re-writing 1000 lines of code is much better than adopting a bad encoding structure. Code scalability, maintainability, and beautiful structure are very important.
Do not write complex data structures for simple objects. In game programming, 90% of the work is data operations. It is extremely important to make data operations simpler and more efficient.


Starting from 3D Game Engine

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.