aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-19 22:22:07 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-19 22:22:07 +0200
commitfe1588ef18163c7557d3d0a62c085f42f2abfab2 (patch)
treed73f54a47ebfe6b2d289ed42c9a738a94e6095d1
parent6a524b46cb9c74c8d1b710c23bbc62bdafd54bb2 (diff)
Change event layout to similar to same as sfml
m---------depends/mgl0
-rw-r--r--include/mglpp/graphics/Color.hpp11
-rw-r--r--include/mglpp/graphics/Drawable.hpp3
-rw-r--r--include/mglpp/graphics/Font.hpp3
-rw-r--r--include/mglpp/graphics/Image.hpp27
-rw-r--r--include/mglpp/graphics/Rectangle.hpp2
-rw-r--r--include/mglpp/graphics/Sprite.hpp7
-rw-r--r--include/mglpp/graphics/Text.hpp1
-rw-r--r--include/mglpp/system/vec.hpp12
-rw-r--r--include/mglpp/window/Event.hpp11
-rw-r--r--include/mglpp/window/Window.hpp14
-rw-r--r--src/graphics/Font.cpp8
-rw-r--r--src/graphics/Image.cpp34
-rw-r--r--src/graphics/Rectangle.cpp8
-rw-r--r--src/graphics/Sprite.cpp20
-rw-r--r--src/graphics/Text.cpp4
-rw-r--r--src/window/Window.cpp41
-rw-r--r--tests/main.cpp19
18 files changed, 187 insertions, 38 deletions
diff --git a/depends/mgl b/depends/mgl
-Subproject 3bdf82eec2c915e91ae487e29d72639f9efcad6
+Subproject 2d4457a5ee926eca221102ee70f118b305ea267
diff --git a/include/mglpp/graphics/Color.hpp b/include/mglpp/graphics/Color.hpp
index 0d0ac15..1cade84 100644
--- a/include/mglpp/graphics/Color.hpp
+++ b/include/mglpp/graphics/Color.hpp
@@ -9,6 +9,17 @@ namespace mgl {
}
+ bool operator == (const Color &other) const {
+ return r == other.r
+ && g == other.g
+ && b == other.b
+ && a == other.a;
+ }
+
+ bool operator != (const Color &other) const {
+ return !(operator==(other));
+ }
+
uint8_t r, g, b, a;
};
}
diff --git a/include/mglpp/graphics/Drawable.hpp b/include/mglpp/graphics/Drawable.hpp
index 3aa8d4f..f4766fb 100644
--- a/include/mglpp/graphics/Drawable.hpp
+++ b/include/mglpp/graphics/Drawable.hpp
@@ -10,8 +10,11 @@ namespace mgl {
friend class Window;
public:
virtual ~Drawable() = default;
+
virtual void set_position(vec2f position) = 0;
virtual void set_color(Color color) = 0;
+
+ virtual vec2f get_position() const = 0;
protected:
virtual void draw(Window &window) = 0;
};
diff --git a/include/mglpp/graphics/Font.hpp b/include/mglpp/graphics/Font.hpp
index 1faa23a..6818277 100644
--- a/include/mglpp/graphics/Font.hpp
+++ b/include/mglpp/graphics/Font.hpp
@@ -11,7 +11,8 @@ namespace mgl {
Font();
~Font();
- bool load_from_file(const char *filepath, unsigned int font_size);
+ bool load_from_file(const char *filepath, unsigned int character_size);
+ unsigned int get_character_size() const;
mgl_font* internal_font();
private:
diff --git a/include/mglpp/graphics/Image.hpp b/include/mglpp/graphics/Image.hpp
new file mode 100644
index 0000000..d8df2c3
--- /dev/null
+++ b/include/mglpp/graphics/Image.hpp
@@ -0,0 +1,27 @@
+#ifndef MGLPP_IMAGE_HPP
+#define MGLPP_IMAGE_HPP
+
+#include "../system/vec.hpp"
+
+extern "C" {
+#include <mgl/graphics/image.h>
+}
+
+namespace mgl {
+ class Image {
+ public:
+ Image();
+ ~Image();
+
+ bool load_from_file(const char *filepath);
+ unsigned char* data();
+ size_t byte_size();
+ vec2i size() const;
+
+ mgl_image* internal_image();
+ private:
+ mgl_image image;
+ };
+}
+
+#endif /* MGLPP_IMAGE_HPP */
diff --git a/include/mglpp/graphics/Rectangle.hpp b/include/mglpp/graphics/Rectangle.hpp
index 7bc9373..44a5f36 100644
--- a/include/mglpp/graphics/Rectangle.hpp
+++ b/include/mglpp/graphics/Rectangle.hpp
@@ -14,6 +14,8 @@ namespace mgl {
void set_position(vec2f position) override;
void set_color(Color color) override;
+ vec2f get_position() const override;
+ void set_size(vec2f size);
protected:
void draw(Window &window) override;
private:
diff --git a/include/mglpp/graphics/Sprite.hpp b/include/mglpp/graphics/Sprite.hpp
index 8a03de5..d3b8885 100644
--- a/include/mglpp/graphics/Sprite.hpp
+++ b/include/mglpp/graphics/Sprite.hpp
@@ -16,6 +16,13 @@ namespace mgl {
void set_position(vec2f position) override;
void set_color(Color color) override;
+ vec2f get_position() const override;
+ void set_scale(vec2f scale);
+ void set_scale(float scale);
+
+ vec2f get_scale() const;
+
+ const Texture& get_texture() const;
protected:
void draw(Window &window) override;
private:
diff --git a/include/mglpp/graphics/Text.hpp b/include/mglpp/graphics/Text.hpp
index d126994..fc02244 100644
--- a/include/mglpp/graphics/Text.hpp
+++ b/include/mglpp/graphics/Text.hpp
@@ -16,6 +16,7 @@ namespace mgl {
void set_position(vec2f position) override;
void set_color(Color color) override;
+ vec2f get_position() const override;
protected:
void draw(Window &window) override;
private:
diff --git a/include/mglpp/system/vec.hpp b/include/mglpp/system/vec.hpp
index 40262f0..ac06a52 100644
--- a/include/mglpp/system/vec.hpp
+++ b/include/mglpp/system/vec.hpp
@@ -3,17 +3,21 @@
namespace mgl {
struct vec2f {
- float x = 0.0f;
- float y = 0.0f;
+ vec2f(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
+
+ float x;
+ float y;
};
struct vec2i {
- int x = 0;
- int y = 0;
+ vec2i(int x = 0, int y = 0) : x(x), y(y) {}
vec2f to_vec2f() const {
return { (float)x, (float)y };
}
+
+ int x;
+ int y;
};
}
diff --git a/include/mglpp/window/Event.hpp b/include/mglpp/window/Event.hpp
new file mode 100644
index 0000000..ecfb380
--- /dev/null
+++ b/include/mglpp/window/Event.hpp
@@ -0,0 +1,11 @@
+#ifndef MGLPP_EVENT_HPP
+#define MGLPP_EVENT_HPP
+
+namespace mgl {
+ class Event {
+ public:
+
+ };
+}
+
+#endif /* MGLPP_EVENT_HPP */
diff --git a/include/mglpp/window/Window.hpp b/include/mglpp/window/Window.hpp
index 79e999d..916e891 100644
--- a/include/mglpp/window/Window.hpp
+++ b/include/mglpp/window/Window.hpp
@@ -1,6 +1,7 @@
#ifndef MGLPP_WINDOW_HPP
#define MGLPP_WINDOW_HPP
+#include "../graphics/Color.hpp"
#include "../system/vec.hpp"
extern "C" {
@@ -8,7 +9,11 @@ extern "C" {
}
namespace mgl {
+ typedef mgl_window_handle WindowHandle;
+
+ class Event;
class Drawable;
+
class Window {
public:
class Delegate {
@@ -17,19 +22,18 @@ namespace mgl {
virtual void draw() = 0;
};
- Window(Delegate *delegate);
+ Window();
~Window();
bool create(const char *title, int width, int height);
- void poll_events();
- void draw();
+ bool poll_event(Event &event);
+ void clear(mgl::Color color = mgl::Color(0, 0, 0, 255));
void draw(Drawable &drawable);
+ void display();
vec2i get_cursor_position() const;
- Delegate* get_delegate();
private:
mgl_window window;
- Delegate *delegate;
};
}
diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp
index 7d157ed..7b5dd68 100644
--- a/src/graphics/Font.cpp
+++ b/src/graphics/Font.cpp
@@ -9,10 +9,14 @@ namespace mgl {
mgl_font_unload(&font);
}
- bool Font::load_from_file(const char *filepath, unsigned int font_size) {
+ bool Font::load_from_file(const char *filepath, unsigned int character_size) {
if(font.texture.id)
return false;
- return mgl_font_load_from_file(&font, filepath, font_size) == 0;
+ return mgl_font_load_from_file(&font, filepath, character_size) == 0;
+ }
+
+ unsigned int Font::get_character_size() const {
+ return font.character_size;
}
mgl_font* Font::internal_font() {
diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp
new file mode 100644
index 0000000..4c9a222
--- /dev/null
+++ b/src/graphics/Image.cpp
@@ -0,0 +1,34 @@
+#include "../../include/mglpp/graphics/Image.hpp"
+#include <string.h>
+
+namespace mgl {
+ Image::Image() {
+ memset(&image, 0, sizeof(image));
+ }
+
+ Image::~Image() {
+ mgl_image_unload(&image);
+ }
+
+ bool Image::load_from_file(const char *filepath) {
+ if(image.data)
+ return false;
+ return mgl_image_load_from_file(&image, filepath) == 0;
+ }
+
+ unsigned char* Image::data() {
+ return image.data;
+ }
+
+ size_t Image::byte_size() {
+ return mgl_image_get_size(&image);
+ }
+
+ vec2i Image::size() const {
+ return { image.width, image.height };
+ }
+
+ mgl_image* Image::internal_image() {
+ return &image;
+ }
+} \ No newline at end of file
diff --git a/src/graphics/Rectangle.cpp b/src/graphics/Rectangle.cpp
index 17060c7..f6961f7 100644
--- a/src/graphics/Rectangle.cpp
+++ b/src/graphics/Rectangle.cpp
@@ -19,6 +19,14 @@ namespace mgl {
rectangle.color = { color.r, color.g, color.b, color.a };
}
+ vec2f Rectangle::get_position() const {
+ return { rectangle.position.x, rectangle.position.y };
+ }
+
+ void Rectangle::set_size(vec2f size) {
+ rectangle.size = { size.x, 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 ce32aa3..63caa79 100644
--- a/src/graphics/Sprite.cpp
+++ b/src/graphics/Sprite.cpp
@@ -22,6 +22,26 @@ namespace mgl {
mgl_sprite_set_color(&sprite, {color.r, color.g, color.b, color.a});
}
+ vec2f Sprite::get_position() const {
+ return { sprite.position.x, sprite.position.y };
+ }
+
+ void Sprite::set_scale(vec2f scale) {
+ sprite.scale = { scale.x, scale.y };
+ }
+
+ void Sprite::set_scale(float scale) {
+ sprite.scale = { scale, scale };
+ }
+
+ vec2f Sprite::get_scale() const {
+ return { sprite.scale.x, sprite.scale.y };
+ }
+
+ const Texture& Sprite::get_texture() const {
+ return texture;
+ }
+
void Sprite::draw(Window&) {
mgl_sprite_draw(mgl_get_context(), &sprite);
}
diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp
index e598b2f..094b198 100644
--- a/src/graphics/Text.cpp
+++ b/src/graphics/Text.cpp
@@ -22,6 +22,10 @@ namespace mgl {
mgl_text_set_color(&text, {color.r, color.g, color.b, color.a});
}
+ vec2f Text::get_position() const {
+ return { text.position.x, text.position.y };
+ }
+
void Text::draw(Window&) {
mgl_text_draw(mgl_get_context(), &text);
}
diff --git a/src/window/Window.cpp b/src/window/Window.cpp
index d4518eb..bc1f5ac 100644
--- a/src/window/Window.cpp
+++ b/src/window/Window.cpp
@@ -1,13 +1,13 @@
#include "../../include/mglpp/window/Window.hpp"
+#include "../../include/mglpp/window/Event.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();
- }
+extern "C" {
+#include <mgl/window/event.h>
+}
- Window::Window(Delegate *delegate) : delegate(delegate) {
+namespace mgl {
+ Window::Window() {
window.window = 0;
}
@@ -18,23 +18,22 @@ namespace mgl {
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;
+ return mgl_window_create_with_params(&window, title, width, height, 0) == 0;
}
- void Window::poll_events() {
+ bool Window::poll_event(Event &event) {
if(!window.window)
- return;
- mgl_window_events_poll(&window);
+ return false;
+
+ /* TODO: Convert c_event to |event| */
+ mgl_event c_event;
+ return mgl_window_poll_event(&window, &c_event);
}
- void Window::draw() {
+ void Window::clear(mgl::Color color) {
if(!window.window)
return;
- mgl_window_draw(&window);
+ mgl_window_clear(&window, mgl_color{color.r, color.g, color.b, color.a});
}
void Window::draw(Drawable &drawable) {
@@ -42,11 +41,13 @@ namespace mgl {
drawable.draw(*this);
}
- vec2i Window::get_cursor_position() const {
- return { window.cursor_position.x, window.cursor_position.y };
+ void Window::display() {
+ if(!window.window)
+ return;
+ mgl_window_display(&window);
}
- Window::Delegate* Window::get_delegate() {
- return delegate;
+ vec2i Window::get_cursor_position() const {
+ return { window.cursor_position.x, window.cursor_position.y };
}
} \ No newline at end of file
diff --git a/tests/main.cpp b/tests/main.cpp
index 3595717..3f149a3 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -1,16 +1,17 @@
#include <stdio.h>
#include <mglpp/mglpp.hpp>
#include <mglpp/window/Window.hpp>
+#include <mglpp/window/Event.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 {
+struct Delegate {
Delegate() {}
- void draw() override {
+ void draw() {
mgl::Rectangle rect(window->get_cursor_position().to_vec2f(), { 100.0f, 500.0f });
rect.set_color({255, 0, 0, 255});
window->draw(rect);
@@ -31,8 +32,7 @@ struct Delegate : public mgl::Window::Delegate {
int main(int argc, char **argv) {
mgl::Init init;
- Delegate delegate;
- mgl::Window window(&delegate);
+ mgl::Window window;
if(!window.create("mglpp", 1920, 1080))
return 1;
@@ -44,13 +44,20 @@ int main(int argc, char **argv) {
if(!font.load_from_file("/usr/share/fonts/noto/NotoSans-Regular.ttf", 32))
return 1;
+ Delegate delegate;
delegate.window = &window;
delegate.texture = &texture;
delegate.font = &font;
+ mgl::Event event;
while(true) {
- window.poll_events();
- window.draw();
+ if(window.poll_event(event)) {
+
+ }
+
+ window.clear(mgl::Color(0, 0, 0, 255));
+ delegate.draw();
+ window.display();
}
return 0;