From 3ed7ef57aec38a39d7887a520c8eaf510f19804e Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Thu, 14 Sep 2017 22:34:04 -0400 Subject: TITANIC: Moved queue logic within CAUdioBuffer to new FixedQueue class This is a cleaner implementation, since all the pointer logic and queue management is now better encapsulated in it's own class. I felt a new FixedQueue class was necessary because the standard Queue class uses a Common::List internally, which would be unsuitable for containing 100,000 elements, since each int value would need it's own list node. This way uses an array internally, like FixedStack --- engines/titanic/sound/audio_buffer.cpp | 33 ++------ engines/titanic/sound/audio_buffer.h | 23 ++---- engines/titanic/support/fixed_queue.h | 142 +++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 42 deletions(-) create mode 100644 engines/titanic/support/fixed_queue.h (limited to 'engines/titanic') diff --git a/engines/titanic/sound/audio_buffer.cpp b/engines/titanic/sound/audio_buffer.cpp index 636f4a5107..0b0b6a53ad 100644 --- a/engines/titanic/sound/audio_buffer.cpp +++ b/engines/titanic/sound/audio_buffer.cpp @@ -26,45 +26,24 @@ namespace Titanic { CAudioBuffer::CAudioBuffer(int maxSize) : _finished(false) { - _data.resize(maxSize); reset(); } void CAudioBuffer::reset() { - _frontP = _backP = &_data[0]; + _data.clear(); } void CAudioBuffer::push(int16 value) { - assert(!full()); - compact(); - - *_backP++ = value; + _data.push(value); } -void CAudioBuffer::push(int16 *values, int count) { - compact(); - assert(freeSize() >= count); - - Common::copy(values, values + count, _backP); - _backP += count; +void CAudioBuffer::push(const int16 *values, int count) { + for (; count > 0; --count, ++values) + _data.push(*values); } int16 CAudioBuffer::pop() { - assert(!empty()); - return *_frontP++; -} - -void CAudioBuffer::compact() { - if (_frontP != &_data[0]) { - Common::copy(_frontP, _backP, &_data[0]); - _backP -= _frontP - &_data[0]; - _frontP = &_data[0]; - } -} - -int CAudioBuffer::freeSize() { - compact(); - return &_data[0] + _data.size() - _backP; + return _data.pop(); } void CAudioBuffer::enterCriticalSection() { diff --git a/engines/titanic/sound/audio_buffer.h b/engines/titanic/sound/audio_buffer.h index 8d27667e93..1f157b9346 100644 --- a/engines/titanic/sound/audio_buffer.h +++ b/engines/titanic/sound/audio_buffer.h @@ -23,7 +23,7 @@ #ifndef TITANIC_AUDIO_BUFFER_H #define TITANIC_AUDIO_BUFFER_H -#include "common/array.h" +#include "titanic/support/fixed_queue.h" #include "common/mutex.h" namespace Titanic { @@ -31,14 +31,7 @@ namespace Titanic { class CAudioBuffer { private: Common::Mutex _mutex; - Common::Array _data; - int16 *_frontP, *_backP; - - /** - * Reclaims any space at the start of the array resulting from - * having read values off the font - */ - void compact(); + FixedQueue _data; public: bool _finished; public: @@ -57,17 +50,17 @@ public: /** * Returns the number of 16-bit entries in the buffer */ - int size() const { return _backP - _frontP; } + int size() const { return _data.size(); } /** - * Returns true if the buffer is full + * Returns the number of entries free in the buffer */ - bool full() const { return (_backP - _frontP) == (int)_data.size(); } + int freeSize() const { return _data.freeSize(); } /** - * Returns the number of entries free in the buffer + * Returns true if the buffer is full */ - int freeSize(); + bool full() const { return _data.full(); } /** * Adds a value to the buffer @@ -77,7 +70,7 @@ public: /** * Adds a value to the buffer */ - void push(int16 *values, int count); + void push(const int16 *values, int count); /** * Removes a value from the buffer diff --git a/engines/titanic/support/fixed_queue.h b/engines/titanic/support/fixed_queue.h new file mode 100644 index 0000000000..a7ba6eac2c --- /dev/null +++ b/engines/titanic/support/fixed_queue.h @@ -0,0 +1,142 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef TITANIC_FIXED_QUEUE_H +#define TITANIC_FIXED_QUEUE_H + +#include "common/scummsys.h" +#include "common/array.h" + +namespace Titanic { + +/** + * Extremly simple fixed size queue class. + */ +template +class FixedQueue { + typedef uint size_type; +protected: + Common::Array _data; + size_type _topIndex; +public: + FixedQueue() : _topIndex(0) { + _data.reserve(MAX_SIZE); + } + + /** + * Returns the size of the queue in use + */ + size_type size() const { return _data.size() - _topIndex; } + + /** + * Returns the amount of free remaining space in the queue + */ + size_type freeSize() const { return MAX_SIZE - size(); } + + /** + * Returns true if the queue is empty + */ + bool empty() const { + return size() == 0; + } + + /** + * Returns true if the queue is full + */ + bool full() const { + return freeSize() == 0; + } + + /** + * Clears the queue + */ + void clear() { + _data.clear(); + _topIndex = 0; + } + + /** + * If the tail of the queue in use has reached the end of the internal + * array, pushes all pending data back to the start of the array + */ + void compact() { + if (_data.size() == MAX_SIZE && _topIndex > 0) { + Common::copy(&_data[_topIndex], &_data[0] + MAX_SIZE, &_data[0]); + _data.resize(size()); + _topIndex = 0; + } + } + + /** + * Adds a value to the end of the queue + */ + void push(const T &v) { + assert(size() < MAX_SIZE); + compact(); + _data.push_back(v); + } + + /** + * Returns the top value on the queue + */ + const T &top() const { + assert(size() > 0); + return _data[_topIndex]; + } + + /** + * Returns the top value on the queue + */ + T &top() { + assert(size() > 0); + return _data[_topIndex]; + } + + /** + * Pops the top value off the queue + */ + T pop() { + T tmp = top(); + ++_topIndex; + return tmp; + } + + /** + * Returns values from within the queue without popping them + */ + T &operator[](size_type i) { + assert(i < size()); + return _data[_topIndex + i]; + } + + /** + * Returns values from within the queue without popping them + */ + const T &operator[](size_type i) const { + assert(i < size()); + return _data[_topIndex + i]; + } +}; + +} // End of namespace Titanic + +#endif -- cgit v1.2.3