ona/source/runtime.cpp

98 lines
2.8 KiB
C++

export module runtime;
import app;
import coral;
import coral.files;
import coral.math;
import coral.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 coral::path config_path{"config.kym"};
bool is_config_loaded{false};
system.res_fs().read_file(config_path, [&](coral::reader & file) {
coral::allocator * const allocator{&system.thread_safe_allocator()};
kym::vm vm{allocator, [&system](coral::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;
}
coral::stack<coral::u8> script_source{allocator};
{
coral::u8 stream_buffer[1024]{0};
coral::sequence_writer script_writer{&script_source};
if (!coral::stream(script_writer, file, stream_buffer).is_ok()) return;
}
vm.with_object(vm.compile(coral::slice{script_source.begin(), script_source.end()}.as_chars()), [&](kym::bound_object & script) {
vm.with_object(script.call({}), [&](kym::bound_object & config) {
coral::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");
coral::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");
if (graphics.show(width, height) != app::graphics::show_result::ok)
return system.log(app::log_level::error, "failed to initialize window");
vm.with_object(config.get_field("title"), [&](kym::bound_object & title) {
coral::stack<coral::u8, 128> title_buffer{&system.thread_safe_allocator()};
coral::sequence_writer title_writer{&title_buffer};
if (!title.stringify(title_writer).is_ok()) {
system.log(app::log_level::error,
"failed to decode `title` property of config");
return;
}
is_config_loaded = true;
});
});
});
});
if (!is_config_loaded) {
coral::stack<coral::u8> error_message{&system.thread_safe_allocator()};
{
coral::sequence_writer error_writer{&error_message};
if (!error_writer.write(coral::slice{"failed to load "}.as_bytes()).is_ok())
return coral::u8_max;
if (!error_writer.write(config_path.as_slice().as_bytes()).is_ok())
return coral::u8_max;
}
return coral::u8_max;
}
// app::canvas canvas_2d();
while (system.poll()) {
// canvas_2d.render(graphics);
graphics.present();
}
return 0;
});
}