aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
m---------depends/mgl0
-rw-r--r--include/mglpp/graphics/Font.hpp4
-rw-r--r--include/mglpp/graphics/Image.hpp3
-rw-r--r--include/mglpp/graphics/Shader.hpp32
-rw-r--r--include/mglpp/window/Window.hpp4
-rw-r--r--src/graphics/Font.cpp4
-rw-r--r--src/graphics/Image.cpp4
-rw-r--r--src/graphics/Shader.cpp40
-rw-r--r--src/window/Window.cpp11
-rw-r--r--tests/main.cpp11
10 files changed, 101 insertions, 12 deletions
diff --git a/depends/mgl b/depends/mgl
-Subproject 34e9c32c38e992a9ce5a666f2788f8d9e2a2008
+Subproject 58a3a90c1130b032a65b019cb468075f535bfd3
diff --git a/include/mglpp/graphics/Font.hpp b/include/mglpp/graphics/Font.hpp
index af70115..6818277 100644
--- a/include/mglpp/graphics/Font.hpp
+++ b/include/mglpp/graphics/Font.hpp
@@ -1,8 +1,6 @@
#ifndef MGLPP_FONT_HPP
#define MGLPP_FONT_HPP
-#include <string>
-
extern "C" {
#include <mgl/graphics/font.h>
}
@@ -13,7 +11,7 @@ namespace mgl {
Font();
~Font();
- bool load_from_file(const std::string &filepath, unsigned int character_size);
+ bool load_from_file(const char *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 2f7aed5..fd82c0c 100644
--- a/include/mglpp/graphics/Image.hpp
+++ b/include/mglpp/graphics/Image.hpp
@@ -1,7 +1,6 @@
#ifndef MGLPP_IMAGE_HPP
#define MGLPP_IMAGE_HPP
-#include <string>
#include "../system/vec.hpp"
extern "C" {
@@ -14,7 +13,7 @@ namespace mgl {
Image();
~Image();
- bool load_from_file(const std::string &filepath);
+ bool load_from_file(const char *filepath);
unsigned char* data();
size_t get_byte_size();
vec2i get_size() const;
diff --git a/include/mglpp/graphics/Shader.hpp b/include/mglpp/graphics/Shader.hpp
new file mode 100644
index 0000000..4e94a7e
--- /dev/null
+++ b/include/mglpp/graphics/Shader.hpp
@@ -0,0 +1,32 @@
+#ifndef MGLPP_SHADER_HPP
+#define MGLPP_SHADER_HPP
+
+#include "../system/vec.hpp"
+
+extern "C" {
+#include <mgl/graphics/shader.h>
+}
+
+namespace mgl {
+ class Shader {
+ public:
+ enum Type {
+ Vertex,
+ Fragment,
+ Geometry
+ };
+
+ Shader();
+ ~Shader();
+
+ bool load_from_file(const char *filepath, Type type);
+ bool set_uniform(const char *name, vec2f value);
+
+ // If |shader| is nullptr then no shader is used
+ static void use(Shader *shader);
+ private:
+ mgl_shader_program shader_program;
+ };
+}
+
+#endif /* MGLPP_SHADER_HPP */
diff --git a/include/mglpp/window/Window.hpp b/include/mglpp/window/Window.hpp
index 916e891..d98f5ef 100644
--- a/include/mglpp/window/Window.hpp
+++ b/include/mglpp/window/Window.hpp
@@ -13,6 +13,7 @@ namespace mgl {
class Event;
class Drawable;
+ class Shader;
class Window {
public:
@@ -28,9 +29,10 @@ namespace mgl {
bool create(const char *title, int width, int height);
bool poll_event(Event &event);
void clear(mgl::Color color = mgl::Color(0, 0, 0, 255));
- void draw(Drawable &drawable);
+ void draw(Drawable &drawable, Shader *shader = nullptr);
void display();
+ vec2i get_size() const;
vec2i get_cursor_position() const;
private:
mgl_window window;
diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp
index 52698d3..7b5dd68 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 std::string &filepath, unsigned int character_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.c_str(), character_size) == 0;
+ return mgl_font_load_from_file(&font, filepath, character_size) == 0;
}
unsigned int Font::get_character_size() const {
diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp
index c45da15..493fad7 100644
--- a/src/graphics/Image.cpp
+++ b/src/graphics/Image.cpp
@@ -10,10 +10,10 @@ namespace mgl {
mgl_image_unload(&image);
}
- bool Image::load_from_file(const std::string &filepath) {
+ bool Image::load_from_file(const char *filepath) {
if(image.data)
return false;
- return mgl_image_load_from_file(&image, filepath.c_str()) == 0;
+ return mgl_image_load_from_file(&image, filepath) == 0;
}
unsigned char* Image::data() {
diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp
new file mode 100644
index 0000000..96fe8ba
--- /dev/null
+++ b/src/graphics/Shader.cpp
@@ -0,0 +1,40 @@
+#include "../../include/mglpp/graphics/Shader.hpp"
+
+namespace mgl {
+ Shader::Shader() {
+ shader_program.id = 0;
+ }
+
+ Shader::~Shader() {
+ mgl_shader_program_deinit(&shader_program);
+ }
+
+ bool Shader::load_from_file(const char *filepath, Type type) {
+ if(shader_program.id)
+ return false;
+
+ int res = mgl_shader_program_init(&shader_program);
+ if(res != 0)
+ return false;
+
+ res = mgl_shader_program_add_shader_from_file(&shader_program, filepath, (mgl_shader_type)type);
+ if(res != 0)
+ return false;
+
+ return mgl_shader_program_finalize(&shader_program) == 0;
+ }
+
+ bool Shader::set_uniform(const char *name, vec2f value) {
+ if(!shader_program.id)
+ return false;
+ return mgl_shader_program_set_uniform_vec2f(&shader_program, name, { value.x, value.y }) == 0;
+ }
+
+ // static
+ void Shader::use(Shader *shader) {
+ if(shader)
+ mgl_shader_program_use(&shader->shader_program);
+ else
+ mgl_shader_program_use(nullptr);
+ }
+} \ No newline at end of file
diff --git a/src/window/Window.cpp b/src/window/Window.cpp
index bc1f5ac..79e015b 100644
--- a/src/window/Window.cpp
+++ b/src/window/Window.cpp
@@ -1,6 +1,7 @@
#include "../../include/mglpp/window/Window.hpp"
#include "../../include/mglpp/window/Event.hpp"
#include "../../include/mglpp/graphics/Drawable.hpp"
+#include "../../include/mglpp/graphics/Shader.hpp"
extern "C" {
#include <mgl/window/event.h>
@@ -36,9 +37,13 @@ namespace mgl {
mgl_window_clear(&window, mgl_color{color.r, color.g, color.b, color.a});
}
- void Window::draw(Drawable &drawable) {
+ 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);
drawable.draw(*this);
+ if(shader)
+ mgl::Shader::use(nullptr);
}
void Window::display() {
@@ -47,6 +52,10 @@ namespace mgl {
mgl_window_display(&window);
}
+ vec2i Window::get_size() const {
+ return { window.size.x, window.size.y };
+ }
+
vec2i Window::get_cursor_position() const {
return { window.cursor_position.x, window.cursor_position.y };
}
diff --git a/tests/main.cpp b/tests/main.cpp
index bca9f67..68e3778 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -8,6 +8,7 @@
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/graphics/VertexBuffer.hpp>
+#include <mglpp/graphics/Shader.hpp>
struct Delegate {
Delegate() {}
@@ -17,9 +18,11 @@ struct Delegate {
rect.set_color({255, 0, 0, 255});
window->draw(rect);
+ 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 });
sprite.set_color({255, 255, 255, 128});
- window->draw(sprite);
+ window->draw(sprite, shader_program);
mgl::Text text("hello world!\nGood bye world!", { 0.0f, 0.0f }, *font);
window->draw(text);
@@ -32,6 +35,7 @@ struct Delegate {
mgl::Texture *texture;
mgl::Font *font;
mgl::VertexBuffer *vertex_buffer;
+ mgl::Shader *shader_program;
};
int main(int argc, char **argv) {
@@ -49,6 +53,10 @@ int main(int argc, char **argv) {
if(!font.load_from_file("/usr/share/fonts/noto/NotoSans-Regular.ttf", 32))
return 1;
+ mgl::Shader shader;
+ if(!shader.load_from_file("depends/mgl/tests/circle_mask.glsl", mgl::Shader::Fragment))
+ return 1;
+
mgl::Vertex vertices[4] = {
{
{0.0f, 0.0f},
@@ -83,6 +91,7 @@ int main(int argc, char **argv) {
delegate.texture = &texture;
delegate.font = &font;
delegate.vertex_buffer = &vertex_buffer;
+ delegate.shader_program = &shader;
mgl::Event event;
while(true) {