Skip to content

Commit 0b06299

Browse files
committed
Merge branch 'main' into json-as-a-resource
2 parents 6982e3e + d70fd7d commit 0b06299

12 files changed

+127
-47
lines changed

extern/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
118118
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES
119119
)
120120
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
121-
if (DEFINED USE_OPENGLES)
121+
if (USE_OPENGLES)
122122
target_link_libraries(ImGui PUBLIC ${OPENGL_GLESv2_LIBRARY})
123123
add_compile_definitions(IMGUI_IMPL_OPENGL_ES3)
124124
else()

src/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ set(Source_Files__Window__Gui
251251
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/Font.cpp
252252
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/FontFactory.h
253253
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/FontFactory.cpp
254+
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTexture.h
255+
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTexture.cpp
256+
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTextureFactory.h
257+
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTextureFactory.cpp
254258
)
255259

256260
source_group("window/gui" FILES ${Source_Files__Window__Gui})

src/window/gui/GameOverlay.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void GameOverlay::LoadFont(const std::string& name, const std::string& path, flo
2626
std::shared_ptr<Font> font = std::static_pointer_cast<Font>(
2727
Context::GetInstance()->GetResourceManager()->LoadResource(path, false, initData));
2828
if (font != nullptr) {
29-
mFonts[name] = io.Fonts->AddFontFromMemoryTTF(font->Data.data(), font->Data.size(), fontSize);
29+
mFonts[name] = io.Fonts->AddFontFromMemoryTTF(font->Data, font->DataSize, fontSize);
3030
}
3131
}
3232

src/window/gui/Gui.cpp

+16-28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "resource/File.h"
1616
#include <stb/stb_image.h>
1717
#include "window/gui/Fonts.h"
18+
#include "window/gui/resource/GuiTextureFactory.h"
1819

1920
#ifdef __WIIU__
2021
#include <gx2/registers.h> // GX2SetViewport / GX2SetScissor
@@ -150,6 +151,10 @@ void Gui::Init(GuiWindowInitData windowImpl) {
150151
GetGuiWindow("Console")->Init();
151152
GetGameOverlay()->Init();
152153

154+
Context::GetInstance()->GetResourceManager()->GetResourceLoader()->RegisterResourceFactory(
155+
std::make_shared<ResourceFactoryBinaryGuiTextureV0>(), RESOURCE_FORMAT_BINARY, "GuiTexture",
156+
static_cast<uint32_t>(RESOURCE_TYPE_GUI_TEXTURE), 0);
157+
153158
ImGuiWMInit();
154159
ImGuiBackendInit();
155160
#ifdef __SWITCH__
@@ -230,39 +235,22 @@ void Gui::ImGuiBackendInit() {
230235
}
231236

232237
void Gui::LoadTextureFromRawImage(const std::string& name, const std::string& path) {
233-
// GuiTexture (put in Gui)
234-
const auto res = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFileRaw(path);
235-
236-
if (!res) {
237-
SPDLOG_ERROR("Failed to load resource");
238-
return;
239-
}
240-
if (!res->Buffer || res->Buffer->empty()) {
241-
SPDLOG_ERROR("Buffer is null or empty");
242-
return;
243-
}
244-
245-
GuiTexture asset;
246-
asset.Width = 0;
247-
asset.Height = 0;
248-
uint8_t* imgData = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(res->Buffer->data()), res->Buffer->size(),
249-
&asset.Width, &asset.Height, nullptr, 4);
250-
251-
if (imgData == nullptr) {
252-
SPDLOG_ERROR("Error loading imgui texture {}", stbi_failure_reason());
253-
return;
254-
}
238+
auto initData = std::make_shared<ResourceInitData>();
239+
initData->Format = RESOURCE_FORMAT_BINARY;
240+
initData->Type = static_cast<uint32_t>(RESOURCE_TYPE_GUI_TEXTURE);
241+
initData->ResourceVersion = 0;
242+
auto guiTexture = std::static_pointer_cast<GuiTexture>(
243+
Context::GetInstance()->GetResourceManager()->LoadResource(path, false, initData));
255244

256245
GfxRenderingAPI* api = gfx_get_current_rendering_api();
257246

258247
// TODO: Nothing ever unloads the texture from Fast3D here.
259-
asset.RendererTextureId = api->new_texture();
260-
api->select_texture(0, asset.RendererTextureId);
248+
guiTexture->Metadata.RendererTextureId = api->new_texture();
249+
api->select_texture(0, guiTexture->Metadata.RendererTextureId);
261250
api->set_sampler_parameters(0, false, 0, 0);
262-
api->upload_texture(imgData, asset.Width, asset.Height);
251+
api->upload_texture(guiTexture->Data, guiTexture->Metadata.Width, guiTexture->Metadata.Height);
263252

264-
mGuiTextures[name] = asset;
265-
stbi_image_free(imgData);
253+
mGuiTextures[name] = guiTexture->Metadata;
266254
}
267255

