Files
XCEngine/new_editor/app/Support/EmbeddedPngLoader.h

74 lines
1.9 KiB
C++

#pragma once
#include <Rendering/Native/NativeRenderer.h>
#include <XCEngine/UI/Types.h>
#include <cstddef>
#include <cstdint>
#include <string>
#include <windows.h>
namespace XCEngine::UI::Editor::Support {
inline bool LoadEmbeddedPngBytes(
UINT resourceId,
const std::uint8_t*& outData,
std::size_t& outSize,
std::string& outError) {
outData = nullptr;
outSize = 0u;
outError.clear();
HMODULE module = GetModuleHandleW(nullptr);
if (module == nullptr) {
outError = "GetModuleHandleW(nullptr) returned null.";
return false;
}
HRSRC resource = FindResourceW(module, MAKEINTRESOURCEW(resourceId), L"PNG");
if (resource == nullptr) {
outError = "FindResourceW failed.";
return false;
}
HGLOBAL resourceData = LoadResource(module, resource);
if (resourceData == nullptr) {
outError = "LoadResource failed.";
return false;
}
const DWORD resourceSize = SizeofResource(module, resource);
if (resourceSize == 0u) {
outError = "SizeofResource returned zero.";
return false;
}
const void* lockedBytes = LockResource(resourceData);
if (lockedBytes == nullptr) {
outError = "LockResource failed.";
return false;
}
outData = reinterpret_cast<const std::uint8_t*>(lockedBytes);
outSize = static_cast<std::size_t>(resourceSize);
return true;
}
inline bool LoadEmbeddedPngTexture(
Host::NativeRenderer& renderer,
UINT resourceId,
::XCEngine::UI::UITextureHandle& outTexture,
std::string& outError) {
const std::uint8_t* bytes = nullptr;
std::size_t byteCount = 0u;
if (!LoadEmbeddedPngBytes(resourceId, bytes, byteCount, outError)) {
return false;
}
return renderer.LoadTextureFromMemory(bytes, byteCount, outTexture, outError);
}
} // namespace XCEngine::UI::Editor::Support