aboutsummaryrefslogtreecommitdiff
path: root/src/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/Font.cpp21
-rw-r--r--src/graphics/Rectangle.cpp25
-rw-r--r--src/graphics/Sprite.cpp28
-rw-r--r--src/graphics/Text.cpp28
-rw-r--r--src/graphics/Texture.cpp26
5 files changed, 128 insertions, 0 deletions
diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp
new file mode 100644
index 0000000..7d157ed
--- /dev/null
+++ b/src/graphics/Font.cpp
@@ -0,0 +1,21 @@
+#include "../../include/mglpp/graphics/Font.hpp"
+#include <string.h>
+namespace mgl {
+ Font::Font() {
+ memset(&font, 0, sizeof(font));
+ }
+
+ Font::~Font() {
+ mgl_font_unload(&font);
+ }
+
+ bool Font::load_from_file(const char *filepath, unsigned int font_size) {
+ if(font.texture.id)
+ return false;
+ return mgl_font_load_from_file(&font, filepath, font_size) == 0;
+ }
+
+ mgl_font* Font::internal_font() {
+ return &font;
+ }
+} \ No newline at end of file
diff --git a/src/graphics/Rectangle.cpp b/src/graphics/Rectangle.cpp
new file mode 100644
index 0000000..61a45f4
--- /dev/null
+++ b/src/graphics/Rectangle.cpp
@@ -0,0 +1,25 @@
+#include "../../include/mglpp/graphics/Rectangle.hpp"
+
+extern "C" {
+#include <mgl/mgl.h>
+}
+
+namespace mgl {
+ Rectangle::Rectangle(vec2f position, vec2f size) {
+ rectangle.color = { 1.0f, 1.0f, 1.0f, 1.0f };
+ rectangle.position = { position.x, position.y };
+ rectangle.size = { size.x, size.y };
+ }
+
+ void Rectangle::set_position(vec2f position) {
+ rectangle.position = { position.x, position.y };
+ }
+
+ void Rectangle::set_color(Color color) {
+ rectangle.color = { color.r, color.g, color.b, color.a };
+ }
+
+ void Rectangle::draw(Window&) {
+ mgl_rectangle_draw(mgl_get_context(), &rectangle);
+ }
+} \ No newline at end of file
diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp
new file mode 100644
index 0000000..ce32aa3
--- /dev/null
+++ b/src/graphics/Sprite.cpp
@@ -0,0 +1,28 @@
+#include "../../include/mglpp/graphics/Sprite.hpp"
+#include "../../include/mglpp/graphics/Texture.hpp"
+
+extern "C" {
+#include <mgl/mgl.h>
+}
+
+namespace mgl {
+ Sprite::Sprite(Texture &texture, vec2f position) : texture(texture) {
+ mgl_sprite_init(&sprite, texture.internal_texture(), position.x, position.y);
+ }
+
+ Sprite::~Sprite() {
+
+ }
+
+ void Sprite::set_position(vec2f position) {
+ mgl_sprite_set_position(&sprite, {position.x, position.y});
+ }
+
+ void Sprite::set_color(Color color) {
+ mgl_sprite_set_color(&sprite, {color.r, color.g, color.b, color.a});
+ }
+
+ void Sprite::draw(Window&) {
+ mgl_sprite_draw(mgl_get_context(), &sprite);
+ }
+} \ No newline at end of file
diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp
new file mode 100644
index 0000000..e598b2f
--- /dev/null
+++ b/src/graphics/Text.cpp
@@ -0,0 +1,28 @@
+#include "../../include/mglpp/graphics/Text.hpp"
+#include "../../include/mglpp/graphics/Font.hpp"
+
+extern "C" {
+#include <mgl/mgl.h>
+}
+
+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() {
+ mgl_text_deinit(&text);
+ }
+
+ void Text::set_position(vec2f position) {
+ mgl_text_set_position(&text, {position.x, position.y});
+ }
+
+ void Text::set_color(Color color) {
+ mgl_text_set_color(&text, {color.r, color.g, color.b, color.a});
+ }
+
+ void Text::draw(Window&) {
+ mgl_text_draw(mgl_get_context(), &text);
+ }
+} \ No newline at end of file
diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp
new file mode 100644
index 0000000..0e6123c
--- /dev/null
+++ b/src/graphics/Texture.cpp
@@ -0,0 +1,26 @@
+#include "../../include/mglpp/graphics/Texture.hpp"
+
+namespace mgl {
+ Texture::Texture() {
+ texture.id = 0;
+ }
+
+ Texture::~Texture() {
+ mgl_texture_unload(&texture);
+ }
+
+ bool Texture::load_from_file(const char *filepath) {
+ if(texture.id)
+ return false;
+ /* TODO: use the last arg (load options) */
+ return mgl_texture_load_from_file(&texture, filepath, nullptr) == 0;
+ }
+
+ vec2i Texture::size() const {
+ return { texture.width, texture.height };
+ }
+
+ mgl_texture* Texture::internal_texture() {
+ return &texture;
+ }
+} \ No newline at end of file