aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-28 17:33:57 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-29 14:31:31 +0200
commit8d525bc1c3506f15a5f68672245f845cebe18eef (patch)
tree243376e2cae0be40b6870ec8fe0082845996df87
parenta80bf6bb6cb8ab8c5a1430f9f9dbc214f71bdddf (diff)
More, todo interfaces
m---------depends/mgl0
-rw-r--r--include/mglpp/graphics/Font.hpp15
-rw-r--r--include/mglpp/graphics/Image.hpp2
-rw-r--r--include/mglpp/graphics/PrimitiveType.hpp18
-rw-r--r--include/mglpp/graphics/Rectangle.hpp1
-rw-r--r--include/mglpp/graphics/Shader.hpp2
-rw-r--r--include/mglpp/graphics/Sprite.hpp9
-rw-r--r--include/mglpp/graphics/Text.hpp15
-rw-r--r--include/mglpp/graphics/Texture.hpp5
-rw-r--r--include/mglpp/graphics/Vertex.hpp19
-rw-r--r--include/mglpp/graphics/VertexBuffer.hpp31
-rw-r--r--include/mglpp/system/Clock.hpp21
-rw-r--r--include/mglpp/system/FloatRect.hpp26
-rw-r--r--include/mglpp/system/vec.hpp34
-rw-r--r--include/mglpp/window/Clipboard.hpp14
-rw-r--r--include/mglpp/window/Window.hpp18
-rw-r--r--src/graphics/Font.cpp17
-rw-r--r--src/graphics/Image.cpp6
-rw-r--r--src/graphics/Rectangle.cpp4
-rw-r--r--src/graphics/Shader.cpp4
-rw-r--r--src/graphics/Sprite.cpp23
-rw-r--r--src/graphics/Text.cpp36
-rw-r--r--src/graphics/Texture.cpp19
-rw-r--r--src/graphics/VertexBuffer.cpp17
-rw-r--r--src/system/Clock.cpp15
-rw-r--r--src/window/Clipboard.cpp15
-rw-r--r--src/window/Window.cpp65
-rw-r--r--tests/main.cpp34
28 files changed, 423 insertions, 62 deletions
diff --git a/depends/mgl b/depends/mgl
-Subproject 58a3a90c1130b032a65b019cb468075f535bfd3
+Subproject 275e851e8a2b11c2efe7c39787bf331dd3b7527
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 <mgl/graphics/font.h>
}
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 <string>
#include "Drawable.hpp"
+#include "../system/FloatRect.hpp"
extern "C" {
#include <mgl/graphics/text.h>
@@ -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 <mgl/graphics/vertex_buffer.h>
}
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 <mgl/system/clock.h>
+}
+
+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 <typename T>
+ class Rect {
+ public:
+ Rect() : position(0.0f, 0.0f), size(0.0f, 0.0f) {}
+ Rect(const vec2<T>& position, const vec2<T>& size) : position(position), size(size) {}
+
+ bool contains(const vec2<T>& point) const {
+ return point.x >= position.x && point.x <= position.x + size.x
+ && point.y >= position.y && point.y <= position.y + size.y;
+ }
+
+ vec2<T> position;
+ vec2<T> size;
+ };
+
+ typedef Rect<int> IntRect;
+ typedef Rect<float> 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<T> operator / (T v) const {
+ return { x / v, y / v };
+ }
+
+ vec2<T>& operator += (vec2<T> other) {
+ x += other.x;
+ y += other.y;
+ return *this;
+ }
+
+ vec2<T>& operator -= (vec2<T> other) {
+ x -= other.x;
+ y -= other.y;
+ return *this;
+ }
+
+ vec2<T>& operator *= (vec2<T> other) {
+ x *= other.x;
+ y *= other.y;
+ return *this;
+ }
+
+ vec2<T>& operator *= (T v) {
+ x *= v;
+ y *= v;
+ return *this;
+ }
+
+ vec2<T>& operator /= (T v) {
+ x /= v;
+ y /= v;
+ return *this;
+ }
+
vec2<float> 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 <string>
+
+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 <stddef.h>
extern "C" {
#include <mgl/window/window.h>
@@ -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;
};
diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp
index 7b5dd68..ae87625 100644
--- a/src/graphics/Font.cpp
+++ b/src/graphics/Font.cpp
@@ -1,5 +1,7 @@
#include "../../include/mglpp/graphics/Font.hpp"
+#include "../../include/mglpp/graphics/Texture.hpp"
#include <string.h>
+
namespace mgl {
Font::Font() {
memset(&font, 0, sizeof(font));
@@ -19,6 +21,21 @@ namespace mgl {
return font.character_size;
}
+ FontGlyph Font::get_glyph(uint32_t codepoint) const {
+ FontGlyph font_glyph;
+ if(font.texture.id == 0)
+ return font_glyph;
+
+ mgl_font_get_glyph(&font, codepoint, (mgl_font_glyph*)&font_glyph);
+ return font_glyph;
+ }
+
+ Texture Font::get_texture() const {
+ if(font.texture.id == 0)
+ return Texture();
+ return Texture::reference(font.texture);
+ }
+
mgl_font* Font::internal_font() {
return &font;
}
diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp
index 493fad7..2d9b58d 100644
--- a/src/graphics/Image.cpp
+++ b/src/graphics/Image.cpp
@@ -16,6 +16,12 @@ namespace mgl {
return mgl_image_load_from_file(&image, filepath) == 0;
}
+ bool Image::load_from_memory(const unsigned char *data, size_t size) {
+ if(image.data)
+ return false;
+ return mgl_image_load_from_memory(&image, data, size) == 0;
+ }
+
unsigned char* Image::data() {
return image.data;
}
diff --git a/src/graphics/Rectangle.cpp b/src/graphics/Rectangle.cpp
index c43d2c4..179d6ee 100644
--- a/src/graphics/Rectangle.cpp
+++ b/src/graphics/Rectangle.cpp
@@ -5,7 +5,9 @@ extern "C" {
}
namespace mgl {
- Rectangle::Rectangle() : Rectangle(mgl::vec2f(0.0f, 0.0f), mgl::vec2f(0.0f, 0.0f)) {}
+ Rectangle::Rectangle() : Rectangle(vec2f(0.0f, 0.0f), vec2f(0.0f, 0.0f)) {}
+
+ Rectangle::Rectangle(vec2f size) : Rectangle(vec2f(0.0f, 0.0f), size) {}
Rectangle::Rectangle(vec2f position, vec2f size) {
rectangle.color = { 255, 255, 255, 255 };
diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp
index 96fe8ba..c23d32b 100644
--- a/src/graphics/Shader.cpp
+++ b/src/graphics/Shader.cpp
@@ -30,6 +30,10 @@ namespace mgl {
return mgl_shader_program_set_uniform_vec2f(&shader_program, name, { value.x, value.y }) == 0;
}
+ bool Shader::is_valid() const {
+ return shader_program.id != 0;
+ }
+
// static
void Shader::use(Shader *shader) {
if(shader)
diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp
index baf118e..b9066ee 100644
--- a/src/graphics/Sprite.cpp
+++ b/src/graphics/Sprite.cpp
@@ -6,14 +6,21 @@ extern "C" {
}
namespace mgl {
- Sprite::Sprite(Texture &texture, vec2f position) : texture(texture) {
- mgl_sprite_init(&sprite, texture.internal_texture(), position.x, position.y);
+ Sprite::Sprite() : Sprite(nullptr, vec2f(0.0f, 0.0f)) {}
+
+ Sprite::Sprite(Texture *texture, vec2f position) : texture(texture) {
+ mgl_sprite_init(&sprite, texture ? texture->internal_texture() : nullptr, position.x, position.y);
}
Sprite::~Sprite() {
}
+ void Sprite::set_texture(Texture *texture) {
+ this->texture = texture;
+ mgl_sprite_set_texture(&sprite, texture ? texture->internal_texture() : nullptr);
+ }
+
void Sprite::set_position(vec2f position) {
mgl_sprite_set_position(&sprite, {position.x, position.y});
}
@@ -34,12 +41,22 @@ namespace mgl {
sprite.scale = { scale, scale };
}
+ // TODO: Implement
+ void Sprite::set_rotation(float degrees) {
+
+ }
+
+ // TODO: Implement
+ void Sprite::set_origin(vec2f origin) {
+
+ }
+
vec2f Sprite::get_scale() const {
return { sprite.scale.x, sprite.scale.y };
}
const Texture* Sprite::get_texture() const {
- return &texture;
+ return texture;
}
void Sprite::draw(Window&) {
diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp
index 7d08c22..dbaa7c1 100644
--- a/src/graphics/Text.cpp
+++ b/src/graphics/Text.cpp
@@ -6,12 +6,21 @@ extern "C" {
}
namespace mgl {
- Text::Text(std::string str, Font &font) : Text(std::move(str), mgl::vec2f(0.0f, 0.0f), font){}
+ Text::Text() : font(nullptr) {
+ mgl_text_init(&text, nullptr, nullptr, 0.0f, 0.0f);
+ }
+
+ Text::Text(std::string str, Font &font) : Text(std::move(str), vec2f(0.0f, 0.0f), font) {}
- Text::Text(std::string str, vec2f position, Font &font) : font(font), str(std::move(str)) {
+ 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(const Text &other) {
+ font = other.font;
+ mgl_text_init(&text, font ? font->internal_font() : nullptr, other.str.c_str(), other.text.position.x, other.text.position.y);
+ }
+
Text::~Text() {
mgl_text_deinit(&text);
}
@@ -28,6 +37,29 @@ namespace mgl {
return { text.position.x, text.position.y };
}
+ // TODO: Implement
+ FloatRect Text::get_local_bounds() {
+ return FloatRect();
+ }
+
+ void Text::set_string(std::string str) {
+ this->str = std::move(str);
+ mgl_text_set_string(&text, this->str.c_str());
+ }
+
+ const std::string& Text::get_string() const {
+ return str;
+ }
+
+ // TODO: Implement
+ vec2f Text::find_character_pos(size_t index) {
+ return vec2f();
+ }
+
+ Font* Text::get_font() {
+ return font;
+ }
+
void Text::draw(Window&) {
mgl_text_draw(mgl_get_context(), &text);
}
diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp
index 14e18df..cdaa3ba 100644
--- a/src/graphics/Texture.cpp
+++ b/src/graphics/Texture.cpp
@@ -1,4 +1,5 @@
#include "../../include/mglpp/graphics/Texture.hpp"
+#include "../../include/mglpp/graphics/Image.hpp"
namespace mgl {
Texture::Texture() {
@@ -6,7 +7,16 @@ namespace mgl {
}
Texture::~Texture() {
- mgl_texture_unload(&texture);
+ if(owned)
+ mgl_texture_unload(&texture);
+ }
+
+ // static
+ Texture Texture::reference(mgl_texture ref) {
+ Texture instance;
+ instance.texture = ref;
+ instance.owned = false;
+ return instance;
}
bool Texture::load_from_file(const char *filepath) {
@@ -16,6 +26,13 @@ namespace mgl {
return mgl_texture_load_from_file(&texture, filepath, nullptr) == 0;
}
+ bool Texture::load_from_image(Image &image) {
+ if(texture.id)
+ return false;
+ /* TODO: use the last arg (load options) */
+ return mgl_texture_load_from_image(&texture, image.internal_image(), nullptr) == 0;
+ }
+
vec2i Texture::get_size() const {
return { texture.width, texture.height };
}
diff --git a/src/graphics/VertexBuffer.cpp b/src/graphics/VertexBuffer.cpp
index 2eb06a8..a821a5d 100644
--- a/src/graphics/VertexBuffer.cpp
+++ b/src/graphics/VertexBuffer.cpp
@@ -6,8 +6,8 @@ extern "C" {
}
namespace mgl {
- VertexBuffer::VertexBuffer(PrimitiveType primitive_type, Usage usage) : texture(nullptr), primitive_type(primitive_type), usage(usage) {
- vertex_buffer.id = 0;
+ VertexBuffer::VertexBuffer() : texture(nullptr) {
+ mgl_vertex_buffer_init(&vertex_buffer);
}
VertexBuffer::~VertexBuffer() {
@@ -35,13 +35,12 @@ namespace mgl {
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;
+ size_t VertexBuffer::size() const {
+ return vertex_buffer.vertex_count;
+ }
+
+ bool VertexBuffer::update(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Usage usage) {
+ return mgl_vertex_buffer_update(&vertex_buffer, (const mgl_vertex*)vertices, vertex_count, (mgl_primitive_type)primitive_type, (mgl_vertex_buffer_usage)usage) == 0;
}
void VertexBuffer::draw(Window&) {
diff --git a/src/system/Clock.cpp b/src/system/Clock.cpp
new file mode 100644
index 0000000..3fdc781
--- /dev/null
+++ b/src/system/Clock.cpp
@@ -0,0 +1,15 @@
+#include "../../include/mglpp/system/Clock.hpp"
+
+namespace mgl {
+ Clock::Clock() {
+ mgl_clock_init(&clock);
+ }
+
+ double Clock::restart() {
+ return mgl_clock_restart(&clock);
+ }
+
+ double Clock::get_elapsed_time_seconds() {
+ return mgl_clock_get_elapsed_time_seconds(&clock);
+ }
+} \ No newline at end of file
diff --git a/src/window/Clipboard.cpp b/src/window/Clipboard.cpp
new file mode 100644
index 0000000..b5b156e
--- /dev/null
+++ b/src/window/Clipboard.cpp
@@ -0,0 +1,15 @@
+#include "../../include/mglpp/window/Clipboard.hpp"
+
+namespace mgl {
+ // TODO: Implement
+ // static
+ void Clipboard::set_string(std::string str) {
+
+ }
+
+ // TODO: Implement
+ // static
+ std::string Clipboard::get_string() {
+ return "";
+ }
+} \ No newline at end of file
diff --git a/src/window/Window.cpp b/src/window/Window.cpp
index 79e015b..9e1fb75 100644
--- a/src/window/Window.cpp
+++ b/src/window/Window.cpp
@@ -4,7 +4,9 @@
#include "../../include/mglpp/graphics/Shader.hpp"
extern "C" {
+#include <mgl/graphics/vertex.h>
#include <mgl/window/event.h>
+#include <mgl/mgl.h>
}
namespace mgl {
@@ -22,6 +24,12 @@ namespace mgl {
return mgl_window_create_with_params(&window, title, width, height, 0) == 0;
}
+ bool Window::create(WindowHandle existing_window) {
+ if(window.window)
+ return false;
+ return mgl_window_init_from_existing_window(&window, existing_window) == 0;
+ }
+
bool Window::poll_event(Event &event) {
if(!window.window)
return false;
@@ -31,7 +39,7 @@ namespace mgl {
return mgl_window_poll_event(&window, &c_event);
}
- void Window::clear(mgl::Color color) {
+ void Window::clear(Color color) {
if(!window.window)
return;
mgl_window_clear(&window, mgl_color{color.r, color.g, color.b, color.a});
@@ -40,10 +48,22 @@ namespace mgl {
void Window::draw(Drawable &drawable, Shader *shader) {
// TODO: Make the opengl context active for this thread and window, if it already isn't
if(shader)
- mgl::Shader::use(shader);
+ Shader::use(shader);
drawable.draw(*this);
if(shader)
- mgl::Shader::use(nullptr);
+ Shader::use(nullptr);
+ }
+
+ void Window::draw(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Shader *shader) {
+ // TODO: Make the opengl context active for this thread and window, if it already isn't
+ if(shader)
+ Shader::use(shader);
+
+ mgl_context *context = mgl_get_context();
+ mgl_vertices_draw(context, (const mgl_vertex*)vertices, vertex_count, (mgl_primitive_type)primitive_type);
+
+ if(shader)
+ Shader::use(nullptr);
}
void Window::display() {
@@ -52,6 +72,41 @@ namespace mgl {
mgl_window_display(&window);
}
+ // TODO: Implement
+ bool Window::is_open() const {
+ return true;
+ }
+
+ // TODO: Implement
+ bool Window::has_focus() const {
+ return true;
+ }
+
+ // TODO: Implement
+ void Window::close() {
+
+ }
+
+ // TODO: Implement
+ void Window::set_title(const char *title) {
+
+ }
+
+ // TODO: Implement
+ void Window::set_framerate_limit(unsigned int fps) {
+
+ }
+
+ // TODO: Implement
+ void Window::set_key_repeat_enabled(bool enabled) {
+
+ }
+
+ // TODO: Implement
+ void Window::set_cursor_visible(bool visible) {
+
+ }
+
vec2i Window::get_size() const {
return { window.size.x, window.size.y };
}
@@ -59,4 +114,8 @@ namespace mgl {
vec2i Window::get_cursor_position() const {
return { window.cursor_position.x, window.cursor_position.y };
}
+
+ WindowHandle Window::get_system_handle() const {
+ return window.window;
+ }
} \ No newline at end of file
diff --git a/tests/main.cpp b/tests/main.cpp
index 68e3778..163aece 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -8,7 +8,9 @@
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/graphics/VertexBuffer.hpp>
+#include <mglpp/graphics/Vertex.hpp>
#include <mglpp/graphics/Shader.hpp>
+#include <mglpp/system/Clock.hpp>
struct Delegate {
Delegate() {}
@@ -20,11 +22,11 @@ struct Delegate {
shader_program->set_uniform("resolution", mgl::vec2f(window->get_size().x, window->get_size().y));
- mgl::Sprite sprite(*texture, { 100.0f - 10.0f, 0.0f });
+ mgl::Sprite sprite(texture, { 100.0f - 10.0f, 0.0f });
sprite.set_color({255, 255, 255, 128});
window->draw(sprite, shader_program);
- mgl::Text text("hello world!\nGood bye world!", { 0.0f, 0.0f }, *font);
+ mgl::Text text("hello world!\nelapsed time: " + std::to_string(clock.get_elapsed_time_seconds()), { 0.0f, 0.0f }, *font);
window->draw(text);
vertex_buffer->set_position({ window->get_cursor_position().x + 100, window->get_cursor_position().y });
@@ -36,6 +38,7 @@ struct Delegate {
mgl::Font *font;
mgl::VertexBuffer *vertex_buffer;
mgl::Shader *shader_program;
+ mgl::Clock clock;
};
int main(int argc, char **argv) {
@@ -57,34 +60,35 @@ int main(int argc, char **argv) {
if(!shader.load_from_file("depends/mgl/tests/circle_mask.glsl", mgl::Shader::Fragment))
return 1;
+ mgl::Texture font_texture = font.get_texture();
mgl::Vertex vertices[4] = {
{
{0.0f, 0.0f},
- {255, 0, 0, 100},
- {0.0f, 0.0f}
+ {0.0f, 0.0f},
+ {255, 0, 0, 100}
},
{
- {texture.get_size().x, 0.0f},
- {0, 255, 0, 100},
- {1.0f, 0.0f}
+ {font_texture.get_size().x, 0.0f},
+ {1.0f, 0.0f},
+ {0, 255, 0, 100}
},
{
- {texture.get_size().x, texture.get_size().y},
- {0, 0, 255, 100},
- {1.0f, 1.0f}
+ {font_texture.get_size().x, font_texture.get_size().y},
+ {1.0f, 1.0f},
+ {0, 0, 255, 100}
},
{
- {0.0f, texture.get_size().y},
+ {0.0f, font_texture.get_size().y},
+ {0.0f, 1.0f},
{255, 0, 255, 100},
- {0.0f, 1.0f}
}
};
- mgl::VertexBuffer vertex_buffer(mgl::VertexBuffer::Quads, mgl::VertexBuffer::Static);
- if(!vertex_buffer.update(vertices, 4))
+ mgl::VertexBuffer vertex_buffer;
+ if(!vertex_buffer.update(vertices, 4, mgl::PrimitiveType::Quads, mgl::VertexBuffer::Static))
return 1;
- vertex_buffer.set_texture(&texture);
+ vertex_buffer.set_texture(&font_texture);
Delegate delegate;
delegate.window = &window;