Load widget resources from lumps

Add banner
This commit is contained in:
Magnus Norddahl 2023-12-28 18:39:04 +01:00 committed by Christoph Oelckers
commit 71ff4d3685
10 changed files with 667 additions and 63 deletions

View file

@ -95,20 +95,7 @@ LauncherWindow::LauncherWindow(WadStuff* wads, int numwads, int defaultiwad, int
GamesList->AddItem(work.GetChars());
}
/*try
{
auto filedata = ReadAllBytes("C:/Development/ZWidget/example/banner.png");
std::vector<unsigned char> pixels;
unsigned long width = 0, height = 0;
int result = decodePNG(pixels, width, height, (const unsigned char*)filedata.data(), filedata.size(), true);
if (result == 0)
{
Logo->SetImage(Image::Create(width, height, ImageFormat::R8G8B8A8, pixels.data()));
}
}
catch (...)
{
}*/
Logo->SetImage(Image::LoadResource("banner.png"));
}
void LauncherWindow::OnGeometryChanged()

View file

@ -1,62 +1,41 @@
#include <zwidget/core/resourcedata.h>
#include "filesystem.h"
#include "printf.h"
// To do: this should load data from lumps
FResourceFile* WidgetResources;
#ifdef WIN32
#include <Windows.h>
#include <stdexcept>
static std::wstring to_utf16(const std::string& str)
void InitWidgetResources(const char* filename)
{
if (str.empty()) return {};
int needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);
if (needed == 0)
throw std::runtime_error("MultiByteToWideChar failed");
std::wstring result;
result.resize(needed);
needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &result[0], (int)result.size());
if (needed == 0)
throw std::runtime_error("MultiByteToWideChar failed");
return result;
WidgetResources = FResourceFile::OpenResourceFile(filename);
if (!WidgetResources)
I_FatalError("Unable to open %s", filename);
}
static std::vector<uint8_t> ReadAllBytes(const std::string& filename)
std::vector<uint8_t> LoadWidgetFontData(const std::string& name)
{
HANDLE handle = CreateFile(to_utf16(filename).c_str(), 0x0001/*FILE_READ_ACCESS*/, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (handle == INVALID_HANDLE_VALUE)
throw std::runtime_error("Could not open " + filename);
LARGE_INTEGER fileSize;
BOOL result = GetFileSizeEx(handle, &fileSize);
if (result == FALSE)
{
CloseHandle(handle);
throw std::runtime_error("GetFileSizeEx failed");
}
std::vector<uint8_t> buffer(fileSize.QuadPart);
DWORD bytesRead = 0;
result = ReadFile(handle, buffer.data(), (DWORD)buffer.size(), &bytesRead, nullptr);
if (result == FALSE || bytesRead != buffer.size())
{
CloseHandle(handle);
throw std::runtime_error("ReadFile failed");
}
CloseHandle(handle);
auto lump = WidgetResources->FindLump("newmenufont.ttf");
if (!lump)
I_FatalError("Unable to find %s", name.c_str());
auto data = lump->Lock();
std::vector<uint8_t> buffer;
buffer.resize(lump->Size());
memcpy(buffer.data(), data, buffer.size());
lump->Unlock();
return buffer;
}
std::vector<uint8_t> LoadWidgetFontData(const std::string& name)
std::vector<uint8_t> LoadWidgetImageData(const std::string& name)
{
return ReadAllBytes("C:\\Windows\\Fonts\\segoeui.ttf");
}
auto lump = WidgetResources->FindLump(name.c_str());
if (!lump)
I_FatalError("Unable to find %s", name.c_str());
#else
std::vector<uint8_t> LoadWidgetFontData(const std::string& name)
{
return {};
auto data = lump->Lock();
std::vector<uint8_t> buffer;
buffer.resize(lump->Size());
memcpy(buffer.data(), data, buffer.size());
lump->Unlock();
return buffer;
}
#endif

View file

@ -159,6 +159,7 @@ void I_UpdateDiscordPresence(bool SendPresence, const char* curstatus, const cha
bool M_SetSpecialMenu(FName& menu, int param); // game specific checks
const FIWADInfo *D_FindIWAD(TArray<FString> &wadfiles, const char *iwad, const char *basewad);
void InitWidgetResources(const char* basewad);
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
@ -3672,6 +3673,7 @@ static int D_DoomMain_Internal (void)
I_FatalError("Cannot find " BASEWAD);
}
LoadHexFont(wad); // load hex font early so we have it during startup.
InitWidgetResources(wad);
C_InitConsole(80*8, 25*8, false);
I_DetectOS();