Skip to content

Commit d70fd7d

Browse files
authored
GuiTexture resource (Kenix3#456)
1 parent f0f91b4 commit d70fd7d

8 files changed

+110
-38
lines changed

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/Gui.cpp

+16-27
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,38 +235,22 @@ void Gui::ImGuiBackendInit() {
230235
}
231236

232237
void Gui::LoadTextureFromRawImage(const std::string& name, const std::string& path) {
233-
const auto res = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFileRaw(path);
234-
235-
if (!res) {
236-
SPDLOG_ERROR("Failed to load resource");
237-
return;
238-
}
239-
if (!res->Buffer || res->Buffer->empty()) {
240-
SPDLOG_ERROR("Buffer is null or empty");
241-
return;
242-
}
243-
244-
GuiTexture asset;
245-
asset.Width = 0;
246-
asset.Height = 0;
247-
uint8_t* imgData = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(res->Buffer->data()), res->Buffer->size(),
248-
&asset.Width, &asset.Height, nullptr, 4);
249-
250-
if (imgData == nullptr) {
251-
SPDLOG_ERROR("Error loading imgui texture {}", stbi_failure_reason());
252-
return;
253-
}
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));
254244

255245
GfxRenderingAPI* api = gfx_get_current_rendering_api();
256246

257247
// TODO: Nothing ever unloads the texture from Fast3D here.
258-
asset.RendererTextureId = api->new_texture();
259-
api->select_texture(0, asset.RendererTextureId);
248+
guiTexture->Metadata.RendererTextureId = api->new_texture();
249+
api->select_texture(0, guiTexture->Metadata.RendererTextureId);
260250
api->set_sampler_parameters(0, false, 0, 0);
261-
api->upload_texture(imgData, asset.Width, asset.Height);
251+
api->upload_texture(guiTexture->Data, guiTexture->Metadata.Width, guiTexture->Metadata.Height);
262252

263-
mGuiTextures[name] = asset;
264-
stbi_image_free(imgData);
253+
mGuiTextures[name] = guiTexture->Metadata;
265254
}
266255

267256
bool Gui::SupportsViewports() {
@@ -831,7 +820,7 @@ void Gui::LoadGuiTexture(const std::string& name, const std::string& path, const
831820
texBuffer[pixel * 4 + 3] *= tint.w;
832821
}
833822

834-
GuiTexture asset;
823+
GuiTextureMetadata asset;
835824
asset.RendererTextureId = api->new_texture();
836825
asset.Width = res->Width;
837826
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/FontFactory.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ std::shared_ptr<IResource> ResourceFactoryBinaryFontV0::ReadResource(std::shared
1616
font->Data = new char[font->DataSize];
1717
reader->Read(font->Data, font->DataSize);
1818

19-
// for (uint32_t i = 0; i < dataSize; i++) {
20-
// font->Data.push_back(reader->ReadChar());
21-
// }
22-
2319
return font;
2420
}
2521
} // namespace LUS
+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)