From 1c19fb5173c59b8dd7de10af93347bc9d9279e7e Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Wed, 17 Apr 2024 09:19:52 +0200 Subject: Simplify TmwAthena::MessageOut Since for tmwAthena we're writing messages directly into the output buffer, the MessageOut implementation does not need any members. Also used SDL_SwapLE16 and SDL_SwapLE32 for convenience. --- src/net/tmwa/messageout.cpp | 48 +++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 32 deletions(-) (limited to 'src/net/tmwa/messageout.cpp') diff --git a/src/net/tmwa/messageout.cpp b/src/net/tmwa/messageout.cpp index 19f5ee49..12c9419a 100644 --- a/src/net/tmwa/messageout.cpp +++ b/src/net/tmwa/messageout.cpp @@ -23,54 +23,40 @@ #include "net/tmwa/network.h" -#include #include #include namespace TmwAthena { -MessageOut::MessageOut(uint16_t id): - mNetwork(TmwAthena::Network::instance()), - mData(mNetwork->mOutBuffer + mNetwork->mOutSize) +MessageOut::MessageOut(uint16_t id) { writeInt16(id); } -void MessageOut::expand(size_t bytes) +char *MessageOut::expand(size_t bytes) { - mNetwork->mOutSize += bytes; + Network &net = *Network::mInstance; + char *data = net.mOutBuffer + net.mOutSize; + net.mOutSize += bytes; + return data; } void MessageOut::writeInt8(uint8_t value) { - expand(1); - mData[mPos] = value; - mPos += 1; + *expand(1) = value; } void MessageOut::writeInt16(uint16_t value) { - expand(2); -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint16_t swap = SDL_Swap16(value); - memcpy(mData + mPos, &swap, sizeof(uint16_t)); -#else - memcpy(mData + mPos, &value, sizeof(uint16_t)); -#endif - mPos += 2; + value = SDL_SwapLE16(value); + memcpy(expand(sizeof(uint16_t)), &value, sizeof(uint16_t)); } void MessageOut::writeInt32(uint32_t value) { - expand(4); -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint32_t swap = SDL_Swap32(value); - memcpy(mData + mPos, &swap, sizeof(uint32_t)); -#else - memcpy(mData + mPos, &value, sizeof(uint32_t)); -#endif - mPos += 4; + value = SDL_SwapLE32(value); + memcpy(expand(sizeof(uint32_t)), &value, sizeof(uint32_t)); } void MessageOut::writeString(const std::string &string, int length) @@ -87,24 +73,22 @@ void MessageOut::writeString(const std::string &string, int length) // Make sure the length of the string is no longer than specified stringLength = length; } - expand(length); + + char *data = expand(length); // Write the actual string - memcpy(mData + mPos, string.data(), stringLength); + memcpy(data, string.data(), stringLength); // Pad remaining space with zeros if (length > stringLength) { - memset(mData + mPos + stringLength, '\0', length - stringLength); + memset(data + stringLength, '\0', length - stringLength); } - mPos += length; } void MessageOut::writeCoordinates(uint16_t x, uint16_t y, uint8_t direction) { - char *data = mData + mPos; - expand(3); - mPos += 3; + char *data = expand(3); uint16_t temp = x; temp <<= 6; -- cgit v1.2.3-70-g09d2