Commenting
This commit is contained in:
@ -27,7 +27,8 @@ game_t * gameInit(platform_t *platform) {
|
|||||||
|
|
||||||
// Prepare the camera.
|
// Prepare the camera.
|
||||||
game->camera = cameraCreate();
|
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);
|
cameraPerspective(game->camera, 45.0f, 1920.0f/1080.0f, 0.5f, 500.0f);
|
||||||
|
|
||||||
// Load the world
|
// Load the world
|
||||||
|
6
src/dawn/entity/entity.h
Normal file
6
src/dawn/entity/entity.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// Copyright (c) 2021 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
@ -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"
|
#include "chunk.h"
|
||||||
|
|
||||||
void chunkLoad(world_t *world, chunk_t *chunk,
|
void chunkLoad(world_t *world, chunk_t *chunk, int32_t x, int32_t y, int32_t z){
|
||||||
int32_t x, int32_t y, int32_t z
|
|
||||||
) {
|
|
||||||
int32_t tx, ty, tz, i;
|
int32_t tx, ty, tz, i;
|
||||||
tile_t *tile;
|
tile_t *tile;
|
||||||
tiledef_t *tileDef;
|
tiledef_t *tileDef;
|
||||||
@ -30,7 +35,7 @@ void chunkLoad(world_t *world, chunk_t *chunk,
|
|||||||
|
|
||||||
// Now load from the buffer.
|
// Now load from the buffer.
|
||||||
current = strtok_r(seek, ";", &seek);
|
current = strtok_r(seek, ";", &seek);
|
||||||
tile->id = current[0] - 48;
|
tile->id = current[0] - CHUNK_TILE_LOAD_ASCII;
|
||||||
|
|
||||||
if(tile->id == TILE_NULL) continue;
|
if(tile->id == TILE_NULL) continue;
|
||||||
tileDef = world->tilemap->tileDefinitions + tile->id;
|
tileDef = world->tilemap->tileDefinitions + tile->id;
|
||||||
|
@ -5,10 +5,16 @@
|
|||||||
#include "../../engine/file/asset.h"
|
#include "../../engine/file/asset.h"
|
||||||
#include "../../engine/util/string.h"
|
#include "../../engine/util/string.h"
|
||||||
|
|
||||||
|
/** Width (in tiles) of chunks */
|
||||||
#define CHUNK_WIDTH 16
|
#define CHUNK_WIDTH 16
|
||||||
|
/** Heihgt (in tiles) of chunks */
|
||||||
#define CHUNK_HEIGHT 16
|
#define CHUNK_HEIGHT 16
|
||||||
|
/** Depth (in tiles) of chunks */
|
||||||
#define CHUNK_DEPTH 8
|
#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.
|
* Loads a given chunk.
|
||||||
*
|
*
|
||||||
@ -18,9 +24,7 @@
|
|||||||
* @param y Y of the chunk.
|
* @param y Y of the chunk.
|
||||||
* @param z Z of the chunk.
|
* @param z Z of the chunk.
|
||||||
*/
|
*/
|
||||||
void chunkLoad(world_t *world, chunk_t *chunk,
|
void chunkLoad(world_t *world, chunk_t *chunk, int32_t x, int32_t y, int32_t z);
|
||||||
int32_t x, int32_t y, int32_t z
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unload a given chunk.
|
* Unload a given chunk.
|
||||||
|
@ -27,7 +27,7 @@ void tileMapDispose(tilemap_t *tilemap) {
|
|||||||
void tileRender(world_t *world, chunk_t *chunk,
|
void tileRender(world_t *world, chunk_t *chunk,
|
||||||
tile_t *tile, tiledef_t *tileDef, int32_t x, int32_t y, int32_t z
|
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;
|
tilesetdiv_t *div = world->tileset->divisions + tile->id;
|
||||||
quadBuffer(chunk->primitive, z,
|
quadBuffer(chunk->primitive, z,
|
||||||
x, y, div->x0, div->y0,
|
x, y, div->x0, div->y0,
|
||||||
|
@ -14,8 +14,13 @@
|
|||||||
#include "../../engine/display/primitives/quad.h"
|
#include "../../engine/display/primitives/quad.h"
|
||||||
#include "../../engine/display/primitives/cube.h"
|
#include "../../engine/display/primitives/cube.h"
|
||||||
|
|
||||||
#define TILE_NULL 0
|
/** The tile id that represents a NULL tile */
|
||||||
#define TILE_FLAG_DYNAMIC (tileflag_t)1
|
#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.
|
* Creates a tilemap from a tileset.
|
||||||
|
@ -13,6 +13,7 @@ world_t * worldLoad(char *assetWorldDirectory) {
|
|||||||
char *reading, *version, *textureFilename, *temp;
|
char *reading, *version, *textureFilename, *temp;
|
||||||
world_t *world;
|
world_t *world;
|
||||||
chunk_t *chunk;
|
chunk_t *chunk;
|
||||||
|
tiledef_t *tileDef;
|
||||||
int32_t i, x, y, z;
|
int32_t i, x, y, z;
|
||||||
|
|
||||||
// Load the world file.
|
// Load the world file.
|
||||||
@ -25,40 +26,50 @@ world_t * worldLoad(char *assetWorldDirectory) {
|
|||||||
// Create the world
|
// Create the world
|
||||||
world = malloc(sizeof(world_t));
|
world = malloc(sizeof(world_t));
|
||||||
world->assetWorldDirectory = assetWorldDirectory;
|
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
|
// Now begin parsing, first we need to know which version of the file format
|
||||||
// we are using.
|
// we are using.
|
||||||
reading = worldData;
|
reading = worldData;
|
||||||
version = strtok_r(reading, ";", &reading);
|
version = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
|
||||||
|
|
||||||
// Now load the tileset texture filename
|
// Now load the tileset texture filename
|
||||||
textureFilename = strtok_r(reading, ";", &reading);
|
textureFilename = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
|
||||||
|
|
||||||
// Load tileset texture.
|
// Load tileset texture.
|
||||||
file[0] = '\0';
|
file[0] = '\0';
|
||||||
strcat(file, assetWorldDirectory);
|
strcat(file, assetWorldDirectory);
|
||||||
strcat(file, textureFilename);
|
strcat(file, textureFilename);
|
||||||
world->texture = assetTextureLoad(file);
|
world->texture = assetTextureLoad(file);
|
||||||
|
|
||||||
// Finished actual loading.
|
|
||||||
free(worldData);
|
|
||||||
|
|
||||||
// Create the tileset
|
// Create the tileset
|
||||||
world->tileset = tilesetCreate(
|
world->tileset = tilesetCreate(
|
||||||
world->texture->width/16, world->texture->height/16,
|
world->texture->width/TILE_WIDTH, world->texture->height/TILE_HEIGHT,
|
||||||
world->texture->width, world->texture->height,
|
world->texture->width, world->texture->height,
|
||||||
0, 0, 0, 0
|
0, 0, 0, 0
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create the Tilemap
|
// Create the Tilemap
|
||||||
world->tilemap = tileMapCreate(world->tileset->columns, world->tileset->rows);
|
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
|
// Load Tilemap Definitions. Skip
|
||||||
world->x = 0, world->y = 0, world->z = 0;
|
for(i = 0; i < world->tileset->count; i++) {
|
||||||
world->count = WORLD_WIDTH * WORLD_HEIGHT * WORLD_DEPTH;
|
tileDef = world->tilemap->tileDefinitions + i;
|
||||||
world->tileCount = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH;
|
|
||||||
|
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
|
//Create chunks
|
||||||
world->chunks = malloc(world->count * sizeof(chunk_t));
|
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);
|
y = (chunk->y * CHUNK_HEIGHT);
|
||||||
z = (chunk->z * CHUNK_DEPTH);
|
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);
|
shaderUsePosition(shader, x, y, z, 0, 0, 0);
|
||||||
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
|
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,16 @@
|
|||||||
#include "../../engine/util/string.h"
|
#include "../../engine/util/string.h"
|
||||||
#include "../../engine/util/math.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_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.
|
* Create a world object.
|
||||||
|
@ -6,40 +6,81 @@
|
|||||||
#include "../../engine/display/texture.h"
|
#include "../../engine/display/texture.h"
|
||||||
#include "../../engine/display/tileset.h"
|
#include "../../engine/display/tileset.h"
|
||||||
|
|
||||||
|
|
||||||
|
/** Bitwise Flags from tiles. */
|
||||||
typedef uint8_t tileflag_t;
|
typedef uint8_t tileflag_t;
|
||||||
|
|
||||||
|
|
||||||
|
/** Tile ID */
|
||||||
typedef uint8_t tileid_t;
|
typedef uint8_t tileid_t;
|
||||||
|
|
||||||
|
|
||||||
|
/** Representation of a unique tile within a chunk. */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
/** ID of the tile */
|
||||||
tileid_t id;
|
tileid_t id;
|
||||||
void *data;
|
|
||||||
|
/** Rendering indice and vertice offsets for the tile. */
|
||||||
int32_t indiceStart, verticeStart;
|
int32_t indiceStart, verticeStart;
|
||||||
} tile_t;
|
} tile_t;
|
||||||
|
|
||||||
|
|
||||||
|
/** Representation of the information of a tile within a tilemap. */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
/** Flags of the tile */
|
||||||
tileflag_t flags;
|
tileflag_t flags;
|
||||||
|
|
||||||
|
/** How many indices and vertices a tile with this definition has. */
|
||||||
int32_t indiceCount, verticeCount;
|
int32_t indiceCount, verticeCount;
|
||||||
} tiledef_t;
|
} tiledef_t;
|
||||||
|
|
||||||
|
|
||||||
|
/** Representation of the tile definitions within a tilemap. */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
/** Tile definitions within the tilemap */
|
||||||
tiledef_t *tileDefinitions;
|
tiledef_t *tileDefinitions;
|
||||||
} tilemap_t;
|
} tilemap_t;
|
||||||
|
|
||||||
|
|
||||||
|
/** Representation of a chunk, a group of tiles that can be buffered around. */
|
||||||
typedef struct chunk_t {
|
typedef struct chunk_t {
|
||||||
|
/** Position (in absolute chunk coordinates) of this chunk */
|
||||||
int32_t x, y, z;
|
int32_t x, y, z;
|
||||||
|
|
||||||
|
/** Array of tiles within the chunk */
|
||||||
tile_t *tiles;
|
tile_t *tiles;
|
||||||
|
|
||||||
|
/** Ready to be rendered chunk 3d primitive */
|
||||||
primitive_t *primitive;
|
primitive_t *primitive;
|
||||||
} chunk_t;
|
} chunk_t;
|
||||||
|
|
||||||
|
|
||||||
|
/** Representation of the world. */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
/** Asset subdir name */
|
||||||
char *assetWorldDirectory;
|
char *assetWorldDirectory;
|
||||||
|
|
||||||
|
/** Tileset texture */
|
||||||
texture_t *texture;
|
texture_t *texture;
|
||||||
|
|
||||||
|
/** Tileset predivided */
|
||||||
tileset_t *tileset;
|
tileset_t *tileset;
|
||||||
|
|
||||||
|
/** Tilemap of the world */
|
||||||
tilemap_t *tilemap;
|
tilemap_t *tilemap;
|
||||||
|
|
||||||
|
/** Current (chunk) coordinates of the first chunk in the chunk list */
|
||||||
int32_t x, y, z;
|
int32_t x, y, z;
|
||||||
|
|
||||||
|
/** Count of chunks within the chunk list */
|
||||||
int32_t count;
|
int32_t count;
|
||||||
|
|
||||||
|
/** Count of tiles within each chunk */
|
||||||
int32_t tileCount;
|
int32_t tileCount;
|
||||||
|
|
||||||
|
/** Current chunk list, ordered */
|
||||||
chunk_t **chunkList;
|
chunk_t **chunkList;
|
||||||
|
|
||||||
|
/** Chunk array (unordered) */
|
||||||
chunk_t *chunks;
|
chunk_t *chunks;
|
||||||
} world_t;
|
} world_t;
|
@ -15,13 +15,13 @@ tileset_t * tilesetCreate(
|
|||||||
) {
|
) {
|
||||||
tileset_t *tileset;
|
tileset_t *tileset;
|
||||||
float divX, divY, tdivX;
|
float divX, divY, tdivX;
|
||||||
int32_t x, y, i, count;
|
int32_t x, y, i;
|
||||||
|
|
||||||
tileset = malloc(sizeof(tileset_t));
|
tileset = malloc(sizeof(tileset_t));
|
||||||
if(tileset == NULL) return NULL;
|
if(tileset == NULL) return NULL;
|
||||||
|
|
||||||
count = rows * columns;
|
tileset->count = rows * columns;
|
||||||
tileset->divisions = malloc(sizeof(tilesetdiv_t) * count);
|
tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count);
|
||||||
if(tileset->divisions == NULL) {
|
if(tileset->divisions == NULL) {
|
||||||
free(tileset);
|
free(tileset);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -17,6 +17,9 @@ typedef struct {
|
|||||||
/** Count of X/Y divisions */
|
/** Count of X/Y divisions */
|
||||||
int32_t columns, rows;
|
int32_t columns, rows;
|
||||||
|
|
||||||
|
/** Count of divisions (unused) */
|
||||||
|
int32_t count;
|
||||||
|
|
||||||
/** Division information */
|
/** Division information */
|
||||||
tilesetdiv_t *divisions;
|
tilesetdiv_t *divisions;
|
||||||
} tileset_t;
|
} tileset_t;
|
||||||
|
@ -1,6 +1,56 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
let buffer = "";
|
// Constants
|
||||||
for(let i = 0; i < (16*16*8); i++) buffer += "0;";
|
const WORLD_LOAD_TOKEN = ";";
|
||||||
|
const TILE_FLAG_DYNAMIC = 1;
|
||||||
|
|
||||||
fs.writeFileSync('out.txt', buffer);
|
// 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);
|
Reference in New Issue
Block a user