bruh(2)
This commit is contained in:
@ -1,3 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
#include "primitive.h"
|
#include "primitive.h"
|
||||||
|
|
||||||
primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) {
|
primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) {
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
@ -30,6 +30,7 @@ game_t * gameInit(platform_t *platform) {
|
|||||||
|
|
||||||
// Load the world
|
// Load the world
|
||||||
game->world = worldLoad("testworld/");
|
game->world = worldLoad("testworld/");
|
||||||
|
playerCreate(&game->world->entities, 0);
|
||||||
|
|
||||||
// Pass to the main game to handle
|
// Pass to the main game to handle
|
||||||
return game;
|
return game;
|
||||||
@ -45,7 +46,10 @@ uint32_t gameUpdate(game_t *game) {
|
|||||||
// Prepare for rendering
|
// Prepare for rendering
|
||||||
shaderUse(game->shader);
|
shaderUse(game->shader);
|
||||||
shaderUseCamera(game->shader, game->camera);
|
shaderUseCamera(game->shader, game->camera);
|
||||||
worldRender(game->world, game->shader, game->camera);
|
worldRender(game->world,
|
||||||
|
game->shader, game->camera,
|
||||||
|
game->engine->input
|
||||||
|
);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
#include "../display/shader.h"
|
#include "../display/shader.h"
|
||||||
#include "../world/world.h"
|
#include "../world/world.h"
|
||||||
|
|
||||||
|
#include "../world/entity/player.h"
|
||||||
|
|
||||||
/////////////////////////////////// CONSTANTS //////////////////////////////////
|
/////////////////////////////////// CONSTANTS //////////////////////////////////
|
||||||
/** Name of the Game */
|
/** Name of the Game */
|
||||||
#define GAME_NAME "Dawn Game"
|
#define GAME_NAME "Dawn Game"
|
||||||
@ -27,13 +29,14 @@
|
|||||||
|
|
||||||
/////////////////////////////// TYPE DEFINITIONS ///////////////////////////////
|
/////////////////////////////// TYPE DEFINITIONS ///////////////////////////////
|
||||||
/** Information about the current game context. */
|
/** Information about the current game context. */
|
||||||
typedef struct {
|
typedef struct game_t {
|
||||||
/** The engine context for the game */
|
/** The engine context for the game */
|
||||||
engine_t *engine;
|
engine_t *engine;
|
||||||
|
|
||||||
/** Rendering items */
|
/** Rendering items */
|
||||||
camera_t *camera;
|
camera_t *camera;
|
||||||
shader_t *shader;
|
shader_t *shader;
|
||||||
|
|
||||||
world_t *world;
|
world_t *world;
|
||||||
} game_t;
|
} game_t;
|
||||||
|
|
||||||
|
@ -7,9 +7,11 @@
|
|||||||
|
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
|
|
||||||
void chunkLoad(world_t *world, chunk_t *chunk, int32_t x, int32_t y, int32_t z){
|
void chunkLoad(chunk_t *chunk, tilemap_t *tilemap,
|
||||||
|
int32_t x, int32_t y, int32_t z
|
||||||
|
){
|
||||||
int32_t tx, ty, tz, i;
|
int32_t tx, ty, tz, i;
|
||||||
tile_t *tile;
|
tileid_t tileId;
|
||||||
tiledef_t *tileDef;
|
tiledef_t *tileDef;
|
||||||
tilesetdiv_t *div;
|
tilesetdiv_t *div;
|
||||||
char *buffer, *seek, *current;
|
char *buffer, *seek, *current;
|
||||||
@ -17,9 +19,7 @@ void chunkLoad(world_t *world, chunk_t *chunk, int32_t x, int32_t y, int32_t z){
|
|||||||
|
|
||||||
// Determine file name of the chunk.
|
// Determine file name of the chunk.
|
||||||
fileName[0] = '\0';
|
fileName[0] = '\0';
|
||||||
strcat(fileName, world->assetWorldDirectory);
|
strcat(fileName, "world/chunks/%d_%d_%d.txt");
|
||||||
strcat(fileName, "chunks/");
|
|
||||||
strcat(fileName, "%d_%d_%d.txt");
|
|
||||||
sprintf(fileName, fileName, x, y, z);
|
sprintf(fileName, fileName, x, y, z);
|
||||||
|
|
||||||
// Load contents of chunk
|
// Load contents of chunk
|
||||||
@ -30,55 +30,57 @@ void chunkLoad(world_t *world, chunk_t *chunk, int32_t x, int32_t y, int32_t z){
|
|||||||
// Determine the size of our primitive.
|
// Determine the size of our primitive.
|
||||||
int32_t indiceCount = 0, verticeCount = 0;
|
int32_t indiceCount = 0, verticeCount = 0;
|
||||||
for(i = 0; i < CHUNK_TILE_COUNT; i++) {
|
for(i = 0; i < CHUNK_TILE_COUNT; i++) {
|
||||||
// Get the tile
|
|
||||||
tile = chunk->tiles + i;
|
|
||||||
|
|
||||||
// Now load from the buffer.
|
// Now load from the buffer.
|
||||||
current = strtok_r(seek, ";", &seek);
|
current = strtok_r(seek, ";", &seek);
|
||||||
tile->id = current[0] - CHUNK_TILE_LOAD_ASCII;
|
*(chunk->tiles + i) = current[0] - CHUNK_TILE_LOAD_ASCII;
|
||||||
|
tileId = *(chunk->tiles + i);
|
||||||
if(tile->id == TILE_NULL) continue;
|
if(tileId == TILE_NULL) continue;
|
||||||
tileDef = world->tilemap->tileDefinitions + tile->id;
|
|
||||||
if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
|
|
||||||
|
|
||||||
tile->verticeStart = verticeCount;
|
|
||||||
tile->indiceStart = indiceCount;
|
|
||||||
|
|
||||||
|
// Increment tile definition.
|
||||||
|
tileDef = tilemap->tileDefinitions+tileId;
|
||||||
verticeCount += tileDef->verticeCount;
|
verticeCount += tileDef->verticeCount;
|
||||||
indiceCount += tileDef->indiceCount;
|
indiceCount += tileDef->indiceCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we know how big the primitive needs to be!
|
// Now we know how big the primitive needs to be!
|
||||||
free(buffer);
|
free(buffer);
|
||||||
if(indiceCount > 0) {
|
if(indiceCount == 0) return;
|
||||||
chunk->primitive = primitiveCreate(verticeCount, indiceCount);
|
chunk->primitive = primitiveCreate(verticeCount, indiceCount);
|
||||||
}
|
|
||||||
|
|
||||||
// For each tile
|
// For each tile
|
||||||
i = 0;
|
i = 0;
|
||||||
|
verticeCount = 0, indiceCount = 0;
|
||||||
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
|
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
|
||||||
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
|
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
|
||||||
for(tz = 0; tz < CHUNK_DEPTH; tz++) {
|
for(tz = 0; tz < CHUNK_DEPTH; tz++) {
|
||||||
// Get tile for position...
|
// Get tile for position...
|
||||||
tile = chunk->tiles + (i++);
|
tileId = *(chunk->tiles + (i++));
|
||||||
|
if(tileId == TILE_NULL) continue;
|
||||||
|
|
||||||
// Should Chunk bother rendering?
|
// Get the definition.
|
||||||
if(tile->id == TILE_NULL) continue;
|
tileDef = tilemap->tileDefinitions+tileId;
|
||||||
tileDef = world->tilemap->tileDefinitions + tile->id;
|
if(tileDef->tileRender == NULL) continue;
|
||||||
if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
|
|
||||||
|
|
||||||
// Render the tile's primitive
|
// Render the tile's primitive
|
||||||
tileRender(world, chunk, tile, tileDef, tx, ty, tz);
|
tileDef->tileRender(tileId, tilemap, tileDef,
|
||||||
|
chunk->primitive, verticeCount, indiceCount,
|
||||||
|
tx, ty, tz
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prepare for the next render.
|
||||||
|
verticeCount += tileDef->verticeCount;
|
||||||
|
indiceCount += tileDef->indiceCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void chunkUnload(world_t *world, chunk_t *chunk) {
|
void chunkUnload(chunk_t *chunk) {
|
||||||
// Unload the primitive
|
|
||||||
primitiveDispose(chunk->primitive);
|
|
||||||
chunk->primitive = NULL;
|
|
||||||
|
|
||||||
// Load chunks to zero. TODO: Necessary?
|
// Load chunks to zero. TODO: Necessary?
|
||||||
memset(chunk->tiles, TILE_NULL, CHUNK_TILE_COUNT);
|
memset(chunk->tiles, TILE_NULL, CHUNK_TILE_COUNT);
|
||||||
|
|
||||||
|
// Unload the primitive
|
||||||
|
if(chunk->primitive == NULL) return;
|
||||||
|
primitiveDispose(chunk->primitive);
|
||||||
|
chunk->primitive = NULL;
|
||||||
}
|
}
|
@ -1,28 +1,49 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "worldtypes.h"
|
|
||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
#include "../file/asset.h"
|
#include "../file/asset.h"
|
||||||
|
#include "../input/input.h"
|
||||||
#include "../util/string.h"
|
#include "../util/string.h"
|
||||||
|
|
||||||
/** When loading a chunk, how many chars to offset (ASCII char to byte) */
|
/** When loading a chunk, how many chars to offset (ASCII char to byte) */
|
||||||
#define CHUNK_TILE_LOAD_ASCII 48
|
#define CHUNK_TILE_LOAD_ASCII 48
|
||||||
|
|
||||||
|
/** 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 {
|
||||||
|
/** Position (in absolute chunk coordinates) of this chunk */
|
||||||
|
int32_t x, y, z;
|
||||||
|
|
||||||
|
/** Array of tiles within the chunk */
|
||||||
|
tileid_t tiles[CHUNK_TILE_COUNT];
|
||||||
|
|
||||||
|
/** Ready to be rendered chunk 3d primitive */
|
||||||
|
primitive_t *primitive;
|
||||||
|
} chunk_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a given chunk.
|
* Loads a given chunk.
|
||||||
*
|
*
|
||||||
* @param list World to load the chunk for.
|
|
||||||
* @param chunk The chunk to load.
|
* @param chunk The chunk to load.
|
||||||
* @param x X of the chunk.
|
* @param x X of the chunk.
|
||||||
* @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, int32_t x, int32_t y, int32_t z);
|
void chunkLoad(chunk_t *chunk, tilemap_t *tilemap,
|
||||||
|
int32_t x, int32_t y, int32_t z
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unload a given chunk.
|
* Unload a given chunk.
|
||||||
*
|
*
|
||||||
* @param world World that the chunk belongs to.
|
|
||||||
* @param chunk Chunk to unload.
|
* @param chunk Chunk to unload.
|
||||||
*/
|
*/
|
||||||
void chunkUnload(world_t *world, chunk_t *chunk);
|
void chunkUnload(chunk_t *chunk);
|
21
src/world/entity/entity.c
Normal file
21
src/world/entity/entity.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "entity.h"
|
||||||
|
|
||||||
|
void entitySystemInit(entitysystem_t *entitySystem) {
|
||||||
|
memset(&entitySystem->entities, ENTITY_TYPE_NULL,
|
||||||
|
sizeof(entityid_t) * ENTITY_COUNT_MAX
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void entitySystemUpdate(entitysystem_t *entitySystem,
|
||||||
|
shader_t *shader, camera_t *camera,
|
||||||
|
input_t *input
|
||||||
|
) {
|
||||||
|
entitySystem->callbacks[0].render(entitySystem, 0, shader, camera, input);
|
||||||
|
}
|
@ -7,29 +7,33 @@
|
|||||||
|
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
void playerCreate(world_t *world, entity_t *entity) {
|
void playerCreate(entitysystem_t *system, int32_t index) {
|
||||||
// Setup the update state.
|
system->entities[index] = ENTITY_TYPE_PLAYER;
|
||||||
entity->entityUpdate = &playerUpdate;
|
system->callbacks[index].dispose = &playerDispose;
|
||||||
entity->entityDispose = &playerDispose;
|
system->callbacks[index].render = &playerRender;
|
||||||
|
system->positions[index].x = 0;
|
||||||
world->entityPrimitives[entity->id] = quadCreate(0,
|
system->positions[index].y = 0;
|
||||||
|
system->positions[index].z = 0;
|
||||||
|
system->primitives[index] = quadCreate(0,
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
1, 1, 1, 1
|
1, 1, 1, 1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void playerUpdate(world_t *world, entity_t *entity, shader_t *shader,
|
void playerDispose(entitysystem_t *system, int32_t index) {
|
||||||
camera_t *camera
|
}
|
||||||
|
|
||||||
|
void playerRender(entitysystem_t *system, int32_t index,
|
||||||
|
shader_t *shader, camera_t *camera,
|
||||||
|
input_t *input
|
||||||
) {
|
) {
|
||||||
shaderUsePosition(shader, 0, 0, 0, 0, 0, 0);
|
shaderUsePosition(shader,
|
||||||
primitiveDraw(world->entityPrimitives[entity->id],
|
system->positions[index].x,
|
||||||
0, world->entityPrimitives[entity->id]->indiceCount
|
system->positions[index].y,
|
||||||
|
system->positions[index].z,
|
||||||
|
0, 0, 0
|
||||||
|
);
|
||||||
|
primitiveDraw(system->primitives[index],
|
||||||
|
0, system->primitives[index]->indiceCount
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void playerDispose(world_t *world, entity_t *entity) {
|
|
||||||
primitiveDispose(world->entityPrimitives[entity->id]);
|
|
||||||
|
|
||||||
entity->entityDispose = NULL;
|
|
||||||
entity->entityUpdate = NULL;
|
|
||||||
}
|
|
@ -6,13 +6,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "../worldtypes.h"
|
#include "entity.h"
|
||||||
|
#include "../../display/primitive.h"
|
||||||
#include "../../display/primitives/quad.h"
|
#include "../../display/primitives/quad.h"
|
||||||
#include "../../input/input.h"
|
|
||||||
|
|
||||||
void playerCreate(world_t *world, entity_t *entity);
|
void playerCreate(entitysystem_t *system, int32_t index);
|
||||||
void playerUpdate(world_t *world, entity_t *entity, shader_t *shader,
|
void playerDispose(entitysystem_t *system, int32_t index);
|
||||||
camera_t *camera
|
void playerRender(entitysystem_t *system, int32_t index,
|
||||||
|
shader_t *shader, camera_t *camera,
|
||||||
|
input_t *input
|
||||||
);
|
);
|
||||||
|
|
||||||
void playerDispose(world_t *world, entity_t *entity);
|
|
@ -7,32 +7,48 @@
|
|||||||
|
|
||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
|
|
||||||
tilemap_t * tileMapCreate(int32_t columns, int32_t rows) {
|
tilemap_t * tileMapCreate(texture_t *texture) {
|
||||||
tilemap_t *tilemap = malloc(sizeof(tilemap_t));
|
tilemap_t *tilemap = malloc(sizeof(tilemap_t));
|
||||||
if(tilemap == NULL) return NULL;
|
if(tilemap == NULL) return NULL;
|
||||||
|
|
||||||
tilemap->tileDefinitions = calloc(columns * rows, sizeof(tiledef_t));
|
tilemap->texture = texture;
|
||||||
if(!tilemap->tileDefinitions) {
|
|
||||||
|
// Create the tileset for the texture.
|
||||||
|
tilemap->tileset = tilesetCreate(
|
||||||
|
texture->width/TILE_WIDTH, texture->height/TILE_HEIGHT,
|
||||||
|
texture->width, texture->height,
|
||||||
|
0, 0, 0, 0
|
||||||
|
);
|
||||||
|
if(tilemap->tileset == NULL) {
|
||||||
free(tilemap);
|
free(tilemap);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup the tile definitions for each tile within the tileset.
|
||||||
|
tilemap->tileDefinitions = calloc(tilemap->tileset->count, sizeof(tiledef_t));
|
||||||
|
if(!tilemap->tileDefinitions) {
|
||||||
|
tilesetDispose(tilemap->tileset);
|
||||||
|
free(tilemap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return tilemap;
|
return tilemap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tileMapDispose(tilemap_t *tilemap) {
|
void tileMapDispose(tilemap_t *tilemap) {
|
||||||
free(tilemap->tileDefinitions);
|
free(tilemap->tileDefinitions);
|
||||||
|
tilesetDispose(tilemap->tileset);
|
||||||
free(tilemap);
|
free(tilemap);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tileRender(world_t *world, chunk_t *chunk,
|
void tileQuadRender(tileid_t id, tilemap_t *tilemap, tiledef_t *tileDef,
|
||||||
tile_t *tile, tiledef_t *tileDef, int32_t x, int32_t y, int32_t z
|
primitive_t *primitive, int32_t verticeStart, int32_t indiceStart,
|
||||||
|
int32_t x, int32_t y, int32_t z
|
||||||
) {
|
) {
|
||||||
if(tileDef->indiceCount == 6) {
|
tilesetdiv_t *div = tilemap->tileset->divisions + id;
|
||||||
tilesetdiv_t *div = world->tileset->divisions + tile->id;
|
quadBuffer(primitive, z,
|
||||||
quadBuffer(chunk->primitive, z,
|
|
||||||
x, y, div->x0, div->y0,
|
x, y, div->x0, div->y0,
|
||||||
x+1, y+1, div->x1, div->y1,
|
x+1, y+1, div->x1, div->y1,
|
||||||
tile->verticeStart, tile->indiceStart
|
verticeStart, indiceStart
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
@ -6,22 +6,62 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include "worldtypes.h"
|
|
||||||
#include "../display/tileset.h"
|
#include "../display/tileset.h"
|
||||||
|
#include "../display/texture.h"
|
||||||
#include "../display/primitives/quad.h"
|
#include "../display/primitives/quad.h"
|
||||||
#include "../display/primitives/cube.h"
|
#include "../display/primitives/cube.h"
|
||||||
|
|
||||||
/** The tile id that represents a NULL tile */
|
/** The tile id that represents a NULL tile */
|
||||||
#define TILE_NULL (tileid_t)0
|
#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
|
||||||
|
|
||||||
|
/** Bitwise Flags from tiles. */
|
||||||
|
typedef uint8_t tileflag_t;
|
||||||
|
|
||||||
|
/** Tile ID */
|
||||||
|
typedef uint8_t tileid_t;
|
||||||
|
|
||||||
|
// Forwarders
|
||||||
|
typedef struct tiledef_t tiledef_t;
|
||||||
|
typedef struct tilemap_t tilemap_t;
|
||||||
|
|
||||||
|
/** Representation of the information of a tile within a tilemap. */
|
||||||
|
typedef struct tiledef_t {
|
||||||
|
/** Flags of the tile */
|
||||||
|
tileflag_t flags;
|
||||||
|
|
||||||
|
/** How many indices and vertices a tile with this definition has. */
|
||||||
|
int32_t indiceCount, verticeCount;
|
||||||
|
|
||||||
|
/** Callback for the tile definition for rendering the tile */
|
||||||
|
void (*tileRender)(tileid_t id, tilemap_t *tilemap, tiledef_t *tileDef,
|
||||||
|
primitive_t *primitive, int32_t verticeStart, int32_t indiceStart,
|
||||||
|
int32_t x, int32_t y, int32_t z
|
||||||
|
);
|
||||||
|
} tiledef_t;
|
||||||
|
|
||||||
|
/** Representation of the tile definitions within a tilemap. */
|
||||||
|
typedef struct tilemap_t {
|
||||||
|
/** Tile definitions within the tilemap */
|
||||||
|
tiledef_t *tileDefinitions;
|
||||||
|
|
||||||
|
/** Tileset predivided */
|
||||||
|
tileset_t *tileset;
|
||||||
|
|
||||||
|
/** Texture of the tileset */
|
||||||
|
texture_t *texture;
|
||||||
|
} tilemap_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a tilemap from a tileset.
|
* Creates a tilemap from a tileset.
|
||||||
*
|
* @param The texture to base the tilemap on.
|
||||||
* @param columns Count of columns in the tilemap.
|
|
||||||
* @param rows Count of rows in the tilemap.
|
|
||||||
* @returns The tilemap.
|
* @returns The tilemap.
|
||||||
*/
|
*/
|
||||||
tilemap_t * tileMapCreate(int32_t columns, int32_t rows);
|
tilemap_t * tileMapCreate(texture_t *texture);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroys a previously created tilemap.
|
* Destroys a previously created tilemap.
|
||||||
@ -31,16 +71,19 @@ tilemap_t * tileMapCreate(int32_t columns, int32_t rows);
|
|||||||
void tileMapDispose(tilemap_t *tilemap);
|
void tileMapDispose(tilemap_t *tilemap);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rendedrs a given chunk tile.
|
* Renders a tile as a standard quad.
|
||||||
*
|
*
|
||||||
* @param world The world that the chunk belongs to.
|
* @param id ID of the Tile.
|
||||||
* @param chunk The chunk to render against.
|
* @param tilemap The tilemap that the tile belongs to.
|
||||||
* @param tile The tile to render.
|
* @param tileDef The tiledef of the tile.
|
||||||
* @param tileDef The tile's definition information.
|
* @param primitive The primitive to render to.
|
||||||
* @param x The X Coordinate of the tile (chunk space).
|
* @param verticeStart The start index of the vertice.
|
||||||
* @param y The Y Coordinate of the tile (chunk space).
|
* @param indiceStart The start index of the indice.
|
||||||
* @param z The Z Coordinate of the tile (chunk space).
|
* @param x Tile (local) X position coordinate.
|
||||||
|
* @param y Tile (local) Y position coordinate.
|
||||||
|
* @param z Tile (local) Z position coordinate.
|
||||||
*/
|
*/
|
||||||
void tileRender(world_t *world, chunk_t *chunk,
|
void tileQuadRender(tileid_t id, tilemap_t *tilemap, tiledef_t *tileDef,
|
||||||
tile_t *tile, tiledef_t *tileDef, int32_t x, int32_t y, int32_t z
|
primitive_t *primitive, int32_t verticeStart, int32_t indiceStart,
|
||||||
|
int32_t x, int32_t y, int32_t z
|
||||||
);
|
);
|
@ -7,36 +7,26 @@
|
|||||||
|
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
|
|
||||||
world_t * worldLoad(char *assetWorldDirectory) {
|
world_t * worldLoad() {
|
||||||
// Define our local variables
|
// Define our local variables
|
||||||
char file [128];
|
char file [128];
|
||||||
char *reading, *version, *textureFilename, *temp;
|
char *reading, *version, *textureFilename, *temp;
|
||||||
world_t *world;
|
world_t *world;
|
||||||
chunk_t *chunk;
|
chunk_t *chunk;
|
||||||
entity_t *entity;
|
|
||||||
tiledef_t *tileDef;
|
tiledef_t *tileDef;
|
||||||
|
texture_t *texture;
|
||||||
int32_t i, x, y, z;
|
int32_t i, x, y, z;
|
||||||
|
|
||||||
// Load the world file.
|
// Load the world file.
|
||||||
file[0] = '\0';
|
file[0] = '\0';
|
||||||
strcat(file, assetWorldDirectory);
|
strcat(file, "world/world.txt");
|
||||||
strcat(file, "world.txt");
|
|
||||||
char *worldData = assetStringLoad(file);
|
char *worldData = assetStringLoad(file);
|
||||||
if(worldData == NULL) return NULL;
|
if(worldData == NULL) return NULL;
|
||||||
|
|
||||||
// Create the world
|
// Create the world
|
||||||
world = malloc(sizeof(world_t));
|
world = malloc(sizeof(world_t));
|
||||||
world->assetWorldDirectory = assetWorldDirectory;
|
|
||||||
world->x = 0, world->y = 0, world->z = 0;
|
world->x = 0, world->y = 0, world->z = 0;
|
||||||
|
|
||||||
// Init the entities
|
|
||||||
for(i = 0; i < WORLD_ENTITY_COUNT; i++) {
|
|
||||||
entity = world->entities + i;
|
|
||||||
entity->id = (entityid_t)i;
|
|
||||||
entity->entityUpdate = NULL;
|
|
||||||
entity->entityDispose = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
||||||
@ -47,22 +37,15 @@ world_t * worldLoad(char *assetWorldDirectory) {
|
|||||||
|
|
||||||
// Load tileset texture.
|
// Load tileset texture.
|
||||||
file[0] = '\0';
|
file[0] = '\0';
|
||||||
strcat(file, assetWorldDirectory);
|
strcat(file, "world/");
|
||||||
strcat(file, textureFilename);
|
strcat(file, textureFilename);
|
||||||
world->texture = assetTextureLoad(file);
|
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
|
// Create the Tilemap
|
||||||
world->tilemap = tileMapCreate(world->tileset->columns, world->tileset->rows);
|
world->tilemap = tileMapCreate(texture);
|
||||||
|
|
||||||
// Load Tilemap Definitions. Skip
|
// Load Tilemap Definitions. Skip
|
||||||
for(i = 0; i < world->tileset->count; i++) {
|
for(i = 0; i < world->tilemap->tileset->count; i++) {
|
||||||
tileDef = world->tilemap->tileDefinitions + i;
|
tileDef = world->tilemap->tileDefinitions + i;
|
||||||
|
|
||||||
temp = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
|
temp = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
|
||||||
@ -72,7 +55,9 @@ world_t * worldLoad(char *assetWorldDirectory) {
|
|||||||
tileDef->indiceCount = atoi(temp);
|
tileDef->indiceCount = atoi(temp);
|
||||||
|
|
||||||
temp = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
|
temp = strtok_r(reading, WORLD_LOAD_TOKEN, &reading);
|
||||||
|
|
||||||
tileDef->flags = 0x00;
|
tileDef->flags = 0x00;
|
||||||
|
tileDef->tileRender = &tileQuadRender;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finished actual loading.
|
// Finished actual loading.
|
||||||
@ -93,46 +78,44 @@ world_t * worldLoad(char *assetWorldDirectory) {
|
|||||||
chunk->primitive = NULL;
|
chunk->primitive = NULL;
|
||||||
|
|
||||||
// Init the chunk.
|
// Init the chunk.
|
||||||
chunkLoad(world, chunk, x, y, z);
|
chunkLoad(chunk, world->tilemap, x, y, z);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare the entity system
|
||||||
|
entitySystemInit(&world->entities);
|
||||||
|
|
||||||
return world;
|
return world;
|
||||||
}
|
}
|
||||||
|
|
||||||
void worldRender(world_t *world, shader_t *shader, camera_t *camera) {
|
void worldRender(world_t *world,
|
||||||
|
shader_t *shader, camera_t *camera,
|
||||||
|
input_t *input
|
||||||
|
) {
|
||||||
int32_t i, j, tx, ty, tz;
|
int32_t i, j, tx, ty, tz;
|
||||||
float x, y, z;
|
float x, y, z;
|
||||||
chunk_t *chunk;
|
chunk_t *chunk;
|
||||||
tile_t *tile;
|
|
||||||
tiledef_t *tileDef;
|
|
||||||
entity_t *entity;
|
|
||||||
|
|
||||||
// Bind world texture
|
// Bind tilemap texture
|
||||||
shaderUseTexture(shader, world->texture);
|
shaderUseTexture(shader, world->tilemap->texture);
|
||||||
|
|
||||||
// Render each chunk.
|
// Render each chunk.
|
||||||
for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
|
for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
|
||||||
chunk = world->chunks + i;
|
chunk = world->chunks + i;
|
||||||
if(chunk->primitive == NULL) continue;
|
if(chunk->primitive == NULL) continue;
|
||||||
|
|
||||||
|
// Offset the primitive (into world space)
|
||||||
x = (chunk->x * CHUNK_WIDTH);
|
x = (chunk->x * CHUNK_WIDTH);
|
||||||
y = (chunk->y * CHUNK_HEIGHT);
|
y = (chunk->y * CHUNK_HEIGHT);
|
||||||
z = (chunk->z * CHUNK_DEPTH);
|
z = (chunk->z * CHUNK_DEPTH);
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tick entities
|
// Entity System tick
|
||||||
for(i = 0; i < WORLD_ENTITY_COUNT; i++) {
|
entitySystemUpdate(&world->entities, shader, camera, input);
|
||||||
entity = world->entities + i;
|
|
||||||
if(entity->entityUpdate == NULL) break;
|
|
||||||
|
|
||||||
entity->entityUpdate(world, entity, shader, camera);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void worldDispose(world_t *world) {
|
void worldDispose(world_t *world) {
|
||||||
@ -142,11 +125,10 @@ void worldDispose(world_t *world) {
|
|||||||
// Unload the chunks
|
// Unload the chunks
|
||||||
for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
|
for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
|
||||||
chunk = world->chunks + i;
|
chunk = world->chunks + i;
|
||||||
chunkUnload(world, chunk);
|
chunkUnload(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
textureDispose(world->texture);
|
textureDispose(world->tilemap->texture);
|
||||||
tilesetDispose(world->tileset);
|
|
||||||
tileMapDispose(world->tilemap);
|
tileMapDispose(world->tilemap);
|
||||||
free(world);
|
free(world);
|
||||||
}
|
}
|
||||||
@ -194,8 +176,8 @@ void worldShift(world_t *world, int32_t x, int32_t y, int32_t z) {
|
|||||||
az = lz + nz;
|
az = lz + nz;
|
||||||
|
|
||||||
// Load new chunk.
|
// Load new chunk.
|
||||||
chunkUnload(world, chunk);
|
chunkUnload(chunk);
|
||||||
chunkLoad(world, chunk, ax, ay, az);
|
chunkLoad(chunk, world->tilemap, ax, ay, az);
|
||||||
|
|
||||||
// Update the absolute coordinates.
|
// Update the absolute coordinates.
|
||||||
chunk->x = ax;
|
chunk->x = ax;
|
||||||
|
@ -6,27 +6,52 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "worldtypes.h"
|
|
||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
|
#include "entity/entity.h"
|
||||||
#include "../display/shader.h"
|
#include "../display/shader.h"
|
||||||
#include "../display/camera.h"
|
#include "../display/camera.h"
|
||||||
#include "../display/primitive.h"
|
#include "../display/primitive.h"
|
||||||
#include "../display/tileset.h"
|
#include "../display/tileset.h"
|
||||||
#include "../display/texture.h"
|
#include "../display/texture.h"
|
||||||
#include "../file/asset.h"
|
#include "../file/asset.h"
|
||||||
|
#include "../input/input.h"
|
||||||
#include "../util/string.h"
|
#include "../util/string.h"
|
||||||
#include "../util/math.h"
|
#include "../util/math.h"
|
||||||
|
|
||||||
/** Token in the world data file to split on. */
|
/** Token in the world data file to split on. */
|
||||||
#define WORLD_LOAD_TOKEN ";"
|
#define WORLD_LOAD_TOKEN ";"
|
||||||
|
/** 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 world_t {
|
||||||
|
// Chunks
|
||||||
|
/** 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];
|
||||||
|
|
||||||
|
entitysystem_t entities;
|
||||||
|
} world_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a world object.
|
* Create a world object.
|
||||||
*
|
*
|
||||||
* @returns The newly created world.
|
* @returns The newly created world.
|
||||||
*/
|
*/
|
||||||
world_t * worldLoad(char *fileName);
|
world_t * worldLoad();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a world object to the graphics device.
|
* Render a world object to the graphics device.
|
||||||
@ -34,7 +59,10 @@ world_t * worldLoad(char *fileName);
|
|||||||
* @param world The world to render.
|
* @param world The world to render.
|
||||||
* @param shader The shader to render to.
|
* @param shader The shader to render to.
|
||||||
*/
|
*/
|
||||||
void worldRender(world_t *world, shader_t *shader, camera_t *camera);
|
void worldRender(world_t *world,
|
||||||
|
shader_t *shader, camera_t *camera,
|
||||||
|
input_t *input
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleans up a previously created world.
|
* Cleans up a previously created world.
|
||||||
|
@ -1,150 +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 "../display/shader.h"
|
|
||||||
#include "../display/primitive.h"
|
|
||||||
#include "../display/camera.h"
|
|
||||||
#include "../display/texture.h"
|
|
||||||
#include "../display/tileset.h"
|
|
||||||
|
|
||||||
// Forwarders
|
|
||||||
typedef struct entity_t entity_t;
|
|
||||||
typedef struct world_t world_t;
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Entities
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
typedef uint8_t entityid_t;
|
|
||||||
|
|
||||||
/** Entity ID */
|
|
||||||
typedef struct entity_t {
|
|
||||||
entityid_t id;
|
|
||||||
|
|
||||||
/** Callback for frame events, or NULL */
|
|
||||||
void (*entityUpdate)(world_t *world, entity_t *entity, shader_t *shader,
|
|
||||||
camera_t *camera
|
|
||||||
);
|
|
||||||
|
|
||||||
/** Callback for cleanup events, or NULL */
|
|
||||||
void (*entityDispose)(world_t *world, entity_t *entity);
|
|
||||||
} entity_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 {
|
|
||||||
/** 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
|
|
||||||
|
|
||||||
#define WORLD_ENTITY_COUNT 32
|
|
||||||
|
|
||||||
/** Representation of the world. */
|
|
||||||
typedef struct world_t {
|
|
||||||
/** Asset subdir name */
|
|
||||||
char *assetWorldDirectory;
|
|
||||||
|
|
||||||
// Tiles
|
|
||||||
/** Tileset texture */
|
|
||||||
texture_t *texture;
|
|
||||||
/** Tileset predivided */
|
|
||||||
tileset_t *tileset;
|
|
||||||
/** Tilemap of the world */
|
|
||||||
tilemap_t *tilemap;
|
|
||||||
|
|
||||||
// Chunks
|
|
||||||
/** 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];
|
|
||||||
|
|
||||||
/** Entity Definitions */
|
|
||||||
entity_t entities[WORLD_ENTITY_COUNT];
|
|
||||||
entitypos_t entityPositions[WORLD_ENTITY_COUNT];
|
|
||||||
primitive_t *entityPrimitives[WORLD_ENTITY_COUNT];
|
|
||||||
} world_t;
|
|
Reference in New Issue
Block a user