Skip to content
This repository
Browse code

DDS loader fixed, ALBuffer/ALSource++

- ozCore
  * alignment calculation++: (size + align - 1) / align
  * some variable renames, cleanups
- ozEngine
  * ALBuffer::createSource(), creating buffers/sources without data/buffer
    atttached
  * DDS loader fixed for non-square, non-power-of-two and uncompressed images
    and GL ES
  • Loading branch information...
commit 75f6ba99c6c82f86bf0d07b7b8a06fc56ae63e1c 1 parent 535c74d
Davorin Učakar authored
4 src/ozCore/Alloc.hh
@@ -163,7 +163,7 @@ class Alloc
163 163 OZ_ALWAYS_INLINE
164 164 static size_t alignUp( size_t size )
165 165 {
166   - return ( ( size - 1 ) & ~( ALIGNMENT - 1 ) ) + ALIGNMENT;
  166 + return ( size + ALIGNMENT - 1 ) & ~( ALIGNMENT - 1 );
167 167 }
168 168
169 169 /**
@@ -183,7 +183,7 @@ class Alloc
183 183 OZ_ALWAYS_INLINE
184 184 static Type* alignUp( Type* p )
185 185 {
186   - return reinterpret_cast<Type*>( ( size_t( p - 1 ) & ~( ALIGNMENT - 1 ) ) + ALIGNMENT );
  186 + return reinterpret_cast<Type*>( size_t( p + ALIGNMENT - 1 ) & ~( ALIGNMENT - 1 ) );
187 187 }
188 188
189 189 };
2  src/ozCore/Array.hh
@@ -156,7 +156,7 @@ class Array
156 156 }
157 157
158 158 /**
159   - * Always false since static array cannot have zero size.
  159 + * Always false since static array cannot have zero elements.
160 160 */
161 161 OZ_ALWAYS_INLINE
162 162 bool isEmpty() const
4 src/ozCore/Bitset.hh
@@ -66,7 +66,7 @@ class Bitset
66 66 size = 0;
67 67 }
68 68 else {
69   - size = ( nBits - 1 ) / ULONG_BITSIZE + 1;
  69 + size = ( nBits + ULONG_BITSIZE - 1 ) / ULONG_BITSIZE;
70 70 data = new ulong[size];
71 71 }
72 72 }
@@ -461,7 +461,7 @@ class Bitset
461 461 {
462 462 hard_assert( size == 0 && nBits > 0 );
463 463
464   - int nUnits = ( nBits - 1 ) / ULONG_BITSIZE + 1;
  464 + int nUnits = ( nBits + ULONG_BITSIZE - 1 ) / ULONG_BITSIZE;
465 465
466 466 data = new ulong[nUnits];
467 467 size = nUnits;
10 src/ozCore/Buffer.cc
@@ -40,7 +40,7 @@ Buffer::Buffer( const char* data_, int size_ ) :
40 40 }
41 41
42 42 Buffer::Buffer( const String& s ) :
43   - data( s.length() == 0 ? nullptr : new char[s.length()] ), size( s.length() )
  43 + data( s.length() == 0 ? nullptr : new char[ s.length() ] ), size( s.length() )
44 44 {
45 45 mCopy( data, s.cstr(), size_t( size ) );
46 46 }
@@ -119,12 +119,12 @@ void Buffer::resize( int newSize )
119 119 size = newSize;
120 120 }
121 121
122   -void Buffer::allocate( int size_ )
  122 +void Buffer::allocate( int newSize )
123 123 {
124   - hard_assert( size == 0 && size_ > 0 );
  124 + hard_assert( size == 0 && newSize > 0 );
125 125
126   - data = new char[size_];
127   - size = size_;
  126 + data = new char[newSize];
  127 + size = newSize;
128 128 }
129 129
130 130 void Buffer::deallocate()
4 src/ozCore/Buffer.hh
@@ -176,9 +176,9 @@ class Buffer
176 176 void resize( int newSize );
177 177
178 178 /**
179   - * For an empty buffer, allocate new storage of `size` bytes.
  179 + * For an empty buffer, allocate new storage of `newSize` bytes.
180 180 */
181   - void allocate( int size );
  181 + void allocate( int newSize );
182 182
183 183 /**
184 184 * Deallocate storage.
20 src/ozCore/List.hh
@@ -76,11 +76,11 @@ class List
76 76 * Capacity is doubled, if it doesn't suffice, it is set to the least multiple of `GRANULARITY`
77 77 * able to hold the requested number of elements.
78 78 */
79   - void ensureCapacity( int desiredSize )
  79 + void ensureCapacity( int capacity )
