From 66c61a6633ddb00401d3f4b50e62dd85b9d40d67 Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Sat, 16 Oct 2021 22:57:19 -0700
Subject: [PATCH] Did some tweaks to the CMakeLists

---
 CMakeLists.txt                        |  30 +++++---
 client/glfwclient/CMakeLists.txt      |  30 ++++++--
 client/glfwclient/glfwclient.c        |   7 ++
 client/glfwclient/glfwclient.h        |   5 +-
 src/CMakeLists.txt                    |  41 ++++++----
 src/engine/client.c                   |  12 +++
 src/engine/client.h                   |  23 ++++++
 src/engine/engine.c                   |   5 +-
 src/engine/engine.h                   |   8 ++
 src/file/xml.c                        |   1 +
 test/CMakeLists.txt                   |  30 +++++++-
 test/poker/dealer.h                   |   2 +
 test/poker/pot.c                      |   2 +-
 test/poker/winner.h                   |   1 +
 tools/utils/args.js                   |  12 ---
 tools/utils/image.js                  | 103 --------------------------
 tools/vn/character-sheet-generator.js |  88 ----------------------
 tools/vn/character-sheet-maker.c      |  12 +++
 tools/vn/character-sheet-maker.h      |   9 +++
 19 files changed, 178 insertions(+), 243 deletions(-)
 create mode 100644 src/engine/client.c
 create mode 100644 src/engine/client.h
 delete mode 100644 tools/utils/args.js
 delete mode 100644 tools/utils/image.js
 delete mode 100644 tools/vn/character-sheet-generator.js
 create mode 100644 tools/vn/character-sheet-maker.c
 create mode 100644 tools/vn/character-sheet-maker.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 25aa0875..21a2f725 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,16 +3,21 @@
 # This software is released under the MIT License.
 # https://opensource.org/licenses/MIT
 
-#################################### CMAKE #####################################
+# Set up the base CMake stuff.
 cmake_minimum_required(VERSION 3.13)
 set(CMAKE_C_STANDARD 99)
 set(CMAKE_C_STANDARD_REQUIRED ON)
-project(Dawn VERSION 1.0)
 
-# Targets
+# Set some global flags
+add_compile_definitions(
+  _CRT_SECURE_NO_WARNINGS=1
+  SETTING_ASSET_PREFIX="../../../assets/"
+)
+
+# Which game are we building?
 if(TARGET_GAME STREQUAL poker)
   add_compile_definitions(
-    SETTING_GAME_NAME="Penny's Poker"
+    GAME_NAME="Penny's Poker"
     GAME_FILE="poker/game.h"
     GAME_TYPE=pokergame_t
     GAME_INIT=pokerGameInit
@@ -22,16 +27,19 @@ if(TARGET_GAME STREQUAL poker)
   )
 endif()
 
-# Shared
+# Set up the project.
+project(Dawn VERSION 1.0)
+add_executable(${PROJECT_NAME})
+
+# Add libraries
 add_subdirectory(lib)
+
+# Add sources
 add_subdirectory(src)
 
-# Targets
-if(TARGET_GROUP STREQUAL test)
+# Are we building a game, a tool or running tests?
+if(TARGET_TYPE STREQUAL test)
   add_subdirectory(test)
 else()
   add_subdirectory(client)
