Skip to content

Commit

Permalink
QDENGINE: Eliminated global constructors in RLEBuffer class
Browse files Browse the repository at this point in the history
  • Loading branch information
sev- committed Aug 16, 2024
1 parent 18f12ce commit 5c8d742
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
3 changes: 3 additions & 0 deletions engines/qdengine/qd_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "qdengine/qdcore/util/ResourceDispatcher.h"
#include "qdengine/qdcore/util/WinVideo.h"
#include "qdengine/system/graphics/gr_dispatcher.h"
#include "qdengine/system/graphics/rle_compress.h"
#include "qdengine/system/input/input_wndproc.h"
#include "qdengine/system/input/mouse_input.h"
#include "qdengine/system/input/keyboard_input.h"
Expand Down Expand Up @@ -319,6 +320,8 @@ int QDEngineEngine::engineMain() {

winVideo::done();

RLEBuffer::releaseBuffers();

return 0;
}

Expand Down
51 changes: 45 additions & 6 deletions engines/qdengine/system/graphics/rle_compress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,18 @@

namespace QDEngine {

Std::vector<byte> RLEBuffer::_buffer0(4096);
Std::vector<byte> RLEBuffer::_buffer1(4096);
byte *g_buffer0 = nullptr;
byte *g_buffer1 = nullptr;
int g_buffersLen = 0;

static void ensureBuffers() {
if (g_buffer0 == nullptr) {
g_buffer0 = (byte *)calloc(4096, 1);
g_buffer1 = (byte *)calloc(4096, 1);

g_buffersLen = 4096;
}
}

bool operator == (const RLEBuffer &buf1, const RLEBuffer &buf2) {
if (!(buf1._header_offset == buf2._header_offset)) return false;
Expand All @@ -49,6 +59,8 @@ RLEBuffer::RLEBuffer(const RLEBuffer &buf) : _header_offset(buf._header_offset),
_header(buf._header),
_data(buf._data),
_bits_per_pixel(buf._bits_per_pixel) {

ensureBuffers();
}

RLEBuffer::~RLEBuffer() {
Expand All @@ -59,6 +71,31 @@ RLEBuffer::~RLEBuffer() {
_data.clear();
}

bool RLEBuffer::decode_line(int y, int buffer_id) const {
ensureBuffers();

if (buffer_id)
return decode_line(y, g_buffer1);
else
return decode_line(y, g_buffer0);
}

const byte *RLEBuffer::get_buffer(int buffer_id) {
ensureBuffers();

if (buffer_id)
return g_buffer1;
else
return g_buffer0;
}

void RLEBuffer::releaseBuffers() {
free(g_buffer0);
g_buffer0 = nullptr;
free(g_buffer1);
g_buffer1 = nullptr;
}

RLEBuffer &RLEBuffer::operator = (const RLEBuffer &buf) {
if (this == &buf) return *this;

Expand Down Expand Up @@ -258,10 +295,12 @@ bool RLEBuffer::convert_data(int bits_per_pixel) {
void RLEBuffer::resize_buffers() {
uint32 len = line_length() * sizeof(uint32);

if (_buffer0.size() < len)
_buffer0.resize(len);
if (_buffer1.size() < len)
_buffer1.resize(len);
if (g_buffersLen < len) {
if (!realloc(g_buffer0, len) || !realloc(g_buffer1, len))
error("RLEBuffer::resize_buffers(): Cannot realloc buffers");

g_buffersLen = len;
}
}

int RLEBuffer::line_length() {
Expand Down
17 changes: 4 additions & 13 deletions engines/qdengine/system/graphics/rle_compress.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,11 @@ class RLEBuffer {

bool decode_line(int y, byte *out_buf) const;

inline bool decode_line(int y, int buffer_id = 0) const {
if (buffer_id)
return decode_line(y, &*_buffer1.begin());
else
return decode_line(y, &*_buffer0.begin());
}
bool decode_line(int y, int buffer_id = 0) const;

bool decode_pixel(int x, int y, uint32 &pixel);

static inline const byte *get_buffer(int buffer_id) {
if (buffer_id) return &*_buffer1.begin();
else return &*_buffer0.begin();
}
static const byte *get_buffer(int buffer_id);

void resize_buffers();

Expand All @@ -76,6 +68,8 @@ class RLEBuffer {

bool convert_data(int bits_per_pixel = 16);

static void releaseBuffers();

private:
Std::vector<uint32> _header_offset;
Std::vector<uint32> _data_offset;
Expand All @@ -85,9 +79,6 @@ class RLEBuffer {

int _bits_per_pixel;

static Std::vector<byte> _buffer0;
static Std::vector<byte> _buffer1;

friend bool operator == (const RLEBuffer &buf1, const RLEBuffer &buf2);
};

Expand Down

0 comments on commit 5c8d742

Please sign in to comment.