aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-18 01:54:59 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-18 01:54:59 +0200
commit3e081d1669622bbc276b038ddc38ecf0600683c3 (patch)
tree0a92c34f257c75a2b26e63b0ea9da3d594e89cc8
Initial commit with an example
-rw-r--r--.gitignore5
-rw-r--r--.gitmodules3
-rw-r--r--LICENSE19
-rw-r--r--README.md7
m---------depends/mgl0
-rw-r--r--include/mglpp/graphics/Color.hpp13
-rw-r--r--include/mglpp/graphics/Drawable.hpp20
-rw-r--r--include/mglpp/graphics/Font.hpp22
-rw-r--r--include/mglpp/graphics/Rectangle.hpp24
-rw-r--r--include/mglpp/graphics/Sprite.hpp27
-rw-r--r--include/mglpp/graphics/Text.hpp27
-rw-r--r--include/mglpp/graphics/Texture.hpp25
-rw-r--r--include/mglpp/mglpp.hpp12
-rw-r--r--include/mglpp/system/vec.hpp20
-rw-r--r--include/mglpp/window/Window.hpp36
-rw-r--r--project.conf8
-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
-rw-r--r--tests/main.cpp57
24 files changed, 519 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..636c6b9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+# Compiled sibs files
+sibs-build/
+compile_commands.json
+tests/sibs-build/
+tests/compile_commands.json
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..bd1f76a
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "depends/mgl"]
+ path = depends/mgl
+ url = git://git.dec05eba.com/mgl
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..c2e5149
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2021 dec05eba
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE. \ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..9e31fe9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,7 @@
+# Minimal Graphics Library (C++)
+C++ wrapper for [mgl](https://git.dec05eba.com/mgl/about/)
+# Dependencies
+## Build
+`xlib`
+## Runtime
+`libglvnd (libGL.so)` \ No newline at end of file
diff --git a/depends/mgl b/depends/mgl
new file mode 160000
+Subproject 4a96d4a354fbde7fd614075c060aea78a3411b4
diff --git a/include/mglpp/graphics/Color.hpp b/include/mglpp/graphics/Color.hpp
new file mode 100644
index 0000000..d1c1745
--- /dev/null
+++ b/include/mglpp/graphics/Color.hpp
@@ -0,0 +1,13 @@
+#ifndef MGLPP_COLOR_HPP
+#define MGLPP_COLOR_HPP
+
+namespace mgl {
+ struct Color {
+ float r = 1.0f;
+ float g = 1.0f;
+ float b = 1.0f;
+ float a = 1.0f;
+ };
+}
+
+#endif /* MGLPP_COLOR_HPP */
diff --git a/include/mglpp/graphics/Drawable.hpp b/include/mglpp/graphics/Drawable.hpp
new file mode 100644
index 0000000..3aa8d4f
--- /dev/null
+++ b/include/mglpp/graphics/Drawable.hpp
@@ -0,0 +1,20 @@
+#ifndef MGLPP_DRAWABLE_HPP
+#define MGLPP_DRAWABLE_HPP
+
+#include "Color.hpp"
+#include "../system/vec.hpp"
+
+namespace mgl {
+ class Window;
+ class Drawable {
+ friend class Window;
+ public:
+ virtual ~Drawable() = default;
+ virtual void set_position(vec2f position) = 0;
+ virtual void set_color(Color color) = 0;
+ protected:
+ virtual void draw(Window &window) = 0;
+ };
+}
+
+#endif /* MGLPP_DRAWABLE_HPP */
diff --git a/include/mglpp/graphics/Font.hpp b/include/mglpp/graphics/Font.hpp
new file mode 100644
index 0000000..1faa23a
--- /dev/null
+++ b/include/mglpp/graphics/Font.hpp
@@ -0,0 +1,22 @@
+#ifndef MGLPP_FONT_HPP
+#define MGLPP_FONT_HPP
+
+extern "C" {
+#include <mgl/graphics/font.h>
+}
+
+namespace mgl {
+ class Font {
+ public:
+ Font();
+ ~Font();
+
+ bool load_from_file(const char *filepath, unsigned int font_size);
+
+ mgl_font* internal_font();
+ private:
+ mgl_font font;
+ };
+}
+
+#endif /* MGLPP_FONT_HPP */
diff --git a/include/mglpp/graphics/Rectangle.hpp b/include/mglpp/graphics/Rectangle.hpp
new file mode 100644
index 0000000..7bc9373
--- /dev/null
+++ b/include/mglpp/graphics/Rectangle.hpp
@@ -0,0 +1,24 @@
+#ifndef MGLPP_RECTANGLE_HPP
+#define MGLPP_RECTANGLE_HPP
+
+#include "Drawable.hpp"
+
+extern "C" {
+#include <mgl/graphics/rectangle.h>
+}
+
+namespace mgl {
+ class Rectangle : public Drawable {
+ public:
+ Rectangle(vec2f position, vec2f size);
+
+ void set_position(vec2f position) override;
+ void set_color(Color color) override;
+ protected:
+ void draw(Window &window) override;
+ private:
+ mgl_rectangle rectangle;
+ };
+}
+
+#endif /* MGLPP_RECTANGLE_HPP */
diff --git a/include/mglpp/graphics/Sprite.hpp b/include/mglpp/graphics/Sprite.hpp
new file mode 100644
index 0000000..8a03de5
--- /dev/null
+++ b/include/mglpp/graphics/Sprite.hpp
@@ -0,0 +1,27 @@
+#ifndef MGLPP_SPRITE_HPP
+#define MGLPP_SPRITE_HPP
+
+#include "Drawable.hpp"
+
+extern "C" {
+#include <mgl/graphics/sprite.h>
+}
+
+namespace mgl {
+ class Texture;
+ class Sprite : public Drawable {
+ public:
+ Sprite(Texture &texture, vec2f position);
+ ~Sprite();
+
+ void set_position(vec2f position) override;
+ void set_color(Color color) override;
+ protected:
+ void draw(Window &window) override;
+ private:
+ mgl_sprite sprite;
+ Texture &texture;
+ };
+}
+
+#endif /* MGLPP_SPRITE_HPP */
diff --git a/include/mglpp/graphics/Text.hpp b/include/mglpp/graphics/Text.hpp
new file mode 100644
index 0000000..d126994
--- /dev/null
+++ b/include/mglpp/graphics/Text.hpp
@@ -0,0 +1,27 @@
+#ifndef MGLPP_TEXT_HPP
+#define MGLPP_TEXT_HPP
+
+#include "Drawable.hpp"
+
+extern "C" {
+#include <mgl/graphics/text.h>
+}
+
+namespace mgl {
+ class Font;
+ class Text : public Drawable {
+ public:
+ Text(const char *str, vec2f position, Font &font);
+ ~Text();
+
+ void set_position(vec2f position) override;
+ void set_color(Color color) override;
+ protected:
+ void draw(Window &window) override;
+ private:
+ mgl_text text;
+ Font &font;
+ };
+}
+
+#endif /* MGLPP_TEXT_HPP */
diff --git a/include/mglpp/graphics/Texture.hpp b/include/mglpp/graphics/Texture.hpp
new file mode 100644
index 0000000..0bd1740
--- /dev/null
+++ b/include/mglpp/graphics/Texture.hpp
@@ -0,0 +1,25 @@
+#ifndef MGLPP_TEXTURE_HPP
+#define MGLPP_TEXTURE_HPP
+
+#include "../system/vec.hpp"
+
+extern "C" {
+#include <mgl/graphics/texture.h>
+}
+
+namespace mgl {
+ class Texture {
+ public:
+ Texture();
+ ~Texture();
+
+ bool load_from_file(const char *filepath);
+ vec2i size() const;
+
+ mgl_texture* internal_texture();
+ private:
+ mgl_texture texture;
+ };
+}
+
+#endif /* MGLPP_TEXTURE_HPP */
diff --git a/include/mglpp/mglpp.hpp b/include/mglpp/mglpp.hpp
new file mode 100644
index 0000000..6e790af
--- /dev/null
+++ b/include/mglpp/mglpp.hpp
@@ -0,0 +1,12 @@
+#ifndef MGLPP_MGLPP_HPP
+#define MGLPP_MGLPP_HPP
+
+namespace mgl {
+ class Init {
+ public:
+ Init();
+ ~Init();
+ };
+}
+
+#endif /* MGLPP_MGLPP_HPP */
diff --git a/include/mglpp/system/vec.hpp b/include/mglpp/system/vec.hpp
new file mode 100644
index 0000000..40262f0
--- /dev/null
+++ b/include/mglpp/system/vec.hpp
@@ -0,0 +1,20 @@
+#ifndef MGLPP_VEC_HPP
+#define MGLPP_VEC_HPP
+
+namespace mgl {
+ struct vec2f {
+ float x = 0.0f;
+ float y = 0.0f;
+ };
+
+ struct vec2i {
+ int x = 0;
+ int y = 0;
+
+ vec2f to_vec2f() const {
+ return { (float)x, (float)y };
+ }
+ };
+}
+
+#endif /* MGLPP_VEC_HPP */
diff --git a/include/mglpp/window/Window.hpp b/include/mglpp/window/Window.hpp
new file mode 100644
index 0000000..79e999d
--- /dev/null
+++ b/include/mglpp/window/Window.hpp
@@ -0,0 +1,36 @@
+#ifndef MGLPP_WINDOW_HPP
+#define MGLPP_WINDOW_HPP
+
+#include "../system/vec.hpp"
+
+extern "C" {
+#include <mgl/window/window.h>
+}
+
+namespace mgl {
+ class Drawable;
+ class Window {
+ public:
+ class Delegate {
+ public:
+ virtual ~Delegate() = default;
+ virtual void draw() = 0;
+ };
+
+ Window(Delegate *delegate);
+ ~Window();
+
+ bool create(const char *title, int width, int height);
+ void poll_events();
+ void draw();
+ void draw(Drawable &drawable);
+
+ vec2i get_cursor_position() const;
+ Delegate* get_delegate();
+ private:
+ mgl_window window;
+ Delegate *delegate;
+ };
+}
+
+#endif /* MGLPP_WINDOW_HPP */
diff --git a/project.conf b/project.conf
new file mode 100644
index 0000000..5e35843
--- /dev/null
+++ b/project.conf
@@ -0,0 +1,8 @@
+[package]
+name = "mglpp"
+type = "static"
+version = "0.1.0"
+platforms = ["posix"]
+
+[config]
+expose_include_dirs = ["include"] \ No newline at end of file
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
diff --git a/tests/main.cpp b/tests/main.cpp
new file mode 100644
index 0000000..bddfc3f
--- /dev/null
+++ b/tests/main.cpp
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <mglpp/mglpp.hpp>
+#include <mglpp/window/Window.hpp>
+#include <mglpp/graphics/Texture.hpp>
+#include <mglpp/graphics/Sprite.hpp>
+#include <mglpp/graphics/Font.hpp>
+#include <mglpp/graphics/Text.hpp>
+#include <mglpp/graphics/Rectangle.hpp>
+
+struct Delegate : public mgl::Window::Delegate {
+ Delegate() {}
+
+ void draw() override {
+ mgl::Rectangle rect(window->get_cursor_position().to_vec2f(), { 100.0f, 500.0f });
+ rect.set_color({1.0f, 0.0f, 0.0f, 1.0f});
+ window->draw(rect);
+
+ mgl::Sprite sprite(*texture, { 100.0f - 10.0f, 0.0f });
+ sprite.set_color({1.0f, 1.0f, 1.0f, 0.5f});
+ window->draw(sprite);
+
+ mgl::Text text("hello world!\nGood bye world!", { 0.0f, 0.0f }, *font);
+ window->draw(text);
+ }
+
+ mgl::Window *window;
+ mgl::Texture *texture;
+ mgl::Font *font;
+};
+
+int main(int argc, char **argv) {
+ mgl::Init init;
+
+ Delegate delegate;
+ mgl::Window window(&delegate);
+ if(!window.create("mglpp", 1920, 1080))
+ return 1;
+
+ mgl::Texture texture;
+ if(!texture.load_from_file("depends/mgl/tests/X11.png"))
+ return 1;
+
+ mgl::Font font;
+ if(!font.load_from_file("/usr/share/fonts/noto/NotoSans-Regular.ttf", 32))
+ return 1;
+
+ delegate.window = &window;
+ delegate.texture = &texture;
+ delegate.font = &font;
+
+ while(true) {
+ window.poll_events();
+ window.draw();
+ }
+
+ return 0;
+}