Added memory FS and put the default font there so it is easier to load it.

This commit is contained in:
Patrick 2025-03-28 11:48:16 +01:00
parent 6cf748b05d
commit 63fcb6181f
11 changed files with 341 additions and 96 deletions

4
.gitignore vendored
View File

@ -28,6 +28,10 @@ compile_commands.json
/config.py /config.py
/config_*.py /config_*.py
# Generated files
*.gen.*
!*.gen.*.jinja
# Prerequisites # Prerequisites
*.d *.d

View File

@ -1,10 +1,13 @@
config = { config = {
'PROJECT_NAME': 'RAID Framework', 'PROJECT_NAME': 'RAID Framework',
'CXX_NO_EXCEPTIONS': True 'TOOLS': ['jinja']
} }
env = SConscript('external/scons-plus-plus/SConscript', exports = ['config']) env = SConscript('external/scons-plus-plus/SConscript', exports = ['config'])
env.Append(CPPPATH = [Dir('private'), Dir('public')]) env.Append(CPPPATH = [Dir('private'), Dir('public')])
# add the default recipe repository
env.RecipeRepo('mewin-stable', 'https://git.mewin.de/mewin/spp_recipes.git', 'stable')
# library # library
env = env.Module('private/raid/SModule') env = env.Module('private/raid/SModule')

@ -1 +1 @@
Subproject commit c994752c3244fdf9835b1d3a5238094d2a799855 Subproject commit 2769fd801fa456d809e9e7c8bf2d1be4953a734d

View File

@ -3,11 +3,18 @@ import json
Import('env') Import('env')
if not hasattr(env, 'Jinja'):
env.Error('RAID requires the Jinja tool.')
src_files = Split(""" src_files = Split("""
application.cpp application.cpp
fonts.gen.cpp
stb_image.cpp stb_image.cpp
""") """)
env.Jinja("fonts.gen.cpp")
env.Jinja("fonts.gen.hpp")
with open('../../dependencies.json', 'r') as f: with open('../../dependencies.json', 'r') as f:
dependencies = env.DepsFromJson(json.load(f)) dependencies = env.DepsFromJson(json.load(f))

View File

@ -16,6 +16,7 @@
#include <backends/imgui_impl_opengl3.h> #include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_sdl3.h> #include <backends/imgui_impl_sdl3.h>
#include <stb_image.h> #include <stb_image.h>
#include "./fonts.gen.hpp"
namespace raid namespace raid
{ {
@ -242,7 +243,7 @@ void Application::configureImgui()
std::vector<FontConfig> Application::getDefaultFonts() std::vector<FontConfig> Application::getDefaultFonts()
{ {
return {{ return {{
.path = "/data/fonts/NotoSans-Regular.ttf" .path = DEFAULT_FONT_PATH
}}; }};
} }
@ -295,6 +296,9 @@ bool Application::init()
); );
}; };
mMemoryFS = mFS.emplaceAdapter<mijin::MemoryFileSystemAdapter>();
mMemoryFS->addFile(DEFAULT_FONT_PATH, NOTO_SANS_DATA);
addConfigDir(mijin::getKnownFolder(mijin::KnownFolder::USER_CONFIG_ROOT) / getFolderName()); addConfigDir(mijin::getKnownFolder(mijin::KnownFolder::USER_CONFIG_ROOT) / getFolderName());
addDataDir(mijin::getKnownFolder(mijin::KnownFolder::USER_DATA_ROOT) / getFolderName()); addDataDir(mijin::getKnownFolder(mijin::KnownFolder::USER_DATA_ROOT) / getFolderName());

View File

@ -0,0 +1,7 @@
#include "./fonts.gen.hpp"
namespace raid
{
extern const std::array<std::uint8_t, NOTO_SANS_SIZE> NOTO_SANS_DATA = { {{ file_content_hex('#res/fonts/NotoSans-Regular.ttf') }} };
}

View File

