Changeset 347
- Timestamp:
- 01/03/15 20:28:18 (10 years ago)
- Location:
- trunk/nv
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/core/common.hh
r322 r347 196 196 { 197 197 protected: 198 // noncopyable() = default; 199 // ~noncopyable() = default; 200 // noncopyable( const noncopyable& ) = delete; 201 // noncopyable& operator=( const noncopyable& ) = delete; 202 noncopyable() {} 203 ~noncopyable() {} 204 private: 205 noncopyable( const noncopyable& ); 206 noncopyable& operator=( const noncopyable& ); 198 noncopyable() = default; 199 ~noncopyable() = default; 200 noncopyable( const noncopyable& ) = delete; 201 noncopyable& operator=( const noncopyable& ) = delete; 207 202 }; 208 203 -
trunk/nv/core/handle.hh
r339 r347 67 67 }; 68 68 69 70 template < typename HANDLE, typename TINDEX = sint32 > 71 class index_store 72 { 73 public: 69 template < typename HANDLE > 70 class handle_manager 71 { 72 static const sint32 NONE = -1; 73 static const sint32 USED = -2; 74 public: 75 74 76 typedef HANDLE handle; 75 typedef TINDEX index_type;76 77 typedef typename HANDLE::value_type value_type; 77 78 78 index_store() : m_first_free(-1), m_last_free(-1) {} 79 80 handle create_handle( index_type index ) 81 { 82 typedef handle_operator<HANDLE> hop; 83 value_type i = get_free_entry(); 84 m_entries[i].index = index; 79 handle_manager() : m_first_free( NONE ), m_last_free( NONE ) {} 80 81 handle create_handle() 82 { 83 typedef handle_operator<HANDLE> hop; 84 value_type i = get_free_entry(); 85 85 m_entries[i].counter++; 86 m_entries[i].next_free = USED; 86 87 return hop::create( i, m_entries[i].counter ); 87 88 } … … 89 90 void free_handle( handle h ) 90 91 { 91 m_entries[ h.index() ].index = -1; 92 m_entries[ h.index() ].next_free = -1; 92 m_entries[h.index()].next_free = NONE; 93 93 if ( m_last_free == -1 ) 94 94 { … … 96 96 return; 97 97 } 98 m_entries[ (unsigned)m_last_free].next_free = h.index();98 m_entries[(unsigned)m_last_free].next_free = h.index(); 99 99 m_last_free = h.index(); 100 100 } 101 101 102 void swap_indices( handle h1, handle h2 )103 {104 std::swap( m_entries[ h1.index() ].index, m_entries[ h2.index() ].index );105 }106 107 102 bool is_valid( handle h ) const 108 103 { 109 typedef handle_operator<HANDLE> hop; 104 typedef handle_operator<HANDLE> hop; 110 105 if ( h.is_nil() ) return false; 111 106 if ( h.index() >= m_entries.size() ) return false; 112 const index_entry& entry = m_entries[ h.index() ]; 113 return entry.index >= 0 && entry.counter == hop::get_counter(h); 114 } 115 116 sint32 get_index( handle h ) const 117 { 118 typedef handle_operator<HANDLE> hop; 119 return m_entries[ h.index() ].counter == hop::get_counter(h) ? m_entries[ h.index() ].index : -1; 120 } 107 const index_entry& entry = m_entries[h.index()]; 108 return entry.next_free == USED && entry.counter == hop::get_counter( h ); 109 } 110 121 111 private: 122 112 struct index_entry 123 113 { 124 index_type index;125 114 value_type counter; 126 index_typenext_free;127 128 index_entry() : index(0), counter(0), next_free(-1) {}115 sint32 next_free; 116 117 index_entry() : counter( 0 ), next_free( NONE ) {} 129 118 }; 130 119 … … 135 124 value_type result = (value_type)m_first_free; 136 125 m_first_free = m_entries[result].next_free; 137 m_entries[result].next_free = -1;126 m_entries[result].next_free = USED; 138 127 if ( m_first_free == -1 ) m_last_free = -1; 139 128 return result; … … 143 132 } 144 133 145 index_typem_first_free;146 index_typem_last_free;134 sint32 m_first_free; 135 sint32 m_last_free; 147 136 std::vector< index_entry > m_entries; 148 137 }; … … 188 177 index_type i = m_indexes[ h.index() ]; 189 178 return i >= 0 ? &(m_data[ (unsigned)i ]) : nullptr; 179 } 180 181 const T* get( handle h ) const 182 { 183 if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr; 184 index_type i = m_indexes[h.index()]; 185 return i >= 0 ? &( m_data[(unsigned)i] ) : nullptr; 190 186 } 191 187 … … 214 210 } 215 211 212 handle get_handle( index_type i ) const { return m_handles[(unsigned)i]; } 213 216 214 const value_type& operator[] ( index_type i ) const { return m_data[i]; } 217 215 value_type& operator[] ( index_type i ) { return m_data[i]; } … … 245 243 246 244 template < typename T, typename HANDLE = handle<>, typename TINDEX = sint32 > 247 class entity_store245 class handle_store 248 246 { 249 247 public: … … 257 255 typedef typename storage::const_reference const_reference; 258 256 259 entity_store() {} 260 261 explicit entity_store( uint32 reserve ) 262 { 263 m_handles.reserve( reserve ); 257 handle_store() {} 258 259 explicit handle_store( uint32 reserve ) 260 { 264 261 m_data.reserve( reserve ); 265 262 } … … 267 264 handle create() 268 265 { 269 m_data.emplace_back();270 m_ handles.push_back( m_indexes.create_handle( sint32( m_data.size() - 1 ) ));271 return m_handles.back();266 handle h = m_indexes.create_handle(); 267 m_data.insert( h ); 268 return h; 272 269 } 273 270 274 271 bool is_valid( handle h ) 275 272 { 276 return m_indexes.is_valid( h);273 return m_indexes.is_valid( h ); 277 274 } 278 275 279 276 const value_type* get( handle h ) const 280 277 { 281 if ( h.is_nil() ) return nullptr; 282 sint32 eindex = m_indexes.get_index( h ); 283 return eindex >= 0 ? &(m_data[ (unsigned)eindex ]) : nullptr; 278 return m_data.get( h ); 284 279 } 285 280 286 281 value_type* get( handle h ) 287 282 { 288 if ( h.is_nil() ) return nullptr; 289 sint32 eindex = m_indexes.get_index( h ); 290 return eindex >= 0 ? &(m_data[ (unsigned)eindex ]) : nullptr; 283 return m_data.get( h ); 291 284 } 292 285 293 286 void destroy( handle e ) 294 287 { 295 handle swap_handle = m_handles.back(); 296 sint32 dead_eindex = m_indexes.get_index( e ); 297 if ( dead_eindex != (sint32)m_data.size()-1 ) 298 { 299 m_data[ (unsigned)dead_eindex ] = m_data.back(); 300 m_handles[ (unsigned)dead_eindex ] = m_handles.back(); 301 } 302 m_data.pop_back(); 303 m_handles.pop_back(); 304 m_indexes.swap_indices( e, swap_handle ); 288 m_data.remove( e ); 305 289 m_indexes.free_handle( e ); 306 290 } 307 291 308 handle get_handle( index_type i ) const { return m_ handles[(unsigned)i]; }292 handle get_handle( index_type i ) const { return m_data.get_handle(i); } 309 293 const value_type& operator[] ( index_type i ) const { return m_data[(unsigned)i]; } 310 294 value_type& operator[] ( index_type i ) { return m_data[(unsigned)i]; } 311 295 size_t size() const { return m_data.size(); } 312 296 313 iterator begin() 297 iterator begin() { return m_data.begin(); } 314 298 const_iterator begin() const { return m_data.cbegin(); } 315 299 const_iterator cbegin() const { return m_data.cbegin(); } 316 300 317 iterator end() 301 iterator end() { return m_data.end(); } 318 302 const_iterator end() const { return m_data.cend(); } 319 303 const_iterator cend() const { return m_data.cend(); } 320 304 321 305 private: 322 std::vector< handle > m_handles; 323 storage m_data; 324 index_store< handle, TINDEX > m_indexes; 325 }; 326 306 packed_indexed_array< T, handle, TINDEX > m_data; 307 handle_manager< handle > m_indexes; 308 }; 309 327 310 } 328 311 -
trunk/nv/engine/particle_engine.hh
r323 r347 190 190 vec3 m_camera_pos; 191 191 192 entity_store< particle_system_info, particle_system > m_systems;192 handle_store< particle_system_info, particle_system > m_systems; 193 193 std::unordered_map< std::string, uint32 > m_names; 194 194 std::vector< particle_system_data > m_data; -
trunk/nv/fmod/fmod_audio.hh
r330 r347 40 40 virtual ~audio(); 41 41 private: 42 entity_store< sound_info, sound > m_sounds;42 handle_store< sound_info, sound > m_sounds; 43 43 vec3 m_position; 44 44 void* m_system; -
trunk/nv/gl/gl_context.hh
r342 r347 92 92 void* m_handle; 93 93 94 entity_store< vertex_array_info, vertex_array > m_vertex_arrays;95 entity_store< gl_framebuffer_info, framebuffer > m_framebuffers;94 handle_store< vertex_array_info, vertex_array > m_vertex_arrays; 95 handle_store< gl_framebuffer_info, framebuffer > m_framebuffers; 96 96 }; 97 97 -
trunk/nv/gl/gl_device.hh
r331 r347 67 67 bool compile( uint32 sh_type, const std::string& shader_code, unsigned& glid ); 68 68 std::string m_shader_header; 69 entity_store< gl_texture_info, texture > m_textures;70 entity_store< gl_buffer_info, buffer > m_buffers;71 entity_store< gl_program_info, program > m_programs;69 handle_store< gl_texture_info, texture > m_textures; 70 handle_store< gl_buffer_info, buffer > m_buffers; 71 handle_store< gl_program_info, program > m_programs; 72 72 }; 73 73 -
trunk/nv/gui/gui_environment.hh
r319 r347 58 58 void recalculate_absolute( handle e ); 59 59 60 entity_store< element, handle > m_elements;60 handle_store< element, handle > m_elements; 61 61 renderer* m_renderer; 62 62 window* m_window; -
trunk/nv/sdl/sdl_audio.hh
r330 r347 41 41 vec3 m_forward; 42 42 vec3 m_up; 43 entity_store< sound_info, sound > m_sounds;43 handle_store< sound_info, sound > m_sounds; 44 44 }; 45 45 }
Note: See TracChangeset
for help on using the changeset viewer.