Embedded Resources
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -48,5 +48,7 @@
|
||||
imgui.ini
|
||||
|
||||
.cache/
|
||||
__pycache__/
|
||||
|
||||
out/
|
||||
Build/
|
||||
@ -9,6 +9,8 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
|
||||
project(IAEngine)
|
||||
|
||||
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
message(STATUS "Configuring IAEngine for Debug..")
|
||||
#add_compile_options(-O0)
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
set(SRC_FILES
|
||||
"imp/cpp/IAEngine.cpp"
|
||||
|
||||
"imp/cpp/EmbeddedResources.cpp"
|
||||
)
|
||||
|
||||
add_library(IAEngine STATIC ${SRC_FILES})
|
||||
|
||||
60
Src/IAEngine/imp/cpp/EmbeddedResources.cpp
Normal file
60
Src/IAEngine/imp/cpp/EmbeddedResources.cpp
Normal file
File diff suppressed because one or more lines are too long
34
Src/IAEngine/imp/hpp/EmbeddedResources.hpp
Normal file
34
Src/IAEngine/imp/hpp/EmbeddedResources.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
// IAEngine: 2D Game Engine by IA
|
||||
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <IAEngine/Base.hpp>
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
class EmbeddedResources
|
||||
{
|
||||
public:
|
||||
STATIC VOID Initialize();
|
||||
STATIC VOID Terminate();
|
||||
|
||||
STATIC CONST Vector<UINT8>& GetResource(IN CONST String& name);
|
||||
|
||||
private:
|
||||
STATIC Map<String, Vector<UINT8>> s_resources;
|
||||
};
|
||||
}
|
||||
@ -16,7 +16,22 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <IACore/Exception.hpp>
|
||||
#include <IACore/Logger.hpp>
|
||||
#include <IACore/Map.hpp>
|
||||
#include <IACore/Memory.hpp>
|
||||
#include <IACore/String.hpp>
|
||||
#include <IACore/Vector.hpp>
|
||||
|
||||
#define IAE_LOG_TAG "IAE"
|
||||
|
||||
#define IAE_LOG_INFO(...) ia::Logger::Info(IAE_LOG_TAG, __VA_ARGS__)
|
||||
#define IAE_LOG_WARN(...) ia::Logger::Warn(IAE_LOG_TAG, __VA_ARGS__)
|
||||
#define IAE_LOG_ERROR(...) ia::Logger::Error(IAE_LOG_TAG, __VA_ARGS__)
|
||||
#define IAE_LOG_SUCCESS(...) ia::Logger::Success(IAE_LOG_TAG, __VA_ARGS__)
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
|
||||
using Handle = INT64;
|
||||
STATIC CONSTEXPR Handle INVALID_HANDLE = -1;
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ import os
|
||||
import sys
|
||||
|
||||
def main(args: list[str]):
|
||||
os.system("cmake -S. -B./Build/Windows -DCMAKE_BUILD_TYPE=Debug -DBUILD_SAMPLES=ON")
|
||||
os.system("cmake -S. -B./Build/Windows -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug -DBUILD_SAMPLES=ON")
|
||||
os.system("cmake --build ./Build/Windows")
|
||||
|
||||
main(sys.argv)
|
||||
@ -117,4 +117,5 @@ def main(args: list[str]):
|
||||
add_source_file(args[3], headerPath, fileName, fileDirectory)
|
||||
|
||||
|
||||
main(sys.argv)
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv)
|
||||
@ -18,7 +18,7 @@
|
||||
|
||||
import sys
|
||||
|
||||
FILE_TEMPLATE = """// IAEngine: 2D Game Engine by IA
|
||||
FILE_CONTENT_PREFIX = """// IAEngine: 2D Game Engine by IA
|
||||
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
@ -40,18 +40,17 @@ FILE_TEMPLATE = """// IAEngine: 2D Game Engine by IA
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
|
||||
}
|
||||
"""
|
||||
FILE_CONTENT_SUFFIX = "\n}"
|
||||
|
||||
def on_invalid_args():
|
||||
print(f"\033[33mUsage: {sys.argv[0]} <ProjectName> <PRIVATE|PUBLIC> <FileName>\033[39m")
|
||||
exit(1)
|
||||
|
||||
def add_header_file(fileName: str, fileDirectory: str):
|
||||
def add_header_file(fileName: str, fileDirectory: str, content: str):
|
||||
print(f"\033[32mAdding Header File '{fileName}' to '{fileDirectory}'..\033[39m")
|
||||
with open(f"{fileDirectory}/{fileName}", 'w') as f:
|
||||
f.write(FILE_TEMPLATE)
|
||||
f.write(f"{FILE_CONTENT_PREFIX}{content}{FILE_CONTENT_SUFFIX}")
|
||||
|
||||
def main(args: list[str]):
|
||||
if (len(args) < 4) or ((args[2] != "PUBLIC") and (args[2] != "PRIVATE")):
|
||||
@ -62,6 +61,7 @@ def main(args: list[str]):
|
||||
fileDirectory = f"Src/{args[1]}/inc/{args[1]}"
|
||||
else:
|
||||
fileDirectory = f"Src/{args[1]}/imp/hpp"
|
||||
add_header_file(fileName, fileDirectory)
|
||||
add_header_file(fileName, fileDirectory, "")
|
||||
|
||||
main(sys.argv)
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv)
|
||||
|
||||
@ -34,26 +34,26 @@ FILE_TEMPLATE = """// IAEngine: 2D Game Engine by IA
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
|
||||
}
|
||||
"""
|
||||
|
||||
def on_invalid_args():
|
||||
print(f"\033[33mUsage: {sys.argv[0]} <ProjectName> <FileName>\033[39m")
|
||||
exit(1)
|
||||
|
||||
def add_source_file(fileName: str, fileDirectory: str):
|
||||
def add_source_file(fileName: str, fileDirectory: str, content: str):
|
||||
print(f"\033[32mAdding Source File '{fileName}' to '{fileDirectory}'..\033[39m")
|
||||
with open(f"{fileDirectory}/{fileName}", 'w') as f:
|
||||
f.write(FILE_TEMPLATE)
|
||||
f.write(f"{FILE_TEMPLATE}{content}")
|
||||
|
||||
def main(args: list[str]):
|
||||
if len(args) < 3:
|
||||
on_invalid_args()
|
||||
fileName = f"{args[3]}.cpp"
|
||||
fileDirectory = f"Src/{args[1]}/imp/cpp"
|
||||
add_source_file(fileName, fileDirectory)
|
||||
add_source_file(fileName, fileDirectory, """namespace ia::iae
|
||||
{
|
||||
|
||||
main(sys.argv)
|
||||
}""")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv)
|
||||
|
||||
@ -16,3 +16,117 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import glob
|
||||
|
||||
from AddHeaderFile import add_header_file
|
||||
from AddSourceFile import add_source_file
|
||||
|
||||
class Resource:
|
||||
def __init__(self) -> None:
|
||||
self.name = ""
|
||||
self.path = ""
|
||||
self.data = b''
|
||||
self.Type = "" # Binary, Text, Shader
|
||||
|
||||
def load_resources() -> list[Resource]:
|
||||
result = []
|
||||
|
||||
imagePaths = glob.glob("Resources/Images/*.*", recursive=True)
|
||||
for t in imagePaths:
|
||||
r = Resource()
|
||||
r.path = t.replace('\\', '/')
|
||||
r.path = r.path[r.path.find('/') + 1:]
|
||||
r.name = r.path.split('/')[-1].upper().replace('.', '_')
|
||||
r.Type = "Binary"
|
||||
with open(t, 'rb') as f:
|
||||
r.data = f.read()
|
||||
result.append(r)
|
||||
|
||||
fontPaths = glob.glob("Resources/Fonts/*.ttf", recursive=True)
|
||||
for t in fontPaths:
|
||||
r = Resource()
|
||||
r.path = t.replace('\\', '/')
|
||||
r.path = r.path[r.path.find('/') + 1:]
|
||||
r.name = r.path.split('/')[-1].upper().replace('.', '_')
|
||||
r.Type = "Binary"
|
||||
with open(t, 'rb') as f:
|
||||
r.data = f.read()
|
||||
result.append(r)
|
||||
|
||||
shaderPaths = glob.glob("Resources/Shaders/*.*", recursive=True)
|
||||
for t in shaderPaths:
|
||||
r = Resource()
|
||||
r.path = t.replace('\\', '/')
|
||||
r.path = r.path[r.path.find('/') + 1:]
|
||||
r.name = r.path.split('/')[-1].upper().replace('.', '_')
|
||||
r.Type = "Binary"
|
||||
os.system(f"glslc {t} -o {t}.spv")
|
||||
with open(f"{t}.spv", 'rb') as f:
|
||||
r.data = f.read()
|
||||
os.remove(f"{t}.spv")
|
||||
result.append(r)
|
||||
|
||||
return result
|
||||
|
||||
def main():
|
||||
resoruces = load_resources()
|
||||
|
||||
add_header_file("EmbeddedResources.hpp", "Src/IAEngine/imp/hpp", """ class EmbeddedResources
|
||||
{
|
||||
public:
|
||||
STATIC VOID Initialize();
|
||||
STATIC VOID Terminate();
|
||||
|
||||
STATIC CONST Vector<UINT8>& GetResource(IN CONST String& name);
|
||||
|
||||
private:
|
||||
STATIC Map<String, Vector<UINT8>> s_resources;
|
||||
};""")
|
||||
|
||||
resourceData = []
|
||||
resourceInit = []
|
||||
|
||||
for r in resoruces:
|
||||
d = "\t\t"
|
||||
for b in r.data:
|
||||
d += f"{hex(b)},"
|
||||
|
||||
resourceData.append(f"\tCONST Vector<UINT8> DATA_{r.name} = {{")
|
||||
resourceData.append(d)
|
||||
resourceData.append("\t};\n")
|
||||
|
||||
resourceInit.append(f"\t\ts_resources[\"{r.path}\"] = DATA_{r.name};")
|
||||
|
||||
add_source_file("EmbeddedResources.cpp", "Src/IAEngine/imp/cpp", f"""#include <EmbeddedResources.hpp>
|
||||
|
||||
namespace ia::iae
|
||||
{{
|
||||
{'\n'.join(resourceData)}
|
||||
}}
|
||||
|
||||
namespace ia::iae
|
||||
{{
|
||||
Map<String, Vector<UINT8>> EmbeddedResources::s_resources;
|
||||
|
||||
VOID EmbeddedResources::Initialize()
|
||||
{{
|
||||
{'\n'.join(resourceInit)}
|
||||
}}
|
||||
|
||||
VOID EmbeddedResources::Terminate()
|
||||
{{
|
||||
for(auto& t: s_resources)
|
||||
t->Value.reset();
|
||||
}}
|
||||
|
||||
CONST Vector<UINT8>& EmbeddedResources::GetResource(IN CONST String& name)
|
||||
{{
|
||||
return s_resources[name];
|
||||
}}
|
||||
}}
|
||||
""")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user