From c9ee5e1c1feccb073863ba17cbfdcf094f235886 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 22 Oct 2021 07:05:55 +0200 Subject: Add vertex buffer --- depends/mgl | 2 +- include/mglpp/graphics/Font.hpp | 4 +- include/mglpp/graphics/Image.hpp | 7 +- include/mglpp/graphics/Rectangle.hpp | 2 + include/mglpp/graphics/Sprite.hpp | 2 +- include/mglpp/graphics/Text.hpp | 5 +- include/mglpp/graphics/Texture.hpp | 3 +- include/mglpp/graphics/VertexBuffer.hpp | 61 +++++++++++++++++ include/mglpp/system/vec.hpp | 35 +++++++--- include/mglpp/window/Event.hpp | 116 +++++++++++++++++++++++++++++++- include/mglpp/window/Keyboard.hpp | 113 +++++++++++++++++++++++++++++++ include/mglpp/window/Mouse.hpp | 22 ++++++ src/graphics/Font.cpp | 4 +- src/graphics/Image.cpp | 8 +-- src/graphics/Rectangle.cpp | 6 ++ src/graphics/Sprite.cpp | 4 +- src/graphics/Text.cpp | 6 +- src/graphics/Texture.cpp | 6 +- src/graphics/VertexBuffer.cpp | 50 ++++++++++++++ tests/main.cpp | 35 ++++++++++ 20 files changed, 461 insertions(+), 30 deletions(-) create mode 100644 include/mglpp/graphics/VertexBuffer.hpp create mode 100644 include/mglpp/window/Keyboard.hpp create mode 100644 include/mglpp/window/Mouse.hpp create mode 100644 src/graphics/VertexBuffer.cpp diff --git a/depends/mgl b/depends/mgl index 2d4457a..34e9c32 160000 --- a/depends/mgl +++ b/depends/mgl @@ -1 +1 @@ -Subproject commit 2d4457a5ee926eca221102ee70f118b305ea2670 +Subproject commit 34e9c32c38e992a9ce5a666f2788f8d9e2a20088 diff --git a/include/mglpp/graphics/Font.hpp b/include/mglpp/graphics/Font.hpp index 6818277..af70115 100644 --- a/include/mglpp/graphics/Font.hpp +++ b/include/mglpp/graphics/Font.hpp @@ -1,6 +1,8 @@ #ifndef MGLPP_FONT_HPP #define MGLPP_FONT_HPP +#include + extern "C" { #include } @@ -11,7 +13,7 @@ namespace mgl { Font(); ~Font(); - bool load_from_file(const char *filepath, unsigned int character_size); + bool load_from_file(const std::string &filepath, unsigned int character_size); unsigned int get_character_size() const; mgl_font* internal_font(); diff --git a/include/mglpp/graphics/Image.hpp b/include/mglpp/graphics/Image.hpp index d8df2c3..2f7aed5 100644 --- a/include/mglpp/graphics/Image.hpp +++ b/include/mglpp/graphics/Image.hpp @@ -1,6 +1,7 @@ #ifndef MGLPP_IMAGE_HPP #define MGLPP_IMAGE_HPP +#include #include "../system/vec.hpp" extern "C" { @@ -13,10 +14,10 @@ namespace mgl { Image(); ~Image(); - bool load_from_file(const char *filepath); + bool load_from_file(const std::string &filepath); unsigned char* data(); - size_t byte_size(); - vec2i size() const; + size_t get_byte_size(); + vec2i get_size() const; mgl_image* internal_image(); private: diff --git a/include/mglpp/graphics/Rectangle.hpp b/include/mglpp/graphics/Rectangle.hpp index 44a5f36..b1ed1ce 100644 --- a/include/mglpp/graphics/Rectangle.hpp +++ b/include/mglpp/graphics/Rectangle.hpp @@ -10,12 +10,14 @@ extern "C" { namespace mgl { class Rectangle : public Drawable { public: + Rectangle(); Rectangle(vec2f position, vec2f size); void set_position(vec2f position) override; void set_color(Color color) override; vec2f get_position() const override; void set_size(vec2f size); + vec2f get_size() const; protected: void draw(Window &window) override; private: diff --git a/include/mglpp/graphics/Sprite.hpp b/include/mglpp/graphics/Sprite.hpp index d3b8885..3cfe92b 100644 --- a/include/mglpp/graphics/Sprite.hpp +++ b/include/mglpp/graphics/Sprite.hpp @@ -22,7 +22,7 @@ namespace mgl { vec2f get_scale() const; - const Texture& get_texture() 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 fc02244..c169075 100644 --- a/include/mglpp/graphics/Text.hpp +++ b/include/mglpp/graphics/Text.hpp @@ -1,6 +1,7 @@ #ifndef MGLPP_TEXT_HPP #define MGLPP_TEXT_HPP +#include #include "Drawable.hpp" extern "C" { @@ -11,7 +12,8 @@ namespace mgl { class Font; class Text : public Drawable { public: - Text(const char *str, vec2f position, Font &font); + Text(std::string str, Font &font); + Text(std::string str, vec2f position, Font &font); ~Text(); void set_position(vec2f position) override; @@ -22,6 +24,7 @@ namespace mgl { private: mgl_text text; Font &font; + std::string str; }; } diff --git a/include/mglpp/graphics/Texture.hpp b/include/mglpp/graphics/Texture.hpp index 0bd1740..c584a7b 100644 --- a/include/mglpp/graphics/Texture.hpp +++ b/include/mglpp/graphics/Texture.hpp @@ -14,7 +14,8 @@ namespace mgl { ~Texture(); bool load_from_file(const char *filepath); - vec2i size() const; + vec2i get_size() const; + bool is_valid() const; mgl_texture* internal_texture(); private: diff --git a/include/mglpp/graphics/VertexBuffer.hpp b/include/mglpp/graphics/VertexBuffer.hpp new file mode 100644 index 0000000..ca3defd --- /dev/null +++ b/include/mglpp/graphics/VertexBuffer.hpp @@ -0,0 +1,61 @@ +#ifndef MGLPP_VERTEX_BUFFER_HPP +#define MGLPP_VERTEX_BUFFER_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 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(); + + void set_position(vec2f position) override; + void set_color(Color color) override; + vec2f get_position() const override; + + void set_texture(Texture *texture); + const Texture* get_texture() const; + + bool update(const Vertex *vertices, size_t vertex_count); + protected: + void draw(Window &window) override; + private: + mgl_vertex_buffer vertex_buffer; + Texture *texture; + PrimitiveType primitive_type; + Usage usage; + }; +} + +#endif /* MGLPP_VERTEX_BUFFER_HPP */ diff --git a/include/mglpp/system/vec.hpp b/include/mglpp/system/vec.hpp index ac06a52..a9de662 100644 --- a/include/mglpp/system/vec.hpp +++ b/include/mglpp/system/vec.hpp @@ -2,23 +2,38 @@ #define MGLPP_VEC_HPP namespace mgl { - struct vec2f { - vec2f(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} + template + struct vec2 { + vec2(T x = 0, T y = 0) : x(x), y(y) {} - float x; - float y; - }; + vec2 operator + (vec2 other) const { + return { x + other.x, y + other.y }; + } + + vec2 operator - (vec2 other) const { + return { x - other.x, y - other.y }; + } - struct vec2i { - vec2i(int x = 0, int y = 0) : x(x), y(y) {} + vec2 operator * (vec2 other) const { + return { x * other.x, y * other.y }; + } + + vec2 operator * (T v) const { + return { x * v, y * v }; + } - vec2f to_vec2f() const { + vec2 to_vec2f() const { return { (float)x, (float)y }; } - int x; - int y; + T x; + T y; }; + + using vec2f = vec2; + using vec2d = vec2; + using vec2i = vec2; + using vec2u = vec2; } #endif /* MGLPP_VEC_HPP */ diff --git a/include/mglpp/window/Event.hpp b/include/mglpp/window/Event.hpp index ecfb380..47718ff 100644 --- a/include/mglpp/window/Event.hpp +++ b/include/mglpp/window/Event.hpp @@ -1,10 +1,124 @@ #ifndef MGLPP_EVENT_HPP #define MGLPP_EVENT_HPP +#include "Keyboard.hpp" +#include "Mouse.hpp" +#include + namespace mgl { class Event { public: - + struct SizeEvent { + unsigned int width; + unsigned int height; + }; + + struct KeyEvent { + Keyboard::Key code; + bool alt; + bool control; + bool shift; + bool system; + }; + + struct TextEvent { + uint32_t unicode; + }; + + struct MouseMoveEvent { + int x; + int y; + }; + + struct MouseButtonEvent { + Mouse::Button button; + int x; + int y; + }; + + struct MouseWheelEvent { + int delta; + int x; + int y; + }; + + struct MouseWheelScrollEvent { + Mouse::Wheel wheel; + float delta; + int x; + int y; + }; + + struct JoystickConnectEvent { + unsigned int joystick_id; + }; + + struct JoystickMoveEvent { + unsigned int joystick_id; + /*Joystick::Axis axis;*/ + float position; + }; + + struct JoystickButtonEvent { + unsigned int joystick_id; + unsigned int button; + }; + + struct TouchEvent { + unsigned int finger; + int x; + int y; + }; + + struct SensorEvent { + /*Sensor::Type type;*/ + float x; + float y; + float z; + }; + + enum EventType { + Closed, + Resized, + LostFocus, + GainedFocus, + TextEntered, + KeyPressed, + KeyReleased, + MouseWheelMoved, + MouseWheelScrolled, + MouseButtonPressed, + MouseButtonReleased, + MouseMoved, + MouseEntered, + MouseLeft, + JoystickButtonPressed, + JoystickButtonReleased, + JoystickMoved, + JoystickConnected, + JoystickDisconnected, + TouchBegan, + TouchMoved, + TouchEnded, + SensorChanged + }; + + EventType type; + + union { + SizeEvent size; + KeyEvent key; + TextEvent text; + MouseMoveEvent mouse_move; + MouseButtonEvent mouse_button; + MouseWheelEvent mouse_wheel; + MouseWheelScrollEvent mouse_wheel_scroll; + JoystickMoveEvent joystick_move; + JoystickButtonEvent joystick_button; + JoystickConnectEvent joystick_connect; + TouchEvent touch; + SensorEvent sensor; + }; }; } diff --git a/include/mglpp/window/Keyboard.hpp b/include/mglpp/window/Keyboard.hpp new file mode 100644 index 0000000..2757871 --- /dev/null +++ b/include/mglpp/window/Keyboard.hpp @@ -0,0 +1,113 @@ +#ifndef MGLPP_KEYBOARD_HPP +#define MGLPP_KEYBOARD_HPP + +namespace mgl { + class Keyboard { + public: + enum Key { + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + Num0, + Num1, + Num2, + Num3, + Num4, + Num5, + Num6, + Num7, + Num8, + Num9, + Escape, + LControl, + LShift, + LAlt, + LSystem, + RControl, + RShift, + RAlt, + RSystem, + Menu, + LBracket, + RBracket, + Semicolon, + Comma, + Period, + Quote, + Slash, + Backslash, + Tilde, + Equal, + Hyphen, + Space, + Enter, + Backspace, + Tab, + PageUp, + PageDown, + End, + Home, + Insert, + Delete, + Add, + Subtract, + Multiply, + Divide, + Left, + Right, + Up, + Down, + Numpad0, + Numpad1, + Numpad2, + Numpad3, + Numpad4, + Numpad5, + Numpad6, + Numpad7, + Numpad8, + Numpad9, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + F13, + F14, + F15, + Pause + }; + }; +} + +#endif /* MGLPP_KEYBOARD_HPP */ diff --git a/include/mglpp/window/Mouse.hpp b/include/mglpp/window/Mouse.hpp new file mode 100644 index 0000000..d77659a --- /dev/null +++ b/include/mglpp/window/Mouse.hpp @@ -0,0 +1,22 @@ +#ifndef MGLPP_MOUSE_HPP +#define MGLPP_MOUSE_HPP + +namespace mgl { + class Mouse { + public: + enum Button { + Left, + Right, + Middle, + XButton1, + XButton2 + }; + + enum Wheel { + VerticalWheel, + HorizontalWheel + }; + }; +} + +#endif /* MGLPP_MOUSE_HPP */ diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 7b5dd68..52698d3 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -9,10 +9,10 @@ namespace mgl { mgl_font_unload(&font); } - bool Font::load_from_file(const char *filepath, unsigned int character_size) { + bool Font::load_from_file(const std::string &filepath, unsigned int character_size) { if(font.texture.id) return false; - return mgl_font_load_from_file(&font, filepath, character_size) == 0; + return mgl_font_load_from_file(&font, filepath.c_str(), character_size) == 0; } unsigned int Font::get_character_size() const { diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp index 4c9a222..c45da15 100644 --- a/src/graphics/Image.cpp +++ b/src/graphics/Image.cpp @@ -10,21 +10,21 @@ namespace mgl { mgl_image_unload(&image); } - bool Image::load_from_file(const char *filepath) { + bool Image::load_from_file(const std::string &filepath) { if(image.data) return false; - return mgl_image_load_from_file(&image, filepath) == 0; + return mgl_image_load_from_file(&image, filepath.c_str()) == 0; } unsigned char* Image::data() { return image.data; } - size_t Image::byte_size() { + size_t Image::get_byte_size() { return mgl_image_get_size(&image); } - vec2i Image::size() const { + vec2i Image::get_size() const { return { image.width, image.height }; } diff --git a/src/graphics/Rectangle.cpp b/src/graphics/Rectangle.cpp index f6961f7..c43d2c4 100644 --- a/src/graphics/Rectangle.cpp +++ b/src/graphics/Rectangle.cpp @@ -5,6 +5,8 @@ extern "C" { } namespace mgl { + Rectangle::Rectangle() : Rectangle(mgl::vec2f(0.0f, 0.0f), mgl::vec2f(0.0f, 0.0f)) {} + Rectangle::Rectangle(vec2f position, vec2f size) { rectangle.color = { 255, 255, 255, 255 }; rectangle.position = { position.x, position.y }; @@ -27,6 +29,10 @@ namespace mgl { rectangle.size = { size.x, size.y }; } + vec2f Rectangle::get_size() const { + return { rectangle.size.x, rectangle.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 63caa79..baf118e 100644 --- a/src/graphics/Sprite.cpp +++ b/src/graphics/Sprite.cpp @@ -38,8 +38,8 @@ namespace mgl { return { sprite.scale.x, sprite.scale.y }; } - const Texture& Sprite::get_texture() const { - return texture; + const Texture* Sprite::get_texture() const { + return &texture; } void Sprite::draw(Window&) { diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp index 094b198..7d08c22 100644 --- a/src/graphics/Text.cpp +++ b/src/graphics/Text.cpp @@ -6,8 +6,10 @@ extern "C" { } 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(std::string str, Font &font) : Text(std::move(str), mgl::vec2f(0.0f, 0.0f), font){} + + Text::Text(std::string str, vec2f position, Font &font) : font(font), str(std::move(str)) { + mgl_text_init(&text, font.internal_font(), this->str.c_str(), position.x, position.y); } Text::~Text() { diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp index 0e6123c..14e18df 100644 --- a/src/graphics/Texture.cpp +++ b/src/graphics/Texture.cpp @@ -16,10 +16,14 @@ namespace mgl { return mgl_texture_load_from_file(&texture, filepath, nullptr) == 0; } - vec2i Texture::size() const { + vec2i Texture::get_size() const { return { texture.width, texture.height }; } + bool Texture::is_valid() const { + return texture.id != 0; + } + mgl_texture* Texture::internal_texture() { return &texture; } diff --git a/src/graphics/VertexBuffer.cpp b/src/graphics/VertexBuffer.cpp new file mode 100644 index 0000000..2eb06a8 --- /dev/null +++ b/src/graphics/VertexBuffer.cpp @@ -0,0 +1,50 @@ +#include "../../include/mglpp/graphics/VertexBuffer.hpp" +#include "../../include/mglpp/graphics/Texture.hpp" + +extern "C" { +#include +} + +namespace mgl { + VertexBuffer::VertexBuffer(PrimitiveType primitive_type, Usage usage) : texture(nullptr), primitive_type(primitive_type), usage(usage) { + vertex_buffer.id = 0; + } + + VertexBuffer::~VertexBuffer() { + mgl_vertex_buffer_deinit(&vertex_buffer); + } + + void VertexBuffer::set_position(vec2f position) { + mgl_vertex_buffer_set_position(&vertex_buffer, {position.x, position.y}); + } + + void VertexBuffer::set_color(Color color) { + // No-op, because the vertex buffer has colors for each vertex + (void)color; + } + + vec2f VertexBuffer::get_position() const { + return { vertex_buffer.position.x, vertex_buffer.position.y }; + } + + void VertexBuffer::set_texture(Texture *texture) { + this->texture = texture; + } + + const Texture* VertexBuffer::get_texture() const { + return texture; + } + + bool VertexBuffer::update(const Vertex *vertices, size_t vertex_count) { + if(vertex_buffer.id == 0) { + int res = mgl_vertex_buffer_init(&vertex_buffer, (mgl_primitive_type)primitive_type, (mgl_vertex_buffer_usage)usage); + if(res != 0) + return false; + } + return mgl_vertex_buffer_update(&vertex_buffer, (const mgl_vertex*)vertices, vertex_count) == 0; + } + + void VertexBuffer::draw(Window&) { + mgl_vertex_buffer_draw(mgl_get_context(), &vertex_buffer, texture ? texture->internal_texture() : nullptr); + } +} \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp index 3f149a3..bca9f67 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include struct Delegate { Delegate() {} @@ -22,11 +23,15 @@ struct Delegate { mgl::Text text("hello world!\nGood bye world!", { 0.0f, 0.0f }, *font); window->draw(text); + + vertex_buffer->set_position({ window->get_cursor_position().x + 100, window->get_cursor_position().y }); + window->draw(*vertex_buffer); } mgl::Window *window; mgl::Texture *texture; mgl::Font *font; + mgl::VertexBuffer *vertex_buffer; }; int main(int argc, char **argv) { @@ -44,10 +49,40 @@ int main(int argc, char **argv) { if(!font.load_from_file("/usr/share/fonts/noto/NotoSans-Regular.ttf", 32)) return 1; + mgl::Vertex vertices[4] = { + { + {0.0f, 0.0f}, + {255, 0, 0, 100}, + {0.0f, 0.0f} + }, + { + {texture.get_size().x, 0.0f}, + {0, 255, 0, 100}, + {1.0f, 0.0f} + }, + { + {texture.get_size().x, texture.get_size().y}, + {0, 0, 255, 100}, + {1.0f, 1.0f} + }, + { + {0.0f, texture.get_size().y}, + {255, 0, 255, 100}, + {0.0f, 1.0f} + } + }; + + mgl::VertexBuffer vertex_buffer(mgl::VertexBuffer::Quads, mgl::VertexBuffer::Static); + if(!vertex_buffer.update(vertices, 4)) + return 1; + + vertex_buffer.set_texture(&texture); + Delegate delegate; delegate.window = &window; delegate.texture = &texture; delegate.font = &font; + delegate.vertex_buffer = &vertex_buffer; mgl::Event event; while(true) { -- cgit v1.2.3