aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/circle_mask.glsl17
-rw-r--r--tests/main.c19
2 files changed, 36 insertions, 0 deletions
diff --git a/tests/circle_mask.glsl b/tests/circle_mask.glsl
new file mode 100644
index 0000000..0564cf3
--- /dev/null
+++ b/tests/circle_mask.glsl
@@ -0,0 +1,17 @@
+uniform sampler2D texture;
+uniform vec2 resolution;
+
+vec4 circle(vec2 uv, vec2 pos, float rad, vec4 color) {
+ float d = smoothstep(0.0, 2.0, length(pos - uv) - rad);
+ float t = clamp(d, 0.0, 1.0);
+ return vec4(color.rgb, color.a * (1.0 - t));
+}
+
+void main() {
+ vec2 uv = gl_TexCoord[0].xy * resolution;
+ vec2 center = resolution * 0.5;
+ float radius = 0.5 * resolution.y - 1.0;
+
+ vec4 texture_color = texture2D(texture, gl_TexCoord[0].xy);
+ gl_FragColor = circle(uv, center, radius, texture_color);
+} \ No newline at end of file
diff --git a/tests/main.c b/tests/main.c
index 8631c35..0e6e199 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -8,11 +8,13 @@
#include <mgl/graphics/font.h>
#include <mgl/graphics/text.h>
#include <mgl/graphics/vertex_buffer.h>
+#include <mgl/graphics/shader.h>
typedef struct {
mgl_texture *texture;
mgl_font *font;
mgl_vertex_buffer *vertex_buffer;
+ mgl_shader_program *shader_program;
} Userdata;
static void draw(mgl_window *window, void *userdata) {
@@ -26,10 +28,15 @@ static void draw(mgl_window *window, void *userdata) {
};
mgl_rectangle_draw(context, &rect);
+ /*mgl_shader_program_set_uniform_texture2d(u->shader_program, "texture", u->texture);*/
+ mgl_shader_program_set_uniform_vec2f(u->shader_program, "resolution", (mgl_vec2f){ window->size.x, window->size.y });
+
mgl_sprite sprite;
mgl_sprite_init(&sprite, u->texture, 100.0f - 10.0f, 0.0f);
mgl_sprite_set_color(&sprite, (mgl_color){255, 255, 255, 128});
+ mgl_shader_program_use(u->shader_program);
mgl_sprite_draw(context, &sprite);
+ mgl_shader_program_use(NULL);
mgl_text text;
mgl_text_init(&text, u->font, "hello world!\nGood bye world!", 0.0f, 0.0f);
@@ -47,11 +54,13 @@ int main(int argc, char **argv) {
mgl_texture texture;
mgl_font font;
mgl_vertex_buffer vertex_buffer;
+ mgl_shader_program shader_program;
Userdata userdata;
userdata.texture = &texture;
userdata.font = &font;
userdata.vertex_buffer = &vertex_buffer;
+ userdata.shader_program = &shader_program;
mgl_window window;
if(mgl_window_create(&window, "mgl", 1280, 720) != 0)
@@ -63,6 +72,15 @@ int main(int argc, char **argv) {
if(mgl_font_load_from_file(&font, "/usr/share/fonts/noto/NotoSans-Regular.ttf", 32) != 0)
return 1;
+ if(mgl_shader_program_init(&shader_program) != 0)
+ return 1;
+
+ if(mgl_shader_program_add_shader_from_file(&shader_program, "tests/circle_mask.glsl", MGL_SHADER_FRAGMENT) != 0)
+ return 1;
+
+ if(mgl_shader_program_finalize(&shader_program) != 0)
+ return 1;
+
if(mgl_vertex_buffer_init(&vertex_buffer, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0)
return 1;
@@ -106,6 +124,7 @@ int main(int argc, char **argv) {
}
mgl_vertex_buffer_deinit(&vertex_buffer);
+ mgl_shader_program_deinit(&shader_program);
mgl_font_unload(&font);
mgl_texture_unload(&texture);
mgl_window_deinit(&window);