Recently reading the "Game Engine architecture" this book, although feeling a lot of theory, and many things can not understand, but still simple to some feel good ideas and methods or concepts recorded.
1. With regard to the initialization of individual parts, one of the simplest and most brutal ways is to arrange the parts in order, not only to see the initialization sequence easily, but also to modify it easily.
2. About memory allocation:
A) The most common rule in the game is to maintain a minimum heap allocation and never use heap allocations in a compact loop.
b) Custom memory allocator, first request a large chunk of memory, and then build your own memory allocator (stack allocator-stacking allocator or pool allocator, single frame splitter, double buffer allocator)
c) The smaller the high-performance code, the better, the key code to avoid function calls, if you want to call the function, it is best to put the function near the calling function, especially not in other compilation units! Use inline functions with caution, excessive inlining makes the code too bulky, critical code cannot be fully loaded into memory, resulting in a hit failure.
3. With regard to iterators, it is best to use a pre-increment, or ++p, to back up their values, which can result in a performance penalty.
4. Use STL, but use it with caution.
5. String-Related: external strings involve localization issues and need to consider translation scenarios. Internal strings need to consider performance issues, and a string is named for an object, but comparing or locating the string is a significant performance overhead.
6. Unique identifier: GUID <globally unique identifier> The object in the game has a global identifier, usually the path name of the object resource, but the object name is very long and consumes a lot of performance each time it is searched. So consider the solution: hash The string hash: Map the string to a unique number (string id). This ID corresponds to a unique string, consider wrapping the string ID and string in a class (unreal practice)
7. Configuration file: The engine will have a lot of configuration information, some are to be used for the players, some are used in the development process, but also for the players to use the same game with different players in the information, which should be stored up. A good way to do this is to use an XML file. Use global or singleton mode to save unique configuration information after reading.
8. Resource Manager <resource manager> (asset manager <assetmanager>, Media manager <media manager>). Manage resources in various games to ensure that resources are not re-loaded. For resource reads, you can use native APIs but if it comes to platform porting, it's a good idea to wrap up the file's APIs and invoke different APIs based on the platform. The file API that comes with C/S is synchronous, and you can use libraries to implement asynchronous loading.
9. Asynchronous read: The main thread puts a request into a queue, the I/O thread reads the request from the queue, synchronizes the read (I/O thread is blocked by a function such as fread), and when it is done, invokes the callback function provided by the main thread to tell the operation is complete.
10. The resource manager consists of two areas, on the one hand, an offline tool for building resources, responsible for creating resources and translating them into tools available to the engine. On the other hand, resource management, which is loaded during the implementation period, is responsible for loading resources before use and removing resources when they are not used.
11. With regard to resources, there are generally multiple dependencies, such as a three-dimensional model, one or more triangular meshes, an optional skeleton, an optional set of animations, and a material for each mesh, one for one or more textures. When using an object, be careful to import all of its dependent resources.
12. The game loop, the engine subsystem needs to provide service periodically, and the subsystem frequency may be different.
A) The simplest is to update all subsystems in a single cycle.
b) Windows message pump, the Windows message loop, adds an entry to the engine as a loop, but the downside is that Windows will prioritize system messages, most notably when moving windows and the game will get stuck.
C) Callback driver framework, some engines have already written the framework, just want us to fill in the content.
d) An event-based update mechanism.
13. Handling of game time:
A) The earliest processing does not accurately amount to the actual time in the game loop, the speed of the game depends entirely on the CPU speed.
b) based on the time of the update: the start of a frame and the end of the time difference, you can accurately get two frames to the interval, based on the time of the previous frame to estimate the next frame of time, but this may have a vicious circle, so the average.
c) If you know the frame rate of the best time difference, the direct use of this time difference to control the frame rate is not good. If the time is not finished, then white and so on the next target time, if done early, then sleep a while.
In short, the most important thing is to stabilize the frame rate, can ensure that the picture smooth, or for the recording function, etc. to provide protection.
14. Picture tearing: This anomaly occurs when the monitor electron gun swaps the foreground buffer and the background buffer during the scanning process.
The solution to this problem is vertical synchronization, can be opened to prevent the screen when the game screen tearing phenomenon, of course, if your game screen fps can reach or exceed the refresh rate of your display, then your game screen fps is limited to the refresh rate of your display. You will feel that the original movement of the game screen is so comfortable, if not to appear to different degrees of jumping frame phenomenon, fps and refresh rate gap is larger jump frame more serious. After closing the game in addition to high-speed games, other games basically do not see the picture tearing phenomenon.
15. The game cycle is controlled by time, then through time we can control the speed of the game, and even can directly encapsulate a time-related class, this class can record absolute time, game time, and can be based on parameters to control the speed of the game, to achieve fast-forward or slow-release effect.
16. As computer support for parallel processing becomes more and more good, it is also a good way to separate the various functions from the individual threads or processors, but one disadvantage is that if the entire function is given to a thread, it can lead to a large grain size, and if one function takes too long, the other functions wait for the thread. A better solution is to use smaller-grained jobs to maximize the utilization of the processor.
17. Human interface Related:
A) Dead zone: If the analog signal, there may be a slight interference, to eliminate this interference, you need to set a dead zone, to ensure that the input results are not disturbed.
b) A good idea: to determine the button state, Save the button state of the previous frame, and this frame XOR, there is a change in this frame operation, and then judge the situation of these buttons this frame, if it is pressed, the button is pressed, if lifted, indicating that the button was released.
c) Chord: Refers to a group of buttons are pressed, there will be extra functions, but we are often not so precise operation, so the code must be robust enough to handle the N-frame and the n+m frame between the player's action, and if the buttons have a separate function, but also to detect the other button state.
There is also a sequence detection, such as a string of input, you can set a buffer, each input buffer, and detect whether the time is timed out, if the timeout is emptied, if required to execute the function.
d) on different platform input processing, depending on the platform if__endif but a better approach is to have a hardware abstraction layer.
e) The re-mapping of the input: the player may want to change the mode of operation, so that we can not talk about the key itself and function, but with a middle layer to solve, more specifically a mapping table, the function and key map, the player modifies the operation mode only need to modify the mapping of this map.
f) Context-sensitive control: In our game, a button may have multiple functions, such as pickup and conversation, a simple implementation is using a state machine, another related concept is control ownership, the input button belongs to different objects in the game control operations, such as cameras and characters.
g) Disable control: Sometimes we ask that the game is not controllable, or that some keys are invalid. The poor way is to directly from the keys to prohibit, but the consequences may be very large, in case of problems, may not be able to change back, can only restart. The prohibition of this function is best written in the logic of the game rather than in the keyed place.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Game Engine Architecture reading experience (i)