aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-12-02 17:51:53 +0100
committerdec05eba <dec05eba@protonmail.com>2021-12-02 17:52:40 +0100
commita77da0acc88c7fb861043fd0dcb9cc6536e591f1 (patch)
tree1711f4c5fd4f0474a080caf872ccb144f5d028dd
parent8792ee2cc5b501f0611e6304f529226a495825db (diff)
Window: add scissor
-rw-r--r--include/mgl/gl.h1
-rw-r--r--include/mgl/window/window.h17
-rw-r--r--src/gl.c1
-rw-r--r--src/window/window.c11
4 files changed, 30 insertions, 0 deletions
diff --git a/include/mgl/gl.h b/include/mgl/gl.h
index 894e57b..2546a68 100644
--- a/include/mgl/gl.h
+++ b/include/mgl/gl.h
@@ -20,6 +20,7 @@ typedef struct {
void (*glXSwapBuffers)(Display *dpy, GLXDrawable drawable);
void (*glViewport)(int x, int y, int width, int height);
+ void (*glScissor)(int x, int y, int width, int height);
void (*glClearColor)(float red, float green, float blue, float alpha);
void (*glClear)(unsigned int mask);
void (*glEnable)(unsigned int cap);
diff --git a/include/mgl/window/window.h b/include/mgl/window/window.h
index 2d1a774..a948932 100644
--- a/include/mgl/window/window.h
+++ b/include/mgl/window/window.h
@@ -22,6 +22,11 @@ typedef struct {
mgl_vec2i size;
} mgl_view;
+typedef struct {
+ mgl_vec2i position;
+ mgl_vec2i size;
+} mgl_scissor;
+
struct mgl_window {
mgl_window_handle window;
void *context;
@@ -29,6 +34,7 @@ struct mgl_window {
/* relative to the top left of the window. only updates when the cursor is inside the window */
mgl_vec2i cursor_position;
mgl_view view;
+ mgl_scissor scissor;
bool open;
bool focused;
bool key_repeat_enabled;
@@ -59,10 +65,21 @@ void mgl_window_display(mgl_window *self);
Make sure to set the view back to the previous view after rendering items
by saving the previous view with |mgl_window_get_view| and then call
|mgl_window_set_view| with that saved view.
+ The view is set to the window size when the window is resized (window resize event).
*/
void mgl_window_set_view(mgl_window *self, mgl_view *new_view);
void mgl_window_get_view(mgl_window *self, mgl_view *view);
+/*
+ This should be called every frame to retain the scissor.
+ Make sure to set the scissor back to the previous view after rendering items
+ by saving the previous scissor with |mgl_window_get_scissor| and then call
+ |mgl_window_set_scissor| with that saved scissor.
+ The scissor is set to the window size when the window is resized (window resize event).
+*/
+void mgl_window_set_scissor(mgl_window *self, mgl_scissor *new_scissor);
+void mgl_window_get_scissor(mgl_window *self, mgl_scissor *scissor);
+
bool mgl_window_is_open(const mgl_window *self);
bool mgl_window_has_focus(const mgl_window *self);
bool mgl_window_is_key_pressed(const mgl_window *self, mgl_key key);
diff --git a/src/gl.c b/src/gl.c
index f7607c2..e9eeacb 100644
--- a/src/gl.c
+++ b/src/gl.c
@@ -34,6 +34,7 @@ int mgl_gl_load(mgl_gl *self) {
{ &self->glXSwapBuffers, "glXSwapBuffers" },
{ &self->glViewport, "glViewport" },
+ { &self->glScissor, "glScissor" },
{ &self->glClearColor, "glClearColor" },
{ &self->glClear, "glClear" },
{ &self->glEnable, "glEnable" },
diff --git a/src/window/window.c b/src/window/window.c
index 13bb4ba..8a6edbb 100644
--- a/src/window/window.c
+++ b/src/window/window.c
@@ -231,6 +231,7 @@ static void mgl_window_on_resize(mgl_window *self, int width, int height) {
view.position = (mgl_vec2i){ 0, 0 };
view.size = self->size;
mgl_window_set_view(self, &view);
+ mgl_window_set_scissor(self, &(mgl_scissor){ .position = { 0, 0 }, .size = self->size });
}
static int mgl_window_init(mgl_window *self, const char *title, const mgl_window_create_params *params, Window existing_window) {
@@ -721,6 +722,16 @@ void mgl_window_get_view(mgl_window *self, mgl_view *view) {
*view = self->view;
}
+void mgl_window_set_scissor(mgl_window *self, mgl_scissor *new_scissor) {
+ mgl_context *context = mgl_get_context();
+ self->scissor = *new_scissor;
+ context->gl.glScissor(self->scissor.position.x, self->scissor.position.y, self->scissor.size.x, self->scissor.size.y);
+}
+
+void mgl_window_get_scissor(mgl_window *self, mgl_scissor *scissor) {
+ *scissor = self->scissor;
+}
+
bool mgl_window_is_open(const mgl_window *self) {
return self->open;
}