From 76307d2746b97b1c6edd132de65e603a2788a41f Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Sun, 23 May 2021 22:27:19 -0700
Subject: [PATCH] Moving some code around

---
 IDEA.md                         |  2 ++
 include/dawn/display/gui/font.h | 11 ++++---
 include/dawn/poker/poker.h      | 10 +++++--
 src/display/framebuffer.c       | 10 +++++++
 src/display/framebuffer.h       | 10 +++++++
 src/display/gui/font.c          | 32 +++++++++++---------
 src/display/gui/font.h          |  8 +++--
 src/file/asset.c                |  4 +--
 src/file/asset.h                |  2 +-
 src/game/game.c                 | 50 +++++--------------------------
 src/poker/poker.c               | 12 +++++---
 src/poker/poker.h               |  7 ++++-
 src/poker/render/frame.c        | 53 +++++++++++++++++++++++++++++++++
 src/poker/render/frame.h        | 41 +++++++++++++++++++++++++
 src/poker/render/talk.c         |  8 +++++
 src/poker/render/talk.h         | 11 +++++++
 src/poker/round/deal.c          |  2 +-
 src/poker/round/match.c         |  6 ++++
 src/poker/round/match.h         |  1 +
 19 files changed, 205 insertions(+), 75 deletions(-)
 create mode 100644 IDEA.md
 create mode 100644 src/poker/render/frame.c
 create mode 100644 src/poker/render/frame.h
 create mode 100644 src/poker/render/talk.c
 create mode 100644 src/poker/render/talk.h

diff --git a/IDEA.md b/IDEA.md
new file mode 100644
index 00000000..e3d380cf
--- /dev/null
+++ b/IDEA.md
@@ -0,0 +1,2 @@
+- Make font measure take in a pointer to a primitive to offer auto buffering of verts
+- Make a clone of the string into font measure pointing only to real chars?
\ No newline at end of file
diff --git a/include/dawn/display/gui/font.h b/include/dawn/display/gui/font.h
index 6a404b19..cff50344 100644
--- a/include/dawn/display/gui/font.h
+++ b/include/dawn/display/gui/font.h
@@ -15,6 +15,9 @@
 /** How many characters (after the first char) to generate */
 #define FONT_NUM_CHARS 96
 
+/** Passed to STBTT for scaling the font nicely */
+#define FONT_TEXTURE_SIZE 64
+
 /** Width of the loaded font texture */
 #define FONT_TEXTURE_WIDTH 512
 
@@ -26,12 +29,12 @@
 
 #define FONT_NEWLINE '\n'
 #define FONT_SPACE ' '
-#define FONT_LINE_HEIGHT 0.75
+#define FONT_LINE_HEIGHT FONT_TEXTURE_SIZE*0.75
 #define FONT_INITIAL_LINE FONT_LINE_HEIGHT*0.75
-#define FONT_SPACE_SIZE 0.35
+#define FONT_SPACE_SIZE FONT_TEXTURE_SIZE*0.75
+#define FONT_SCALE_ADJUST 0.03
 
 typedef struct {
-  float size;
   texture_t texture;
   stbtt_bakedchar characterData[FONT_NUM_CHARS];
 } font_t;
@@ -43,6 +46,6 @@ typedef struct {
 } fonttextinfo_t;
 
 typedef struct {
-  float width, height;
+  float width, height, scale;
   stbtt_aligned_quad *quads;
 } fontmeasure_t;
\ No newline at end of file
diff --git a/include/dawn/poker/poker.h b/include/dawn/poker/poker.h
index 66286233..8b4c5a39 100644
--- a/include/dawn/poker/poker.h
+++ b/include/dawn/poker/poker.h
@@ -15,6 +15,7 @@
 #include "../display/shader.h"
 #include "../display/texture.h"
 #include "../display/tileset.h"
+#include "../display/framebuffer.h"
 
 /** Rounds that the game can be in */
 #define POKER_ROUND_MATCH 0x00
