Reshuffled

This commit is contained in:
2021-04-19 21:30:34 +10:00
parent cc94f7c775
commit ff8b84b542
45 changed files with 218 additions and 140 deletions

View File

@ -1,63 +0,0 @@
/**
* Copyright (c) 2021 Dominic Msters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dawngame.h"
game_t * gameInit(platform_t *platform) {
// Create the game
game_t *game = malloc(sizeof(game_t));
if(game == NULL) return NULL;
// Load the game engine
game->engine = engineInit(platform, GAME_NAME, GAME_INPUT_COUNT);
if(game->engine == NULL) {
free(game);
return NULL;
}
// Load the Shader
game->shader = assetShaderLoad("shaders/test.vert", "shaders/test.frag");
// Prepare the camera.
game->camera = cameraCreate();
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
game->world = worldLoad("testworld/");
// Pass to the main game to handle
return game;
}
uint32_t gameUpdate(game_t *game) {
uint32_t result;
// Update the engine.
result = engineUpdate(game->engine);
if(result != 0) return result;
// Prepare for rendering
shaderUse(game->shader);
shaderUseCamera(game->shader, game->camera);
worldRender(game->world, game->shader);
return 0;
}
void gameDispose(game_t *game) {
worldDispose(game->world);
shaderDipose(game->shader);
cameraDispose(game->camera);
engineDispose(game->engine);
free(game);
}
engine_t * gameGetEngine(game_t *game) {
return game->engine;
}

View File

@ -1,41 +0,0 @@
// Copyright (c) 2021 Dominic Msters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <malloc.h>
#include "../engine/engine.h"
#include "../engine/file/asset.h"
#include "../engine/display/shader.h"
#include "../engine/display/camera.h"
#include "world/world.h"
/////////////////////////////// TYPE DEFINITIONS ///////////////////////////////
/** Context about Dawn Game */
typedef struct {
/** The engine context for the game */
engine_t *engine;
/** Rendering items */
camera_t *camera;
shader_t *shader;
world_t *world;
} dawngame_t;
#define GAMETYPE_T dawngame_t
////////////////////////////// TYPE BOUND INCLUDES /////////////////////////////
#include "../engine/game/game.h"
/////////////////////////////////// CONSTANTS //////////////////////////////////
/** Name of the Game */
#define GAME_NAME "Dawn Game"
/** Inputs */
#define GAME_INPUT_UP (inputbind_t)0x01
#define GAME_INPUT_DOWN (inputbind_t)0x02
#define GAME_INPUT_LEFT (inputbind_t)0x03
#define GAME_INPUT_RIGHT (inputbind_t)0x04
#define GAME_INPUT_COUNT 5

View File

