ona/source/runtime.cpp

84 lines
2.3 KiB
C++
Raw Normal View History

2023-02-18 04:34:40 +01:00
export module runtime;
import app;
import app.sdl;
import core;
2023-02-18 20:40:12 +01:00
import core.lalgebra;
2023-02-18 04:34:40 +01:00
import core.sequence;
import kym;
import kym.environment;
extern "C" int main(int argc, char const * const * argv) {
return app::display("Ona Runtime", [](app::system & system, app::graphics & graphics) -> int {
constexpr app::path config_path = app::path::empty().joined("config.kym");
bool is_config_loaded = false;
2023-02-18 20:40:12 +01:00
system.bundle().read_file(config_path, [&](core::readable const & file) {
2023-02-18 04:34:40 +01:00
kym::vm vm{&system.thread_safe_allocator(), [&system](core::slice<char const> const & error_message) {
system.log(app::log_level::error, error_message);
}};
if (!vm.init({
.datastack_size = 64,
.callstack_size = 64,
})) {
system.log(app::log_level::error, "failed to allocate memory for config vm");
return;
}
2023-02-18 20:40:12 +01:00
core::stack<core::u8> script_source{&system.thread_safe_allocator()};
core::u8 stream_buffer[1024] = {};
2023-02-18 04:34:40 +01:00
2023-02-18 20:40:12 +01:00
if (!core::stream(core::sequence_writer{&script_source}, file, stream_buffer).is_ok())
return;
2023-02-18 04:34:40 +01:00
2023-02-18 20:40:12 +01:00
vm.with_object(vm.compile(core::slice{script_source.begin(), script_source.end()}.as_chars()), [&](kym::bound_object & script) {
vm.with_object(script.call({}), [&](kym::bound_object & config) {
2023-02-18 04:34:40 +01:00
core::u16 const width = config.get_field("width").as_u16().value_or(0);
if (width == 0) return system.log(app::log_level::error,
"failed to decode `width` property of config");
2023-02-18 04:34:40 +01:00
core::u16 const height = config.get_field("height").as_u16().value_or(0);
if (height == 0) return system.log(app::log_level::error,
"failed to decode `height` property of config");
2023-02-18 04:34:40 +01:00
graphics.show(width, height);
vm.with_object(config.get_field("title"), [&](kym::bound_object & title) {
core::stack<core::u8, 128> title_buffer{&system.thread_safe_allocator()};
if (!title.stringify(core::sequence_writer(&title_buffer)).is_ok()) {
system.log(app::log_level::error,
"failed to decode `title` property of config");
2023-02-18 04:34:40 +01:00
return;
}
2023-02-18 04:34:40 +01:00
is_config_loaded = true;
});
});
});
});
if (!is_config_loaded) {
system.log(app::log_level::error, "failed to load config");
return core::u8_max;
}
// app::canvas canvas_2d();
while (system.poll()) {
// canvas_2d.render(graphics);
graphics.present();
}
return 0;
});
}