aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
-rw-r--r--src/mglpp.cpp14
-rw-r--r--src/window/Window.cpp52
7 files changed, 194 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
diff --git a/src/mglpp.cpp b/src/mglpp.cpp
new file mode 100644
index 0000000..6bf6307
--- /dev/null
+++ b/src/mglpp.cpp
@@ -0,0 +1,14 @@
+#include "../include/mglpp/mglpp.hpp"
+extern "C" {
+#include <mgl/mgl.h>
+}
+
+namespace mgl {
+ Init::Init() {
+ mgl_init();
+ }
+
+ Init::~Init() {
+ mgl_deinit();
+ }
+} \ No newline at end of file
diff --git a/src/window/Window.cpp b/src/window/Window.cpp
new file mode 100644
index 0000000..d4518eb
--- /dev/null
+++ b/src/window/Window.cpp
@@ -0,0 +1,52 @@
+#include "../../include/mglpp/window/Window.hpp"
+#include "../../include/mglpp/graphics/Drawable.hpp"
+
+namespace mgl {
+ static void draw_callback(mgl_window *window, void *userdata) {
+ Window *windowpp = (Window*)userdata;
+ windowpp->get_delegate()->draw();
+ }
+
+ Window::Window(Delegate *delegate) : delegate(delegate) {
+ window.window = 0;
+ }
+
+ Window::~Window() {
+ mgl_window_deinit(&window);
+ }
+
+ bool Window::create(const char *title, int width, int height) {
+ if(window.window)
+ return false;
+
+ mgl_window_callback callback;
+ callback.userdata = this;
+ callback.draw = draw_callback;
+ return mgl_window_create_with_params(&window, title, width, height, 0, &callback) == 0;
+ }
+
+ void Window::poll_events() {
+ if(!window.window)
+ return;
+ mgl_window_events_poll(&window);
+ }
+
+ void Window::draw() {
+ if(!window.window)
+ return;
+ mgl_window_draw(&window);
+ }
+
+ void Window::draw(Drawable &drawable) {
+ // TODO: Make the opengl context active for this thread and window, if it already isn't
+ drawable.draw(*this);
+ }
+
+ vec2i Window::get_cursor_position() const {
+ return { window.cursor_position.x, window.cursor_position.y };
+ }
+
+ Window::Delegate* Window::get_delegate() {
+ return delegate;
+ }
+} \ No newline at end of file