@ -1,84 +0,0 @@
/**
* 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){
int32_t tx, ty, tz, i;
tile_t *tile;
tiledef_t *tileDef;
tilesetdiv_t *div;
char *buffer, *seek, *current;
char fileName[128];
// Determine file name of the chunk.
fileName[0] = '\0';
strcat(fileName, world->assetWorldDirectory);
strcat(fileName, "chunks/");
strcat(fileName, "%d_%d_%d.txt");
sprintf(fileName, fileName, x, y, z);
// Load contents of chunk
buffer = assetStringLoad(fileName);
if(buffer == NULL) return;
seek = buffer;
// Determine the size of our primitive.
int32_t indiceCount = 0, verticeCount = 0;
for(i = 0; i < CHUNK_TILE_COUNT; i++) {
// Get the tile
tile = chunk->tiles + i;
// Now load from the buffer.
current = strtok_r(seek, ";", &seek);
tile->id = current[0] - CHUNK_TILE_LOAD_ASCII;
if(tile->id == TILE_NULL) continue;
tileDef = world->tilemap->tileDefinitions + tile->id;
if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
tile->verticeStart = verticeCount;
tile->indiceStart = indiceCount;
verticeCount += tileDef->verticeCount;
indiceCount += tileDef->indiceCount;
}
// Now we know how big the primitive needs to be!
free(buffer);
if(indiceCount > 0) {
chunk->primitive = primitiveCreate(verticeCount, indiceCount);
}
// For each tile
i = 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 + (i++);
// Should Chunk bother rendering?
if(tile->id == TILE_NULL) continue;
tileDef = world->tilemap->tileDefinitions + tile->id;
if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
// Render the tile's primitive
tileRender(world, chunk, tile, tileDef, tx, ty, tz);
}
}
}
}
void chunkUnload(world_t *world, chunk_t *chunk) {
// Unload the primitive
primitiveDispose(chunk->primitive);
chunk->primitive = NULL;
// Load chunks to zero. TODO: Necessary?
memset(chunk->tiles, TILE_NULL, CHUNK_TILE_COUNT);
}

View File

@ -1,28 +0,0 @@
#pragma once
#include <string.h>
#include "worldtypes.h"
#include "tile.h"
#include "../../engine/file/asset.h"
#include "../../engine/util/string.h"
/** When loading a chunk, how many chars to offset (ASCII char to byte) */
#define CHUNK_TILE_LOAD_ASCII 48
/**
* Loads a given chunk.
*
* @param list World to load the chunk for.
* @param chunk The chunk to load.
* @param x X of the chunk.
* @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);
/**
* Unload a given chunk.
*
* @param world World that the chunk belongs to.
* @param chunk Chunk to unload.
*/
void chunkUnload(world_t *world, chunk_t *chunk);

View File

