diff --git a/openVulkanoCpp/Host/Windows/EmbeddedResourceLoaderWindows.cpp b/openVulkanoCpp/Host/Windows/EmbeddedResourceLoaderWindows.cpp new file mode 100644 index 0000000..9ea8756 --- /dev/null +++ b/openVulkanoCpp/Host/Windows/EmbeddedResourceLoaderWindows.cpp @@ -0,0 +1,80 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "EmbeddedResourceLoaderWindows.hpp" +#include "Base/Utils.hpp" +#include "Base/Logger.hpp" +#include "../resources/resource.h" +#include +#include +#include + + +namespace OpenVulkano +{ + namespace + { + void* HANDLE = ResourceLoader::RegisterResourceLoader(std::make_unique()); + } + + std::string EmbeddedResourceLoaderWindows::GetResourcePath(const std::string& resourceName) + { + return ""; + } + + Array EmbeddedResourceLoaderWindows::GetResource(const std::string& resourceName) + { + return {}; + //return GetResource(resourceName, GetResourceType(resourceName)); + } + + //LPSTR EmbeddedResourceLoaderWindows::GetResourceType(const std::string& resourceName) const + //{ + // std::filesystem::path path(resourceName); + // std::string ext = path.extension().string(); + // if (ext.empty() || ext == ".") + // { + // Logger::APP->warn("Could not load embedded resource {}. Unsupported extension", resourceName, ext); + // return {}; + // } + // if (ext == ".ico" || ext == ".icon") + // { + // return RT_ICON; + // } + // else if (ext == ".ttf" || ext == ".fnt" || ext == ".font") + // { + // return RT_FONT; + // } + // // custom raw data + // return RT_RCDATA; + //} + + Array EmbeddedResourceLoaderWindows::GetResource(LPSTR resId, LPSTR resourceType) const + { + HMODULE exe = GetModuleHandleA(NULL); + LPCSTR resourceName = resId; + HRSRC hRes = FindResourceA(exe, resourceName, resourceType); + if (!hRes) + { + Logger::APP->warn("Could not find embedded resource"); + return {}; + } + + HGLOBAL hResData = LoadResource(exe, hRes); + if (!hResData) + { + Logger::APP->warn("Embedded resource found, but could not load."); + return {}; + } + + void* data = LockResource(hResData); + DWORD size = SizeofResource(exe, hRes); + Array dst(size); + std::memcpy(dst.Data(), data, size); + return dst; + } + +} \ No newline at end of file diff --git a/openVulkanoCpp/Host/Windows/EmbeddedResourceLoaderWindows.hpp b/openVulkanoCpp/Host/Windows/EmbeddedResourceLoaderWindows.hpp new file mode 100644 index 0000000..c2a300f --- /dev/null +++ b/openVulkanoCpp/Host/Windows/EmbeddedResourceLoaderWindows.hpp @@ -0,0 +1,23 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "Host/ResourceLoader.hpp" +#include + +namespace OpenVulkano +{ + class EmbeddedResourceLoaderWindows final : public ResourceLoader + { + public: + std::string GetResourcePath(const std::string& resourceName) override; + Array GetResource(const std::string& resourceName) override; + Array GetResource(char* resId, char* resourceType) const; + private: + char* GetResourceType(const std::string& resourceName) const; + }; +} \ No newline at end of file diff --git a/resources/icon.rc b/resources/icon.rc new file mode 100644 index 0000000..d0aa0e0 --- /dev/null +++ b/resources/icon.rc @@ -0,0 +1,62 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "winres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (United States) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 9, 1 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""winres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + +#endif // English (United States) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + +IDI_MADVOXEL_ICON ICON "madvoxel_icon.ico" +MADVOXEL_ICON ICON "madvoxel_icon.ico" diff --git a/resources/madvoxel_icon.ico b/resources/madvoxel_icon.ico new file mode 100644 index 0000000..c178264 Binary files /dev/null and b/resources/madvoxel_icon.ico differ diff --git a/resources/resource.h b/resources/resource.h new file mode 100644 index 0000000..2c4237e --- /dev/null +++ b/resources/resource.h @@ -0,0 +1 @@ +#define IDI_MADVOXEL_ICON 101 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 297f739..e38cd8f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,12 +2,16 @@ cmake_minimum_required(VERSION 3.28 FATAL_ERROR) include(SetupVulkan) include(Utils) +include(Filter) -file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") +cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH ROOT_FOLDER) +file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp" + "${ROOT_FOLDER}/resources/*.rc" "${ROOT_FOLDER}/resources/*.h") + +FilterPlatformPaths(SOURCES) if (NOT ENABLE_CURL) list(FILTER SOURCES EXCLUDE REGEX "WebResourceLoader") endif() -source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${SOURCES}) add_executable(OpenVulkano_Tests ${SOURCES}) target_include_directories(OpenVulkano_Tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/tests/Host/Windows/ResourceLoaderTests.cpp b/tests/Host/Windows/ResourceLoaderTests.cpp new file mode 100644 index 0000000..4e92ae1 --- /dev/null +++ b/tests/Host/Windows/ResourceLoaderTests.cpp @@ -0,0 +1,35 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "Host/Windows/EmbeddedResourceLoaderWindows.hpp" +#include "Base/Logger.hpp" +#include "../resources/resource.h" +#include +#include +#include + +using namespace OpenVulkano; + +TEST_CASE("Load icon") +{ + Logger::SetupLogger("", "tests.log"); + EmbeddedResourceLoaderWindows loader; + + // the icon is split into RT_GROUP_ICON and RT_ICON. resource.h defines RT_GROUP_ICON entry + // RT_GROUP_ICON data + Array groupData = loader.GetResource(MAKEINTRESOURCE(IDI_MADVOXEL_ICON), RT_GROUP_ICON); + + // load resource that has no numeric ID + Array groupData2 = loader.GetResource("MADVOXEL_ICON", RT_GROUP_ICON); + REQUIRE((!groupData.Empty() && groupData.Size() == groupData2.Size())); + + // ICON data + Array iconData = loader.GetResource(MAKEINTRESOURCE(1), RT_ICON); + REQUIRE((!iconData.Empty() && iconData.Size() == 194344)); + +}