From 8d525bc1c3506f15a5f68672245f845cebe18eef Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 28 Oct 2021 17:33:57 +0200 Subject: More, todo interfaces --- include/mglpp/graphics/Font.hpp | 15 ++++++++++++++ include/mglpp/graphics/Image.hpp | 2 ++ include/mglpp/graphics/PrimitiveType.hpp | 18 +++++++++++++++++ include/mglpp/graphics/Rectangle.hpp | 1 + include/mglpp/graphics/Shader.hpp | 2 ++ include/mglpp/graphics/Sprite.hpp | 9 +++++++-- include/mglpp/graphics/Text.hpp | 15 +++++++++++++- include/mglpp/graphics/Texture.hpp | 5 +++++ include/mglpp/graphics/Vertex.hpp | 19 ++++++++++++++++++ include/mglpp/graphics/VertexBuffer.hpp | 31 +++++++---------------------- include/mglpp/system/Clock.hpp | 21 ++++++++++++++++++++ include/mglpp/system/FloatRect.hpp | 26 ++++++++++++++++++++++++ include/mglpp/system/vec.hpp | 34 ++++++++++++++++++++++++++++++++ include/mglpp/window/Clipboard.hpp | 14 +++++++++++++ include/mglpp/window/Window.hpp | 18 ++++++++++++++++- 15 files changed, 202 insertions(+), 28 deletions(-) create mode 100644 include/mglpp/graphics/PrimitiveType.hpp create mode 100644 include/mglpp/graphics/Vertex.hpp create mode 100644 include/mglpp/system/Clock.hpp create mode 100644 include/mglpp/system/FloatRect.hpp create mode 100644 include/mglpp/window/Clipboard.hpp (limited to 'include') diff --git a/include/mglpp/graphics/Font.hpp b/include/mglpp/graphics/Font.hpp index 6818277..b032e13 100644 --- a/include/mglpp/graphics/Font.hpp +++ b/include/mglpp/graphics/Font.hpp @@ -1,11 +1,23 @@ #ifndef MGLPP_FONT_HPP #define MGLPP_FONT_HPP +#include "../system/vec.hpp" + extern "C" { #include } namespace mgl { + class Texture; + + struct FontGlyph { + vec2f position; + vec2f size; + vec2f texture_position; + vec2f texture_size; + float advance = 0.0f; + }; + class Font { public: Font(); @@ -13,7 +25,10 @@ namespace mgl { bool load_from_file(const char *filepath, unsigned int character_size); unsigned int get_character_size() const; + // Returns 0 sized glyph if the font doesn't have the codepoint + FontGlyph get_glyph(uint32_t codepoint) const; + Texture get_texture() const; mgl_font* internal_font(); private: mgl_font font; diff --git a/include/mglpp/graphics/Image.hpp b/include/mglpp/graphics/Image.hpp index fd82c0c..1ddeb88 100644 --- a/include/mglpp/graphics/Image.hpp +++ b/include/mglpp/graphics/Image.hpp @@ -14,6 +14,8 @@ namespace mgl { ~Image(); bool load_from_file(const char *filepath); + bool load_from_memory(const unsigned char *data, size_t size); + unsigned char* data(); size_t get_byte_size(); vec2i get_size() const; diff --git a/include/mglpp/graphics/PrimitiveType.hpp b/include/mglpp/graphics/PrimitiveType.hpp new file mode 100644 index 0000000..d68446c --- /dev/null +++ b/include/mglpp/graphics/PrimitiveType.hpp @@ -0,0 +1,18 @@ +#ifndef MGLPP_PRIMITIVE_TYPE_HPP +#define MGLPP_PRIMITIVE_TYPE_HPP + +namespace mgl { + enum class PrimitiveType { + Points, + Lines, + LineStrip, + Triangles, + TriangleStrip, + TriangleFan, + Quads, + QuadStrip, + Polygon + }; +} + +#endif /* MGLPP_PRIMITIVE_TYPE_HPP */ diff --git a/include/mglpp/graphics/Rectangle.hpp b/include/mglpp/graphics/Rectangle.hpp index b1ed1ce..ca13d5f 100644 --- a/include/mglpp/graphics/Rectangle.hpp +++ b/include/mglpp/graphics/Rectangle.hpp @@ -11,6 +11,7 @@ namespace mgl { class Rectangle : public Drawable { public: Rectangle(); + Rectangle(vec2f size); Rectangle(vec2f position, vec2f size); void set_position(vec2f position) override; diff --git a/include/mglpp/graphics/Shader.hpp b/include/mglpp/graphics/Shader.hpp index 4e94a7e..47e9689 100644 --- a/include/mglpp/graphics/Shader.hpp +++ b/include/mglpp/graphics/Shader.hpp @@ -22,6 +22,8 @@ namespace mgl { bool load_from_file(const char *filepath, Type type); bool set_uniform(const char *name, vec2f value); + bool is_valid() const; + // If |shader| is nullptr then no shader is used static void use(Shader *shader); private: diff --git a/include/mglpp/graphics/Sprite.hpp b/include/mglpp/graphics/Sprite.hpp index 3cfe92b..ec9f898 100644 --- a/include/mglpp/graphics/Sprite.hpp +++ b/include/mglpp/graphics/Sprite.hpp @@ -11,14 +11,19 @@ namespace mgl { class Texture; class Sprite : public Drawable { public: - Sprite(Texture &texture, vec2f position); + Sprite(); + Sprite(Texture *texture, vec2f position = vec2f(0.0f, 0.0f)); ~Sprite(); + void set_texture(Texture *texture); + 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); + void set_rotation(float degrees); + void set_origin(vec2f origin); vec2f get_scale() const; @@ -27,7 +32,7 @@ namespace mgl { void draw(Window &window) override; private: mgl_sprite sprite; - Texture &texture; + Texture *texture; }; } diff --git a/include/mglpp/graphics/Text.hpp b/include/mglpp/graphics/Text.hpp index c169075..23f847c 100644 --- a/include/mglpp/graphics/Text.hpp +++ b/include/mglpp/graphics/Text.hpp @@ -3,6 +3,7 @@ #include #include "Drawable.hpp" +#include "../system/FloatRect.hpp" extern "C" { #include @@ -12,18 +13,30 @@ namespace mgl { class Font; class Text : public Drawable { public: + Text(); Text(std::string str, Font &font); Text(std::string str, vec2f position, Font &font); + Text(const Text &other); ~Text(); void set_position(vec2f position) override; void set_color(Color color) override; vec2f get_position() const override; + + FloatRect get_local_bounds(); + void set_string(std::string str); + const std::string& get_string() const; + + // Returns the visual position of a character from its index. + // If the index is out of range, then the position of the end of the string is returned. + vec2f find_character_pos(size_t index); + + Font* get_font(); protected: void draw(Window &window) override; private: mgl_text text; - Font &font; + Font *font; std::string str; }; } diff --git a/include/mglpp/graphics/Texture.hpp b/include/mglpp/graphics/Texture.hpp index c584a7b..9559829 100644 --- a/include/mglpp/graphics/Texture.hpp +++ b/include/mglpp/graphics/Texture.hpp @@ -8,18 +8,23 @@ extern "C" { } namespace mgl { + class Image; class Texture { public: Texture(); ~Texture(); + static Texture reference(mgl_texture ref); + bool load_from_file(const char *filepath); + bool load_from_image(Image &image); vec2i get_size() const; bool is_valid() const; mgl_texture* internal_texture(); private: mgl_texture texture; + bool owned = true; }; } diff --git a/include/mglpp/graphics/Vertex.hpp b/include/mglpp/graphics/Vertex.hpp new file mode 100644 index 0000000..afa27c1 --- /dev/null +++ b/include/mglpp/graphics/Vertex.hpp @@ -0,0 +1,19 @@ +#ifndef MGLPP_VERTEX_HPP +#define MGLPP_VERTEX_HPP + +#include "Color.hpp" +#include "../system/vec.hpp" + +namespace mgl { + struct Vertex { + Vertex() = default; + Vertex(vec2f position, Color color) : position(position), color(color) {} + Vertex(vec2f position, vec2f texcoords, Color color) : position(position), texcoords(texcoords), color(color) {} + + vec2f position; + vec2f texcoords; + Color color; + }; +} + +#endif /* MGLPP_VERTEX_HPP */ diff --git a/include/mglpp/graphics/VertexBuffer.hpp b/include/mglpp/graphics/VertexBuffer.hpp index ca3defd..03d8763 100644 --- a/include/mglpp/graphics/VertexBuffer.hpp +++ b/include/mglpp/graphics/VertexBuffer.hpp @@ -1,43 +1,26 @@ #ifndef MGLPP_VERTEX_BUFFER_HPP #define MGLPP_VERTEX_BUFFER_HPP +#include "PrimitiveType.hpp" #include "Drawable.hpp" -#include "Color.hpp" -#include "../system/vec.hpp" extern "C" { #include } namespace mgl { - struct Vertex { - vec2f position; - Color color; - vec2f texcoords; - }; - class Texture; + class Vertex; + class VertexBuffer : public Drawable { public: - enum PrimitiveType { - Points, - Lines, - LineStrip, - Triangles, - TriangleStrip, - TriangleFan, - Quads, - QuadStrip, - Polygon - }; - enum Usage { Stream, Dynamic, Static }; - VertexBuffer(PrimitiveType primitive_type, Usage usage); + VertexBuffer(); ~VertexBuffer(); void set_position(vec2f position) override; @@ -47,14 +30,14 @@ namespace mgl { void set_texture(Texture *texture); const Texture* get_texture() const; - bool update(const Vertex *vertices, size_t vertex_count); + size_t size() const; + + bool update(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Usage usage); protected: void draw(Window &window) override; private: mgl_vertex_buffer vertex_buffer; Texture *texture; - PrimitiveType primitive_type; - Usage usage; }; } diff --git a/include/mglpp/system/Clock.hpp b/include/mglpp/system/Clock.hpp new file mode 100644 index 0000000..0c24471 --- /dev/null +++ b/include/mglpp/system/Clock.hpp @@ -0,0 +1,21 @@ +#ifndef MGLPP_CLOCK_HPP +#define MGLPP_CLOCK_HPP + +extern "C" { +#include +} + +namespace mgl { + class Clock { + public: + Clock(); + + /* Returns the elapsed time in seconds since the last restart or init, before resetting the clock */ + double restart(); + double get_elapsed_time_seconds(); + private: + mgl_clock clock; + }; +} + +#endif /* MGLPP_CLOCK_HPP */ diff --git a/include/mglpp/system/FloatRect.hpp b/include/mglpp/system/FloatRect.hpp new file mode 100644 index 0000000..e187b50 --- /dev/null +++ b/include/mglpp/system/FloatRect.hpp @@ -0,0 +1,26 @@ +#ifndef MGLPP_FLOAT_RECT_HPP +#define MGLPP_FLOAT_RECT_HPP + +#include "../system/vec.hpp" + +namespace mgl { + template + class Rect { + public: + Rect() : position(0.0f, 0.0f), size(0.0f, 0.0f) {} + Rect(const vec2& position, const vec2& size) : position(position), size(size) {} + + bool contains(const vec2& point) const { + return point.x >= position.x && point.x <= position.x + size.x + && point.y >= position.y && point.y <= position.y + size.y; + } + + vec2 position; + vec2 size; + }; + + typedef Rect IntRect; + typedef Rect FloatRect; +} + +#endif /* MGLPP_FLOAT_RECT_HPP */ diff --git a/include/mglpp/system/vec.hpp b/include/mglpp/system/vec.hpp index a9de662..ea6eba5 100644 --- a/include/mglpp/system/vec.hpp +++ b/include/mglpp/system/vec.hpp @@ -22,6 +22,40 @@ namespace mgl { return { x * v, y * v }; } + vec2 operator / (T v) const { + return { x / v, y / v }; + } + + vec2& operator += (vec2 other) { + x += other.x; + y += other.y; + return *this; + } + + vec2& operator -= (vec2 other) { + x -= other.x; + y -= other.y; + return *this; + } + + vec2& operator *= (vec2 other) { + x *= other.x; + y *= other.y; + return *this; + } + + vec2& operator *= (T v) { + x *= v; + y *= v; + return *this; + } + + vec2& operator /= (T v) { + x /= v; + y /= v; + return *this; + } + vec2 to_vec2f() const { return { (float)x, (float)y }; } diff --git a/include/mglpp/window/Clipboard.hpp b/include/mglpp/window/Clipboard.hpp new file mode 100644 index 0000000..7d5c9d4 --- /dev/null +++ b/include/mglpp/window/Clipboard.hpp @@ -0,0 +1,14 @@ +#ifndef MGLPP_CLIPBOARD_HPP +#define MGLPP_CLIPBOARD_HPP + +#include + +namespace mgl { + class Clipboard { + public: + static void set_string(std::string str); + static std::string get_string(); + }; +} + +#endif /* MGLPP_CLIPBOARD_HPP */ diff --git a/include/mglpp/window/Window.hpp b/include/mglpp/window/Window.hpp index d98f5ef..88a025c 100644 --- a/include/mglpp/window/Window.hpp +++ b/include/mglpp/window/Window.hpp @@ -1,8 +1,10 @@ #ifndef MGLPP_WINDOW_HPP #define MGLPP_WINDOW_HPP +#include "../graphics/PrimitiveType.hpp" #include "../graphics/Color.hpp" #include "../system/vec.hpp" +#include extern "C" { #include @@ -14,6 +16,7 @@ namespace mgl { class Event; class Drawable; class Shader; + class Vertex; class Window { public: @@ -27,13 +30,26 @@ namespace mgl { ~Window(); bool create(const char *title, int width, int height); + // Initialize this window from an existing window + bool create(WindowHandle existing_window); + bool poll_event(Event &event); - void clear(mgl::Color color = mgl::Color(0, 0, 0, 255)); + + void clear(Color color = Color(0, 0, 0, 255)); void draw(Drawable &drawable, Shader *shader = nullptr); + void draw(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Shader *shader = nullptr); void display(); + bool is_open() const; + bool has_focus() const; + void close(); + void set_title(const char *title); + void set_framerate_limit(unsigned int fps); + void set_key_repeat_enabled(bool enabled); + void set_cursor_visible(bool visible); vec2i get_size() const; vec2i get_cursor_position() const; + WindowHandle get_system_handle() const; private: mgl_window window; }; -- cgit v1.2.3