diff options
| author | Magnus Auvinen <magnus.auvinen@gmail.com> | 2008-09-24 09:03:49 +0000 |
|---|---|---|
| committer | Magnus Auvinen <magnus.auvinen@gmail.com> | 2008-09-24 09:03:49 +0000 |
| commit | eb21e9d6bb772c6fba533bfe4b421dc7efe181b1 (patch) | |
| tree | 0d94fb38f7244e2495527d66d8e5ae2c9e155da2 /src/game/server/entity.hpp | |
| parent | d9d37b945ee5796553794ef33249c22490494391 (diff) | |
| download | zcatch-eb21e9d6bb772c6fba533bfe4b421dc7efe181b1.tar.gz zcatch-eb21e9d6bb772c6fba533bfe4b421dc7efe181b1.zip | |
cleaned up the code a bit more. pooling of character and player objects with reusable macros. fixed crashing when changing maps and a couple of other bugs
Diffstat (limited to 'src/game/server/entity.hpp')
| -rw-r--r-- | src/game/server/entity.hpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/game/server/entity.hpp b/src/game/server/entity.hpp index d3ae3a1c..8ccb2d9a 100644 --- a/src/game/server/entity.hpp +++ b/src/game/server/entity.hpp @@ -1,14 +1,59 @@ #ifndef GAME_SERVER_ENTITY_H #define GAME_SERVER_ENTITY_H +#include <new> #include <base/vmath.hpp> +#define MACRO_ALLOC_HEAP() \ + public: \ + void *operator new(size_t size) \ + { \ + void *p = mem_alloc(size, 1); \ + /*dbg_msg("", "++ %p %d", p, size);*/ \ + mem_zero(p, size); \ + return p; \ + } \ + void operator delete(void *p) \ + { \ + /*dbg_msg("", "-- %p", p);*/ \ + mem_free(p); \ + } \ + private: + +#define MACRO_ALLOC_POOL_ID() \ + public: \ + void *operator new(size_t size, int id); \ + void operator delete(void *p); \ + private: + +#define MACRO_ALLOC_POOL_ID_IMPL(POOLTYPE, poolsize) \ + static char pool_data_##POOLTYPE[poolsize][sizeof(POOLTYPE)] = {{0}}; \ + static int pool_used_##POOLTYPE[poolsize] = {0}; \ + void *POOLTYPE::operator new(size_t size, int id) \ + { \ + dbg_assert(sizeof(POOLTYPE) == size, "size error"); \ + dbg_assert(!pool_used_##POOLTYPE[id], "already used"); \ + /*dbg_msg("pool", "++ %s %d", #POOLTYPE, id);*/ \ + pool_used_##POOLTYPE[id] = 1; \ + mem_zero(pool_data_##POOLTYPE[id], size); \ + return pool_data_##POOLTYPE[id]; \ + } \ + void POOLTYPE::operator delete(void *p) \ + { \ + int id = (POOLTYPE*)p - (POOLTYPE*)pool_data_##POOLTYPE; \ + dbg_assert(pool_used_##POOLTYPE[id], "not used"); \ + /*dbg_msg("pool", "-- %s %d", #POOLTYPE, id);*/ \ + pool_used_##POOLTYPE[id] = 0; \ + mem_zero(pool_data_##POOLTYPE[id], sizeof(POOLTYPE)); \ + } + /* Class: Entity Basic entity class. */ class ENTITY { + MACRO_ALLOC_HEAP() private: friend class GAMEWORLD; // thy these? ENTITY *prev_entity; @@ -21,7 +66,6 @@ protected: int id; int objtype; public: - ENTITY(int objtype); virtual ~ENTITY(); |