Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
và các lập trình viên tìm hiểu các phương pháp hiệu quả nhất cho việc sử dụng ngôn ngữ để tạo ra trò chơi. Điều này chủ yếu là một cuốn sách về những ý tưởng và khái niệm: nó không phải là chỉ là một nguồn của C + + mã để sử dụng trong dự án trò chơi của bạn. | Chapter 7 Memory Allocation 155 In addition we need to be extremely careful with any initialization done in the constructor. We plan to create static arrays of those objects and as you may recall static initialization is a sticky issue with C . In short you have very little control over what order things become initialized. So we cannot rely on our pathfinding data being ready when the enemy objects are initialized or the effects system being ready when the special effects objects are created. As a matter of fact in a situation like this it is probably best to leave all initializations until later and not do anything at all in the constructor other than set default values and mark the object as uninitialized. When to Use Static Allocation The question is still open when is static allocation preferable to dynamic allocation The answer is it depends on the circumstances. A good piece of advice would be to only use static initialization when there is no other easy way around it. If the platform you are using has extreme penalties for any type of dynamic memory allocation then it would be a good idea to only use static allocation. Also if your game is a mostly static world with the player running around then it might be advantageous to statically allocate everything you will need. However games are moving in the direction of increasing interachvity not less which makes static allocation more difficult. Players nowadays expect to interact with any pan of their environment to pick up things to destroy things to move things to create new things. Dynamic memory allocation is a better fit to that type of environment. How about mixing the two approaches Objects that will not change during the course of the game if there are any will be created statically and everything else will be created dynamically. The combination is not as attractive as it seems. In a way it seems to introduce the worst problems from each world instead of the other way around. By having some objects .