Object oriented Game Development -P11

Object oriented Game Development -P11: There’s a better than 50% chance that you have picked this title off the shelf in a bookshop and are wondering if it’s going to be another one of those ‘secrets of the inner circle’ type of titles. You know, the ones that promise to tell you unspoken truths on how to write really cool games but in reality offer up a rehash of a manual you may already have for free anyway. | 286 Object-oriented game development template class T T mem_Pool T Allocate T pItem return pItem template class T void mem_Pool T Free T pItem Some security checks required here to prevent multiple frees frees of invalid data and so on. pItem template class T void mem_Pool T FreeAll for int i 0 i m_iNumItems i m_Items i Notice that the pool class allocates an array of objects dynamically. This means that any class that requires pooling must provide a default constructor. Notice also that simply allocating from the pool returns an object that may have been used previously and so contains garbage no constructor will have been called. This motivates the use of the placement form of operator new this calls a constructor on an existing memory block class Duck public enum Gender DUCK DRAKE enum Breed MALLARD POCHARD RUDDY TUFTED Duck Breed eBreed Gender eGender etc. Game objects 287 namespace mem_Pool Duck s_DuckPool 100 Duck pDuck Call placement new on our raw data. new pDuck Duck MALLARD DRAKE Some ad hoc tests on my PIII 500 laptop indicate the pool class to be about ten times faster than malloc . The class is extremely simple and robust and does not demand the sort of person-power resources that a global allocator does. Furthermore plumbing it in to your code is as simple as adding operator new and delete for each class to be pool allocated 3 class Thing public void operator new size_t n void operator delete void p namespace const int MAX_THINGS 10 0 mem_Pool Thing s_Pool MAX_THINGS void Thing operator new size_t return void Thing operator delete void p reinterpret_cast Thing p 3 You may also wish to add a static Initialised function to the class to call pool T FreeAll at certain points within the code. 288 Object-oriented game development A really dull application for pools In the course of development you ll write .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
13    103    1    21-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.