@ -1,38 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "tile.h"
tilemap_t * tileMapCreate(int32_t columns, int32_t rows) {
tilemap_t *tilemap = malloc(sizeof(tilemap_t));
if(tilemap == NULL) return NULL;
tilemap->tileDefinitions = calloc(columns * rows, sizeof(tiledef_t));
if(!tilemap->tileDefinitions) {
free(tilemap);
return NULL;
}
return tilemap;
}
void tileMapDispose(tilemap_t *tilemap) {
free(tilemap->tileDefinitions);
free(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(tileDef->indiceCount == 6) {
tilesetdiv_t *div = world->tileset->divisions + tile->id;
quadBuffer(chunk->primitive, z,
x, y, div->x0, div->y0,
x+1, y+1, div->x1, div->y1,
tile->verticeStart, tile->indiceStart
);
}
}

View File

@ -1,46 +0,0 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <stdint.h>
#include <malloc.h>
#include "worldtypes.h"
#include "../../engine/display/tileset.h"
#include "../../engine/display/primitives/quad.h"
#include "../../engine/display/primitives/cube.h"
/** The tile id that represents a NULL tile */
#define TILE_NULL (tileid_t)0
/**
* Creates a tilemap from a tileset.
*
* @param columns Count of columns in the tilemap.
* @param rows Count of rows in the tilemap.
* @returns The tilemap.
*/
tilemap_t * tileMapCreate(int32_t columns, int32_t rows);
/**
* Destroys a previously created tilemap.
*
* @param tilemap Tilemap to dispose.
*/
void tileMapDispose(tilemap_t *tilemap);
/**
* Rendedrs a given chunk tile.
*
* @param world The world that the chunk belongs to.
* @param chunk The chunk to render against.
* @param tile The tile to render.
* @param tileDef The tile's definition information.
* @param x The X Coordinate of the tile (chunk space).
* @param y The Y Coordinate of the tile (chunk space).
* @param z The Z Coordinate of the tile (chunk space).
*/
void tileRender(world_t *world, chunk_t *chunk,
tile_t *tile, tiledef_t *tileDef, int32_t x, int32_t y, int32_t z
);

View File

@ -1,211 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "world.h"
world_t * worldLoad(char *assetWorldDirectory) {
// Define our local variables
char file [128];
char *reading, *version, *textureFilename, *temp;
world_t *world;
chunk_t *chunk;
tiledef_t *tileDef;
int32_t i, x, y, z;
// Load the world file.
file[0] = '\0';
strcat(file, assetWorldDirectory);
strcat(file, "world.txt");
char *worldData = assetStringLoad(file);
if(worldData == NULL) return NULL;
// Create the world
world = malloc(sizeof(world_t));
world->assetWorldDirectory = assetWorldDirectory;
world->x = 0, world->y = 0, world->z = 0;
// Now begin parsing, first we need to know which version of the file format
// we are using.
reading = worldData;
version = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
// Now load the tileset texture filename
textureFilename = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
// Load tileset texture.
file[0] = '\0';
strcat(file, assetWorldDirectory);
strcat(file, textureFilename);
world->texture = assetTextureLoad(file);
// Create the tileset
world->tileset = tilesetCreate(
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);
// 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);
// Load initial chunks, ZYX order is important here.
i = 0;
for(z = 0; z < WORLD_DEPTH; z++) {
for(y = 0; y < WORLD_HEIGHT; y++) {
for(x = 0; x < WORLD_WIDTH; x++) {
chunk = world->chunks + i;
world->chunkList[i] = chunk;
// Load initial coordinates
chunk->x = x;
chunk->y = y;
chunk->z = z;
chunk->primitive = NULL;
// Init the chunk.
chunkLoad(world, chunk, x, y, z);
i++;
}
}
}
return world;
}
void worldRender(world_t *world, shader_t *shader) {
int32_t i, j, tx, ty, tz;
float x, y, z;
chunk_t *chunk;
tile_t *tile;
tiledef_t *tileDef;
shaderUseTexture(shader, world->texture);
for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
chunk = world->chunks + i;
if(chunk->primitive == NULL) continue;
x = (chunk->x * CHUNK_WIDTH);
y = (chunk->y * CHUNK_HEIGHT);
z = (chunk->z * CHUNK_DEPTH);
shaderUsePosition(shader, x, y, z, 0, 0, 0);
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
}
}
void worldDispose(world_t *world) {
int32_t i;
chunk_t *chunk;
// Unload the chunks
for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
chunk = world->chunks + i;
chunkUnload(world, chunk);
}
textureDispose(world->texture);
tilesetDispose(world->tileset);
tileMapDispose(world->tilemap);
free(world);
}
void worldShift(world_t *world, int32_t x, int32_t y, int32_t z) {
int32_t i,
/** New List Coordinates */
lx, ly, lz,
/** New Local Chunk Coordinates */
nx, ny, nz,
/** New Absolute Chunk Coordinates */
ax, ay, az,
/** New Chunk Index */
ni,
/** Precalculated width * height */
wh
;
chunk_t *chunk;
chunk_t *chunkList[WORLD_CHUNK_COUNT];
// Calculate the new chunklist coordinates
lx = world->x + x;
ly = world->y + y;
lz = world->z + z;
// Precalc width * height.
wh = WORLD_WIDTH * WORLD_HEIGHT;
for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
chunk = world->chunkList[i];
// Calculate the new local positions for the chunk.
nx = mathMod(chunk->x - world->x - x, WORLD_WIDTH);
ny = mathMod(chunk->y - world->y - y, WORLD_HEIGHT);
nz = mathMod(chunk->z - world->z - z, WORLD_DEPTH);
// Load the chunk if we need to. We also use this to calculate new absolutes
if(
(ax = lx + nx) != chunk->x ||
(ly + ny) != chunk->y ||
(lz + nz) != chunk->z
) {
// Calculate those things that may have not been done within the if
ay = ly + ny;
az = lz + nz;
// Load new chunk.
chunkUnload(world, chunk);
chunkLoad(world, chunk, ax, ay, az);
// Update the absolute coordinates.
chunk->x = ax;
chunk->y = ay;
chunk->z = az;
}
// Now, based off those new local positions, calculate the new index.
ni = (
nx +
(ny * WORLD_WIDTH) +
(nz * wh)
);
chunkList[ni] = chunk;
}
// Update Absolutes.
world->x = lx;
world->y = ly;
world->z = lz;
// Now copy that array over.
memcpy(world->chunkList, chunkList, sizeof(chunk_t *) * WORLD_CHUNK_COUNT);
free(chunkList);
}
void worldAlign(world_t *world, int32_t x, int32_t y, int32_t z) {
int32_t lx, ly, lz;
lx = x - world->x;
ly = y - world->y;
lz = z - world->z;
worldShift(world, lx, ly, lz);
}

