From 737214b71be8fe5fdf51155ad50bb064b3156bd3 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Mon, 3 Nov 2025 17:48:56 +0530 Subject: Add embedded assets, splash screens, and asset loading support Features added: - Embedded main.lua and Lua files support (EMBED_MAIN option) - Embedded assets support (EMBED_ASSETS option) - Splash screens with dual logo display (always embedded) - Asset loading progress tracking API (BeginAssetLoading, UpdateAssetLoading, EndAssetLoading) - Custom font embedding for splash/loading screens - --log flag for Windows console control - --no-logo flag to skip splash screens in development - Python scripts for embedding (embed_lua.py, embed_assets.py, embed_logo.py, embed_font.py) - Documentation (EMBEDDING.md, ASSET_LOADING.md, SPLASH_SCREENS.md) This allows building single-executable releases with all Lua code and assets embedded. --- embed_lua.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 embed_lua.py (limited to 'embed_lua.py') diff --git a/embed_lua.py b/embed_lua.py new file mode 100644 index 0000000..9562354 --- /dev/null +++ b/embed_lua.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +""" +Embed multiple Lua files into a C header file for inclusion in the executable. +Usage: python embed_lua.py [file2.lua] [file3.lua] ... + +Embeds all specified Lua files into a C header with a virtual filesystem. +The first file is treated as main.lua (entry point). +""" +import sys +import os + +def sanitize_name(filename): + """Convert filename to valid C identifier""" + name = os.path.basename(filename) + name = name.replace('.', '_').replace('-', '_').replace('/', '_').replace('\\', '_') + return name + +def embed_files(output_file, input_files): + with open(output_file, 'w') as f: + f.write('#ifndef EMBEDDED_MAIN_H\n') + f.write('#define EMBEDDED_MAIN_H\n\n') + f.write('/* Auto-generated file - do not edit manually */\n\n') + + # Embed each file as a separate array + for idx, input_file in enumerate(input_files): + with open(input_file, 'rb') as inf: + data = inf.read() + + var_name = sanitize_name(input_file) + f.write(f'/* Embedded file: {input_file} */\n') + f.write(f'static const unsigned char embedded_lua_{idx}_{var_name}[] = {{\n') + + for i, byte in enumerate(data): + if i % 12 == 0: + f.write(' ') + f.write(f'0x{byte:02x}') + if i < len(data) - 1: + f.write(',') + if (i + 1) % 12 == 0: + f.write('\n') + else: + f.write(' ') + + f.write('\n};\n') + f.write(f'static const unsigned int embedded_lua_{idx}_{var_name}_len = {len(data)};\n\n') + + # Create the file table + f.write('/* File table for virtual filesystem */\n') + f.write('typedef struct {\n') + f.write(' const char* name;\n') + f.write(' const unsigned char* data;\n') + f.write(' unsigned int size;\n') + f.write('} EmbeddedLuaFile;\n\n') + + f.write('static const EmbeddedLuaFile embedded_lua_files[] = {\n') + for idx, input_file in enumerate(input_files): + var_name = sanitize_name(input_file) + # Store both original filename and basename for require compatibility + basename = os.path.basename(input_file) + f.write(f' {{ "{basename}", embedded_lua_{idx}_{var_name}, embedded_lua_{idx}_{var_name}_len }},\n') + f.write('};\n\n') + + f.write(f'static const int embedded_lua_file_count = {len(input_files)};\n\n') + + # Main entry point (first file) + var_name = sanitize_name(input_files[0]) + f.write('/* Main entry point */\n') + f.write(f'#define embedded_main_lua embedded_lua_0_{var_name}\n') + f.write(f'#define embedded_main_lua_len embedded_lua_0_{var_name}_len\n\n') + + f.write('#endif /* EMBEDDED_MAIN_H */\n') + +if __name__ == '__main__': + if len(sys.argv) < 3: + print('Usage: python embed_lua.py [file2.lua] ...') + print(' The first Lua file is treated as the main entry point.') + sys.exit(1) + + output_file = sys.argv[1] + input_files = sys.argv[2:] + + # Check all input files exist + for f in input_files: + if not os.path.exists(f): + print(f'Error: File not found: {f}') + sys.exit(1) + + embed_files(output_file, input_files) + print(f'Embedded {len(input_files)} file(s) into {output_file}') + for f in input_files: + print(f' - {f}') + -- cgit v1.2.3