ona/source/app.cpp

277 lines
6.5 KiB
C++
Raw Normal View History

2023-02-19 17:45:40 +01:00
module;
#include <SDL2/SDL.h>
2023-02-18 04:34:40 +01:00
export module app;
2023-02-19 17:50:29 +01:00
import coral;
import coral.files;
import coral.image;
import coral.math;
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
import oar;
2023-02-18 04:34:40 +01:00
export namespace app {
2023-02-19 17:50:29 +01:00
struct directory : public coral::fs {
2023-02-19 17:45:40 +01:00
struct rules {
bool can_read;
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
bool can_write;
};
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
directory() : path_buffer{0} {}
2023-02-18 04:34:40 +01:00
2023-02-19 18:16:43 +01:00
void read_file(coral::path const & file_path,
2023-02-19 17:50:29 +01:00
coral::callable<void(coral::readable const &)> const & then) {
2023-02-18 04:34:40 +01:00
2023-02-19 18:16:43 +01:00
if (this->prefix_length == 0) return;
2023-02-18 04:34:40 +01:00
2023-02-19 18:16:43 +01:00
if (!this->access_rules.can_read) return;
2023-02-18 04:34:40 +01:00
2023-02-19 18:16:43 +01:00
::SDL_RWops * rw_ops{this->open_rw(file_path, {.can_read = true})};
2023-02-18 04:34:40 +01:00
2023-02-19 18:16:43 +01:00
if (rw_ops == nullptr) return;
2023-02-19 17:45:40 +01:00
2023-02-19 17:50:29 +01:00
then([rw_ops](coral::slice<uint8_t> const & buffer) -> size_t {
2023-02-19 17:45:40 +01:00
return ::SDL_RWread(rw_ops, buffer.pointer, sizeof(uint8_t), buffer.length);
});
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
::SDL_RWclose(rw_ops);
2023-02-18 04:34:40 +01:00
}
2023-02-19 17:50:29 +01:00
void target(coral::slice<char const> const & directory_path, rules const & access_rules) {
2023-02-19 17:45:40 +01:00
this->access_rules = access_rules;
2023-02-19 17:50:29 +01:00
this->prefix_length = coral::min(directory_path.length, path_max - 1);
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
{
2023-02-19 17:50:29 +01:00
coral::slice const path_buffer_slice{this->path_buffer};
2023-02-18 04:34:40 +01:00
2023-02-19 17:50:29 +01:00
coral::copy(path_buffer_slice.sliced(0, this->prefix_length),
2023-02-19 17:45:40 +01:00
directory_path.sliced(0, this->prefix_length).as_bytes());
2023-02-18 04:34:40 +01:00
2023-02-19 17:50:29 +01:00
coral::zero(path_buffer_slice.sliced(this->prefix_length, path_max - this->prefix_length));
2023-02-19 17:45:40 +01:00
}
2023-02-18 04:34:40 +01:00
}
2023-02-19 18:16:43 +01:00
void write_file(coral::path const & file_path,
2023-02-19 17:50:29 +01:00
coral::callable<void(coral::writable const &)> const & then) {
2023-02-19 17:45:40 +01:00
2023-02-19 18:16:43 +01:00
if (this->prefix_length == 0) return;
2023-02-19 17:45:40 +01:00
2023-02-19 18:16:43 +01:00
if (!this->access_rules.can_write) return;
2023-02-19 17:45:40 +01:00
2023-02-19 18:16:43 +01:00
::SDL_RWops * rw_ops{this->open_rw(file_path, {.can_write = true})};
2023-02-19 17:45:40 +01:00
2023-02-19 18:16:43 +01:00
if (rw_ops == nullptr) return;
2023-02-19 17:45:40 +01:00
2023-02-19 17:50:29 +01:00
then([rw_ops](coral::slice<uint8_t const> const & buffer) -> size_t {
2023-02-19 17:45:40 +01:00
return ::SDL_RWwrite(rw_ops, buffer.pointer, sizeof(uint8_t), buffer.length);
});
::SDL_RWclose(rw_ops);
2023-02-18 04:34:40 +01:00
}
2023-02-19 17:45:40 +01:00
private:
2023-02-19 17:50:29 +01:00
static constexpr coral::usize path_max = 4096;
2023-02-19 17:45:40 +01:00
rules access_rules;
2023-02-19 17:50:29 +01:00
coral::usize prefix_length;
2023-02-19 17:45:40 +01:00
2023-02-19 17:50:29 +01:00
coral::u8 path_buffer[path_max];
2023-02-19 17:45:40 +01:00
2023-02-19 17:50:29 +01:00
::SDL_RWops * open_rw(coral::path const & file_path, rules const & file_rules) {
2023-02-19 18:16:43 +01:00
coral::u8 * const path_begin{this->path_buffer + this->prefix_length};
2023-02-19 17:45:40 +01:00
2023-02-19 17:50:29 +01:00
coral::slice const path_remaining =
2023-02-19 17:45:40 +01:00
{path_begin, path_begin + (path_max - this->prefix_length) - 1};
if (path_remaining.length < file_path.byte_size()) return nullptr;
coral::copy(path_remaining, file_path.as_slice().as_bytes());
2023-02-19 17:45:40 +01:00
return ::SDL_RWFromFile(reinterpret_cast<char const *>(this->path_buffer), "r");
}
2023-02-18 04:34:40 +01:00
};
enum class log_level {
notice,
warning,
error,
};
struct system {
2023-02-19 17:50:29 +01:00
system(coral::path const & title) : res_archive{} {
2023-02-19 17:45:40 +01:00
constexpr directory::rules read_only_rules = {.can_read = true};
{
2023-02-19 18:16:43 +01:00
char * const path{::SDL_GetBasePath()};
2023-02-19 17:45:40 +01:00
if (path == nullptr) {
this->cwd_directory.target("./", read_only_rules);
} else {
2023-02-19 18:16:43 +01:00
coral::usize path_length{0};
2023-02-19 17:45:40 +01:00
while (path[path_length] != 0) path_length += 1;
if (path_length == 0) {
this->cwd_directory.target("./", read_only_rules);
} else {
this->cwd_directory.target({path, path_length}, read_only_rules);
}
}
::SDL_free(path);
}
{
2023-02-19 18:16:43 +01:00
char * const path{::SDL_GetPrefPath("ona", title.begin())};
2023-02-19 17:45:40 +01:00
if (path != nullptr) {
2023-02-19 18:16:43 +01:00
coral::usize path_length{0};
2023-02-19 17:45:40 +01:00
while (path[path_length] != 0) path_length += 1;
if (path_length != 0) this->cwd_directory.target({path, path_length}, {
.can_read = true,
});
}
::SDL_free(path);
}
}
2023-02-19 17:50:29 +01:00
coral::fs & cwd_fs() {
2023-02-19 17:45:40 +01:00
return this->cwd_directory;
}
bool poll() {
while (::SDL_PollEvent(&this->sdl_event) != 0) {
switch (this->sdl_event.type) {
case SDL_QUIT: return false;
}
}
return true;
}
2023-02-19 17:50:29 +01:00
coral::fs & res_fs() {
2023-02-19 17:45:40 +01:00
return this->res_archive;
}
2023-02-19 17:50:29 +01:00
void log(app::log_level level, coral::slice<char const> const & message) {
2023-02-19 18:16:43 +01:00
coral::i32 const length{static_cast<coral::i32>(
coral::min(message.length, static_cast<size_t>(coral::i32_max)))};
2023-02-19 17:45:40 +01:00
::SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,
SDL_LOG_PRIORITY_INFO, "%.*s", length, message.pointer);
}
2023-02-19 17:50:29 +01:00
coral::allocator & thread_safe_allocator() {
2023-02-19 17:45:40 +01:00
return this->allocator;
}
2023-02-19 17:50:29 +01:00
coral::fs & user_fs() {
2023-02-19 17:45:40 +01:00
return this->user_directory;
}
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
private:
::SDL_Event sdl_event;
2023-02-18 04:34:40 +01:00
2023-02-19 17:50:29 +01:00
struct : public coral::allocator {
coral::u8 * reallocate(coral::u8 * maybe_allocation, coral::usize requested_size) override {
return reinterpret_cast<coral::u8 *>(::SDL_malloc(requested_size));
2023-02-19 17:45:40 +01:00
}
void deallocate(void * allocation) override {
::SDL_free(allocation);
}
} allocator;
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
oar::archive res_archive;
directory cwd_directory;
directory user_directory;
2023-02-18 04:34:40 +01:00
};
struct graphics {
2023-02-19 17:45:40 +01:00
enum class [[nodiscard]] show_result {
ok,
2023-02-18 04:34:40 +01:00
out_of_memory,
};
struct canvas {
2023-02-19 17:50:29 +01:00
coral::color background_color;
2023-02-18 04:34:40 +01:00
};
2023-02-19 17:50:29 +01:00
graphics(coral::path const & title) {
2023-02-19 17:45:40 +01:00
this->retitle(title);
}
2023-02-18 04:34:40 +01:00
2023-02-19 18:16:43 +01:00
void present() {
if (this->sdl_renderer != nullptr) {
::SDL_RenderPresent(this->sdl_renderer);
}
}
2023-02-19 17:45:40 +01:00
void render(canvas & source_canvas) {
if (this->sdl_renderer != nullptr) {
SDL_SetRenderDrawColor(this->sdl_renderer, source_canvas.background_color.to_r8(),
source_canvas.background_color.to_g8(), source_canvas.background_color.to_b8(),
source_canvas.background_color.to_a8());
2023-02-18 04:34:40 +01:00
2023-02-19 17:45:40 +01:00
SDL_RenderClear(this->sdl_renderer);
}
}
2023-02-18 04:34:40 +01:00
2023-02-19 17:50:29 +01:00
void retitle(coral::path const & title) {
2023-02-19 17:45:40 +01:00
this->title = title;
if (this->sdl_window != nullptr)
::SDL_SetWindowTitle(this->sdl_window, this->title.begin());
}
2023-02-19 17:50:29 +01:00
show_result show(coral::u16 physical_width, coral::u16 physical_height) {
2023-02-19 17:45:40 +01:00
if (this->sdl_window == nullptr) {
constexpr int sdl_windowpos = SDL_WINDOWPOS_UNDEFINED;
2023-02-19 17:50:29 +01:00
constexpr coral::u32 sdl_windowflags = 0;
2023-02-19 17:45:40 +01:00
this->sdl_window = ::SDL_CreateWindow(this->title.begin(), sdl_windowpos,
sdl_windowpos, static_cast<int>(physical_width), static_cast<int>(physical_height),
sdl_windowflags);
if (this->sdl_window == nullptr) return show_result::out_of_memory;
} else {
::SDL_ShowWindow(this->sdl_window);
}
if (this->sdl_renderer == nullptr) {
2023-02-19 17:50:29 +01:00
constexpr coral::u32 sdl_rendererflags = 0;
2023-02-19 17:45:40 +01:00
this->sdl_renderer = ::SDL_CreateRenderer(this->sdl_window, -1, sdl_rendererflags);
if (this->sdl_renderer == nullptr) return show_result::out_of_memory;
}
return show_result::ok;
}
private:
2023-02-19 17:50:29 +01:00
coral::path title;
2023-02-19 17:45:40 +01:00
::SDL_Window * sdl_window = nullptr;
::SDL_Renderer * sdl_renderer = nullptr;
2023-02-18 04:34:40 +01:00
};
2023-02-19 17:45:40 +01:00
2023-02-19 17:50:29 +01:00
using graphical_runnable = coral::callable<int(system &, graphics &)>;
2023-02-19 17:45:40 +01:00
2023-02-19 17:50:29 +01:00
int display(coral::path const & title, graphical_runnable const & run) {
2023-02-19 17:45:40 +01:00
system app_system{title};
graphics app_graphics{title};
return run(app_system, app_graphics);
}
2023-02-18 04:34:40 +01:00
}