From 786cff50f170d6dd75a7edf83c8625e7c535c7e8 Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Mon, 19 Apr 2021 15:56:16 +1000
Subject: [PATCH] Commenting

---
 src/dawn/dawngame.c          |  3 +-
 src/dawn/entity/entity.h     |  6 ++++
 src/dawn/world/chunk.c       | 13 ++++++---
 src/dawn/world/chunk.h       | 10 +++++--
 src/dawn/world/tile.c        |  2 +-
 src/dawn/world/tile.h        |  9 ++++--
 src/dawn/world/world.c       | 56 ++++++++++++++++--------------------
 src/dawn/world/world.h       | 13 +++++----
 src/dawn/world/worldtypes.h  | 43 ++++++++++++++++++++++++++-
 src/engine/display/tileset.c |  6 ++--
 src/engine/display/tileset.h |  3 ++
 tools/map/index.js           | 56 ++++++++++++++++++++++++++++++++++--
 12 files changed, 165 insertions(+), 55 deletions(-)
 create mode 100644 src/dawn/entity/entity.h

diff --git a/src/dawn/dawngame.c b/src/dawn/dawngame.c
index 3774ad5f..78b58c61 100644
--- a/src/dawn/dawngame.c
+++ b/src/dawn/dawngame.c
@@ -27,7 +27,8 @@ game_t * gameInit(platform_t *platform) {
   
   // Prepare the camera.
   game->camera = cameraCreate();
-  cameraLookAt(game->camera,  50, 50, 50,  0, 0, 0 );
+  uint32_t d = 10;
+  cameraLookAt(game->camera,  d, d, d,  0, 0, 0);
   cameraPerspective(game->camera, 45.0f, 1920.0f/1080.0f, 0.5f, 500.0f);
 
   // Load the world
diff --git a/src/dawn/entity/entity.h b/src/dawn/entity/entity.h
new file mode 100644
index 00000000..9fec6c6f
--- /dev/null
+++ b/src/dawn/entity/entity.h
@@ -0,0 +1,6 @@
+// Copyright (c) 2021 Dominic Masters
+// 
+// This software is released under the MIT License.
+// https://opensource.org/licenses/MIT
+
+#pragma once
\ No newline at end of file
diff --git a/src/dawn/world/chunk.c b/src/dawn/world/chunk.c
index 12ac9f0b..863e4835 100644
--- a/src/dawn/world/chunk.c
+++ b/src/dawn/world/chunk.c
@@ -1,8 +1,13 @@
+/**
+ * Copyright (c) 2021 Dominic Masters
+ * 
+ * This software is released under the MIT License.
+ * https://opensource.org/licenses/MIT
+ */
+
 #include "chunk.h"
 
-void chunkLoad(world_t *world, chunk_t *chunk,
-  int32_t x, int32_t y, int32_t z
-) {
+void chunkLoad(world_t *world, chunk_t *chunk, int32_t x, int32_t y, int32_t z){
   int32_t tx, ty, tz, i;
   tile_t *tile;
   tiledef_t *tileDef;
@@ -30,7 +35,7 @@ void chunkLoad(world_t *world, chunk_t *chunk,
 
     // Now load from the buffer.
     current = strtok_r(seek, ";", &seek);
-    tile->id = current[0] - 48;
+    tile->id = current[0] - CHUNK_TILE_LOAD_ASCII;
 
     if(tile->id == TILE_NULL) continue;
     tileDef = world->tilemap->tileDefinitions + tile->id;
diff --git a/src/dawn/world/chunk.h b/src/dawn/world/chunk.h
index 49620613..f72794f0 100644
--- a/src/dawn/world/chunk.h
+++ b/src/dawn/world/chunk.h
@@ -5,10 +5,16 @@
 #include "../../engine/file/asset.h"
 #include "../../engine/util/string.h"
 
+/** Width (in tiles) of chunks */
 #define CHUNK_WIDTH 16
+/** Heihgt (in tiles) of chunks */
 #define CHUNK_HEIGHT 16
+/** Depth (in tiles) of chunks */
 #define CHUNK_DEPTH 8
 
+/** When loading a chunk, how many chars to offset (ASCII char to byte) */
+#define CHUNK_TILE_LOAD_ASCII 48
+
 /**
  * Loads a given chunk.
  * 
@@ -18,9 +24,7 @@
  * @param y Y of the chunk.
  * @param z Z of the chunk.
  */
-void chunkLoad(world_t *world, chunk_t *chunk,
-  int32_t x, int32_t y, int32_t z
-);
+void chunkLoad(world_t *world, chunk_t *chunk, int32_t x, int32_t y, int32_t z);
 
 /**
  * Unload a given chunk.
diff --git a/src/dawn/world/tile.c b/src/dawn/world/tile.c
index 69ae0c0c..5fafaef6 100644
--- a/src/dawn/world/tile.c
+++ b/src/dawn/world/tile.c
@@ -27,7 +27,7 @@ void tileMapDispose(tilemap_t *tilemap) {
 void tileRender(world_t *world, chunk_t *chunk,
   tile_t *tile, tiledef_t *tileDef, int32_t x, int32_t y, int32_t z
 ) {
-  if(tile->id == 1) {
+  if(tileDef->indiceCount == 6) {
     tilesetdiv_t *div = world->tileset->divisions + tile->id;
     quadBuffer(chunk->primitive, z,
         x,   y, div->x0, div->y0,
diff --git a/src/dawn/world/tile.h b/src/dawn/world/tile.h
index 70083957..a2c37c09 100644
--- a/src/dawn/world/tile.h
+++ b/src/dawn/world/tile.h
@@ -14,8 +14,13 @@
 #include "../../engine/display/primitives/quad.h"
 #include "../../engine/display/primitives/cube.h"
 
-#define TILE_NULL 0
-#define TILE_FLAG_DYNAMIC (tileflag_t)1
+/** The tile id that represents a NULL tile */
+#define TILE_NULL (tileid_t)0
+
+/** Width of a tile (in pixels) */
+#define TILE_WIDTH 16
+/** Height of a tile (in pixels) */
+#define TILE_HEIGHT 16
 
 /**
  * Creates a tilemap from a tileset.
diff --git a/src/dawn/world/world.c b/src/dawn/world/world.c
index 3454910c..47368fd1 100644
--- a/src/dawn/world/world.c
+++ b/src/dawn/world/world.c
@@ -13,6 +13,7 @@ world_t * worldLoad(char *assetWorldDirectory) {
   char *reading, *version, *textureFilename, *temp;
   world_t *world;
   chunk_t *chunk;
+  tiledef_t *tileDef;
   int32_t i, x, y, z;
 
   // Load the world file.
@@ -25,40 +26,50 @@ world_t * worldLoad(char *assetWorldDirectory) {
   // Create the world
   world = malloc(sizeof(world_t));
   world->assetWorldDirectory = assetWorldDirectory;
+  world->x = 0, world->y = 0, world->z = 0;
+  world->count = WORLD_WIDTH * WORLD_HEIGHT * WORLD_DEPTH;
+  world->tileCount = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH;
 
   // Now begin parsing, first we need to know which version of the file format
   // we are using.
   reading = worldData;
-  version = strtok_r(reading, ";", &reading);
+  version = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
 
   // Now load the tileset texture filename
-  textureFilename = strtok_r(reading, ";", &reading);
+  textureFilename = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
 
   // Load tileset texture.
   file[0] = '\0';
   strcat(file, assetWorldDirectory);
   strcat(file, textureFilename);
   world->texture = assetTextureLoad(file);
-
-  // Finished actual loading.
-  free(worldData);
   
   // Create the tileset
   world->tileset = tilesetCreate(
-    world->texture->width/16, world->texture->height/16,
-    world->texture->width,    world->texture->height,
+    world->texture->width/TILE_WIDTH, world->texture->height/TILE_HEIGHT,
+    world->texture->width,            world->texture->height,
     0, 0, 0, 0
   );
   
   // Create the Tilemap
   world->tilemap = tileMapCreate(world->tileset->columns, world->tileset->rows);
-  (world->tilemap->tileDefinitions + 1)->indiceCount = 6;
-  (world->tilemap->tileDefinitions + 1)->verticeCount = 4;
 
-  // Prepare the chunk lists
-  world->x = 0, world->y = 0, world->z = 0;
-  world->count = WORLD_WIDTH * WORLD_HEIGHT * WORLD_DEPTH;
-  world->tileCount = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH;
+  // Load Tilemap Definitions. Skip 
+  for(i = 0; i < world->tileset->count; i++) {
+    tileDef = world->tilemap->tileDefinitions + i;
+    
+    temp = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
+    tileDef->verticeCount = atoi(temp);
+
+    temp = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
+    tileDef->indiceCount = atoi(temp);
+
+    temp = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
+    tileDef->flags = 0x00;
+  }
+
+  // Finished actual loading.
+  free(worldData);
 
   //Create chunks
   world->chunks = malloc(world->count * sizeof(chunk_t));
@@ -106,25 +117,6 @@ void worldRender(world_t *world, shader_t *shader) {
     y = (chunk->y * CHUNK_HEIGHT);
     z = (chunk->z * CHUNK_DEPTH);
 
-    // Re-render every single tile in the chunk.
-
-    // j = 0;
-    // for(tx = 0; tx < CHUNK_WIDTH; tx++) {
-    //   for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
-    //     for(tz = 0; tz < CHUNK_DEPTH; tz++) {
-    //       // Get tile for position...
-    //       tile = chunk->tiles + (j++);
-
-    //       // Should Tile bother rendering?
-    //       if(tile->id == TILE_NULL) continue;
-    //       tileDef = list->tilemap->tileDefinitions + tile->id;
-    //       if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
-
-    //       tileRender(list, chunk, tile, tileDef, tx, ty, tz);
-    //     }
-    //   }
-    // }
-
     shaderUsePosition(shader, x, y, z, 0, 0, 0);
     primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
   }
diff --git a/src/dawn/world/world.h b/src/dawn/world/world.h
index c3599dca..fb59e334 100644
--- a/src/dawn/world/world.h
+++ b/src/dawn/world/world.h
@@ -17,13 +17,16 @@
 #include "../../engine/util/string.h"
 #include "../../engine/util/math.h"
 
-#define WORLD_WIDTH 1
+/** Width of world (in chunks) */
+#define WORLD_WIDTH 5
+/** Height of world (in chunks) */
 #define WORLD_HEIGHT WORLD_WIDTH
-#define WORLD_DEPTH WORLD_HEIGHT
+/** Depth of world (in chunks) */
+#define WORLD_DEPTH 2
+
+/** Token in the world data file to split on. */
+#define WORLD_LOAD_TOKEN ";"
 
-#define CHUNK_WIDTH 16
-#define CHUNK_HEIGHT 16
-#define CHUNK_DEPTH 8
 
 /**
  * Create a world object.
diff --git a/src/dawn/world/worldtypes.h b/src/dawn/world/worldtypes.h
index a1baffbf..f2755811 100644
--- a/src/dawn/world/worldtypes.h
+++ b/src/dawn/world/worldtypes.h
@@ -6,40 +6,81 @@
 #include "../../engine/display/texture.h"
 #include "../../engine/display/tileset.h"
 
+
+/** Bitwise Flags from tiles. */
 typedef uint8_t tileflag_t;
+
+
+/** Tile ID */
 typedef uint8_t tileid_t;
 
+
+/** Representation of a unique tile within a chunk. */
 typedef struct {
+  /** ID of the tile */
   tileid_t id;
-  void *data;
+
+  /** Rendering indice and vertice offsets for the tile. */
   int32_t indiceStart, verticeStart;
 } tile_t;
 
+
+/** Representation of the information of a tile within a tilemap. */
 typedef struct {
+  /** Flags of the tile */
   tileflag_t flags;
+
+  /** How many indices and vertices a tile with this definition has. */
   int32_t indiceCount, verticeCount;
 } tiledef_t;
 
+
+/** Representation of the tile definitions within a tilemap. */
 typedef struct {
+  /** Tile definitions within the tilemap */
   tiledef_t *tileDefinitions;
 } tilemap_t;
 
+
+/** Representation of a chunk, a group of tiles that can be buffered around. */
 typedef struct chunk_t {
+  /** Position (in absolute chunk coordinates) of this chunk */
   int32_t x, y, z;
+
+  /** Array of tiles within the chunk */
   tile_t *tiles;
+
+  /** Ready to be rendered chunk 3d primitive */
   primitive_t *primitive;
 } chunk_t;
 
+
+/** Representation of the world. */
 typedef struct {
+  /** Asset subdir name */
   char *assetWorldDirectory;
 
+  /** Tileset texture */
   texture_t *texture;
+
+  /** Tileset predivided */
   tileset_t *tileset;
+
+  /** Tilemap of the world */
   tilemap_t *tilemap;
 
+  /** Current (chunk) coordinates of the first chunk in the chunk list */
   int32_t x, y, z;
+
+  /** Count of chunks within the chunk list */
   int32_t count;
+
+  /** Count of tiles within each chunk */
   int32_t tileCount;
+
+  /** Current chunk list, ordered */
   chunk_t **chunkList;
+
+  /** Chunk array (unordered) */
   chunk_t *chunks;
 } world_t;
\ No newline at end of file
diff --git a/src/engine/display/tileset.c b/src/engine/display/tileset.c
index 92e03ef5..e96fa8aa 100644
--- a/src/engine/display/tileset.c
+++ b/src/engine/display/tileset.c
@@ -15,13 +15,13 @@ tileset_t * tilesetCreate(
 ) {
   tileset_t *tileset;
   float divX, divY, tdivX;
-  int32_t x, y, i, count;
+  int32_t x, y, i;
 
   tileset = malloc(sizeof(tileset_t));
   if(tileset == NULL) return NULL;
 
-  count = rows * columns;
-  tileset->divisions = malloc(sizeof(tilesetdiv_t) * count);
+  tileset->count = rows * columns;
+  tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count);
   if(tileset->divisions == NULL) {
     free(tileset);
     return NULL;
diff --git a/src/engine/display/tileset.h b/src/engine/display/tileset.h
index bad980ec..817aa08e 100644
--- a/src/engine/display/tileset.h
+++ b/src/engine/display/tileset.h
@@ -17,6 +17,9 @@ typedef struct {
   /** Count of X/Y divisions */
   int32_t columns, rows;
 
+  /** Count of divisions (unused) */
+  int32_t count;
+
   /** Division information */
   tilesetdiv_t *divisions;
 } tileset_t;
diff --git a/tools/map/index.js b/tools/map/index.js
index e3535231..01add147 100644
--- a/tools/map/index.js
+++ b/tools/map/index.js
@@ -1,6 +1,56 @@
 const fs = require('fs');
+const path = require('path');
 
-let buffer = "";
-for(let i = 0; i < (16*16*8); i++) buffer += "0;";
+// Constants
+const WORLD_LOAD_TOKEN = ";";
+const TILE_FLAG_DYNAMIC = 1;
 
-fs.writeFileSync('out.txt', buffer);
\ No newline at end of file
+// Method.
+const saveWorld = (world) => {
+  const pathWorld = path.join(__dirname, '..', '..', 'assets', world.name);
+  if(!fs.existsSync(pathWorld)) fs.mkdirSync(pathWorld);
+
+  // World string buffer (file data).
+  let strBuffer = "";
+
+  // Header
+  strBuffer += [
+    world.version,
+    world.tileset,
+    ""// Seal
+  ].join(WORLD_LOAD_TOKEN);
+
+  // Tilemap Definition
+  let buffer = [];
+  for(let i = 0; i < world.tilemap.length; i++) {
+    let tileDef = world.tilemap[i];
+    buffer.push(tileDef.verticeCount+'');
+    buffer.push(tileDef.indiceCount+'');
+    buffer.push(tileDef.flags+'');
+  }
+  strBuffer += buffer.join(WORLD_LOAD_TOKEN);
+  
+  fs.writeFileSync(path.join(pathWorld, 'world.txt'), strBuffer);
+}
+
+// Worlds.
+const TILEMAP_WIDTH = 8;
+const TILEMAP_HEIGHT = 298;
+
+let world = {
+  version: '1.00',
+  tileset: 'tileset.png',
+  name: 'testworld',
+  tilemap: [ ]
+};
+
+for(let i = 0; i < TILEMAP_WIDTH * TILEMAP_HEIGHT; i++) {
+  world.tilemap[i] = {
+    indiceCount: 6,
+    verticeCount: 4,
+    flags: 0
+  };
+}
+
+console.log('bruh', world);
+saveWorld(world);
\ No newline at end of file