View File

@ -1,63 +0,0 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <malloc.h>
#include <stdint.h>
#include "worldtypes.h"
#include "tile.h"
#include "chunk.h"
#include "../../engine/display/shader.h"
#include "../../engine/display/primitive.h"
#include "../../engine/display/tileset.h"
#include "../../engine/display/texture.h"
#include "../../engine/file/asset.h"
#include "../../engine/util/string.h"
#include "../../engine/util/math.h"
/** Token in the world data file to split on. */
#define WORLD_LOAD_TOKEN ";"
/**
* Create a world object.
*
* @returns The newly created world.
*/
world_t * worldLoad(char *fileName);
/**
* Render a world object to the graphics device.
*
* @param world The world to render.
* @param shader The shader to render to.
*/
void worldRender(world_t *world, shader_t *shader);
/**
* Cleans up a previously created world.
*
* @param world World to cleanup.
*/
void worldDispose(world_t *world);
/**
* Shift the world chunk list along a set of axis (in absolute space).
*
* @param world World to shift.
* @param x X movement to shift chunk along.
* @param y Y movement to shift chunk along.
* @param z Z movement to shift chunk along.
*/
void worldShift(world_t *world, int32_t x, int32_t y, int32_t z);
/**
* Align the world chunk list (in absolute space).
*
* @param world World to align.
* @param x X movement to shift chunk along.
* @param y Y movement to shift chunk along.
* @param z Z movement to shift chunk along.
*/
void worldAlign(world_t *world, int32_t x, int32_t y, int32_t z);

View File

@ -1,126 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <stdint.h>
#include <malloc.h>
#include "../../engine/display/primitive.h"
#include "../../engine/display/texture.h"
#include "../../engine/display/tileset.h"
////////////////////////////////////////////////////////////////////////////////
// Entities
////////////////////////////////////////////////////////////////////////////////
/** Entity ID */
typedef uint8_t entityid_t;
typedef struct {
float x, y, z;
} entitypos_t;
////////////////////////////////////////////////////////////////////////////////
// Tiles
////////////////////////////////////////////////////////////////////////////////
/** Width of a tile (in pixels) */
#define TILE_WIDTH 16
/** Height of a tile (in pixels) */
#define TILE_HEIGHT 16
/** 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;
/** 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;
////////////////////////////////////////////////////////////////////////////////
// Chunks
////////////////////////////////////////////////////////////////////////////////
/** 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
/** Count of tiles in the chunk. */
#define CHUNK_TILE_COUNT CHUNK_WIDTH*CHUNK_HEIGHT*CHUNK_DEPTH
/** 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[CHUNK_TILE_COUNT];
/** Ready to be rendered chunk 3d primitive */
primitive_t *primitive;
} chunk_t;
////////////////////////////////////////////////////////////////////////////////
// Worlds
////////////////////////////////////////////////////////////////////////////////
/** Width of world (in chunks) */
#define WORLD_WIDTH 5
/** Height of world (in chunks) */
#define WORLD_HEIGHT WORLD_WIDTH
/** Depth of world (in chunks) */
#define WORLD_DEPTH 2
/** Count of chunks in the world */
#define WORLD_CHUNK_COUNT WORLD_WIDTH*WORLD_HEIGHT*WORLD_DEPTH
/** 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;
/** Current chunk list, ordered */
chunk_t *chunkList[WORLD_CHUNK_COUNT];
/** Chunk array (unordered) */
chunk_t chunks[WORLD_CHUNK_COUNT];
} world_t;