1 /* $Id: mapmemory.hpp,v 1.17.4.1 2006/01/20 11:23:31 chfreund Exp $ */ 2 3 #ifndef _MAPMEMORY_HPP_ 4 #define _MAPMEMORY_HPP_ 5 6 #include <cstring> 7 #include <SDL_types.h> 8 9 #include "serializable.hpp" 10 #include "serialize.hpp" 11 12 ////////////////////////////////////////////////////////////////////// 13 // MapMemory<T> is a memory container for a map-like structure of 14 // type T 15 ////////////////////////////////////////////////////////////////////// 16 17 template <class T> 18 class MapMemory : public Serializable { 19 public: 20 typedef T Type; 21 private: 22 // m_sizeX is the width, m_sizeY is the height of the map 23 Uint32 m_sizeX, m_sizeY; 24 // pointer to the array with all the values in the map 25 T* m_memory; 26 public: 27 // Constructor MapMemory(int sizeX,int sizeY)28 MapMemory( int sizeX, int sizeY ) 29 : m_sizeX( sizeX ), m_sizeY( sizeY ) { 30 m_memory = new T[m_sizeX*m_sizeY]; 31 } 32 MapMemory(const MapMemory<T> & memory)33 MapMemory( const MapMemory<T>& memory ) 34 : m_sizeX( memory.getSizeX() ), m_sizeY( memory.getSizeY() ) { 35 m_memory = new T[m_sizeX*m_sizeY]; 36 memcpy( static_cast<void*>( m_memory ), 37 static_cast<void*>( memory.m_memory ), 38 m_sizeX * m_sizeY * sizeof( T ) ); 39 } 40 41 // Destructor ~MapMemory()42 virtual ~MapMemory() { 43 delete[] m_memory; 44 } 45 getSizeX() const46 Uint32 getSizeX() const { 47 return m_sizeX; 48 } 49 getSizeY() const50 Uint32 getSizeY() const { 51 return m_sizeY; 52 } 53 54 //! resize the map resize(const Uint32 sizeX,const Uint32 sizeY)55 bool resize( const Uint32 sizeX, const Uint32 sizeY ) { 56 if( sizeX * sizeY != m_sizeX * m_sizeY ) { 57 delete [] m_memory; 58 ASSERT( 0x0 != (m_memory = new T[sizeX * sizeY]), 59 "MapMemory::resize(%u, %u): could not " 60 "allocate memory.\n", sizeX, sizeY ); 61 } 62 m_sizeX = sizeX; 63 m_sizeY = sizeY; 64 65 return true; 66 } 67 68 // returns the pointer the raw memory getMemory() const69 T* getMemory() const { 70 return m_memory; 71 } 72 73 // return the value of the map at the point (i, j) getValueAt(int i,int j) const74 T getValueAt( int i, int j ) const { 75 return m_memory[m_sizeX*j + i]; 76 } 77 78 // sets the value at the point (i, j) setValueAt(int i,int j,T value)79 void setValueAt( int i, int j, T value ) { 80 m_memory[m_sizeX*j + i] = value; 81 } 82 83 // return the reference for a value of the map at the point (i, j) getReferenceTo(int i,int j)84 T& getReferenceTo( int i, int j ) { 85 return m_memory[m_sizeX*j + i]; 86 } 87 88 // returns the size of the serialized object getSerializeBufferSize() const89 virtual Uint32 getSerializeBufferSize() const { 90 return 2 * Serialize<Uint32>::sizeOf() + 91 m_sizeX * m_sizeY * Serialize<T>::sizeOf(); 92 } 93 94 // serialize the map into the buffer starting at bufferPointer 95 // After this method bufferPointer points to next char after 96 // the last char of the serialization of the map serialize(Uint8 * & bufferPointer) const97 void serialize( Uint8*& bufferPointer ) const { 98 // serialize m_SizeX 99 Serialize<Uint32>::serialize( m_sizeX, bufferPointer ); 100 101 // serialize m_SizeY 102 Serialize<Uint32>::serialize( m_sizeY, bufferPointer ); 103 104 // serialize m_memory 105 Serialize<T>::serialize( m_sizeX*m_sizeY, m_memory, bufferPointer ); 106 /* 107 for ( Uint32 i = 0; i < m_sizeX * m_sizeY; i++ ) { 108 Serialize<T>::serialize( m_memory[i], bufferPointer ); 109 } 110 */ 111 } 112 113 // create a new map out of a serialization in the buffer starting 114 // at bufferPointer 115 // After this method bufferPointer points to next char after 116 // the last char of the serialization of the map createAndDeserialize(Uint8 * & bufferPointer)117 static MapMemory<T>* createAndDeserialize( Uint8*& bufferPointer ) { 118 // deserialize m_sizeX 119 Uint32 sizeX; 120 Serialize<Uint32>::deserialize( bufferPointer, sizeX ); 121 122 // deserialize m_sizeY 123 Uint32 sizeY; 124 Serialize<Uint32>::deserialize( bufferPointer, sizeY ); 125 126 // create object and fill memory from buffer 127 MapMemory<T>* newObject = new MapMemory<T>( sizeX, sizeY ); 128 T* memoryPointer = newObject->getMemory(); 129 for ( Uint32 i = 0; i < sizeX * sizeY; i++ ) { 130 Serialize<T>::deserialize( bufferPointer, *memoryPointer++ ); 131 } 132 133 return newObject; 134 } 135 136 // replace the existing map by deserializing the contents of 137 // the buffer pointed to by bufferPointer 138 // After this method bufferPointer points to next char after 139 // the last char of the serialization of the map deserialize(Uint8 * & bufferPointer)140 void deserialize( Uint8*& bufferPointer ) { 141 // deserialize m_sizeX 142 Uint32 sizeX; 143 Serialize<Uint32>::deserialize( bufferPointer, sizeX ); 144 145 // deserialize m_sizeY 146 Uint32 sizeY; 147 Serialize<Uint32>::deserialize( bufferPointer, sizeY ); 148 149 // reinitialize object if sizes do not match 150 if ( m_sizeX != sizeX || m_sizeY != sizeY ) { 151 delete[] m_memory; 152 m_sizeX = sizeX; 153 m_sizeY = sizeY; 154 m_memory = new T[m_sizeX*m_sizeY]; 155 } 156 157 // fill memory 158 Serialize<T>::deserialize( m_sizeX*m_sizeY, bufferPointer, m_memory ); 159 /* 160 T* memoryPointer = m_memory; 161 for ( Uint32 i = 0; i < sizeX * sizeY; i++ ) { 162 Serialize<T>::deserialize( bufferPointer, *memoryPointer++ ); 163 } 164 */ 165 } 166 }; 167 168 #endif // _MAPMEMORY_HPP_ 169