1
0
Fork 0

Codechange: let the ReusableBuffer use std::vector as storage

pull/13584/head
Rubidium 2025-02-16 14:24:18 +01:00 committed by rubidium42
parent 59df0ff496
commit 576a96c685
4 changed files with 18 additions and 34 deletions

View File

@ -148,8 +148,7 @@ Sprite *Blitter_8bppOptimized::Encode(const SpriteLoader::SpriteCollection &spri
* memory around as this function is called quite often * memory around as this function is called quite often
* and the memory usage is quite low. */ * and the memory usage is quite low. */
static ReusableBuffer<uint8_t> temp_buffer; static ReusableBuffer<uint8_t> temp_buffer;
SpriteData *temp_dst = (SpriteData *)temp_buffer.Allocate(memory); SpriteData *temp_dst = reinterpret_cast<SpriteData *>(temp_buffer.ZeroAllocate(memory));
memset(temp_dst, 0, sizeof(*temp_dst));
uint8_t *dst = temp_dst->data; uint8_t *dst = temp_dst->data;
/* Make the sprites per zoom-level */ /* Make the sprites per zoom-level */

View File

@ -23,15 +23,9 @@
template <typename T> template <typename T>
class ReusableBuffer { class ReusableBuffer {
private: private:
T *buffer; ///< The real data buffer std::vector<T> buffer;
size_t count; ///< Number of T elements in the buffer
public: public:
/** Create a new buffer */
ReusableBuffer() : buffer(nullptr), count(0) {}
/** Clear the buffer */
~ReusableBuffer() { free(this->buffer); }
/** /**
* Get buffer of at least count times T. * Get buffer of at least count times T.
* @note the buffer might be bigger * @note the buffer might be bigger
@ -41,16 +35,12 @@ public:
*/ */
T *Allocate(size_t count) T *Allocate(size_t count)
{ {
if (this->count < count) { if (this->buffer.size() < count) this->buffer.resize(count);
free(this->buffer); return this->buffer.data();
this->buffer = MallocT<T>(count);
this->count = count;
}
return this->buffer;
} }
/** /**
* Get buffer of at least count times T with zeroed memory. * Get buffer of at least count times T of default initialised elements.
* @note the buffer might be bigger * @note the buffer might be bigger
* @note calling this function invalidates any previous buffers given * @note calling this function invalidates any previous buffers given
* @param count the minimum buffer size * @param count the minimum buffer size
@ -58,14 +48,9 @@ public:
*/ */
T *ZeroAllocate(size_t count) T *ZeroAllocate(size_t count)
{ {
if (this->count < count) { this->buffer.clear();
free(this->buffer); this->buffer.resize(count, {});
this->buffer = CallocT<T>(count); return this->buffer.data();
this->count = count;
} else {
memset(this->buffer, 0, sizeof(T) * count);
}
return this->buffer;
} }
/** /**
@ -74,7 +59,7 @@ public:
*/ */
inline const T *GetBuffer() const inline const T *GetBuffer() const
{ {
return this->buffer; return this->buffer.data();
} }
}; };

View File

@ -296,23 +296,23 @@ static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint p
for (uint y = 0; y < height; y++) { for (uint y = 0; y < height; y++) {
if (y < pad_top || pad_bottom + y >= height) { if (y < pad_top || pad_bottom + y >= height) {
/* Top/bottom padding. */ /* Top/bottom padding. */
MemSetT(data, 0, width); std::fill_n(data, width, SpriteLoader::CommonPixel{});
data += width; data += width;
} else { } else {
if (pad_left > 0) { if (pad_left > 0) {
/* Pad left. */ /* Pad left. */
MemSetT(data, 0, pad_left); std::fill_n(data, pad_left, SpriteLoader::CommonPixel{});
data += pad_left; data += pad_left;
} }
/* Copy pixels. */ /* Copy pixels. */
MemCpyT(data, src, sprite->width); std::copy_n(src, sprite->width, data);
src += sprite->width; src += sprite->width;
data += sprite->width; data += sprite->width;
if (pad_right > 0) { if (pad_right > 0) {
/* Pad right. */ /* Pad right. */
MemSetT(data, 0, pad_right); std::fill_n(data, pad_right, SpriteLoader::CommonPixel{});
data += pad_right; data += pad_right;
} }
} }

View File

@ -31,11 +31,11 @@ class SpriteLoader {
public: public:
/** Definition of a common pixel in OpenTTD's realm. */ /** Definition of a common pixel in OpenTTD's realm. */
struct CommonPixel { struct CommonPixel {
uint8_t r; ///< Red-channel uint8_t r = 0; ///< Red-channel
uint8_t g; ///< Green-channel uint8_t g = 0; ///< Green-channel
uint8_t b; ///< Blue-channel uint8_t b = 0; ///< Blue-channel
uint8_t a; ///< Alpha-channel uint8_t a = 0; ///< Alpha-channel
uint8_t m; ///< Remap-channel uint8_t m = 0; ///< Remap-channel
}; };
/** /**