@@ -77,6 +78,13 @@ typedef struct {
   uint32_t roundBet;
 
   /** Rendering Assets */
+  framebuffer_t frameWorld;
+  framebuffer_t frameGui;
+  font_t font;
+  shader_t shader;
+  camera_t cameraWorld;
+  camera_t cameraGui;
+
   texture_t dealerTexture;
   tileset_t dealerTileset;
   primitive_t dealerPrimitive;
@@ -91,6 +99,4 @@ typedef struct {
   tileset_t cardTileset;
   primitive_t cardPrimitive;
 
-  shader_t shader;
-  camera_t camera;
 } poker_t;
\ No newline at end of file
diff --git a/src/display/framebuffer.c b/src/display/framebuffer.c
index f95c14a5..a98b3d75 100644
--- a/src/display/framebuffer.c
+++ b/src/display/framebuffer.c
@@ -48,6 +48,16 @@ void frameBufferUse(framebuffer_t *frameBuffer, bool clear) {
   }
 }
 
+void frameBufferResize(framebuffer_t *frameBuffer,int32_t width,int32_t height){
+  if((
+    frameBuffer->texture.width == width &&
+    frameBuffer->texture.height == height
+  )) return;
+
+  frameBufferDispose(frameBuffer);
+  frameBufferInit(frameBuffer, width, height);
+}
+
 void frameBufferUnbind(render_t *render, bool clear) {
   glBindFramebuffer(GL_FRAMEBUFFER, 0);
   glViewport(0, 0, render->width, render->height);
diff --git a/src/display/framebuffer.h b/src/display/framebuffer.h
index 9a83c82c..e8bc2e71 100644
--- a/src/display/framebuffer.h
+++ b/src/display/framebuffer.h
@@ -26,6 +26,16 @@ void frameBufferInit(framebuffer_t *buffer, int32_t width, int32_t height);
  */
 void frameBufferUse(framebuffer_t *frameBuffer, bool clear);
 
+/**
+ * Resize an existing frame buffer. This will do the check if resizing is even
+ * necessary for you.
+ * 
+ * @param frameBuffer Frame buffer to resize.
+ * @param width New width of the frame buffer.
+ * @param height New height of the frame buffer.
+ */
+void frameBufferResize(framebuffer_t *frameBuffer,int32_t width,int32_t height);
+
 /**
  * Unbind the currently bound frame buffer.
  * 
diff --git a/src/display/gui/font.c b/src/display/gui/font.c
index 37575831..158242c6 100644
--- a/src/display/gui/font.c
+++ b/src/display/gui/font.c
@@ -13,18 +13,16 @@
   #include <stb_truetype.h>
 #endif
 
-void fontInit(font_t *font, char *data, float size) {
+void fontInit(font_t *font, char *data) {
   int32_t i, s;
   s = FONT_TEXTURE_WIDTH * FONT_TEXTURE_HEIGHT;
 
   uint8_t *bitmapData = malloc(sizeof(uint8_t) * s);
   pixel_t *pixels = malloc(sizeof(pixel_t) * s);
 
-  font->size = size;
-
   // STBTT Loads fonts as single channel values only.
   stbtt_BakeFontBitmap(
-    data, 0, size, bitmapData,
+    data, 0, FONT_TEXTURE_SIZE, bitmapData,
     FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
     FONT_FIRST_CHAR, FONT_NUM_CHARS,
     font->characterData
@@ -49,8 +47,8 @@ fonttextinfo_t fontGetTextInfo(font_t *font, char *text) {
   fonttextinfo_t info = {
     .length = 0,
     .realChars = 0,
-    .lineHeight = FONT_LINE_HEIGHT * font->size,
-    .spaceSize = font->size * FONT_SPACE_SIZE
+    .lineHeight = FONT_LINE_HEIGHT,
+    .spaceSize = FONT_SPACE_SIZE
   };
 
   // Count how many "real characters" are in the string.
@@ -65,18 +63,22 @@ fonttextinfo_t fontGetTextInfo(font_t *font, char *text) {
   return info;
 }
 
-fontmeasure_t * fontTextMeasure(font_t *font,char *text,fonttextinfo_t *info) {
+fontmeasure_t * fontTextMeasure(font_t *font, char *text, fonttextinfo_t *info, 
+  float scale
+) {
   int32_t i, j;
   char c;
   float x, y;
   stbtt_aligned_quad *quad;
   fontmeasure_t *measure;
 
+  scale *= FONT_SCALE_ADJUST;
   measure = malloc(sizeof(fontmeasure_t));
+  measure->scale = scale;
   measure->quads = malloc(sizeof(stbtt_aligned_quad) * info->realChars);
 
   x = 0;
-  y = FONT_INITIAL_LINE * font->size;
+  y = FONT_INITIAL_LINE;
 
   i = 0, j = 0;
   while(c = text[i++]) {
@@ -94,9 +96,11 @@ fontmeasure_t * fontTextMeasure(font_t *font,char *text,fonttextinfo_t *info) {
     // Calculate the quad of the character, store into the array.
     quad = measure->quads + j;
     stbtt_GetBakedQuad(font->characterData,
-      FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, ((int32_t)c)-FONT_FIRST_CHAR,
-      &x, &y, quad, FONT_FILL_MODE
+      FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
+      ((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE
     );
+    quad->x0 *= scale, quad->x1 *= scale;
+    quad->y0 *= scale, quad->y1 *= scale;
     j++;
   }
 
@@ -108,6 +112,10 @@ void fontTextMeasureDispose(fontmeasure_t *measure) {
   free(measure);
 }
 
+void fontDispose(font_t *font) {
+  textureDispose(&font->texture);
+}
+
 void fontTextBufferFromMeasure(font_t *font, primitive_t *primitive, char *text,
   fontmeasure_t *measure
 ) {
@@ -136,8 +144,4 @@ void fontTextInitFromMeasure(font_t *font, primitive_t *primitive, char *text,
     QUAD_VERTICE_COUNT*info->realChars, QUAD_INDICE_COUNT*info->realChars
   );
   fontTextBufferFromMeasure(font, primitive, text, measure);
-}
-
-void fontDispose(font_t *font) {
-  textureDispose(&font->texture);
 }
\ No newline at end of file
diff --git a/src/display/gui/font.h b/src/display/gui/font.h
index a195c2a4..30b26b4e 100644
--- a/src/display/gui/font.h
+++ b/src/display/gui/font.h
@@ -16,9 +16,8 @@
  * Initializes Font from raw TTF data.
  * @param font Font to initialize
  * @param data Data to intialize for.
- * @param size Font size to load the font in.
  */
-void fontInit(font_t *font, char *data, float size);
+void fontInit(font_t *font, char *data);
 
 /**
  * Generates information about how the given font will render the given text.
@@ -34,9 +33,12 @@ fonttextinfo_t fontGetTextInfo(font_t *font, char *text);
  * @param font Font to use for rendering and measuring
  * @param text Text to measure/render.
  * @param info Info about the text being rendered / measured.
+ * @param scale Scale of the text.
  * @returns Font measurement calculation.
  */
-fontmeasure_t * fontTextMeasure(font_t *font, char *text, fonttextinfo_t *info);
+fontmeasure_t * fontTextMeasure(font_t *font, char *text, fonttextinfo_t *info,
+  float scale
+);
 
 /**
  * Disposes a previously calculated font text measurement.
diff --git a/src/file/asset.c b/src/file/asset.c
index 1e04af2a..464cab65 100644
--- a/src/file/asset.c
+++ b/src/file/asset.c
@@ -119,8 +119,8 @@ void assetTextureLoad(texture_t *texture, char *fileName) {
   stbi_image_free(data);
 }
 
-void assetFontLoad(font_t *font, char *assetName, float size) {
+void assetFontLoad(font_t *font, char *assetName) {
   char *data = assetStringLoad(assetName);
-  fontInit(font, data, size);
+  fontInit(font, data);
   free(data);
 }
\ No newline at end of file
diff --git a/src/file/asset.h b/src/file/asset.h
index 04fb0e6e..675150dd 100644
--- a/src/file/asset.h
+++ b/src/file/asset.h
@@ -76,4 +76,4 @@ void assetTextureLoad(texture_t *texture, char *fileName);
  * @param assetName Asset name for the TTF font.
  * @param size Size of the font.
  */
-void assetFontLoad(font_t *font, char *assetName, float size);
\ No newline at end of file
+void assetFontLoad(font_t *font, char *assetName);
\ No newline at end of file
diff --git a/src/game/game.c b/src/game/game.c
index 09820d07..357c53f6 100644
--- a/src/game/game.c
+++ b/src/game/game.c
@@ -7,12 +7,6 @@
 
 #include "game.h"
 
-font_t font;
-primitive_t quad;
-
-texture_t mom;
-primitive_t cube;
-
 bool gameInit(game_t *game) {
   // Init the game
   game->name = GAME_NAME;
@@ -21,18 +15,13 @@ bool gameInit(game_t *game) {
   engineInit(&game->engine, game);
 
   // Hand off to the poker logic.
-  pokerInit(&game->poker);
+  pokerInit(&game->poker, &game->engine.render);
 
-  assetFontLoad(&font, "fonts/opensans/OpenSans-Bold.ttf", 64.0);
-
-  char *text = "Ayy\nNice meme";
-  fonttextinfo_t info = fontGetTextInfo(&font, text);
-  fontmeasure_t *measure = fontTextMeasure(&font, text, &info);
-  fontTextInitFromMeasure(&font, &quad, text, &info, measure);
-  fontTextMeasureDispose(measure);
-
-  quadInit(&cube, 0,  0,0,0,0,  923,576,1,1);
-  assetTextureLoad(&mom, "cards_normal.png");
+  // char *text = "Ayy\nNice meme";
+  // fonttextinfo_t info = fontGetTextInfo(&font, text);
+  // fontmeasure_t *measure = fontTextMeasure(&font, text, &info);
+  // fontTextInitFromMeasure(&font, &quad, text, &info, measure);
+  // fontTextMeasureDispose(measure);
 
   return true;
 }
@@ -42,32 +31,7 @@ bool gameUpdate(game_t *game, float platformDelta) {
   engineUpdateStart(&game->engine, game, platformDelta);
 
   // Hand off to the poker logic
-  shaderUse(&game->poker.shader);
-
-  // cameraPerspective(&game->poker.camera, 45,
-  //   game->engine.render.width/game->engine.render.height,
-  //   0.01, 1000
-  // );
-  // cameraLookAt(&game->poker.camera, 300,300,300, 0,0,0);
-
-  cameraOrtho(&game->poker.camera,
-    0, game->engine.render.width,
-    game->engine.render.height, 0,
-    0.1, 1000
-  );
-  cameraLookAt(&game->poker.camera, 0,0,5, 0,0,-5);
-
-  shaderUseCamera(&game->poker.shader, &game->poker.camera);
-
-  shaderUseTexture(&game->poker.shader, &font.texture);
-  shaderUsePosition(&game->poker.shader, 0,0,0, 0,0,0);
-  primitiveDraw(&quad, 0, -1);
-
-  // shaderUseTexture(&game->poker.shader, &mom);
-  // shaderUsePosition(&game->poker.shader, 0,0,0, 0,0,0);
-  // primitiveDraw(&cube, 0, -1);
-
-  // pokerUpdate(&game->poker, &game->engine.render);
+  pokerUpdate(&game->poker, &game->engine.render);
 
   // Hand back to the engine.
   return engineUpdateEnd(&game->engine, game);
diff --git a/src/poker/poker.c b/src/poker/poker.c
index ad45d9c2..ba60fca0 100644
--- a/src/poker/poker.c
+++ b/src/poker/poker.c
@@ -7,13 +7,17 @@
 
 #include "poker.h"
 
-void pokerInit(poker_t *poker) {
+void pokerInit(poker_t *poker, render_t *render) {
   // Load the main shader
   assetShaderLoad(&poker->shader,
     "shaders/textured.vert", "shaders/textured.frag"
   );
 
+  // Load the main font
+  assetFontLoad(&poker->font, "fonts/opensans/OpenSans-Bold.ttf");
+
   // Initialize the render stuffs.
+  pokerFrameInit(poker, render);
   pokerWorldInit(poker);
   pokerCardInit(poker);
   pokerPlayerInit(poker);
@@ -33,15 +37,15 @@ void pokerUpdate(poker_t *poker, render_t *render) {
   }
   
   // Rendering
-  cameraPerspective(&poker->camera, 20,render->width/render->height,0.001,1000);
   shaderUse(&poker->shader);
-  shaderUseCamera(&poker->shader, &poker->camera);
-
+  pokerFrameWorld(poker, render);
   pokerWorldRender(poker);
   for(uint8_t pi = 0; pi < POKER_PLAYER_COUNT; pi++) {
     uint8_t seat = pokerPlayerGetSeatForPlayer(pi);
     pokerPlayerRender(poker, poker->players + pi, seat);
   }
+
+  pokerFrameGui(poker, render);
 }
 
 void pokerDispose(poker_t * poker) {
diff --git a/src/poker/poker.h b/src/poker/poker.h
index 17b320cc..ffb39dc8 100644
--- a/src/poker/poker.h
+++ b/src/poker/poker.h
@@ -7,20 +7,25 @@
 
 #pragma once
 #include <dawn/dawn.h>
+
 #include "round/match.h"
+#include "render/frame.h"
 #include "render/world.h"
 #include "render/card.h"
 #include "render/player.h"
 #include "render/look.h"
+
 #include "../file/asset.h"
 #include "../display/shader.h"
 #include "../display/camera.h"
+#include "../display/gui/font.h"
 
 /**
  * Initializes the poker context for the first time.
  * @param poker Poker context to initialize.
+ * @param render Rendering context.
  */
-void pokerInit(poker_t *poker);
+void pokerInit(poker_t *poker, render_t *render);
 
 /**
  * Updates the poker context.
diff --git a/src/poker/render/frame.c b/src/poker/render/frame.c
new file mode 100644
index 00000000..0c0c378c
--- /dev/null
+++ b/src/poker/render/frame.c
@@ -0,0 +1,53 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#include "frame.h"
+
+primitive_t bruh;
+
+void pokerFrameInit(poker_t *poker, render_t *render) {
+  frameBufferInit(&poker->frameWorld, render->width, render->height);
+  frameBufferInit(&poker->frameGui, render->width, render->height);
+
+  fonttextinfo_t info;
+  char *text = "Hello\n World";
+  info = fontGetTextInfo(&poker->font, text);
+  fontmeasure_t *measure = fontTextMeasure(&poker->font, text, &info, 16);
+  fontTextInitFromMeasure(&poker->font, &bruh, text, &info, measure);
+  fontTextMeasureDispose(measure);
+}
+
+void pokerFrameWorld(poker_t *poker, render_t *render) {
+  // Update the frame buffer
+  frameBufferResize(&poker->frameWorld, render->width, render->height);
+
+  // Correct the aspect on the perspective camera for the world
+  cameraPerspective(&poker->cameraWorld, 20,
+    render->width / render->height,
+    0.001, 1000
+  );
+  shaderUseCamera(&poker->shader, &poker->cameraWorld);
+}
+
+void pokerFrameGui(poker_t *poker, render_t *render) {
+  // Update FB
+  frameBufferResize(&poker->frameGui, render->width, render->height);
+  // frameBufferUse(&poker->frameGui, true);
+
+  // Correct aspect on the ortho camera
+  cameraOrtho(&poker->cameraGui,
+    0, render->width, 
+    render->height, 0,
+    0.01, 100
+  );
+  cameraLookAt(&poker->cameraGui, 0, 0, 5, 0, 0, 0);
+  shaderUseCamera(&poker->shader, &poker->cameraGui);
+
+  shaderUsePosition(&poker->shader, 0,0,0, 0,0,0);
+  shaderUseTexture(&poker->shader, &poker->font.texture);
+  primitiveDraw(&bruh, 0, -1);
+}
\ No newline at end of file
diff --git a/src/poker/render/frame.h b/src/poker/render/frame.h
new file mode 100644
index 00000000..a5b5baed
--- /dev/null
+++ b/src/poker/render/frame.h
@@ -0,0 +1,41 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#pragma once
+#include <dawn/dawn.h>
+#include "../../display/framebuffer.h"
+#include "../../display/primitive.h"
+#include "../../display/texture.h"
+#include "../../display/shader.h"
+#include "../../display/camera.h"
+#include "../../display/primitives/quad.h"
+
+#include "../../display/gui/font.h"
+
+/**
+ * Initializes the various frame buffers for the poker game.
+ * 
+ * @param poker Poker game to initialize for
+ * @param render Rendering context.
+ */
+void pokerFrameInit(poker_t *poker, render_t *render);
+
+/**
+ * Bind the world frame, this will also correct the camera angles.
+ * 
+ * @param poker Poker game context.
+ * @param render Rendering engine context.
+ */
+void pokerFrameWorld(poker_t *poker, render_t *render);
+
+/**
+ * Bind the frame for GUI rendering.
+ * 
+ * @param poker Poker game context.
+ * @param render Rendering engine context.
+ */ 
+void pokerFrameGui(poker_t *poker, render_t *render);
\ No newline at end of file
diff --git a/src/poker/render/talk.c b/src/poker/render/talk.c
new file mode 100644
index 00000000..60fd1356
--- /dev/null
+++ b/src/poker/render/talk.c
@@ -0,0 +1,8 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#include "talk.h"
\ No newline at end of file
diff --git a/src/poker/render/talk.h b/src/poker/render/talk.h
new file mode 100644
index 00000000..05e74623
--- /dev/null
+++ b/src/poker/render/talk.h
@@ -0,0 +1,11 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#pragma once
+#include <dawn/dawn.h>
+#include "../../display/gui/font.h"
+#include "../../display/primitive.h"
\ No newline at end of file
diff --git a/src/poker/round/deal.c b/src/poker/round/deal.c
index f25af279..f3b33efc 100644
--- a/src/poker/round/deal.c
+++ b/src/poker/round/deal.c
@@ -32,7 +32,7 @@ void pokerDealInit(poker_t *poker) {
   }
 
   // Hard look at the dealer
-  pokerLookAtPlayer(&poker->camera, POKER_SEAT_DEALER);
+  pokerLookAtPlayer(&poker->cameraWorld, POKER_SEAT_DEALER);
 
   // Shuffle the deck
   for(x = 0; x < CARD_DECK_SIZE - 1; x++) {
diff --git a/src/poker/round/match.c b/src/poker/round/match.c
index 44dac23f..6803978c 100644
--- a/src/poker/round/match.c
+++ b/src/poker/round/match.c
@@ -8,6 +8,10 @@
 
 void pokerMatchInit(poker_t *poker) {
   uint8_t x;
+
+  // Look at the dealer
+  pokerLookAtPlayer(&poker->cameraWorld, POKER_SEAT_DEALER);
+
   // Reset the main game state. This does not init the round.
   poker->blindBig = POKER_BLIND_BIG_DEFAULT;
   poker->blindSmall = POKER_BLIND_SMALL_DEFAULT;
@@ -17,6 +21,8 @@ void pokerMatchInit(poker_t *poker) {
     poker->players[x].state = 0x00;
     poker->players[x].chips = POKER_PLAYER_CHIPS_DEFAULT;
   }
+
+  printf("Match start\n");
 }
 
 void pokerMatchUpdate(poker_t *poker) {
diff --git a/src/poker/round/match.h b/src/poker/round/match.h
index e6ce35ea..ef2cb1d3 100644
--- a/src/poker/round/match.h
+++ b/src/poker/round/match.h
@@ -8,6 +8,7 @@
 #pragma once
 #include <dawn/dawn.h>
 #include "deal.h"
+#include "../render/look.h"
 
 /**
  * Init the poker match round.