diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-10-28 17:33:57 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-10-29 14:31:31 +0200 |
commit | 8d525bc1c3506f15a5f68672245f845cebe18eef (patch) | |
tree | 243376e2cae0be40b6870ec8fe0082845996df87 /src | |
parent | a80bf6bb6cb8ab8c5a1430f9f9dbc214f71bdddf (diff) |
More, todo interfaces
Diffstat (limited to 'src')
-rw-r--r-- | src/graphics/Font.cpp | 17 | ||||
-rw-r--r-- | src/graphics/Image.cpp | 6 | ||||
-rw-r--r-- | src/graphics/Rectangle.cpp | 4 | ||||
-rw-r--r-- | src/graphics/Shader.cpp | 4 | ||||
-rw-r--r-- | src/graphics/Sprite.cpp | 23 | ||||
-rw-r--r-- | src/graphics/Text.cpp | 36 | ||||
-rw-r--r-- | src/graphics/Texture.cpp | 19 | ||||
-rw-r--r-- | src/graphics/VertexBuffer.cpp | 17 | ||||
-rw-r--r-- | src/system/Clock.cpp | 15 | ||||
-rw-r--r-- | src/window/Clipboard.cpp | 15 | ||||
-rw-r--r-- | src/window/Window.cpp | 65 |
11 files changed, 202 insertions, 19 deletions
diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 7b5dd68..ae87625 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -1,5 +1,7 @@ #include "../../include/mglpp/graphics/Font.hpp" +#include "../../include/mglpp/graphics/Texture.hpp" #include <string.h> + namespace mgl { Font::Font() { memset(&font, 0, sizeof(font)); @@ -19,6 +21,21 @@ namespace mgl { return font.character_size; } + FontGlyph Font::get_glyph(uint32_t codepoint) const { + 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; + } + + Texture Font::get_texture() const { + if(font.texture.id == 0) + return Texture(); + return Texture::reference(font.texture); + } + mgl_font* Font::internal_font() { return &font; } diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp index 493fad7..2d9b58d 100644 --- a/src/graphics/Image.cpp +++ b/src/graphics/Image.cpp @@ -16,6 +16,12 @@ namespace mgl { return mgl_image_load_from_file(&image, filepath) == 0; } + bool Image::load_from_memory(const unsigned char *data, size_t size) { + if(image.data) + return false; + return mgl_image_load_from_memory(&image, data, size) == 0; + } + unsigned char* Image::data() { return image.data; } diff --git a/src/graphics/Rectangle.cpp b/src/graphics/Rectangle.cpp index c43d2c4..179d6ee 100644 --- a/src/graphics/Rectangle.cpp +++ b/src/graphics/Rectangle.cpp @@ -5,7 +5,9 @@ extern "C" { } namespace mgl { - Rectangle::Rectangle() : Rectangle(mgl::vec2f(0.0f, 0.0f), mgl::vec2f(0.0f, 0.0f)) {} + Rectangle::Rectangle() : Rectangle(vec2f(0.0f, 0.0f), vec2f(0.0f, 0.0f)) {} + + Rectangle::Rectangle(vec2f size) : Rectangle(vec2f(0.0f, 0.0f), size) {} Rectangle::Rectangle(vec2f position, vec2f size) { rectangle.color = { 255, 255, 255, 255 }; diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp index 96fe8ba..c23d32b 100644 --- a/src/graphics/Shader.cpp +++ b/src/graphics/Shader.cpp @@ -30,6 +30,10 @@ namespace mgl { return mgl_shader_program_set_uniform_vec2f(&shader_program, name, { value.x, value.y }) == 0; } + bool Shader::is_valid() const { + return shader_program.id != 0; + } + // static void Shader::use(Shader *shader) { if(shader) diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp index baf118e..b9066ee 100644 --- a/src/graphics/Sprite.cpp +++ b/src/graphics/Sprite.cpp @@ -6,14 +6,21 @@ extern "C" { } namespace mgl { - Sprite::Sprite(Texture &texture, vec2f position) : texture(texture) { - mgl_sprite_init(&sprite, texture.internal_texture(), position.x, position.y); + 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); } Sprite::~Sprite() { } + void Sprite::set_texture(Texture *texture) { + this->texture = texture; + mgl_sprite_set_texture(&sprite, texture ? texture->internal_texture() : nullptr); + } + void Sprite::set_position(vec2f position) { mgl_sprite_set_position(&sprite, {position.x, position.y}); } @@ -34,12 +41,22 @@ namespace mgl { sprite.scale = { scale, scale }; } + // TODO: Implement + void Sprite::set_rotation(float degrees) { + + } + + // TODO: Implement + void Sprite::set_origin(vec2f origin) { + + } + vec2f Sprite::get_scale() const { return { sprite.scale.x, sprite.scale.y }; } const Texture* Sprite::get_texture() const { - return &texture; + return texture; } void Sprite::draw(Window&) { diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp index 7d08c22..dbaa7c1 100644 --- a/src/graphics/Text.cpp +++ b/src/graphics/Text.cpp @@ -6,12 +6,21 @@ extern "C" { } namespace mgl { - Text::Text(std::string str, Font &font) : Text(std::move(str), mgl::vec2f(0.0f, 0.0f), font){} + Text::Text() : font(nullptr) { + mgl_text_init(&text, nullptr, nullptr, 0.0f, 0.0f); + } + + Text::Text(std::string str, Font &font) : Text(std::move(str), vec2f(0.0f, 0.0f), font) {} - Text::Text(std::string str, vec2f position, Font &font) : font(font), str(std::move(str)) { + Text::Text(std::string str, vec2f position, Font &font) : font(&font), str(std::move(str)) { mgl_text_init(&text, font.internal_font(), this->str.c_str(), position.x, position.y); } + Text::Text(const Text &other) { + font = other.font; + mgl_text_init(&text, font ? font->internal_font() : nullptr, other.str.c_str(), other.text.position.x, other.text.position.y); + } + Text::~Text() { mgl_text_deinit(&text); } @@ -28,6 +37,29 @@ namespace mgl { return { text.position.x, text.position.y }; } + // TODO: Implement + FloatRect Text::get_local_bounds() { + return FloatRect(); + } + + void Text::set_string(std::string str) { + this->str = std::move(str); + mgl_text_set_string(&text, this->str.c_str()); + } + + const std::string& Text::get_string() const { + return str; + } + + // TODO: Implement + vec2f Text::find_character_pos(size_t index) { + return vec2f(); + } + + Font* Text::get_font() { + return font; + } + void Text::draw(Window&) { mgl_text_draw(mgl_get_context(), &text); } diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp index 14e18df..cdaa3ba 100644 --- a/src/graphics/Texture.cpp +++ b/src/graphics/Texture.cpp @@ -1,4 +1,5 @@ #include "../../include/mglpp/graphics/Texture.hpp" +#include "../../include/mglpp/graphics/Image.hpp" namespace mgl { Texture::Texture() { @@ -6,7 +7,16 @@ namespace mgl { } Texture::~Texture() { - mgl_texture_unload(&texture); + if(owned) + mgl_texture_unload(&texture); + } + + // static + Texture Texture::reference(mgl_texture ref) { + Texture instance; + instance.texture = ref; + instance.owned = false; + return instance; } bool Texture::load_from_file(const char *filepath) { @@ -16,6 +26,13 @@ namespace mgl { return mgl_texture_load_from_file(&texture, filepath, nullptr) == 0; } + bool Texture::load_from_image(Image &image) { + if(texture.id) + return false; + /* TODO: use the last arg (load options) */ + return mgl_texture_load_from_image(&texture, image.internal_image(), nullptr) == 0; + } + vec2i Texture::get_size() const { return { texture.width, texture.height }; } diff --git a/src/graphics/VertexBuffer.cpp b/src/graphics/VertexBuffer.cpp index 2eb06a8..a821a5d 100644 --- a/src/graphics/VertexBuffer.cpp +++ b/src/graphics/VertexBuffer.cpp @@ -6,8 +6,8 @@ extern "C" { } namespace mgl { - VertexBuffer::VertexBuffer(PrimitiveType primitive_type, Usage usage) : texture(nullptr), primitive_type(primitive_type), usage(usage) { - vertex_buffer.id = 0; + VertexBuffer::VertexBuffer() : texture(nullptr) { + mgl_vertex_buffer_init(&vertex_buffer); } VertexBuffer::~VertexBuffer() { @@ -35,13 +35,12 @@ namespace mgl { return texture; } - bool VertexBuffer::update(const Vertex *vertices, size_t vertex_count) { - if(vertex_buffer.id == 0) { - int res = mgl_vertex_buffer_init(&vertex_buffer, (mgl_primitive_type)primitive_type, (mgl_vertex_buffer_usage)usage); - if(res != 0) - return false; - } - return mgl_vertex_buffer_update(&vertex_buffer, (const mgl_vertex*)vertices, vertex_count) == 0; + size_t VertexBuffer::size() const { + return vertex_buffer.vertex_count; + } + + bool VertexBuffer::update(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Usage usage) { + return mgl_vertex_buffer_update(&vertex_buffer, (const mgl_vertex*)vertices, vertex_count, (mgl_primitive_type)primitive_type, (mgl_vertex_buffer_usage)usage) == 0; } void VertexBuffer::draw(Window&) { diff --git a/src/system/Clock.cpp b/src/system/Clock.cpp new file mode 100644 index 0000000..3fdc781 --- /dev/null +++ b/src/system/Clock.cpp @@ -0,0 +1,15 @@ +#include "../../include/mglpp/system/Clock.hpp" + +namespace mgl { + Clock::Clock() { + mgl_clock_init(&clock); + } + + double Clock::restart() { + return mgl_clock_restart(&clock); + } + + double Clock::get_elapsed_time_seconds() { + return mgl_clock_get_elapsed_time_seconds(&clock); + } +}
\ No newline at end of file diff --git a/src/window/Clipboard.cpp b/src/window/Clipboard.cpp new file mode 100644 index 0000000..b5b156e --- /dev/null +++ b/src/window/Clipboard.cpp @@ -0,0 +1,15 @@ +#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 79e015b..9e1fb75 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -4,7 +4,9 @@ #include "../../include/mglpp/graphics/Shader.hpp" extern "C" { +#include <mgl/graphics/vertex.h> #include <mgl/window/event.h> +#include <mgl/mgl.h> } namespace mgl { @@ -22,6 +24,12 @@ namespace mgl { return mgl_window_create_with_params(&window, title, width, height, 0) == 0; } + bool Window::create(WindowHandle existing_window) { + if(window.window) + return false; + return mgl_window_init_from_existing_window(&window, existing_window) == 0; + } + bool Window::poll_event(Event &event) { if(!window.window) return false; @@ -31,7 +39,7 @@ namespace mgl { return mgl_window_poll_event(&window, &c_event); } - void Window::clear(mgl::Color color) { + void Window::clear(Color color) { if(!window.window) return; mgl_window_clear(&window, mgl_color{color.r, color.g, color.b, color.a}); @@ -40,10 +48,22 @@ namespace mgl { void Window::draw(Drawable &drawable, Shader *shader) { // TODO: Make the opengl context active for this thread and window, if it already isn't if(shader) - mgl::Shader::use(shader); + Shader::use(shader); drawable.draw(*this); if(shader) - mgl::Shader::use(nullptr); + Shader::use(nullptr); + } + + void Window::draw(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Shader *shader) { + // TODO: Make the opengl context active for this thread and window, if it already isn't + if(shader) + Shader::use(shader); + + mgl_context *context = mgl_get_context(); + mgl_vertices_draw(context, (const mgl_vertex*)vertices, vertex_count, (mgl_primitive_type)primitive_type); + + if(shader) + Shader::use(nullptr); } void Window::display() { @@ -52,6 +72,41 @@ namespace mgl { mgl_window_display(&window); } + // TODO: Implement + bool Window::is_open() const { + return true; + } + + // TODO: Implement + bool Window::has_focus() const { + return true; + } + + // TODO: Implement + void Window::close() { + + } + + // TODO: Implement + void Window::set_title(const char *title) { + + } + + // TODO: Implement + void Window::set_framerate_limit(unsigned int fps) { + + } + + // TODO: Implement + void Window::set_key_repeat_enabled(bool enabled) { + + } + + // TODO: Implement + void Window::set_cursor_visible(bool visible) { + + } + vec2i Window::get_size() const { return { window.size.x, window.size.y }; } @@ -59,4 +114,8 @@ namespace mgl { vec2i Window::get_cursor_position() const { return { window.cursor_position.x, window.cursor_position.y }; } + + WindowHandle Window::get_system_handle() const { + return window.window; + } }
\ No newline at end of file |