From 0a78a9d9c1ac2090da56f395d6f2394a8a210010 Mon Sep 17 00:00:00 2001
From: 3gg <3gg@shellblade.net>
Date: Sat, 31 Aug 2024 18:40:28 -0700
Subject: More viewport control.

---
 game/src/game.c             |  2 +-
 gfx/include/gfx/core.h      |  7 +++++--
 gfx/src/core/core.c         | 25 ++++++++++++++++++++-----
 gfx/src/core/core_impl.h    |  2 ++
 gfx/src/renderer/renderer.c |  4 ++--
 5 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/game/src/game.c b/game/src/game.c
index 10c69aa..dc2248b 100644
--- a/game/src/game.c
+++ b/game/src/game.c
@@ -215,7 +215,7 @@ static void Resize(Game* game, int width, int height) {
   game->height = height;
 
   GfxCore* gfxcore = gfx_get_core(game->gfx);
-  gfx_set_viewport(gfxcore, width, height);
+  gfx_set_viewport(gfxcore, 0, 0, width, height);
 
   resize_plugin(game, width, height);
 }
diff --git a/gfx/include/gfx/core.h b/gfx/include/gfx/core.h
index 7d31cca..44509c9 100644
--- a/gfx/include/gfx/core.h
+++ b/gfx/include/gfx/core.h
@@ -321,10 +321,13 @@ void gfx_start_frame(GfxCore*);
 void gfx_end_frame(GfxCore*);
 
 /// Set the render backend's viewport dimensions.
-void gfx_set_viewport(GfxCore*, int width, int height);
+void gfx_set_viewport(GfxCore*, int x, int y, int width, int height);
 
 /// Get the render backend's viewport dimensions.
-void gfx_get_viewport(GfxCore*, int* width, int* height);
+void gfx_get_viewport(GfxCore*, int* x, int* y, int* width, int* height);
+
+/// Clear the viewport.
+void gfx_clear(GfxCore*, vec4 colour);
 
 /// Set blending state.
 void gfx_set_blending(GfxCore*, bool enable);
diff --git a/gfx/src/core/core.c b/gfx/src/core/core.c
index 7a6d9cc..90038c6 100644
--- a/gfx/src/core/core.c
+++ b/gfx/src/core/core.c
@@ -64,7 +64,9 @@ void gfx_del_gfxcore(GfxCore* gfxcore) {
 void gfx_start_frame(GfxCore* gfxcore) {
   assert(gfxcore);
 
-  glViewport(0, 0, gfxcore->viewport.width, gfxcore->viewport.height);
+  glViewport(
+      gfxcore->viewport.x, gfxcore->viewport.y, gfxcore->viewport.width,
+      gfxcore->viewport.height);
   glClearColor(0, 0, 0, 0);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -76,20 +78,33 @@ void gfx_end_frame(GfxCore* gfxcore) {
   ASSERT_GL;
 }
 
-void gfx_set_viewport(GfxCore* gfxcore, int width, int height) {
+void gfx_set_viewport(GfxCore* gfxcore, int x, int y, int width, int height) {
   assert(gfxcore);
-  gfxcore->viewport.width  = width;
-  gfxcore->viewport.height = height;
+  gfxcore->viewport =
+      (Viewport){.x = x, .y = y, .width = width, .height = height};
 }
 
-void gfx_get_viewport(GfxCore* gfxcore, int* width, int* height) {
+void gfx_get_viewport(
+    GfxCore* gfxcore, int* x, int* y, int* width, int* height) {
   assert(gfxcore);
+  assert(x);
+  assert(y);
   assert(width);
   assert(height);
+
+  *x      = gfxcore->viewport.x;
+  *y      = gfxcore->viewport.y;
   *width  = gfxcore->viewport.width;
   *height = gfxcore->viewport.height;
 }
 
+void gfx_clear(GfxCore* gfxcore, vec4 colour) {
+  assert(gfxcore);
+
+  glClearColor(colour.x, colour.y, colour.z, colour.w);
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+}
+
 void gfx_set_blending(GfxCore* gfxcore, bool enable) {
   assert(gfxcore);
   if (enable) {
diff --git a/gfx/src/core/core_impl.h b/gfx/src/core/core_impl.h
index e27c0f2..eefdfbe 100644
--- a/gfx/src/core/core_impl.h
+++ b/gfx/src/core/core_impl.h
@@ -40,6 +40,8 @@ DEF_MEMPOOL(ShaderCache, ShaderCacheEntry, GFX_MAX_NUM_SHADERS)
 DEF_MEMPOOL(ProgramCache, ShaderProgramCacheEntry, GFX_MAX_NUM_SHADER_PROGRAMS)
 
 typedef struct {
+  int x;
+  int y;
   int width;
   int height;
 } Viewport;
diff --git a/gfx/src/renderer/renderer.c b/gfx/src/renderer/renderer.c
index d615918..c2a7dda 100644
--- a/gfx/src/renderer/renderer.c
+++ b/gfx/src/renderer/renderer.c
@@ -341,8 +341,8 @@ void gfx_render_scene(Renderer* renderer, const RenderSceneParams* params) {
     view_matrix     = mat4_id();
   }
 
-  int width, height;
-  gfx_get_viewport(gfxcore, &width, &height);
+  int x, y, width, height;
+  gfx_get_viewport(gfxcore, &x, &y, &width, &height);
   const float aspect = (float)width / (float)height;
 
   RenderState state = {
-- 
cgit v1.2.3