80 80 {
81   - if( size < desiredSize ) {
  81 + if( size < capacity ) {
82 82 size *= 2;
83   - size = size < desiredSize ? ( ( desiredSize - 1 ) / GRANULARITY + 1 ) * GRANULARITY : size;
  83 + size = size < capacity ? ( capacity + GRANULARITY - 1 ) / GRANULARITY * GRANULARITY : size;
84 84 data = aReallocate<Elem>( data, count, size );
85 85 }
86 86 }
@@ -90,8 +90,8 @@ class List
90 90 /**
91 91 * Create an empty list with the given initial capacity.
92 92 */
93   - explicit List( int size_ = 0 ) :
94   - data( size_ == 0 ? nullptr : new Elem[size_] ), count( 0 ), size( size_ )
  93 + explicit List( int capacity = 0 ) :
  94 + data( capacity == 0 ? nullptr : new Elem[capacity] ), count( 0 ), size( capacity )
95 95 {}
96 96
97 97 /**
@@ -631,14 +631,14 @@ class List
631 631 }
632 632
633 633 /**
634   - * For an empty list with no allocated storage, allocate capacity for `size_` elements.
  634 + * For an empty list with no allocated storage, allocate capacity for `capacity` elements.
635 635 */
636   - void allocate( int size_ )
  636 + void allocate( int capacity )
637 637 {
638   - hard_assert( size == 0 && size_ > 0 );
  638 + hard_assert( size == 0 && capacity > 0 );
639 639
640   - data = new Elem[size_];
641   - size = size_;
  640 + data = new Elem[capacity];
  641 + size = capacity;
642 642 }
643 643
644 644 /**
20 src/ozCore/Map.hh
@@ -102,11 +102,11 @@ class Map
102 102 * Capacity is doubled, if it doesn't suffice, it is set to the least multiple of `GRANULARITY`
103 103 * able to hold the requested number of elements.
104 104 */
105   - void ensureCapacity( int desiredSize )
  105 + void ensureCapacity( int capacity )
106 106 {
107   - if( size < desiredSize ) {
  107 + if( size < capacity ) {
108 108 size *= 2;
109   - size = size < desiredSize ? ( ( desiredSize - 1 ) / GRANULARITY + 1 ) * GRANULARITY : size;
  109 + size = size < capacity ? ( capacity + GRANULARITY - 1 ) / GRANULARITY * GRANULARITY : size;
110 110 data = aReallocate<Elem>( data, count, size );
111 111 }
112 112 }
@@ -116,8 +116,8 @@ class Map
116 116 /**
117 117 * Create an empty map with the given initial capacity.
118 118 */
119   - explicit Map( int size_ = 0 ) :
120   - data( size_ == 0 ? nullptr : new Elem[size_] ), count( 0 ), size( size_ )
  119 + explicit Map( int capacity = 0 ) :
  120 + data( capacity == 0 ? nullptr : new Elem[capacity] ), count( 0 ), size( capacity )
121 121 {}
122 122
123 123 /**
@@ -541,14 +541,14 @@ class Map
541 541 }
542 542
543 543 /**
544   - * For an empty map with no allocated storage, allocate capacity for `size_` elements.
  544 + * For an empty map with no allocated storage, allocate capacity for `capacity` elements.
545 545 */
546   - void allocate( int size_ )
  546 + void allocate( int capacity )
547 547 {
548   - hard_assert( size == 0 && size_ > 0 );
  548 + hard_assert( size == 0 && capacity > 0 );
549 549
550   - data = new Elem[size_];
551   - size = size_;
  550 + data = new Elem[capacity];
  551 + size = capacity;
552 552 }
553 553
554 554 /**
20 src/ozCore/Pool.hh
@@ -132,8 +132,8 @@ class Pool
132 132
133 133 Block* firstBlock; ///< Linked list of the allocated blocks.
134 134 Slot* freeSlot; ///< Linked list of free slots or `nullptr` if none.
135   - int size; ///< Capacity.
136 135 int count; ///< Number of occupied slots in the pool.
  136 + int size; ///< Capacity.
137 137
138 138 public:
139 139
@@ -141,7 +141,7 @@ class Pool
141 141 * Create an empty pool, storage is allocated when the first allocation is made.
142 142 */
143 143 explicit Pool() :
144   - firstBlock( nullptr ), freeSlot( nullptr ), size( 0 ), count( 0 )
  144 + firstBlock( nullptr ), freeSlot( nullptr ), count( 0 ), size( 0 )
145 145 {}
146 146
147 147 /**
@@ -156,12 +156,12 @@ class Pool
156 156 * Move constructor, moves storage.
157 157 */
158 158 Pool( Pool&& p ) :
159   - firstBlock( p.firstBlock ), freeSlot( p.freeSlot ), size( p.size ), count( p.count )
  159 + firstBlock( p.firstBlock ), freeSlot( p.freeSlot ), count( p.count ), size( p.size )
160 160 {
161 161 p.firstBlock = nullptr;
162 162 p.freeSlot = nullptr;
163   - p.size = 0;
164 163 p.count = 0;
  164 + p.size = 0;
165 165 }
166 166
167 167 /**
@@ -179,13 +179,13 @@ class Pool
179 179
180 180 firstBlock = p.firstBlock;
181 181 freeSlot = p.freeSlot;
182   - size = p.size;
183 182 count = p.count;
  183 + size = p.size;
184 184
185 185 p.firstBlock = nullptr;
186 186 p.freeSlot = nullptr;
187   - p.size = 0;
188 187 p.count = 0;
  188 + p.size = 0;
189 189
190 190 return *this;
191 191 }
@@ -199,12 +199,14 @@ class Pool
199 199
200 200 if( freeSlot == nullptr ) {
201 201 firstBlock = new Block( firstBlock );
202   - freeSlot = &firstBlock->data[1];
203   - size += BLOCK_SIZE;
  202 + freeSlot = &firstBlock->data[1];
  203 + size += BLOCK_SIZE;
  204 +
204 205 return firstBlock->data[0].content;
205 206 }
206 207 else {
207 208 Slot* slot = freeSlot;
  209 +
208 210 freeSlot = slot->nextSlot;
209 211 return slot;
210 212 }
@@ -283,8 +285,8 @@ class Pool
283 285
284 286 firstBlock = nullptr;
285 287 freeSlot = nullptr;
286   - size = 0;
287 288 count = 0;
  289 + size = 0;
288 290 }
289 291
290 292 };
2  src/ozCore/SBitset.hh
@@ -52,7 +52,7 @@ class SBitset
52 52 static const int ULONG_BITSIZE = int( sizeof( ulong ) * 8 );
53 53
54 54 /// Number of units.
55   - static const int SIZE = ( BITSIZE - 1 ) / ULONG_BITSIZE + 1;
  55 + static const int SIZE = ( BITSIZE + ULONG_BITSIZE - 1 ) / ULONG_BITSIZE;
56 56
57 57 ulong data[SIZE]; ///< Pointer to array of units that holds the data.
58 58
20 src/ozCore/Set.hh
@@ -79,11 +79,11 @@ class Set
79 79 * Capacity is doubled, if it doesn't suffice, it is set to the least multiple of `GRANULARITY`
80 80 * able to hold the requested number of elements.
81 81 */
82   - void ensureCapacity( int desiredSize )
  82 + void ensureCapacity( int capacity )
83 83 {
84   - if( size < desiredSize ) {
  84 + if( size < capacity ) {
85 85 size *= 2;
86   - size = size < desiredSize ? ( ( desiredSize - 1 ) / GRANULARITY + 1 ) * GRANULARITY : size;
  86 + size = size < capacity ? ( capacity + GRANULARITY - 1 ) / GRANULARITY * GRANULARITY : size;
87 87 data = aReallocate<Elem>( data, count, size );
88 88 }
89 89 }
@@ -93,8 +93,8 @@ class Set
93 93 /**
94 94 * Create an empty set with the given initial capacity.
95 95 */
96   - explicit Set( int size_ = 0 ) :
97   - data( size_ == 0 ? nullptr : new Elem[size_] ), count( 0 ), size( size_ )
  96 + explicit Set( int capacity = 0 ) :
  97 + data( capacity == 0 ? nullptr : new Elem[capacity] ), count( 0 ), size( capacity )
98 98 {}
99 99
100 100 /**
@@ -498,14 +498,14 @@ class Set
498 498 }
499 499
500 500 /**
501   - * For an empty set with no allocated storage, allocate capacity for `size_` elements.
  501 + * For an empty set with no allocated storage, allocate capacity for `capacity` elements.
502 502 */
503   - void allocate( int size_ )
  503 + void allocate( int capacity )
504 504 {
505   - hard_assert( size == 0 && size_ > 0 );
  505 + hard_assert( size == 0 && capacity > 0 );
506 506
507   - data = new Elem[size_];
508   - size = size_;
  507 + data = new Elem[capacity];
  508 + size = capacity;
509 509 }
510 510
511 511 /**
2  src/ozCore/Thread.cc
@@ -237,7 +237,7 @@ void Thread::start( const char* name, Type type, Main* main, void* data )
237 237 }
238 238 }
239 239 else {
240   - pthread_attr_t attrib;
  240 + pthread_attr_t attrib;
241 241
242 242 pthread_attr_init( &attrib );
243 243 pthread_attr_setdetachstate( &attrib, PTHREAD_CREATE_DETACHED );
106 src/ozCore/arrays.hh
@@ -223,10 +223,10 @@ inline int aLength( const Elem ( & )[COUNT] )
223 223 * Copy array elements from the first to the last.
224 224 */
225 225 template <typename Elem>
226   -inline void aCopy( Elem* aDest, const Elem* aSrc, int count )
  226 +inline void aCopy( Elem* destArray, const Elem* srcArray, int count )
227 227 {
228 228 for( int i = 0; i < count; ++i ) {
229   - aDest[i] = aSrc[i];
  229 + destArray[i] = srcArray[i];
230 230 }
231 231 }
232 232
@@ -234,10 +234,10 @@ inline void aCopy( Elem* aDest, const Elem* aSrc, int count )
234 234 * Move array elements from the last to the first.
235 235 */
236 236 template <typename Elem>
237   -inline void aCopyBackward( Elem* aDest, const Elem* aSrc, int count )
  237 +inline void aCopyBackward( Elem* destArray, const Elem* srcArray, int count )
238 238 {
239 239 for( int i = count - 1; i >= 0; --i ) {
240   - aDest[i] = aSrc[i];
  240 + destArray[i] = srcArray[i];
241 241 }
242 242 }
243 243
@@ -245,10 +245,10 @@ inline void aCopyBackward( Elem* aDest, const Elem* aSrc, int count )
245 245 * Move array elements from the first to the last.
246 246 */
247 247 template <typename Elem>
248   -inline void aMove( Elem* aDest, Elem* aSrc, int count )
  248 +inline void aMove( Elem* destArray, Elem* srcArray, int count )
249 249 {
250 250 for( int i = 0; i < count; ++i ) {
251   - aDest[i] = static_cast<Elem&&>( aSrc[i] );
  251 + destArray[i] = static_cast<Elem&&>( srcArray[i] );
252 252 }
253 253 }
254 254
@@ -256,10 +256,10 @@ inline void aMove( Elem* aDest, Elem* aSrc, int count )
256 256 * Move array elements from the last to the first.
257 257 */
258 258 template <typename Elem>
259   -inline void aMoveBackward( Elem* aDest, Elem* aSrc, int count )
  259 +inline void aMoveBackward( Elem* destArray, Elem* srcArray, int count )
260 260 {
261 261 for( int i = count - 1; i >= 0; --i ) {
262   - aDest[i] = static_cast<Elem&&>( aSrc[i] );
  262 + destArray[i] = static_cast<Elem&&>( srcArray[i] );
263 263 }
264 264 }
265 265
@@ -267,10 +267,10 @@ inline void aMoveBackward( Elem* aDest, Elem* aSrc, int count )
267 267 * %Set array elements to the given value.
268 268 */
269 269 template <typename Elem, typename Value = Elem>
270   -inline void aFill( Elem* aDest, const Value& value, int count )
  270 +inline void aFill( Elem* array, const Value& value, int count )
271 271 {
272 272 for( int i = 0; i < count; ++i ) {
273   - aDest[i] = value;
  273 + array[i] = value;
274 274 }
275 275 }
276 276
@@ -278,25 +278,25 @@ inline void aFill( Elem* aDest, const Value& value, int count )
278 278 * Swap array elements.
279 279 */
280 280 template <typename Elem>
281   -inline void aSwap( Elem* aDestA, Elem* aDestB, int count )
  281 +inline void aSwap( Elem* arrayA, Elem* arrayB, int count )
282 282 {
283 283 for( int i = 0; i < count; ++i ) {
284   - Elem t = static_cast<Elem&&>( aDestA[i] );
285   - aDestA[i] = static_cast<Elem&&>( aDestB[i] );
286   - aDestB[i] = static_cast<Elem&&>( t );
  284 + Elem t = static_cast<Elem&&>( arrayA[i] );
  285 + arrayA[i] = static_cast<Elem&&>( arrayB[i] );
  286 + arrayB[i] = static_cast<Elem&&>( t );
287 287 }
288 288 }
289 289
290 290 /**
291   - * Swap elements of two same-size static arrays.
  291 + * Swap elements of two same-length static arrays.
292 292 */
293 293 template <typename Elem, int COUNT>
294   -inline void aSwap( Elem ( & aDestA )[COUNT], Elem ( & aDestB )[COUNT] )
  294 +inline void aSwap( Elem ( & arrayA )[COUNT], Elem ( & arrayB )[COUNT] )
295 295 {
296 296 for( int i = 0; i < COUNT; ++i ) {
297   - Elem t = static_cast<Elem&&>( aDestA[i] );
298   - aDestA[i] = static_cast<Elem&&>( aDestB[i] );
299   - aDestB[i] = static_cast<Elem&&>( t );
  297 + Elem t = static_cast<Elem&&>( arrayA[i] );
  298 + arrayA[i] = static_cast<Elem&&>( arrayB[i] );
  299 + arrayB[i] = static_cast<Elem&&>( t );
300 300 }
301 301 }
302 302
@@ -304,10 +304,10 @@ inline void aSwap( Elem ( & aDestA )[COUNT], Elem ( & aDestB )[COUNT] )
304 304 * True iff respective elements are equal.
305 305 */
306 306 template <typename Elem>
307   -inline bool aEquals( const Elem* aSrcA, const Elem* aSrcB, int count )
  307 +inline bool aEquals( const Elem* arrayA, const Elem* arrayB, int count )
308 308 {
309 309 for( int i = 0; i < count; ++i ) {
310   - if( !( aSrcA[i] == aSrcB[i] ) ) {
  310 + if( !( arrayA[i] == arrayB[i] ) ) {
311 311 return false;
312 312 }
313 313 }
@@ -318,10 +318,10 @@ inline bool aEquals( const Elem* aSrcA, const Elem* aSrcB, int count )
318 318 * True iff the given value is found in the array.
319 319 */
320 320 template <typename Elem, typename Value = Elem>
321   -inline bool aContains( const Elem* aSrc, const Value& value, int count )
  321 +inline bool aContains( const Elem* array, const Value& value, int count )
322 322 {
323 323 for( int i = 0; i < count; ++i ) {
324   - if( aSrc[i] == value ) {
  324 + if( array[i] == value ) {
325 325 return true;
326 326 }
327 327 }
@@ -332,11 +332,11 @@ inline bool aContains( const Elem* aSrc, const Value& value, int count )
332 332 * Pointer to the first occurrence or `nullptr` if not found.
333 333 */
334 334 template <typename Elem, typename Value = Elem>
335   -inline Elem* aFind( Elem* aSrc, const Value& value, int count )
  335 +inline Elem* aFind( Elem* array, const Value& value, int count )
336 336 {
337 337 for( int i = 0; i < count; ++i ) {
338   - if( aSrc[i] == value ) {
339   - return &aSrc[i];
  338 + if( array[i] == value ) {
  339 + return &array[i];
340 340 }
341 341 }
342 342 return nullptr;
@@ -346,11 +346,11 @@ inline Elem* aFind( Elem* aSrc, const Value& value, int count )
346 346 * Pointer to the last occurrence or `nullptr` if not found.
347 347 */
348 348 template <typename Elem, typename Value = Elem>
349   -inline Elem* aFindLast( Elem* aSrc, const Value& value, int count )
  349 +inline Elem* aFindLast( Elem* array, const Value& value, int count )
350 350 {
351 351 for( int i = count - 1; i >= 0; --i ) {
352   - if( aSrc[i] == value ) {
353   - return &aSrc[i];
  352 + if( array[i] == value ) {
  353 + return &array[i];
354 354 }
355 355 }
356 356 return nullptr;
@@ -360,10 +360,10 @@ inline Elem* aFindLast( Elem* aSrc, const Value& value, int count )
360 360 * Index of the first occurrence of the value or -1 if not found.
361 361 */
362 362 template <typename Elem, typename Value = Elem>
363   -inline int aIndex( const Elem* aSrc, const Value& value, int count )
  363 +inline int aIndex( const Elem* array, const Value& value, int count )
364 364 {
365 365 for( int i = 0; i < count; ++i ) {
366   - if( aSrc[i] == value ) {
  366 + if( array[i] == value ) {
367 367 return i;
368 368 }
369 369 }
@@ -374,10 +374,10 @@ inline int aIndex( const Elem* aSrc, const Value& value, int count )
374 374 * Index of the last occurrence of the value or -1 if not found.
375 375 */
376 376 template <typename Elem, typename Value = Elem>
377   -inline int aLastIndex( const Elem* aSrc, const Value& value, int count )
  377 +inline int aLastIndex( const Elem* array, const Value& value, int count )
378 378 {
379 379 for( int i = count - 1; i >= 0; --i ) {
380   - if( aSrc[i] == value ) {
  380 + if( array[i] == value ) {
381 381 return i;
382 382 }
383 383 }
@@ -388,50 +388,50 @@ inline int aLastIndex( const Elem* aSrc, const Value& value, int count )
388 388 * Delete objects referenced by elements and set all elements to `nullptr`.
389 389 */
390 390 template <typename Elem>
391   -inline void aFree( Elem* aDest, int count )
  391 +inline void aFree( Elem* array, int count )
392 392 {
393 393 for( int i = 0; i < count; ++i ) {
394   - delete aDest[i];
395   - aDest[i] = nullptr;
  394 + delete array[i];
  395 + array[i] = nullptr;
396 396 }
397 397 }
398 398
399 399 /**
400 400 * Reallocate array.
401 401 *
402   - * Allocate new array of `newCount` elements, copy first `count` elements of the source array `aSrc`
403   - * to the newly created one and delete the source array.
  402 + * Allocate new array of `length` elements, copy first `count` elements of the source array to the
  403 + * newly created one and delete the source array.
404 404 *
405 405 * @return Newly allocated array.
406 406 */
407 407 template <typename Elem>
408   -inline Elem* aReallocate( Elem* aSrc, int count, int newCount )
  408 +inline Elem* aReallocate( Elem* array, int count, int length )
409 409 {
410   - Elem* aNew = nullptr;
  410 + Elem* newArray = nullptr;
411 411
412   - if( newCount != 0 ) {
413   - aNew = new Elem[newCount];
  412 + if( length != 0 ) {
  413 + newArray = new Elem[length];
414 414
415 415 for( int i = 0; i < count; ++i ) {
416   - aNew[i] = static_cast<Elem&&>( aSrc[i] );
  416 + newArray[i] = static_cast<Elem&&>( array[i] );
417 417 }
418 418 }
419   - delete[] aSrc;
  419 + delete[] array;
420 420
421   - return aNew;
  421 + return newArray;
422 422 }
423 423
424 424 /**
425 425 * Reverse the order of array elements.
426 426 */
427 427 template <typename Elem>
428   -inline void aReverse( Elem* aDest, int count )
  428 +inline void aReverse( Elem* array, int count )
429 429 {
430 430 int bottom = 0;
431 431 int top = count - 1;
432 432
433 433 while( bottom < top ) {
434   - swap<Elem>( aDest[bottom], aDest[top] );
  434 + swap<Elem>( array[bottom], array[top] );
435 435 ++bottom;
436 436 --top;
437 437 }
@@ -503,12 +503,12 @@ static void quicksort( Elem* first, Elem* last )
503 503 * Sort array using quicksort algorithm.
504 504 */
505 505 template <typename Elem>
506   -inline void aSort( Elem* aSrc, int count )
  506 +inline void aSort( Elem* array, int count )
507 507 {
508 508 int last = count - 1;
509 509
510 510 if( last > 0 ) {
511   - quicksort<Elem>( aSrc, &aSrc[last] );
  511 + quicksort<Elem>( array, &array[last] );
512 512 }
513 513 }
514 514
@@ -520,23 +520,23 @@ inline void aSort( Elem* aSrc, int count )
520 520 *
521 521 * If all elements are lesser return `count - 1` and if all elements are greater return -1.
522 522 *
523   - * @param aSrc array.
  523 + * @param array array of elements.
524 524 * @param key the key we are looking for.
525 525 * @param count number of elements.
526 526 * @return Index of the last element not greater than `key`, -1 otherwise.
527 527 */
528 528 template <typename Elem, typename Key = Elem>
529   -inline int aBisection( Elem* aSrc, const Key& key, int count )
  529 +inline int aBisection( Elem* array, const Key& key, int count )
530 530 {
531 531 int a = -1;
532 532 int b = count;
533 533
534   - // The algorithm ensures that (a == -1 or aSrc[a] <= key) and (b == count or key < aSrc[b]),
  534 + // The algorithm ensures that (a == -1 or array[a] <= key) and (b == count or key < array[b]),
535 535 // so the key may only lie on position a or nowhere.
536 536 while( b - a > 1 ) {
537 537 int c = ( a + b ) / 2;
538 538
539   - if( key < aSrc[c] ) {
  539 + if( key < array[c] ) {
540 540 b = c;
541 541 }
542 542 else {
17 src/ozCore/common.hh
@@ -117,8 +117,23 @@ namespace oz
117 117 {
118 118
119 119 using std::nullptr_t;
120   -using std::ptrdiff_t;
121 120 using std::size_t;
  121 +using std::ptrdiff_t;
  122 +
  123 +/**
  124 + * Null pointer type.
  125 + */
  126 +typedef std::nullptr_t nullptr_t;
  127 +
  128 +/**
  129 + * Platform-dependent unsigned integer type for memory offsets and sizes.
  130 + */
  131 +typedef std::size_t size_t;
  132 +
  133 +/**
  134 + * Platform-dependent signed integer type for memory offsets and pointer differences.
  135 + */
  136 +typedef std::ptrdiff_t ptrdiff_t;
122 137
123 138 /**
124 139 * Signed byte.
131 src/ozCore/iterables.hh
@@ -178,36 +178,51 @@ inline typename Container::Iterator iter( Container& container )
178 178 }
179 179
180 180 /**
181   - * Copy all elements from `iSrc` to `iDest`.
  181 + * Count elements.
  182 + */
  183 +template <class CIterator>
  184 +inline int iLength( CIterator iter )
  185 +{
  186 + int count = 0;
  187 +
  188 + while( iter.isValid() ) {
  189 + ++count;
  190 + ++iter;
  191 + }
  192 + return count;
  193 +}
  194 +
  195 +/**
  196 + * Copy all elements from `srcIter` to `destIter`.
182 197 */
183 198 template <class IteratorA, class CIteratorB>
184   -inline void iCopy( IteratorA iDest, CIteratorB iSrc )
  199 +inline void iCopy( IteratorA destIter, CIteratorB srcIter )
185 200 {
186   - while( iSrc.isValid() ) {
187   - hard_assert( iDest.isValid() );
  201 + while( srcIter.isValid() ) {
  202 + hard_assert( destIter.isValid() );
188 203
189   - *iDest = *iSrc;
  204 + *destIter = *srcIter;
190 205
191   - ++iDest;
192   - ++iSrc;
  206 + ++destIter;
  207 + ++srcIter;
193 208 }
194 209 }
195 210
196 211 /**
197   - * Move all elements from `iSrc` to `iDest`.
  212 + * Move all elements from `srcIter` to `destIter`.
198 213 */
199 214 template <class IteratorA, class IteratorB>
200   -inline void iMove( IteratorA iDest, IteratorB iSrc )
  215 +inline void iMove( IteratorA destIter, IteratorB srcIter )
201 216 {
202 217 typedef typename IteratorB::ElemType ElemB;
203 218
204   - while( iSrc.isValid() ) {
205   - hard_assert( iDest.isValid() );
  219 + while( srcIter.isValid() ) {
  220 + hard_assert( destIter.isValid() );
206 221
207   - *iDest = static_cast<ElemB&&>( *iSrc );
  222 + *destIter = static_cast<ElemB&&>( *srcIter );
208 223
209   - ++iDest;
210   - ++iSrc;
  224 + ++destIter;
  225 + ++srcIter;
211 226 }
212 227 }
213 228
@@ -215,12 +230,12 @@ inline void iMove( IteratorA iDest, IteratorB iSrc )
215 230 * %Set elements to the given value.
216 231 */
217 232 template <class Iterator, typename Value = typename Iterator::ElemType>
218   -inline void iFill( Iterator iDest, const Value& value )
  233 +inline void iFill( Iterator iter, const Value& value )
219 234 {
220   - while( iDest.isValid() ) {
221   - *iDest = value;
  235 + while( iter.isValid() ) {
  236 + *iter = value;
222 237
223   - ++iDest;
  238 + ++iter;
224 239 }
225 240 }
226 241
@@ -228,78 +243,78 @@ inline void iFill( Iterator iDest, const Value& value )
228 243 * Swap element of two same-length containers.
229 244 */
230 245 template <class IteratorA, class IteratorB>
231   -inline void iSwap( IteratorA iDestA, IteratorB iDestB )
  246 +inline void iSwap( IteratorA iterA, IteratorB iterB )
232 247 {
233 248 typedef typename IteratorA::ElemType ElemA;
234 249 typedef typename IteratorB::ElemType ElemB;
235 250
236   - while( iDestA.isValid() ) {
237   - hard_assert( iDestB.isValid() );
  251 + while( iterA.isValid() ) {
  252 + hard_assert( iterB.isValid() );
238 253
239   - ElemA t = static_cast<ElemA&&>( *iDestA );
240   - *iDestA = static_cast<ElemB&&>( *iDestB );
241   - *iDestB = static_cast<ElemA&&>( t );
  254 + ElemA t = static_cast<ElemA&&>( *iterA );
  255 + *iterA = static_cast<ElemB&&>( *iterB );
  256 + *iterB = static_cast<ElemA&&>( t );
242 257
243   - ++iDestA;
244   - ++iDestB;
  258 + ++iterA;
  259 + ++iterB;
245 260 }
246 261
247   - hard_assert( !iDestB.isValid() );
  262 + hard_assert( !iterB.isValid() );
248 263 }
249 264
250 265 /**
251 266 * True iff same length and respective elements are equal.
252 267 */
253 268 template <class CIteratorA, class CIteratorB>
254   -inline bool iEquals( CIteratorA iSrcA, CIteratorB iSrcB )
  269 +inline bool iEquals( CIteratorA iterA, CIteratorB iterB )
255 270 {
256   - hard_assert( static_cast<void*>( &iSrcA ) != static_cast<void*>( &iSrcB ) );
  271 + hard_assert( static_cast<void*>( &iterA ) != static_cast<void*>( &iterB ) );
257 272
258   - while( iSrcA.isValid() && iSrcB.isValid() && *iSrcA == *iSrcB ) {
259   - ++iSrcA;
260   - ++iSrcB;
  273 + while( iterA.isValid() && iterB.isValid() && *iterA == *iterB ) {
  274 + ++iterA;
  275 + ++iterB;
261 276 }
262   - return !iSrcA.isValid() && !iSrcB.isValid();
  277 + return !iterA.isValid() && !iterB.isValid();
263 278 }
264 279
265 280 /**
266 281 * True iff the given value is found in the container.
267 282 */
268 283 template <class CIterator, typename Value = typename CIterator::ElemType>
269   -inline bool iContains( CIterator iSrc, const Value& value )
  284 +inline bool iContains( CIterator iter, const Value& value )
270 285 {
271   - while( iSrc.isValid() && !( *iSrc == value ) ) {
272   - ++iSrc;
  286 + while( iter.isValid() && !( *iter == value ) ) {
  287 + ++iter;
273 288 }
274   - return iSrc.isValid();
  289 + return iter.isValid();
275 290 }
276 291
277 292 /**
278 293 * %Iterator for the first occurrence or an invalid iterator if not found.
279 294 */
280 295 template <class Iterator, typename Value = typename Iterator::ElemType>
281   -inline Iterator iFind( Iterator iSrc, const Value& value )
  296 +inline Iterator iFind( Iterator iter, const Value& value )
282 297 {
283   - while( iSrc.isValid() && !( *iSrc == value ) ) {
284   - ++iSrc;
  298 + while( iter.isValid() && !( *iter == value ) ) {
  299 + ++iter;
285 300 }
286   - return iSrc;
  301 + return iter;
287 302 }
288 303
289 304 /**
290 305 * %Iterator for the last occurrence or an invalid iterator if not found.
291 306 */
292 307 template <class Iterator, typename Value = typename Iterator::ElemType>
293   -inline Iterator iFindLast( Iterator iSrc, const Value& value )
  308 +inline Iterator iFindLast( Iterator iter, const Value& value )
294 309 {
295 310 Iterator lastOccurence;
296 311
297   - while( iSrc.isValid() ) {
298   - if( *iSrc == value ) {
299   - lastOccurence = iSrc;
  312 + while( iter.isValid() ) {
  313 + if( *iter == value ) {
  314 + lastOccurence = iter;
300 315 }
301 316
302   - ++iSrc;
  317 + ++iter;
303 318 }
304 319 return lastOccurence;
305 320 }
@@ -308,32 +323,32 @@ inline Iterator iFindLast( Iterator iSrc, const Value& value )
308 323 * Index of the first occurrence of the value or -1 if not found.
309 324 */
310 325 template <class CIterator, typename Value = typename CIterator::ElemType>
311   -inline int iIndex( CIterator iSrc, const Value& value )
  326 +inline int iIndex( CIterator iter, const Value& value )
312 327 {
313 328 int index = 0;
314 329
315   - while( iSrc.isValid() && !( *iSrc == value ) ) {
316   - ++iSrc;
  330 + while( iter.isValid() && !( *iter == value ) ) {
  331 + ++iter;
317 332 ++index;
318 333 }
319   - return !iSrc.isValid() ? -1 : index;
  334 + return !iter.isValid() ? -1 : index;
320 335 }
321 336
322 337 /**
323 338 * Index of the last occurrence of the value or -1 if not found.
324 339 */
325 340 template <class CIterator, typename Value = typename CIterator::ElemType>
326   -inline int iLastIndex( CIterator iSrc, const Value& value )
  341 +inline int iLastIndex( CIterator iter, const Value& value )
327 342 {
328 343 int index = 0;
329 344 int lastIndex = -1;
330 345
331   - while( iSrc.isValid() ) {
332   - if( *iSrc == value ) {
  346 + while( iter.isValid() ) {
  347 + if( *iter == value ) {
333 348 lastIndex = index;
334 349 }
335 350
336   - ++iSrc;
  351 + ++iter;
337 352 ++index;
338 353 }
339 354 return lastIndex;
@@ -343,13 +358,13 @@ inline int iLastIndex( CIterator iSrc, const Value& value )
343 358 * Delete objects referenced by elements and set all elements to `nullptr`.
344 359 */
345 360 template <class Iterator>
346   -inline void iFree( Iterator iDest )
  361 +inline void iFree( Iterator iter )
347 362 {
348 363 typedef typename Iterator::ElemType Elem;
349 364
350   - while( iDest.isValid() ) {
351   - Elem& elem = *iDest;
352   - ++iDest;
  365 + while( iter.isValid() ) {
  366 + Elem& elem = *iter;
  367 + ++iter;
353 368
354 369 delete elem;
355 370 elem = nullptr;
40 src/ozEngine/ALBuffer.cc
@@ -81,12 +81,44 @@ ALBuffer::~ALBuffer()
81 81 destroy();
82 82 }
83 83
  84 +ALSource ALBuffer::createSource() const
  85 +{
  86 + ALSource source;
  87 +
  88 + if( bufferId == 0 ) {
  89 + return source;
  90 + }
  91 +
  92 + source.create();
  93 +
  94 + if( source.isCreated() ) {
  95 + alSourcei( source.id(), AL_BUFFER, int( bufferId ) );
  96 + }
  97 + return source;
  98 +}
  99 +
  100 +bool ALBuffer::create()
  101 +{
  102 + destroy();
  103 +
  104 + alGenBuffers( 1, &bufferId );
  105 + return bufferId != 0;
  106 +}
  107 +
84 108 bool ALBuffer::load( const File& file )
85 109 {
86 110 destroy();
87 111
88   - Buffer buffer = file.read();
89   - InputStream istream = buffer.inputStream();
  112 + Buffer buffer;
  113 + InputStream istream;
  114 +
  115 + if( file.isMapped() ) {
  116 + istream = file.inputStream();
  117 + }
  118 + else {
  119 + buffer = file.read();
  120 + istream = buffer.inputStream();
  121 + }
90 122
91 123 if( !istream.isAvailable() ) {
92 124 return false;
@@ -184,7 +216,7 @@ bool ALBuffer::load( const File& file )
184 216 ov_clear( &ovStream );
185 217
186 218 OZ_AL_CHECK_ERROR();
187   - return true;
  219 + return bufferId != 0;
188 220 }
189 221 }
190 222
@@ -193,8 +225,6 @@ void ALBuffer::destroy()
193 225 if( bufferId != 0 ) {
194 226 alDeleteBuffers( 1, &bufferId );
195 227 bufferId = 0;
196   -
197   - OZ_AL_CHECK_ERROR();
198 228 }
199 229 }
200 230
12 src/ozEngine/ALBuffer.hh
@@ -26,7 +26,7 @@
26 26
27 27 #pragma once
28 28
29   -#include "common.hh"
  29 +#include "ALSource.hh"
30 30
31 31 namespace oz
32 32 {
@@ -98,6 +98,16 @@ class ALBuffer
98 98 }
99 99
100 100 /**
  101 + * Create a new OpenAL source for this buffer.
  102 + */
  103 + ALSource createSource() const;
  104 +
  105 + /**
  106 + * Create a new uninitialised OpenAL buffer.
  107 + */
  108 + bool create();
  109 +
  110 + /**
101 111 * Create a new OpenAL buffer from the given WAVE or Ogg Vorbis file.
102 112 */
103 113 bool load( const File& file );
30 src/ozEngine/ALSource.cc
@@ -35,33 +35,23 @@ ALSource::ALSource() :
35 35 sourceId( 0 )
36 36 {}
37 37
38   -ALSource::ALSource( const ALBuffer& buffer ) :
39   - sourceId( 0 )
40   -{
41   - create( buffer );
42   -}
43   -
44 38 ALSource::~ALSource()
45 39 {
46 40 destroy();
47 41 }
48 42
49   -bool ALSource::create( const ALBuffer& buffer )
  43 +bool ALSource::create()
50 44 {
51   - destroy();
52   -
53   - if( !buffer.isLoaded() ) {
54   - return false;
55   - }
56   -
57 45 alGenSources( 1, &sourceId );
58   - alSourcei( sourceId, AL_BUFFER, int( buffer.id() ) );
59   -
60   - // This is not necessary by specification but it seems it's always the case with openalsoft.
61   - hard_assert( sourceId != 0 );
62 46
63   - OZ_AL_CHECK_ERROR();
64   - return true;
  47 + if( alGetError() != AL_NO_ERROR ) {
  48 + sourceId = 0;
  49 + }
  50 + else {
  51 + // This is not necessary by specification but it seems it's always the case with openalsoft.
  52 + hard_assert( sourceId != 0 );
  53 + }
  54 + return sourceId != 0;
65 55 }
66 56
67 57 void ALSource::destroy()
@@ -69,8 +59,6 @@ void ALSource::destroy()
69 59 if( sourceId != 0 ) {
70 60 alDeleteSources( 1, &sourceId );
71 61 sourceId = 0;
72   -
73   - OZ_AL_CHECK_ERROR();
74 62 }
75 63 }
76 64
13 src/ozEngine/ALSource.hh
@@ -26,7 +26,7 @@
26 26
27 27 #pragma once
28 28
29   -#include "ALBuffer.hh"
  29 +#include "common.hh"
30 30
31 31 namespace oz
32 32 {
@@ -48,11 +48,6 @@ class ALSource
48 48 explicit ALSource();
49 49
50 50 /**
51   - * Create a new source for the given buffer. Same as the default constructor plus `create()`.
52   - */
53   - explicit ALSource( const ALBuffer& buffer );
54   -
55   - /**
56 51 * Destructor, destroys OpenAL source if created.
57 52 */
58 53 ~ALSource();
@@ -75,6 +70,8 @@ class ALSource
75 70 return *this;
76 71 }
77 72
  73 + destroy();
  74 +
78 75 sourceId = s.sourceId;
79 76 s.sourceId = 0;
80 77
@@ -98,9 +95,9 @@ class ALSource
98 95 }
99 96
100 97 /**
101   - * Create a new OpenAL source for the given OpenAL buffer.
  98 + * Create a new uninitialised OpenAL source.
102 99 */
103   - bool create( const ALBuffer& buffer );
  100 + bool create();
104 101
105 102 /**
106 103 * Destroy OpenAL source if created.
141 src/ozEngine/ALStreamingBuffer.cc
... ... @@ -0,0 +1,141 @@
  1 +/*
  2 + * ozEngine - OpenZone Engine Library.
  3 + *
  4 + * Copyright © 2002-2013 Davorin Učakar
  5 + *
  6 + * This software is provided 'as-is', without any express or implied warranty.
  7 + * In no event will the authors be held liable for any damages arising from
  8 + * the use of this software.
  9 + *
  10 + * Permission is granted to anyone to use this software for any purpose,
  11 + * including commercial applications, and to alter it and redistribute it
  12 + * freely, subject to the following restrictions:
  13 + *
  14 + * 1. The origin of this software must not be misrepresented; you must not
  15 + * claim that you wrote the original software. If you use this software in
  16 + * a product, an acknowledgement in the product documentation would be
  17 + * appreciated but is not required.
  18 + * 2. Altered source versions must be plainly marked as such, and must not be
  19 + * misrepresented as being the original software.
  20 + * 3. This notice may not be removed or altered from any source distribution.
  21 + */
  22 +
  23 +/**
  24 + * @file ozEngine/ALStreamingBuffer.cc
  25 + */
  26 +
  27 +#include "ALStreamingBuffer.hh"
  28 +
  29 +#include "OpenAL.hh"
  30 +
  31 +// We don't use those callbacks anywhere and they don't compile on MinGW.
  32 +#define OV_EXCLUDE_STATIC_CALLBACKS
  33 +#include <vorbis/vorbisfile.h>
  34 +
  35 +namespace oz
  36 +{
  37 +
  38 +static size_t vorbisRead( void* buffer, size_t size, size_t n, void* handle )
  39 +{
  40 + InputStream* istream = static_cast<InputStream*>( handle );
  41 +
  42 + int blockSize = int( size );
  43 + int nBlocks = min( int( n ), istream->available() / blockSize );
  44 +
  45 + istream->readChars( static_cast<char*>( buffer ), nBlocks * blockSize );
  46 + return size_t( nBlocks );
  47 +}
  48 +
  49 +static int vorbisSeek( void* handle, ogg_int64_t offset, int whence )
  50 +{
  51 + InputStream* istream = static_cast<InputStream*>( handle );
  52 +
  53 + const char* origin = whence == SEEK_CUR ? istream->pos() :
  54 + whence == SEEK_END ? istream->end() : istream->begin();
  55 +
  56 + istream->set( origin + offset );
  57 + return 0;
  58 +}
  59 +
  60 +static long vorbisTell( void* handle )
  61 +{
  62 + InputStream* istream = static_cast<InputStream*>( handle );
  63 +
  64 + return long( istream->tell() );
  65 +}
  66 +
  67 +static ov_callbacks VORBIS_CALLBACKS = { vorbisRead, vorbisSeek, nullptr, vorbisTell };
  68 +
  69 +ALStreamingBuffer::ALStreamingBuffer()
  70 +{
  71 + bufferIds[0] = 0;
  72 + bufferIds[1] = 0;
  73 + sourceId = 0;
  74 +}
  75 +