diff options
-rw-r--r-- | include/mgl/window/window.h | 9 | ||||
-rw-r--r-- | src/window/window.c | 12 | ||||
-rw-r--r-- | tests/main.c | 11 |
3 files changed, 18 insertions, 14 deletions
diff --git a/include/mgl/window/window.h b/include/mgl/window/window.h index 8e3f626..ad8e1dd 100644 --- a/include/mgl/window/window.h +++ b/include/mgl/window/window.h @@ -39,11 +39,12 @@ bool mgl_window_poll_event(mgl_window *self, mgl_event *event); void mgl_window_display(mgl_window *self); /* - The previous view is returned in |prev_view|. |prev_view| may be NULL. This should be called every frame to retain the view. - Make sure to set the view back to |prev_view| after rendering items - inside the view. + 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. */ -void mgl_window_set_view(mgl_window *self, mgl_view *new_view, mgl_view *prev_view); +void mgl_window_set_view(mgl_window *self, mgl_view *new_view); +void mgl_window_get_view(mgl_window *self, mgl_view *view); #endif /* MGL_WINDOW_H */ diff --git a/src/window/window.c b/src/window/window.c index 44e0a23..f975c1e 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -29,14 +29,13 @@ static void set_vertical_sync_enabled(Window window, int enabled) { } static void mgl_window_on_resize(mgl_window *self, int width, int height) { - mgl_context *context = mgl_get_context(); self->size.x = width; self->size.y = height; mgl_view view; view.position = (mgl_vec2i){ 0, 0 }; view.size = self->size; - mgl_window_set_view(self, &view, NULL); + mgl_window_set_view(self, &view); } int mgl_window_create(mgl_window *self, const char *title, int width, int height) { @@ -289,10 +288,7 @@ void mgl_window_display(mgl_window *self) { context->gl.glXSwapBuffers(context->connection, self->window); } -void mgl_window_set_view(mgl_window *self, mgl_view *new_view, mgl_view *prev_view) { - if(prev_view) - *prev_view = self->view; - +void mgl_window_set_view(mgl_window *self, mgl_view *new_view) { mgl_context *context = mgl_get_context(); self->view = *new_view; context->gl.glViewport(new_view->position.x, self->size.y - new_view->size.y - new_view->position.y, new_view->size.x, new_view->size.y); @@ -300,3 +296,7 @@ void mgl_window_set_view(mgl_window *self, mgl_view *new_view, mgl_view *prev_vi context->gl.glLoadIdentity(); context->gl.glOrtho(0.0, new_view->size.x, new_view->size.y, 0.0, 0.0, 1.0); } + +void mgl_window_get_view(mgl_window *self, mgl_view *view) { + *view = self->view; +} diff --git a/tests/main.c b/tests/main.c index 99a8398..878f8ff 100644 --- a/tests/main.c +++ b/tests/main.c @@ -55,17 +55,19 @@ static void draw(mgl_window *window, void *userdata) { mgl_vertex_buffer_set_position(u->vertex_buffer2, (mgl_vec2f){ window->cursor_position.x, window->cursor_position.y + 500 }); mgl_vertex_buffer_draw(context, u->vertex_buffer2, &u->font->texture); + mgl_view prev_view; + mgl_window_get_view(window, &prev_view); + mgl_vec2i view_size = { .x = 200, .y = 200 }; - - mgl_view prev_view; mgl_view new_view = { .position = { window->size.x/2 - view_size.x/2, window->size.y/2 - view_size.y/2 }, .size = view_size }; - mgl_window_set_view(window, &new_view, &prev_view); + mgl_window_set_view(window, &new_view); + mgl_vertex vertices[4] = { (mgl_vertex){ .position = {-new_view.position.x + window->cursor_position.x, -new_view.position.y + window->cursor_position.y}, @@ -88,8 +90,9 @@ static void draw(mgl_window *window, void *userdata) { .color = {255, 255, 0, 255} } }; + mgl_vertices_draw(context, vertices, 4, MGL_PRIMITIVE_QUADS); - mgl_window_set_view(window, &prev_view, NULL); + mgl_window_set_view(window, &prev_view); } int main(int argc, char **argv) { |