diff options
Diffstat (limited to 'src/graphics')
-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 |
8 files changed, 110 insertions, 16 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&) { |