Files
OpenRCT2-Web/src/openrct2/object/WaterObject.cpp
T

145 lines
4.5 KiB
C++
Raw Normal View History

2016-06-26 00:31:16 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2016-06-26 00:31:16 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-06-26 00:31:16 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-06-26 00:31:16 +01:00
*****************************************************************************/
2017-12-14 13:28:08 +00:00
#pragma warning(disable : 4706) // assignment within conditional expression
2018-06-22 23:03:20 +02:00
#include "WaterObject.h"
#include "../OpenRCT2.h"
2021-12-11 18:06:06 -05:00
#include "../common.h"
2016-06-26 00:31:16 +01:00
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
2021-12-07 20:55:27 +00:00
#include "../drawing/Image.h"
2021-12-11 18:06:06 -05:00
#include "../localisation/Formatter.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Language.h"
#include "../localisation/StringIds.h"
#include "../world/Location.hpp"
2016-06-26 00:31:16 +01:00
2021-12-11 18:06:06 -05:00
#include <array>
#include <cstring>
2018-06-22 23:03:20 +02:00
#include <memory>
void WaterObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* stream)
2016-06-26 00:31:16 +01:00
{
stream->Seek(14, OpenRCT2::STREAM_SEEK_CURRENT);
_legacyType.flags = stream->ReadValue<uint16_t>();
2016-06-26 00:31:16 +01:00
GetStringTable().Read(context, stream, ObjectStringID::NAME);
2017-12-11 13:06:59 +00:00
GetImageTable().Read(context, stream);
2016-06-26 00:31:16 +01:00
}
void WaterObject::Load()
{
2017-12-11 13:06:59 +00:00
GetStringTable().Sort();
_legacyType.string_idx = LanguageAllocateObjectString(GetName());
_legacyType.image_id = GfxObjectAllocateImages(GetImageTable().GetImages(), GetImageTable().GetCount());
2017-02-02 18:13:51 +00:00
_legacyType.palette_index_1 = _legacyType.image_id + 1;
_legacyType.palette_index_2 = _legacyType.image_id + 4;
2016-06-26 00:31:16 +01:00
LoadPalette();
2016-06-26 00:31:16 +01:00
}
void WaterObject::Unload()
{
GfxObjectFreeImages(_legacyType.image_id, GetImageTable().GetCount());
LanguageFreeObjectString(_legacyType.string_idx);
_legacyType.string_idx = 0;
_legacyType.image_id = 0;
2016-06-26 00:31:16 +01:00
}
void WaterObject::DrawPreview(DrawPixelInfo* dpi, int32_t width, int32_t height) const
2016-07-02 10:59:50 +01:00
{
// Write (no image)
auto screenCoords = ScreenCoordsXY{ width / 2, height / 2 };
DrawTextBasic(dpi, screenCoords, STR_WINDOW_NO_IMAGE, {}, { TextAlignment::CENTRE });
2016-07-02 10:59:50 +01:00
}
2017-12-14 13:28:08 +00:00
2020-08-12 23:05:23 +01:00
void WaterObject::ReadJson([[maybe_unused]] IReadObjectContext* context, json_t& root)
2017-12-14 13:28:08 +00:00
{
2020-08-12 23:05:23 +01:00
Guard::Assert(root.is_object(), "WaterObject::ReadJson expects parameter root to be object");
2017-12-14 13:28:08 +00:00
2020-08-12 23:05:23 +01:00
auto properties = root["properties"];
2017-12-14 13:28:08 +00:00
2020-08-12 23:05:23 +01:00
PopulateTablesFromJson(context, root);
if (properties.is_object())
2017-12-14 13:28:08 +00:00
{
2020-08-12 23:05:23 +01:00
_legacyType.flags = Json::GetFlags<uint16_t>(
properties,
2017-12-14 13:28:08 +00:00
{
2020-08-12 23:05:23 +01:00
{ "allowDucks", WATER_FLAGS_ALLOW_DUCKS },
});
auto jPalettes = properties["palettes"];
if (jPalettes.is_object())
{
// Images which are actually palette data
static const char* paletteNames[] = {
"general", "waves-0", "waves-1", "waves-2", "sparkles-0", "sparkles-1", "sparkles-2",
};
for (auto paletteName : paletteNames)
{
auto jPalette = jPalettes[paletteName];
if (jPalette.is_object())
{
ReadJsonPalette(jPalette);
}
2017-12-14 13:28:08 +00:00
}
}
}
}
2020-08-12 23:05:23 +01:00
void WaterObject::ReadJsonPalette(json_t& jPalette)
2017-12-14 13:28:08 +00:00
{
2020-08-12 23:05:23 +01:00
Guard::Assert(jPalette.is_object(), "WaterObject::ReadJsonPalette expects parameter jPalette to be object");
2017-12-14 13:28:08 +00:00
2020-08-12 23:05:23 +01:00
auto jColours = jPalette["colours"];
auto numColours = jColours.size();
// This pointer gets memcopied in ImageTable::AddImage so it's fine for the unique_ptr to go out of scope
auto data = std::make_unique<uint8_t[]>(numColours * 3);
2017-12-14 13:28:08 +00:00
size_t dataIndex = 0;
2020-08-12 23:05:23 +01:00
for (auto& jColour : jColours)
2017-12-14 13:28:08 +00:00
{
2020-08-12 23:05:23 +01:00
if (jColour.is_string())
2017-12-14 13:28:08 +00:00
{
2020-08-12 23:05:23 +01:00
auto colour = ParseColour(Json::GetString(jColour));
2017-12-14 17:35:34 +00:00
data[dataIndex + 0] = (colour >> 16) & 0xFF;
2017-12-14 13:28:08 +00:00
data[dataIndex + 1] = (colour >> 8) & 0xFF;
2017-12-14 17:35:34 +00:00
data[dataIndex + 2] = colour & 0xFF;
2017-12-14 13:28:08 +00:00
}
dataIndex += 3;
}
G1Element g1 = {};
2017-12-14 13:28:08 +00:00
g1.offset = data.get();
2020-04-22 17:09:29 +02:00
g1.width = static_cast<int16_t>(numColours);
2020-08-12 23:05:23 +01:00
g1.x_offset = Json::GetNumber<int16_t>(jPalette["index"]);
2017-12-14 17:35:34 +00:00
g1.flags = G1_FLAG_PALETTE;
2017-12-14 13:28:08 +00:00
2018-06-22 23:03:20 +02:00
auto& imageTable = GetImageTable();
2017-12-14 13:28:08 +00:00
imageTable.AddImage(&g1);
}
2018-06-22 23:03:20 +02:00
uint32_t WaterObject::ParseColour(const std::string& s) const
2017-12-14 13:28:08 +00:00
{
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
2017-12-14 13:28:08 +00:00
if (s[0] == '#' && s.size() == 7)
{
// Expect #RRGGBB
r = std::stoul(s.substr(1, 2), nullptr, 16) & 0xFF;
g = std::stoul(s.substr(3, 2), nullptr, 16) & 0xFF;
b = std::stoul(s.substr(5, 2), nullptr, 16) & 0xFF;
}
return (b << 16) | (g << 8) | r;
}