From 3a29b9984760af0b3a85e35190e1dede39e13891 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 16 Nov 2021 11:07:49 +0100 Subject: Implement all interfaces to mgl --- src/graphics/Font.cpp | 18 +++++------ src/graphics/Shader.cpp | 4 --- src/graphics/Sprite.cpp | 9 +++--- src/graphics/Text.cpp | 4 +-- src/graphics/Texture.cpp | 28 ++++++++++++----- src/system/MemoryMappedFile.cpp | 36 +++++++++++++++++++++ src/system/Utf8.cpp | 11 +++++++ src/window/Clipboard.cpp | 15 --------- src/window/Window.cpp | 69 +++++++++++++++++++++++++---------------- 9 files changed, 126 insertions(+), 68 deletions(-) create mode 100644 src/system/MemoryMappedFile.cpp create mode 100644 src/system/Utf8.cpp delete mode 100644 src/window/Clipboard.cpp (limited to 'src') diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index ae87625..b3ef54c 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -1,5 +1,6 @@ #include "../../include/mglpp/graphics/Font.hpp" #include "../../include/mglpp/graphics/Texture.hpp" +#include "../../include/mglpp/system/MemoryMappedFile.hpp" #include namespace mgl { @@ -11,32 +12,31 @@ namespace mgl { mgl_font_unload(&font); } - bool Font::load_from_file(const char *filepath, unsigned int character_size) { + bool Font::load_from_file(const MemoryMappedFile &mapped_file, unsigned int character_size) { if(font.texture.id) return false; - return mgl_font_load_from_file(&font, filepath, character_size) == 0; + return mgl_font_load_from_file(&font, mapped_file.internal_mapped_file(), character_size) == 0; } unsigned int Font::get_character_size() const { return font.character_size; } - FontGlyph Font::get_glyph(uint32_t codepoint) const { + FontGlyph Font::get_glyph(uint32_t codepoint) { FontGlyph font_glyph; - if(font.texture.id == 0) - return font_glyph; - mgl_font_get_glyph(&font, codepoint, (mgl_font_glyph*)&font_glyph); return font_glyph; } + int Font::get_kerning(uint32_t prev_codepoint, uint32_t codepoint) { + return mgl_font_get_kerning(&font, prev_codepoint, codepoint); + } + Texture Font::get_texture() const { - if(font.texture.id == 0) - return Texture(); return Texture::reference(font.texture); } mgl_font* Font::internal_font() { return &font; } -} \ No newline at end of file +} diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp index 7775b78..0d0e651 100644 --- a/src/graphics/Shader.cpp +++ b/src/graphics/Shader.cpp @@ -25,14 +25,10 @@ namespace mgl { } bool Shader::set_uniform(const char *name, float value) { - if(!shader_program.id) - return false; return mgl_shader_program_set_uniform_float(&shader_program, name, value) == 0; } bool Shader::set_uniform(const char *name, vec2f value) { - if(!shader_program.id) - return false; return mgl_shader_program_set_uniform_vec2f(&shader_program, name, { value.x, value.y }) == 0; } diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp index b9066ee..e6cef73 100644 --- a/src/graphics/Sprite.cpp +++ b/src/graphics/Sprite.cpp @@ -9,7 +9,8 @@ namespace mgl { Sprite::Sprite() : Sprite(nullptr, vec2f(0.0f, 0.0f)) {} Sprite::Sprite(Texture *texture, vec2f position) : texture(texture) { - mgl_sprite_init(&sprite, texture ? texture->internal_texture() : nullptr, position.x, position.y); + mgl_sprite_init(&sprite, texture ? texture->internal_texture() : nullptr); + set_position(position); } Sprite::~Sprite() { @@ -41,14 +42,12 @@ namespace mgl { sprite.scale = { scale, scale }; } - // TODO: Implement void Sprite::set_rotation(float degrees) { - + mgl_sprite_set_rotation(&sprite, degrees); } - // TODO: Implement void Sprite::set_origin(vec2f origin) { - + mgl_sprite_set_origin(&sprite, *(mgl_vec2f*)&origin); } vec2f Sprite::get_scale() const { diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp index 953833b..ad6fed2 100644 --- a/src/graphics/Text.cpp +++ b/src/graphics/Text.cpp @@ -76,9 +76,9 @@ namespace mgl { mgl_text_set_string(&text, this->str.c_str(), this->str.size()); } - // TODO: Implement vec2f Text::find_character_pos(size_t index) { - return vec2f(); + mgl_vec2f pos = mgl_text_find_character_pos(&text, index); + return vec2f(pos.x, pos.y); } Font* Text::get_font() { diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp index cdaa3ba..f543c37 100644 --- a/src/graphics/Texture.cpp +++ b/src/graphics/Texture.cpp @@ -19,18 +19,32 @@ namespace mgl { return instance; } - bool Texture::load_from_file(const char *filepath) { + bool Texture::load_from_file(const char *filepath, const LoadOptions load_options) { if(texture.id) return false; - /* TODO: use the last arg (load options) */ - return mgl_texture_load_from_file(&texture, filepath, nullptr) == 0; + + if(mgl_texture_init(&texture) != 0) + return false; + + mgl_texture_load_options texture_load_options = { + load_options.compressed, + load_options.pixel_coordinates + }; + return mgl_texture_load_from_file(&texture, filepath, &texture_load_options) == 0; } - bool Texture::load_from_image(Image &image) { + bool Texture::load_from_image(Image &image, const LoadOptions load_options) { if(texture.id) return false; - /* TODO: use the last arg (load options) */ - return mgl_texture_load_from_image(&texture, image.internal_image(), nullptr) == 0; + + if(mgl_texture_init(&texture) != 0) + return false; + + mgl_texture_load_options texture_load_options = { + load_options.compressed, + load_options.pixel_coordinates + }; + return mgl_texture_load_from_image(&texture, image.internal_image(), &texture_load_options) == 0; } vec2i Texture::get_size() const { @@ -44,4 +58,4 @@ namespace mgl { mgl_texture* Texture::internal_texture() { return &texture; } -} \ No newline at end of file +} diff --git a/src/system/MemoryMappedFile.cpp b/src/system/MemoryMappedFile.cpp new file mode 100644 index 0000000..54f15c1 --- /dev/null +++ b/src/system/MemoryMappedFile.cpp @@ -0,0 +1,36 @@ +#include "../../include/mglpp/system/MemoryMappedFile.hpp" + +namespace mgl { + MemoryMappedFile::MemoryMappedFile() { + memory_mapped_file.data = nullptr; + memory_mapped_file.size = 0; + memory_mapped_file.fd = -1; + } + + MemoryMappedFile::~MemoryMappedFile() { + mgl_mapped_file_unload(&memory_mapped_file); + } + + bool MemoryMappedFile::load(const char *filepath, LoadOptions load_options) { + if(memory_mapped_file.fd != -1) + return false; + + mgl_memory_mapped_file_load_options mapped_file_load_options = { + load_options.readable, + load_options.writable + }; + return mgl_mapped_file_load(filepath, &memory_mapped_file, &mapped_file_load_options) == 0; + } + + void* MemoryMappedFile::data() { + return memory_mapped_file.data; + } + + size_t MemoryMappedFile::size() { + return memory_mapped_file.size; + } + + const mgl_memory_mapped_file* MemoryMappedFile::internal_mapped_file() const { + return &memory_mapped_file; + } +} \ No newline at end of file diff --git a/src/system/Utf8.cpp b/src/system/Utf8.cpp new file mode 100644 index 0000000..be9b2af --- /dev/null +++ b/src/system/Utf8.cpp @@ -0,0 +1,11 @@ +#include "../../include/mglpp/system/Utf8.hpp" + +extern "C" { +#include +} + +namespace mgl { + bool utf8_decode(const unsigned char *str, size_t size, uint32_t *decoded_codepoint, size_t *codepoint_length) { + return mgl_utf8_decode(str, size, decoded_codepoint, codepoint_length); + } +} \ No newline at end of file diff --git a/src/window/Clipboard.cpp b/src/window/Clipboard.cpp deleted file mode 100644 index b5b156e..0000000 --- a/src/window/Clipboard.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "../../include/mglpp/window/Clipboard.hpp" - -namespace mgl { - // TODO: Implement - // static - void Clipboard::set_string(std::string str) { - - } - - // TODO: Implement - // static - std::string Clipboard::get_string() { - return ""; - } -} \ No newline at end of file diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 5dbbe5a..ee984fd 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -18,10 +18,10 @@ namespace mgl { mgl_window_deinit(&window); } - bool Window::create(const char *title, int width, int height) { + bool Window::create(const char *title, CreateParams create_params) { if(window.window) return false; - return mgl_window_create_with_params(&window, title, width, height, 0) == 0; + return mgl_window_create(&window, title, (mgl_window_create_params*)&create_params) == 0; } bool Window::create(WindowHandle existing_window) { @@ -31,14 +31,10 @@ namespace mgl { } bool Window::poll_event(Event &event) { - if(!window.window) - return false; return mgl_window_poll_event(&window, (mgl_event*)&event); } void Window::clear(Color color) { - if(!window.window) - return; mgl_window_clear(&window, mgl_color{color.r, color.g, color.b, color.a}); } @@ -64,36 +60,27 @@ namespace mgl { } void Window::display() { - if(!window.window) - return; mgl_window_display(&window); } - // TODO: Implement bool Window::is_open() const { - if(!window.window) - return false; return mgl_window_is_open(&window); } - // TODO: Implement bool Window::has_focus() const { - return true; + return mgl_window_has_focus(&window); } - // TODO: Implement void Window::close() { - + mgl_window_close(&window); } - // TODO: Implement void Window::set_title(const char *title) { - + mgl_window_set_title(&window, title); } - // TODO: Implement void Window::set_framerate_limit(unsigned int fps) { - + mgl_window_set_framerate_limit(&window, fps); } // TODO: Implement @@ -101,33 +88,63 @@ namespace mgl { } - // TODO: Implement void Window::set_cursor_visible(bool visible) { - + mgl_window_set_cursor_visible(&window, visible); } vec2i Window::get_size() const { return { window.size.x, window.size.y }; } - vec2i Window::get_cursor_position() const { + void Window::set_size(mgl::vec2i size) { + mgl_window_set_size(&window, { size.x, size.y }); + } + + void Window::set_position(mgl::vec2i position) { + mgl_window_set_position(&window, { position.x, position.y }); + } + + void Window::set_size_limits(mgl::vec2i minimum, mgl::vec2i maximum) { + mgl_window_set_size_limits(&window, { minimum.x, minimum.y }, { maximum.x, maximum.y }); + } + + vec2i Window::get_mouse_position() const { return { window.cursor_position.x, window.cursor_position.y }; } void Window::set_view(const View &new_view) { - if(!window.window) - return; mgl_window_set_view(&window, (mgl_view*)&new_view); } View Window::get_view() { View view; - if(!window.window) - return view; mgl_window_get_view(&window, (mgl_view*)&view); return view; } + bool Window::is_key_pressed(Keyboard::Key key) const { + return mgl_window_is_key_pressed(&window, (mgl_key)key); + } + + bool Window::is_mouse_button_pressed(Mouse::Button button) const { + return mgl_window_is_mouse_button_pressed(&window, (mgl_mouse_button)button); + } + + void Window::set_clipboard(const std::string &str) { + mgl_window_set_clipboard(&window, str.c_str(), str.size()); + } + + std::string Window::get_clipboard() { + std::string result; + char *str = nullptr; + size_t size = 0; + if(mgl_window_get_clipboard(&window, &str, &size)) { + result.assign(str, size); + free(str); + } + return result; + } + WindowHandle Window::get_system_handle() const { return window.window; } -- cgit v1.2.3