aboutsummaryrefslogtreecommitdiff
path: root/src/graphics
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-22 07:05:55 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-22 07:05:55 +0200
commitc9ee5e1c1feccb073863ba17cbfdcf094f235886 (patch)
treef6b9f3fdc21cecbaca1fd58425c0f93c3372a9d4 /src/graphics
parentfe1588ef18163c7557d3d0a62c085f42f2abfab2 (diff)
Add vertex buffer
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/Font.cpp4
-rw-r--r--src/graphics/Image.cpp8
-rw-r--r--src/graphics/Rectangle.cpp6
-rw-r--r--src/graphics/Sprite.cpp4
-rw-r--r--src/graphics/Text.cpp6
-rw-r--r--src/graphics/Texture.cpp6
-rw-r--r--src/graphics/VertexBuffer.cpp50
7 files changed, 73 insertions, 11 deletions
diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp
index 7b5dd68..52698d3 100644
--- a/src/graphics/Font.cpp
+++ b/src/graphics/Font.cpp
@@ -9,10 +9,10 @@ namespace mgl {
mgl_font_unload(&font);
}
- bool Font::load_from_file(const char *filepath, unsigned int character_size) {
+ bool Font::load_from_file(const std::string &filepath, 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, filepath.c_str(), character_size) == 0;
}
unsigned int Font::get_character_size() const {
diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp
index 4c9a222..c45da15 100644
--- a/src/graphics/Image.cpp
+++ b/src/graphics/Image.cpp
@@ -10,21 +10,21 @@ namespace mgl {
mgl_image_unload(&image);
}
- bool Image::load_from_file(const char *filepath) {
+ bool Image::load_from_file(const std::string &filepath) {
if(image.data)
return false;
- return mgl_image_load_from_file(&image, filepath) == 0;
+ return mgl_image_load_from_file(&image, filepath.c_str()) == 0;
}
unsigned char* Image::data() {
return image.data;
}
- size_t Image::byte_size() {
+ size_t Image::get_byte_size() {
return mgl_image_get_size(&image);
}
- vec2i Image::size() const {
+ vec2i Image::get_size() const {
return { image.width, image.height };
}
diff --git a/src/graphics/Rectangle.cpp b/src/graphics/Rectangle.cpp
index f6961f7..c43d2c4 100644
--- a/src/graphics/Rectangle.cpp
+++ b/src/graphics/Rectangle.cpp
@@ -5,6 +5,8 @@ extern "C" {
}
namespace mgl {
+ Rectangle::Rectangle() : Rectangle(mgl::vec2f(0.0f, 0.0f), mgl::vec2f(0.0f, 0.0f)) {}
+
Rectangle::Rectangle(vec2f position, vec2f size) {
rectangle.color = { 255, 255, 255, 255 };
rectangle.position = { position.x, position.y };
@@ -27,6 +29,10 @@ namespace mgl {
rectangle.size = { size.x, size.y };
}
+ vec2f Rectangle::get_size() const {
+ return { rectangle.size.x, rectangle.size.y };
+ }
+
void Rectangle::draw(Window&) {
mgl_rectangle_draw(mgl_get_context(), &rectangle);
}
diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp
index 63caa79..baf118e 100644
--- a/src/graphics/Sprite.cpp
+++ b/src/graphics/Sprite.cpp
@@ -38,8 +38,8 @@ namespace mgl {
return { sprite.scale.x, sprite.scale.y };
}
- const Texture& Sprite::get_texture() const {
- return texture;
+ const Texture* Sprite::get_texture() const {
+ return &texture;
}
void Sprite::draw(Window&) {
diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp
index 094b198..7d08c22 100644
--- a/src/graphics/Text.cpp
+++ b/src/graphics/Text.cpp
@@ -6,8 +6,10 @@ extern "C" {
}
namespace mgl {
- Text::Text(const char *str, vec2f position, Font &font) : font(font) {
- mgl_text_init(&text, font.internal_font(), str, position.x, position.y);
+ Text::Text(std::string str, Font &font) : Text(std::move(str), mgl::vec2f(0.0f, 0.0f), font){}
+
+ 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() {
diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp
index 0e6123c..14e18df 100644
--- a/src/graphics/Texture.cpp
+++ b/src/graphics/Texture.cpp
@@ -16,10 +16,14 @@ namespace mgl {
return mgl_texture_load_from_file(&texture, filepath, nullptr) == 0;
}
- vec2i Texture::size() const {
+ vec2i Texture::get_size() const {
return { texture.width, texture.height };
}
+ bool Texture::is_valid() const {
+ return texture.id != 0;
+ }
+
mgl_texture* Texture::internal_texture() {
return &texture;
}
diff --git a/src/graphics/VertexBuffer.cpp b/src/graphics/VertexBuffer.cpp
new file mode 100644
index 0000000..2eb06a8
--- /dev/null
+++ b/src/graphics/VertexBuffer.cpp
@@ -0,0 +1,50 @@
+#include "../../include/mglpp/graphics/VertexBuffer.hpp"
+#include "../../include/mglpp/graphics/Texture.hpp"
+
+extern "C" {
+#include <mgl/mgl.h>
+}
+
+namespace mgl {
+ VertexBuffer::VertexBuffer(PrimitiveType primitive_type, Usage usage) : texture(nullptr), primitive_type(primitive_type), usage(usage) {
+ vertex_buffer.id = 0;
+ }
+
+ VertexBuffer::~VertexBuffer() {
+ mgl_vertex_buffer_deinit(&vertex_buffer);
+ }
+
+ void VertexBuffer::set_position(vec2f position) {
+ mgl_vertex_buffer_set_position(&vertex_buffer, {position.x, position.y});
+ }
+
+ void VertexBuffer::set_color(Color color) {
+ // No-op, because the vertex buffer has colors for each vertex
+ (void)color;
+ }
+
+ vec2f VertexBuffer::get_position() const {
+ return { vertex_buffer.position.x, vertex_buffer.position.y };
+ }
+
+ void VertexBuffer::set_texture(Texture *texture) {
+ this->texture = texture;
+ }
+
+ const Texture* VertexBuffer::get_texture() const {
+ 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;
+ }
+
+ void VertexBuffer::draw(Window&) {
+ mgl_vertex_buffer_draw(mgl_get_context(), &vertex_buffer, texture ? texture->internal_texture() : nullptr);
+ }
+} \ No newline at end of file