268256
bool Gui::SupportsViewports() {
@@ -832,7 +820,7 @@ void Gui::LoadGuiTexture(const std::string& name, const std::string& path, const
832820
texBuffer[pixel * 4 + 3] *= tint.w;
833821
}
834822

835-
GuiTexture asset;
823+
GuiTextureMetadata asset;
836824
asset.RendererTextureId = api->new_texture();
837825
asset.Width = res->Width;
838826
asset.Height = res->Height;

src/window/gui/Gui.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "window/gui/GuiWindow.h"
1919
#include "window/gui/GuiMenuBar.h"
2020
#include "libultraship/libultra/controller.h"
21+
#include "window/gui/resource/GuiTexture.h"
2122

2223
namespace LUS {
2324

@@ -100,18 +101,12 @@ class Gui {
100101
int16_t GetIntegerScaleFactor();
101102

102103
private:
103-
struct GuiTexture {
104-
uint32_t RendererTextureId;
105-
int32_t Width;
106-
int32_t Height;
107-
};
108-
109104
GuiWindowInitData mImpl;
110105
ImGuiIO* mImGuiIo;
111106
bool mNeedsConsoleVariableSave;
112107
std::shared_ptr<GameOverlay> mGameOverlay;
113108
std::shared_ptr<GuiMenuBar> mMenuBar;
114-
std::map<std::string, GuiTexture> mGuiTextures;
109+
std::map<std::string, GuiTextureMetadata> mGuiTextures;
115110
std::map<std::string, std::shared_ptr<GuiWindow>> mGuiWindows;
116111
};
117112
} // namespace LUS

src/window/gui/resource/Font.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ namespace LUS {
44
Font::Font() : Resource(std::shared_ptr<ResourceInitData>()) {
55
}
66

7+
Font::~Font() {
8+
if (Data != nullptr) {
9+
delete Data;
10+
}
11+
}
12+
713
void* Font::GetPointer() {
8-
return Data.data();
14+
return Data;
915
}
1016

1117
size_t Font::GetPointerSize() {
12-
return Data.size() * sizeof(char);
18+
return DataSize * sizeof(char);
1319
}
1420
} // namespace LUS

src/window/gui/resource/Font.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
#include "resource/Resource.h"
44

55
namespace LUS {
6-
#define RESOURCE_TYPE_FONT 0x464F4E54
6+
#define RESOURCE_TYPE_FONT 0x464F4E54 // FONT
77

88
class Font : public Resource<void> {
99
public:
1010
using Resource::Resource;
1111

1212
Font();
13+
~Font();
1314

1415
void* GetPointer() override;
1516
size_t GetPointerSize() override;
1617

17-
std::vector<char> Data;
18+
char* Data = nullptr;
19+
size_t DataSize;
1820
};
1921
}; // namespace LUS

src/window/gui/resource/FontFactory.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ std::shared_ptr<IResource> ResourceFactoryBinaryFontV0::ReadResource(std::shared
1111
auto font = std::make_shared<Font>(file->InitData);
1212
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
1313

14-
uint32_t dataSize = file->Buffer->size();
14+
font->DataSize = file->Buffer->size();
1515

16-
font->Data.reserve(dataSize);
17-
18-
for (uint32_t i = 0; i < dataSize; i++) {
19-
font->Data.push_back(reader->ReadChar());
20-
}
16+
font->Data = new char[font->DataSize];
17+
reader->Read(font->Data, font->DataSize);
2118

2219
return font;
2320
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "GuiTexture.h"
2+
3+
namespace LUS {
4+
GuiTexture::GuiTexture() : Resource(std::shared_ptr<ResourceInitData>()) {
5+
}
6+
7+
GuiTexture::~GuiTexture() {
8+
if (Data != nullptr) {
9+
stbi_image_free(Data);
10+
}
11+
}
12+
13+
void* GuiTexture::GetPointer() {
14+
return Data;
15+
}
16+
17+
size_t GuiTexture::GetPointerSize() {
18+
return DataSize * sizeof(uint8_t);
19+
}
20+
} // namespace LUS

src/window/gui/resource/GuiTexture.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include "resource/Resource.h"
4+
#include <stb/stb_image.h>
5+
6+
namespace LUS {
7+
#define RESOURCE_TYPE_GUI_TEXTURE 0x47544558 // GTEX
8+
9+
struct GuiTextureMetadata {
10+
uint32_t RendererTextureId;
11+
int32_t Width;
12+
int32_t Height;
13+
};
14+
15+
class GuiTexture : public Resource<void> {
16+
public:
17+
using Resource::Resource;
18+
19+
GuiTexture();
20+
~GuiTexture();
21+
22+
void* GetPointer() override;
23+
size_t GetPointerSize() override;
24+
25+
uint8_t* Data;
26+
size_t DataSize;
27+
GuiTextureMetadata Metadata;
28+
};
29+
}; // namespace LUS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "window/gui/resource/GuiTextureFactory.h"
2+
#include "window/gui/resource/GuiTexture.h"
3+
#include "spdlog/spdlog.h"
4+
5+
namespace LUS {
6+
std::shared_ptr<IResource> ResourceFactoryBinaryGuiTextureV0::ReadResource(std::shared_ptr<File> file) {
7+
if (!FileHasValidFormatAndReader(file)) {
8+
return nullptr;
9+
}
10+
11+
auto guiTexture = std::make_shared<GuiTexture>(file->InitData);
12+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
13+
14+
guiTexture->DataSize = file->Buffer->size();
15+
guiTexture->Metadata.Width = 0;
16+
guiTexture->Metadata.Height = 0;
17+
guiTexture->Data =
18+
stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(file->Buffer->data()), guiTexture->DataSize,
19+
&guiTexture->Metadata.Width, &guiTexture->Metadata.Height, nullptr, 4);
20+
21+
if (guiTexture->Data == nullptr) {
22+
SPDLOG_ERROR("Error loading imgui texture {}", stbi_failure_reason());
23+
return nullptr;
24+
}
25+
26+
return guiTexture;
27+
}
28+
} // namespace LUS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "resource/Resource.h"
4+
#include "resource/ResourceFactoryBinary.h"
5+
6+
namespace LUS {
7+
class ResourceFactoryBinaryGuiTextureV0 : public ResourceFactoryBinary {
8+
public:
9+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
10+
};
11+
}; // namespace LUS

0 commit comments

Comments
 (0)