@ -0,0 +1,16 @@
#pragma once
#if !defined(RAID_PRIVATE_RAID_FONTS_GEN_HPP_INCLUDED)
#define RAID_PRIVATE_RAID_FONTS_GEN_HPP_INCLUDED 1
#include <array>
#include <cstdint>
namespace raid
{
inline constexpr std::size_t NOTO_SANS_SIZE = {{ file_size('#res/fonts/NotoSans-Regular.ttf' )}};
extern const std::array<std::uint8_t, NOTO_SANS_SIZE> NOTO_SANS_DATA;
} // namespace raid
#endif // !defined(RAID_PRIVATE_RAID_FONTS_GEN_HPP_INCLUDED)

View File

@ -10,6 +10,7 @@
#include <imgui.h> #include <imgui.h>
#include <mijin/async/coroutine.hpp> #include <mijin/async/coroutine.hpp>
#include <mijin/util/bitflags.hpp> #include <mijin/util/bitflags.hpp>
#include <mijin/virtual_filesystem/memory.hpp>
#include <mijin/virtual_filesystem/stacked.hpp> #include <mijin/virtual_filesystem/stacked.hpp>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
@ -21,6 +22,7 @@ inline constexpr ImGuiWindowFlags DEFAULT_MAIN_WINDOW_FLAGS = 0
| ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoDecoration
| ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoBringToFrontOnFocus
| ImGuiWindowFlags_NoNav; | ImGuiWindowFlags_NoNav;
inline constexpr const char* DEFAULT_FONT_PATH = "/data/fonts/NotoSans-Regular.ttf";
enum class MessageSeverity : unsigned char enum class MessageSeverity : unsigned char
{ {
@ -55,6 +57,7 @@ private:
SDL_GLContext mGLContext = nullptr; SDL_GLContext mGLContext = nullptr;
mijin::StackedFileSystemAdapter mFS; mijin::StackedFileSystemAdapter mFS;
mijin::MemoryFileSystemAdapter* mMemoryFS = nullptr;
mijin::SimpleTaskLoop mTaskLoop; mijin::SimpleTaskLoop mTaskLoop;
std::unordered_map<fs::path, ImTextureID> mTextures; std::unordered_map<fs::path, ImTextureID> mTextures;
@ -91,6 +94,13 @@ public:
[[nodiscard]] [[nodiscard]]
mijin::StackedFileSystemAdapter& getFS() { return mFS; } mijin::StackedFileSystemAdapter& getFS() { return mFS; }
[[nodiscard]]
mijin::MemoryFileSystemAdapter& getMemoryFS()
{
MIJIN_ASSERT_FATAL(mMemoryFS != nullptr, "Memory FS has not been initialized yet.");
return *mMemoryFS;
}
[[nodiscard]] [[nodiscard]]
mijin::SimpleTaskLoop& getLoop() { return mTaskLoop; } mijin::SimpleTaskLoop& getLoop() { return mTaskLoop; }

View File

@ -0,0 +1,194 @@
# source: https://github.com/hgomersall/scons-jinja
# 2025-03-28: Added JINJA_GLOBALS options.
# 2023-12-02: Added JINJA_FILTERS option.
#
# Copyright (c) 2013 Henry Gomersall <heng@kedevelopments.co.uk>
# Copyright (c) 2025 Patrick Wuttke <mewin@mewin.de>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import SCons.Builder
import SCons.Tool
from SCons.Errors import StopError
import jinja2
from jinja2 import FileSystemLoader
from jinja2.utils import open_if_exists
from jinja2.exceptions import TemplateNotFound
import os
class FileSystemLoaderRecorder(FileSystemLoader):
''' A wrapper around FileSystemLoader that records files as they are
loaded. These are contained within loaded_filenames set attribute.
'''
def __init__(self, searchpath, encoding='utf-8'):
self.loaded_filenames = set()
super(FileSystemLoaderRecorder, self).__init__(searchpath, encoding)
def get_source(self, environment, template):
'''Overwritten FileSystemLoader.get_source method that extracts the
filename that is used to load each filename and adds it to
self.loaded_filenames.
'''
for searchpath in self.searchpath:
filename = os.path.join(searchpath, template)
f = open_if_exists(filename)
if f is None:
continue
try:
contents = f.read().decode(self.encoding)
finally:
f.close()
self.loaded_filenames.add(filename)
return super(FileSystemLoaderRecorder, self).get_source(
environment, template)
# If the template isn't found, then we have to drop out.
raise TemplateNotFound(template)
def jinja_scanner(node, env, path):
# Instantiate the file as necessary
node.get_text_contents()
node_dir = os.path.dirname(str(node))
template_dir, filename = os.path.split(str(node))
template_search_path = ([template_dir] +
env.subst(env['JINJA_TEMPLATE_SEARCHPATH']))
template_loader = FileSystemLoaderRecorder(template_search_path)
jinja_env = jinja2.Environment(loader=template_loader,
extensions=['jinja2.ext.do'], **env['JINJA_ENVIRONMENT_VARS'])
jinja_env.filters.update(env['JINJA_FILTERS'])
jinja_env.globals.update(env['JINJA_GLOBALS'])
try:
template = jinja_env.get_template(filename)
except TemplateNotFound as e:
raise StopError('Missing template: ' +
os.path.join(template_dir, str(e)))
# We need to render the template to do all the necessary loading.
#
# It's necessary to respond to missing templates by grabbing
# the content as the exception is raised. This makes sure of the
# existence of the file upon which the current scanned node depends.
#
# I suspect that this is pretty inefficient, but it does
# work reliably.
context = env['JINJA_CONTEXT']
last_missing_file = ''
while True:
try:
template.render(**context)
except TemplateNotFound as e:
if last_missing_file == str(e):
# We've already been round once for this file,
# so need to raise
raise StopError('Missing template: ' +
os.path.join(template_dir, str(e)))
last_missing_file = str(e)
# Find where the template came from (using the same ordering
# as Jinja uses).
for searchpath in template_search_path:
filename = os.path.join(searchpath, last_missing_file)
if os.path.exists(filename):
continue
else:
env.File(filename).get_text_contents()
continue
break
# Get all the files that were loaded. The set includes the current node,
# so we remove that.
found_nodes_names = list(template_loader.loaded_filenames)
try:
found_nodes_names.remove(str(node))
except ValueError as e:
raise StopError('Missing template node: ' + str(node))
return [env.File(f) for f in found_nodes_names]
def render_jinja_template(target, source, env):
output_str = ''
if not source:
source = [f'{target}.jinja']
for template_file in source:
template_dir, filename = os.path.split(str(template_file))
template_search_path = ([template_dir] +
env.subst(env['JINJA_TEMPLATE_SEARCHPATH']))
template_loader = FileSystemLoaderRecorder(template_search_path)
jinja_env = jinja2.Environment(loader=template_loader,
extensions=['jinja2.ext.do'], **env['JINJA_ENVIRONMENT_VARS'])
jinja_env.filters.update(env['JINJA_FILTERS'])
jinja_env.globals.update(env['JINJA_GLOBALS'])
template = jinja_env.get_template(filename)
context = env['JINJA_CONTEXT']
template.render(**context)
output_str += template.render(**context)
with open(str(target[0]), 'w') as target_file:
target_file.write(output_str)
return None
def generate(env):
env.SetDefault(JINJA_CONTEXT={})
env.SetDefault(JINJA_ENVIRONMENT_VARS={})
env.SetDefault(JINJA_FILTERS={})
env.SetDefault(JINJA_GLOBALS={})
env.SetDefault(JINJA_TEMPLATE_SEARCHPATH=[])
env['BUILDERS']['Jinja'] = SCons.Builder.Builder(
action=render_jinja_template)
scanner = env.Scanner(function=jinja_scanner,
skeys=['.jinja'])
env.Append(SCANNERS=scanner)
def exists(env):
try:
import jinja2
except ImportError as e:
raise StopError(ImportError, e.message)