#!/bin/python3 # 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 . 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& GetResource(IN CONST String& name); private: STATIC Map> 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 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 namespace ia::iae {{ {'\n'.join(resourceData)} }} namespace ia::iae {{ Map> EmbeddedResources::s_resources; VOID EmbeddedResources::Initialize() {{ {'\n'.join(resourceInit)} }} VOID EmbeddedResources::Terminate() {{ for(auto& t: s_resources) t->Value.reset(); }} CONST Vector& EmbeddedResources::GetResource(IN CONST String& name) {{ return s_resources[name]; }} }} """) if __name__ == "__main__": main()