-endif()
-
-#################################### ASSETS ####################################
-file(COPY ${CMAKE_CURRENT_LIST_DIR}/assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
\ No newline at end of file
+endif()
\ No newline at end of file
diff --git a/client/glfwclient/CMakeLists.txt b/client/glfwclient/CMakeLists.txt
index 063c6020..d8cec54f 100644
--- a/client/glfwclient/CMakeLists.txt
+++ b/client/glfwclient/CMakeLists.txt
@@ -3,12 +3,32 @@
 # This software is released under the MIT License.
 # https://opensource.org/licenses/MIT
 
+# Definitions
+add_compile_definitions(
+  SETTING_PLATFORM_GLFW=1
+  SETTING_PLATFORM=1
+  SETTING_PLATFORM_USE_GLAD=1
+)
+
+# Libraries
+target_link_libraries(${PROJECT_NAME} 
+  PUBLIC
+    glfw
+    glad
+)
+
+# Sources
 file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
 file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
-add_executable(${PROJECT_NAME} ${SOURCES})
 
-target_link_libraries(${PROJECT_NAME} 
-  game
-  glfw
-  glad
+target_sources(${PROJECT_NAME}
+  PRIVATE
+    ${SOURCES}
+    ${HEADERS}
+)
+
+# Includes
+target_include_directories(${PROJECT_NAME}
+  PUBLIC
+    ${CMAKE_CURRENT_LIST_DIR}
 )
\ No newline at end of file
diff --git a/client/glfwclient/glfwclient.c b/client/glfwclient/glfwclient.c
index 23b8ced0..30ac09d3 100644
--- a/client/glfwclient/glfwclient.c
+++ b/client/glfwclient/glfwclient.c
@@ -72,6 +72,9 @@ int32_t main() {
     inputBind(input, INPUT_MOUSE_X,       GLFW_PLATFORM_INPUT_MOUSE_X);
     inputBind(input, INPUT_MOUSE_Y,       GLFW_PLATFORM_INPUT_MOUSE_Y);
 
+    // Set up the client
+    game->engine.client.setTitle = &glfwClientSetTitle;
+
     // Set up some GLFW stuff
     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
     glfwSetWindowTitle(window, game->engine.name);
@@ -139,4 +142,8 @@ inputsource_t glfwGetInputSourceForKey(int32_t key) {
     key <= GLFW_KEY_MENU ? key - GLFW_KEY_ESCAPE + GLFW_KEY_GRAVE_ACCENT :
     key
   ) % INPUT_SOURCE_COUNT);
+}
+
+void glfwClientSetTitle(char *name) {
+  glfwSetWindowTitle(window, name);
 }
\ No newline at end of file
diff --git a/client/glfwclient/glfwclient.h b/client/glfwclient/glfwclient.h
index cde9d099..b2c40748 100644
--- a/client/glfwclient/glfwclient.h
+++ b/client/glfwclient/glfwclient.h
@@ -52,4 +52,7 @@ void glfwOnCursor(GLFWwindow *window, double x, double y);
  * @param key Key to get the input source for.
  * @return The input source. 
  */
-inputsource_t glfwGetInputSourceForKey(int32_t key);
\ No newline at end of file
+inputsource_t glfwGetInputSourceForKey(int32_t key);
+
+/** GLFW Client Methods */
+void glfwClientSetTitle(char *name);
\ No newline at end of file
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d763f0e3..b6ad078f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -3,21 +3,32 @@
 # This software is released under the MIT License.
 # https://opensource.org/licenses/MIT
 
-# Add Sources
-file(GLOB_RECURSE SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
-add_library(game STATIC ${SRCS})
-target_include_directories(game PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
-target_link_libraries(game PUBLIC
-  glad
-  cglm
-  duktape
-  stb
+# Definitions
+
+# Libraries
+target_link_libraries(${PROJECT_NAME}
+  PUBLIC
+    glad
+    cglm
+    stb
+    duktape
 )
 
-# Set up flags
-add_compile_definitions(
-  SETTING_PLATFORM_GLFW=1
-  SETTING_PLATFORM=1
-  SETTING_PLATFORM_USE_GLAD=1
-  SETTING_ASSET_PREFIX="../../../assets/"
+# Sources
+file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
+file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
+
+list(FILTER SOURCES EXCLUDE REGEX ".*game\\/.*")
+list(FILTER HEADERS EXCLUDE REGEX ".*game\\/.*")
+
+target_sources(${PROJECT_NAME}
+  PRIVATE
+    ${SOURCES}
+    ${HEADERS}
+)
+
+# Includes
+target_include_directories(${PROJECT_NAME}
+  PUBLIC
+    ${CMAKE_CURRENT_LIST_DIR}
 )
\ No newline at end of file
diff --git a/src/engine/client.c b/src/engine/client.c
new file mode 100644
index 00000000..70de40ec
--- /dev/null
+++ b/src/engine/client.c
@@ -0,0 +1,12 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#include "client.h"
+
+void clientInit(client_t *client) {
+  client->setTitle = NULL;
+}
\ No newline at end of file
diff --git a/src/engine/client.h b/src/engine/client.h
new file mode 100644
index 00000000..bac0e3c6
--- /dev/null
+++ b/src/engine/client.h
@@ -0,0 +1,23 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#pragma once
+#include "../libs.h"
+
+/** Callback to set the window title */
+typedef void clientsettitle_t(char *string);
+
+typedef struct {
+  clientsettitle_t *setTitle;
+} client_t;
+
+/**
+ * Initialize the client to its default state.
+ * 
+ * @param client Client to initialize.
+ */
+void clientInit(client_t *client);
\ No newline at end of file
diff --git a/src/engine/engine.c b/src/engine/engine.c
index 16e0a177..fd1334e6 100644
--- a/src/engine/engine.c
+++ b/src/engine/engine.c
@@ -9,9 +9,8 @@
 
 void engineInit(engine_t *engine) {
   randSeed(123);
-  
-  engine->name = SETTING_GAME_NAME;
-
+  engine->name = GAME_NAME;
+  clientInit(&engine->client);
   epochInit(&engine->time);
   renderInit();
   inputInit(&engine->input);
diff --git a/src/engine/engine.h b/src/engine/engine.h
index 9bbe1479..d27d44f3 100644
--- a/src/engine/engine.h
+++ b/src/engine/engine.h
@@ -7,11 +7,16 @@
 
 #pragma once
 #include "../libs.h"
+#include "client.h"
 #include "../util/rand.h"
 #include "../input/input.h"
 #include "../epoch/epoch.h"
 #include "../display/render.h"
 
+// #if !defined(GAME_NAME)
+//   #error You need to define the GAME_NAME string
+// #endif
+
 typedef struct {
   /** Name of the game */
   char *name;
@@ -24,6 +29,9 @@ typedef struct {
 
   /** Input Manager for the game */
   input_t input;
+
+  /** Game client information */
+  client_t client;
 } engine_t;
 
 /**
diff --git a/src/file/xml.c b/src/file/xml.c
index 0162368a..56bdca0f 100644
--- a/src/file/xml.c
+++ b/src/file/xml.c
@@ -169,6 +169,7 @@ int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) {
   }
 
   free(buffer);
+  return i;
 }
 
 void xmlLoad(xml_t *xml, char *data) {
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 28f5f460..9dc3191f 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,6 +1,28 @@
-file(GLOB_RECURSE SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
+# Copyright (c) 2021 Dominic Msters
+# 
+# This software is released under the MIT License.
+# https://opensource.org/licenses/MIT
 
-add_executable(tests ${SRCS})
-target_link_libraries(tests game unity)
+# Definitions
 
-add_test(tests tests)
\ No newline at end of file
+# Libraries
+target_link_libraries(${PROJECT_NAME} 
+  PUBLIC
+    unity
+)
+
+# Sources
+file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
+file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
+
+target_sources(${PROJECT_NAME}
+  PRIVATE
+    ${SOURCES}
+    ${HEADERS}
+)
+
+# Includes
+target_include_directories(${PROJECT_NAME}
+  PUBLIC
+    ${CMAKE_CURRENT_LIST_DIR}
+)
\ No newline at end of file
diff --git a/test/poker/dealer.h b/test/poker/dealer.h
index fe0b63cb..f58f7f0b 100644
--- a/test/poker/dealer.h
+++ b/test/poker/dealer.h
@@ -7,6 +7,8 @@
 
 #pragma once
 #include <unity.h>
+#include <poker/poker.h>
+#include <poker/player.h>
 #include <poker/dealer.h>
 
 int test_dealer_h();
\ No newline at end of file
diff --git a/test/poker/pot.c b/test/poker/pot.c
index c7142428..1059b815 100644
--- a/test/poker/pot.c
+++ b/test/poker/pot.c
@@ -88,7 +88,7 @@ void test_pokerPotAddPlayer_should_AddAPlayer(void) {
   TEST_ASSERT_EQUAL_UINT8(0x00, pot->players[1]);
 }
 
-void test_pokerPotGetSumOfChipsForPlayer_should_SumPlayersPotsChips(){
+void test_pokerPotGetSumOfChipsForPlayer_should_SumPlayersPotsChips(void){
   poker_t poker;
   uint8_t p0i, p1i;
   pokerplayer_t *p0;
diff --git a/test/poker/winner.h b/test/poker/winner.h
index a5325d71..166fedd5 100644
--- a/test/poker/winner.h
+++ b/test/poker/winner.h
@@ -7,6 +7,7 @@
 
 #pragma once
 #include <unity.h>
+#include <poker/poker.h>
 #include <poker/winner.h>
 
 int test_winner_h();
\ No newline at end of file
diff --git a/tools/utils/args.js b/tools/utils/args.js
deleted file mode 100644
index 94efab05..00000000
--- a/tools/utils/args.js
+++ /dev/null
@@ -1,12 +0,0 @@
-const args = process.argv.splice(2).reduce((x,y) => {
-  const bits = y.split('=');
-  const name = bits[0].replace('--', '');
-  const val = bits[1];
-
-  x[name] = val;
-  return x;
-}, {});
-
-module.exports = {
-  args
-};
\ No newline at end of file
diff --git a/tools/utils/image.js b/tools/utils/image.js
deleted file mode 100644
index 5c4b7e61..00000000
--- a/tools/utils/image.js
+++ /dev/null
@@ -1,103 +0,0 @@
-const { PNG } = require("pngjs");
-const path = require('path');
-const fs = require('fs');
-
-/**
- * Loads an image into memory.
- * @param image Image to load
- * @returns A promise that resolves to the loaded image.
- */
-const imageLoad = (image) => new Promise(resolve => {
-  fs.createReadStream(image)
-    .pipe(new PNG({ filterType: 4 }))
-    .on("parsed", function () {
-      // Normalize
-      const pixels = [];
-      for(let y = 0; y < this.height; y++) {
-        for(let x = 0; x < this.width; x++) {
-          const idx = (this.width * y + x) << 2;
-          const r = this.data[idx];
-          const g = this.data[idx + 1];
-          const b = this.data[idx + 2];
-          const a = this.data[idx + 3];
-
-          pixels.push({ r, g, b, a });
-        }
-      }
-      resolve({ pixels, width: this.width, height: this.height });
-    })
-  ;
-});
-
-/**
- * Writes an image to an output file.
- * @param image Image to write.
- * @param file File to write to.
- * @returns A promise that, when resolved, has saved the image.
- */
-const imageWrite = (image, file) => new Promise(resolve => {
-  const png = new PNG({ width: image.width, height: image.height });
-  png.width = image.width;
-  png.height = image.height;
-  
-  for(let y = 0; y < image.height; y++) {
-    for(let x = 0; x < image.width; x++) {
-      const i = (image.width * y + x);
-      const idx = i << 2;
-
-      const pixel = image.pixels[i];
-      png.data[idx] = pixel.r;
-      png.data[idx + 1] = pixel.g;
-      png.data[idx + 2] = pixel.b;
-      png.data[idx + 3] = pixel.a;
-    }
-  }
-
-  png.pack().pipe(fs.createWriteStream(file))
-    .on('close', () => resolve(true))
-  ;
-});
-
-/**
- * Creates a blank image
- * @param width Width of the image.
- * @param height Height of the image.
- * @param fill Optional pixel to fill with, defaults to 0,0,0,0
- * @returns The newly created image.
- */
-const imageCreate = (width, height, pixel) => {
-  if(!pixel || !pixel.r) pixel = { r:0, g:0, b:0, a:0 };
-  const pixels = [];
-  for(let i = 0; i < width * height; i++) pixels.push({ ...pixel });
-  return { pixels, width, height };
-}
-
-/**
- * Copies an area of a source image into a target image.
- * @param target Target image to copy into.
- * @param source Source image to copy from.
- * @param tx Target X position to copy into
- * @param ty Target Y position to copy into
- * @param sub Optional source area to use, defined as { x, y, width, height }.
- */
-const imageCopy = (target, source, tx, ty, sub) => {
-  if(!sub) sub = { x: 0, y: 0, width: source.width, height: source.height };
-
-  for(let x = sub.x; x < sub.x+sub.width; x++) {
-    for(let y = sub.y; y < sub.y+sub.height; y++) {
-      let absX = x - sub.x + tx;
-      let absY = y - sub.y + ty;
-      if(absX > target.width || absY > target.height) continue;
-      let ti = absY * target.width + absX;
-      let si = y * source.width + x;
-      target.pixels[ti] = { ...source.pixels[si] };
-    }
-  }
-}
-
-module.exports = {
-  imageWrite,
-  imageCreate,
-  imageLoad,
-  imageCopy
-}
\ No newline at end of file
diff --git a/tools/vn/character-sheet-generator.js b/tools/vn/character-sheet-generator.js
deleted file mode 100644
index 0144d11b..00000000
--- a/tools/vn/character-sheet-generator.js
+++ /dev/null
@@ -1,88 +0,0 @@
-const path = require('path');
-const { imageCreate, imageWrite, imageLoad, imageCopy } = require('./../utils/image');
-const fs = require('fs');
-const xml = require('xml-js');
-const { args } = require('./../utils/args');
-
-// Parse Args
-if(!args.in) throw new Error(`Missing in argument`);
-if(!args.out) throw new Error(`Missing out argument`);
-if(!args.in.endsWith('xml')) throw new Error(`Invalid in XML`);
-if(!args.out.endsWith('png')) throw new Error(`Invalid out PNG`);
-
-// Determine in and out.
-const file = path.resolve(args.in);
-const outFile = path.resolve(args.out);
-if(fs.existsSync(outFile)) return;
-
-// Load XML
-const data = xml.xml2js(fs.readFileSync(file, 'utf-8'));
-const [ character ] = data.elements;
-
-// Validate file.
-if(!character.attributes.context) throw new Error(`Missing context`)
-const dir = path.resolve('.', 'assets', character.attributes.context);
-
-// Parse base and layers
-const base = character.elements.find(e => e.name == 'base').attributes;
-if(!base) throw new Error(`Failed to find base`);
-const layers = character.elements
-  .filter(e => e.name == 'layer')
-  .map(e => e.attributes)
-  .map(e => ({
-    ...e,
-    x: parseInt(e.x),
-    y: parseInt(e.y),
-    width: parseInt(e.width),
-    height: parseInt(e.height)
-  }))
-;
-
-(async () => {
-  // Load the base
-  const baseImage = await imageLoad(path.join(dir, base.file));
-
-  let columnsMax = 0;
-  let widthMax = 0;
-  layers.forEach((layer,row) => {
-    if(!layer.width || !layer.height || !layer.x || !layer.y) {
-      throw new Error(`Missing layer info`);
-    }
-
-    const layerDir = path.join(dir, layer.directory);
-    const scan = fs.readdirSync(layerDir);
-    columnsMax = Math.max(scan.length, columnsMax);
-    widthMax = Math.max(widthMax, layer.width);
-  });
-
-  // Create the output buffer
-  const out = imageCreate(
-    baseImage.width + (columnsMax * widthMax),
-    baseImage.height
-  );
-
-  // Copy the base
-  imageCopy(out, baseImage, 0, 0);
-  
-  // Now begin copying the children, row is defined by the directory
-  let y = 0;
-  for(let row = 0; row < layers.length; row++) {
-    const layer = layers[row];
-    const layerDir = path.join(dir, layer.directory);
-    const scan = fs.readdirSync(layerDir);
-
-    // Column defined by the file index
-    for(let col = 0; col < scan.length; col++) {
-      const img = await imageLoad(path.join(layerDir, scan[col]));
-      console.log('Copying', scan[col]);
-      imageCopy(out, img,
-        baseImage.width+(col*layer.width), y,
-        layer
-      );
-    }
-
-    y += layer.height;
-  }
-
-  await imageWrite(out, outFile);
-})().catch(console.error);
\ No newline at end of file
diff --git a/tools/vn/character-sheet-maker.c b/tools/vn/character-sheet-maker.c
new file mode 100644
index 00000000..94841dce
--- /dev/null
+++ b/tools/vn/character-sheet-maker.c
@@ -0,0 +1,12 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#include "character-sheet-maker.h"
+
+int32_t main(int32_t argc, char *argv[]) {
+  
+}
\ No newline at end of file
diff --git a/tools/vn/character-sheet-maker.h b/tools/vn/character-sheet-maker.h
new file mode 100644
index 00000000..61d565be
--- /dev/null
+++ b/tools/vn/character-sheet-maker.h
@@ -0,0 +1,9 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
+#pragma once
+#include <libs.h>
\ No newline at end of file