Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image manager plugin API #16144

Merged
merged 1 commit into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions distribution/changelog.txt
Expand Up @@ -10,6 +10,7 @@
- Feature: [#13858] Flatride bases can be made invisible.
- Feature: [#15367] Individual track elements can now be drawn as another ride type.
- Feature: [#16029] [Plugin] Add TrackElement.rideType to API.
- Feature: [#16144] [Plugin] Add ImageManager to API.
- Improved: [#3517] Cheats are now saved with the park.
- Improved: [#10150] Ride stations are now properly checked if they’re sheltered.
- Improved: [#10664, #16072] Visibility status can be modified directly in the Tile Inspector's list.
Expand Down
20 changes: 20 additions & 0 deletions distribution/openrct2.d.ts
Expand Up @@ -1989,6 +1989,7 @@ declare global {
readonly mainViewport: Viewport;
readonly tileSelection: TileSelection;
readonly tool: Tool | null;
readonly imageManager: ImageManager;

getWindow(id: number): Window;
getWindow(classification: string): Window;
Expand Down Expand Up @@ -2679,4 +2680,23 @@ declare global {
*/
create(name: string): TitleSequence;
}

interface ImageManager {
/**
* Gets the image index range for a predefined set of images.
* @param name The name of the image set.
*/
getPredefinedRange(name: string) : ImageIndexRange | null;

/**
* Gets the list of available ranges of unallocated images.
* Useful for displaying how fragmented the allocated image list is.
*/
getAvailableAllocationRanges(): ImageIndexRange[];
}

interface ImageIndexRange {
start: number;
count: number;
}
}
1 change: 1 addition & 0 deletions src/openrct2-ui/libopenrct2ui.vcxproj
Expand Up @@ -55,6 +55,7 @@
<ClInclude Include="scripting\CustomMenu.h" />
<ClInclude Include="scripting\CustomWindow.h" />
<ClInclude Include="scripting\ScGraphicsContext.hpp" />
<ClInclude Include="scripting\ScImageManager.hpp" />
<ClInclude Include="scripting\ScTileSelection.hpp" />
<ClInclude Include="scripting\ScTitleSequence.hpp" />
<ClInclude Include="scripting\ScUi.hpp" />
Expand Down
87 changes: 87 additions & 0 deletions src/openrct2-ui/scripting/ScImageManager.hpp
@@ -0,0 +1,87 @@
/*****************************************************************************
* Copyright (c) 2014-2021 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/

#pragma once

#ifdef ENABLE_SCRIPTING

# include <openrct2/drawing/Image.h>
# include <openrct2/scripting/Duktape.hpp>
# include <openrct2/sprites.h>

namespace OpenRCT2::Scripting
{
class ScImageManager
{
private:
duk_context* _ctx{};

public:
ScImageManager(duk_context* ctx)
: _ctx(ctx)
{
}

static void Register(duk_context* ctx)
{
dukglue_register_method(ctx, &ScImageManager::getPredefinedRange, "getPredefinedRange");
dukglue_register_method(ctx, &ScImageManager::getAvailableAllocationRanges, "getAvailableAllocationRanges");
}

private:
DukValue getPredefinedRange(const std::string& name) const
{
if (name == "g1")
{
return CreateImageIndexRange(0, SPR_G1_END);
}
else if (name == "g2")
{
return CreateImageIndexRange(SPR_G2_BEGIN, SPR_G2_END - SPR_G2_BEGIN);
}
else if (name == "csg")
{
return CreateImageIndexRange(SPR_CSG_BEGIN, SPR_CSG_END - SPR_CSG_BEGIN);
}
else if (name == "allocated")
{
return CreateImageIndexRange(SPR_IMAGE_LIST_BEGIN, SPR_IMAGE_LIST_END - SPR_IMAGE_LIST_BEGIN);
}
else
{
return ToDuk(_ctx, nullptr);
}
}

DukValue getAvailableAllocationRanges() const
{
auto ranges = GetAvailableAllocationRanges();
duk_push_array(_ctx);
duk_uarridx_t index = 0;
for (const auto& range : ranges)
{
auto value = CreateImageIndexRange(range.BaseId, range.Count);
value.push();
duk_put_prop_index(_ctx, /* duk stack index */ -2, index);
index++;
}
return DukValue::take_from_stack(_ctx);
}

DukValue CreateImageIndexRange(size_t start, size_t count) const
{
DukObject obj(_ctx);
obj.Set("start", static_cast<uint32_t>(start));
obj.Set("count", static_cast<uint32_t>(count));
return obj.Take();
}
};
} // namespace OpenRCT2::Scripting

#endif
7 changes: 7 additions & 0 deletions src/openrct2-ui/scripting/ScUi.hpp
Expand Up @@ -13,6 +13,7 @@

# include "../windows/Window.h"
# include "CustomMenu.h"
# include "ScImageManager.hpp"
# include "ScTileSelection.hpp"
# include "ScViewport.hpp"
# include "ScWindow.hpp"
Expand Down Expand Up @@ -149,6 +150,11 @@ namespace OpenRCT2::Scripting
return {};
}

std::shared_ptr<ScImageManager> imageManager_get() const
{
return std::make_shared<ScImageManager>(_scriptEngine.GetContext());
}

std::shared_ptr<ScWindow> openWindow(DukValue desc)
{
using namespace OpenRCT2::Ui::Windows;
Expand Down Expand Up @@ -345,6 +351,7 @@ namespace OpenRCT2::Scripting
dukglue_register_property(ctx, &ScUi::mainViewport_get, nullptr, "mainViewport");
dukglue_register_property(ctx, &ScUi::tileSelection_get, nullptr, "tileSelection");
dukglue_register_property(ctx, &ScUi::tool_get, nullptr, "tool");
dukglue_register_property(ctx, &ScUi::imageManager_get, nullptr, "imageManager");
dukglue_register_method(ctx, &ScUi::openWindow, "openWindow");
dukglue_register_method(ctx, &ScUi::closeWindows, "closeWindows");
dukglue_register_method(ctx, &ScUi::closeAllWindows, "closeAllWindows");
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2-ui/scripting/UiExtensions.cpp
Expand Up @@ -13,6 +13,7 @@

# include "CustomMenu.h"
# include "ScGraphicsContext.hpp"
# include "ScImageManager.hpp"
# include "ScTileSelection.hpp"
# include "ScTitleSequence.hpp"
# include "ScUi.hpp"
Expand All @@ -31,6 +32,7 @@ void UiScriptExtensions::Extend(ScriptEngine& scriptEngine)
dukglue_register_global(ctx, std::make_shared<ScUi>(scriptEngine), "ui");

ScGraphicsContext::Register(ctx);
ScImageManager::Register(ctx);
ScTileSelection::Register(ctx);
ScTool::Register(ctx);
ScUi::Register(ctx);
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/Context.cpp
Expand Up @@ -38,6 +38,7 @@
#include "core/Path.hpp"
#include "core/String.hpp"
#include "drawing/IDrawingEngine.h"
#include "drawing/Image.h"
#include "drawing/LightFX.h"
#include "entity/EntityRegistry.h"
#include "entity/EntityTweener.h"
Expand Down
5 changes: 0 additions & 5 deletions src/openrct2/drawing/Drawing.h
Expand Up @@ -525,11 +525,6 @@ const rct_g1_element* gfx_get_g1_element(ImageId imageId);
const rct_g1_element* gfx_get_g1_element(ImageIndex image_id);
void gfx_set_g1_element(ImageIndex imageId, const rct_g1_element* g1);
bool is_csg_loaded();
uint32_t gfx_object_allocate_images(const rct_g1_element* images, uint32_t count);
void gfx_object_free_images(uint32_t baseImageId, uint32_t count);
void gfx_object_check_all_images_freed();
size_t ImageListGetUsedCount();
size_t ImageListGetMaximum();
void FASTCALL gfx_sprite_to_buffer(rct_drawpixelinfo& dpi, const DrawSpriteArgs& args);
void FASTCALL gfx_bmp_sprite_to_buffer(rct_drawpixelinfo& dpi, const DrawSpriteArgs& args);
void FASTCALL gfx_rle_sprite_to_buffer(rct_drawpixelinfo& dpi, const DrawSpriteArgs& args);
Expand Down
13 changes: 7 additions & 6 deletions src/openrct2/drawing/Image.cpp
Expand Up @@ -7,6 +7,8 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/

#include "Image.h"

#include "../OpenRCT2.h"
#include "../core/Console.hpp"
#include "../core/Guard.hpp"
Expand All @@ -20,12 +22,6 @@ constexpr uint32_t BASE_IMAGE_ID = SPR_IMAGE_LIST_BEGIN;
constexpr uint32_t MAX_IMAGES = SPR_IMAGE_LIST_END - BASE_IMAGE_ID;
constexpr uint32_t INVALID_IMAGE_ID = UINT32_MAX;

struct ImageList
{
uint32_t BaseId;
uint32_t Count;
};

static bool _initialised = false;
static std::list<ImageList> _freeLists;
static uint32_t _allocatedImageCount;
Expand Down Expand Up @@ -254,3 +250,8 @@ size_t ImageListGetMaximum()
{
return MAX_IMAGES;
}

const std::list<ImageList>& GetAvailableAllocationRanges()
{
return _freeLists;
}
29 changes: 29 additions & 0 deletions src/openrct2/drawing/Image.h
@@ -0,0 +1,29 @@
/*****************************************************************************
* Copyright (c) 2014-2021 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/

#pragma once

#include <cstddef>
#include <cstdint>
#include <list>

struct rct_g1_element;

struct ImageList
{
uint32_t BaseId;
uint32_t Count;
};

uint32_t gfx_object_allocate_images(const rct_g1_element* images, uint32_t count);
void gfx_object_free_images(uint32_t baseImageId, uint32_t count);
void gfx_object_check_all_images_freed();
size_t ImageListGetUsedCount();
size_t ImageListGetMaximum();
const std::list<ImageList>& GetAvailableAllocationRanges();
1 change: 1 addition & 0 deletions src/openrct2/interface/InteractiveConsole.cpp
Expand Up @@ -28,6 +28,7 @@
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Font.h"
#include "../drawing/Image.h"
#include "../entity/EntityList.h"
#include "../entity/EntityRegistry.h"
#include "../entity/Staff.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/BannerObject.cpp
Expand Up @@ -12,6 +12,7 @@
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../localisation/Language.h"
#include "../object/Object.h"
#include "../object/ObjectRepository.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/EntranceObject.cpp
Expand Up @@ -13,6 +13,7 @@
#include "../core/Json.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../localisation/Localisation.h"

void EntranceObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* stream)
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/FootpathItemObject.cpp
Expand Up @@ -12,6 +12,7 @@
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../interface/Cursors.h"
#include "../localisation/Localisation.h"
#include "../object/Object.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/FootpathObject.cpp
Expand Up @@ -12,6 +12,7 @@
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../localisation/Language.h"
#include "../world/Footpath.h"

Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/FootpathRailingsObject.cpp
Expand Up @@ -11,6 +11,7 @@

#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../drawing/Image.h"

void FootpathRailingsObject::Load()
{
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/FootpathSurfaceObject.cpp
Expand Up @@ -11,6 +11,7 @@

#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../drawing/Image.h"
#include "../object/ObjectRepository.h"

void FootpathSurfaceObject::Load()
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/LargeSceneryObject.cpp
Expand Up @@ -15,6 +15,7 @@
#include "../core/Json.hpp"
#include "../core/Memory.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../interface/Cursors.h"
#include "../localisation/Language.h"
#include "../world/Banner.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/RideObject.cpp
Expand Up @@ -19,6 +19,7 @@
#include "../core/Memory.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../localisation/Language.h"
#include "../rct2/DATLimits.h"
#include "../rct2/RCT2.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/SceneryGroupObject.cpp
Expand Up @@ -17,6 +17,7 @@
#include "../core/Memory.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../entity/Staff.h"
#include "../localisation/Language.h"
#include "ObjectManager.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/SmallSceneryObject.cpp
Expand Up @@ -16,6 +16,7 @@
#include "../core/Memory.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../interface/Cursors.h"
#include "../localisation/Language.h"
#include "../world/Scenery.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/StationObject.cpp
Expand Up @@ -13,6 +13,7 @@
#include "../core/Json.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../localisation/Localisation.h"
#include "../world/Banner.h"

Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/TerrainEdgeObject.cpp
Expand Up @@ -14,6 +14,7 @@
#include "../core/Json.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../localisation/Localisation.h"
#include "ObjectManager.h"

Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/TerrainSurfaceObject.cpp
Expand Up @@ -16,6 +16,7 @@
#include "../core/Json.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../localisation/Localisation.h"
#include "../world/Location.hpp"
#include "ObjectManager.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/WallObject.cpp
Expand Up @@ -13,6 +13,7 @@
#include "../core/Json.hpp"
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../drawing/Image.h"
#include "../interface/Cursors.h"
#include "../localisation/Language.h"
#include "../world/Banner.h"
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/object/WaterObject.cpp
Expand Up @@ -14,6 +14,7 @@
#include "../OpenRCT2.h"
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../drawing/Image.h"
#include "../localisation/Language.h"
#include "../localisation/StringIds.h"
#include "../world/Location.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/scripting/ScriptEngine.h
Expand Up @@ -46,7 +46,7 @@ namespace OpenRCT2

namespace OpenRCT2::Scripting
{
static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 40;
static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 41;

// Versions marking breaking changes.
static constexpr int32_t API_VERSION_33_PEEP_DEPRECATION = 33;
Expand Down