diff --git a/src/engine/display/primitives/cube.c b/src/engine/display/primitives/cube.c new file mode 100644 index 00000000..9d568116 --- /dev/null +++ b/src/engine/display/primitives/cube.c @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "cube.h" + +primitive_t * cubeCreate(float w, float h, float d) { + primitive_t *cube = primitiveCreate(8, 36); + + vertice_t *vertices = malloc(sizeof(vertice_t) * cube->verticeCount); + indice_t *indices = malloc(sizeof(indice_t) * cube->indiceCount); + + float hw = w / 2, hh = h / 2, hd = d / 2; + vertices[0].x = -hw, vertices[0].y = -hh, vertices[0].z = -hd; + vertices[0].u = 0, vertices[0].v = 0; + + vertices[1].x = hw, vertices[1].y = -hh, vertices[1].z = -hd; + vertices[1].u = 1, vertices[1].v = 0; + + vertices[2].x = -hw, vertices[2].y = hh, vertices[2].z = -hd; + vertices[2].u = 0, vertices[2].v = 1; + + vertices[3].x = hw, vertices[3].y = hh, vertices[3].z = -hd; + vertices[3].u = 1, vertices[3].v = 1; + + vertices[4].x = -hw, vertices[4].y = -hh, vertices[4].z = hd; + vertices[4].u = 0, vertices[4].v = 0; + + vertices[5].x = hw, vertices[5].y = -hh, vertices[5].z = hd; + vertices[5].u = 1, vertices[5].v = 0; + + vertices[6].x = -hw, vertices[6].y = hh, vertices[6].z = hd; + vertices[6].u = 0, vertices[6].v = 1; + + vertices[7].x = hw, vertices[7].y = hh, vertices[7].z = hd; + vertices[7].u = 1, vertices[7].v = 1; + + // Back + indices[ 0] = 0, indices[ 1] = 1, indices[ 2] = 3; + indices[ 3] = 0, indices[ 4] = 2, indices[ 5] = 3; + + // Right + indices[ 6] = 1, indices[ 7] = 5, indices[ 8] = 7; + indices[ 9] = 1, indices[10] = 3, indices[11] = 7; + + // Left + indices[12] = 4, indices[13] = 0, indices[14] = 2; + indices[15] = 4, indices[16] = 6, indices[17] = 2; + + // Front + indices[18] = 5, indices[19] = 4, indices[20] = 6; + indices[21] = 5, indices[22] = 7, indices[23] = 6; + + // Top + indices[24] = 7, indices[25] = 2, indices[26] = 6; + indices[27] = 7, indices[28] = 3, indices[29] = 2; + + // Bottom + indices[30] = 1, indices[31] = 0, indices[32] = 4; + indices[33] = 1, indices[34] = 4, indices[35] = 5; + + + primitiveBufferVertices(cube, 0, cube->verticeCount, vertices); + primitiveBufferIndices(cube, 0, cube->indiceCount, indices); + free(vertices); + free(indices); + + return cube; +} \ No newline at end of file diff --git a/src/engine/display/primitives/cube.h b/src/engine/display/primitives/cube.h new file mode 100644 index 00000000..78080b58 --- /dev/null +++ b/src/engine/display/primitives/cube.h @@ -0,0 +1,11 @@ +// Copyright (c) 2021 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include +#include +#include "../primitive.h" + +primitive_t * cubeCreate(float w, float h, float d); \ No newline at end of file diff --git a/src/engine/display/primitives/quad.c b/src/engine/display/primitives/quad.c index 331b3213..8eae6227 100644 --- a/src/engine/display/primitives/quad.c +++ b/src/engine/display/primitives/quad.c @@ -27,13 +27,13 @@ void quadBuffer(primitive_t *primitive, vertices[3].x = x1, vertices[3].y = y1, vertices[3].z = 0; vertices[3].u = u1, vertices[3].v = v1; - indices[0] = (indice_t)verticeStart + 0; - indices[1] = (indice_t)verticeStart + 1; - indices[2] = (indice_t)verticeStart + 2; + indices[0] = (indice_t)(verticeStart + 0); + indices[1] = (indice_t)(verticeStart + 1); + indices[2] = (indice_t)(verticeStart + 2); - indices[3] = (indice_t)verticeStart + 1; - indices[4] = (indice_t)verticeStart + 2; - indices[5] = (indice_t)verticeStart + 3; + indices[3] = (indice_t)(verticeStart + 1); + indices[4] = (indice_t)(verticeStart + 2); + indices[5] = (indice_t)(verticeStart + 3); primitiveBufferVertices(primitive, verticeStart, 4, vertices); primitiveBufferIndices(primitive, indiceStart, 6, indices); diff --git a/src/engine/engine.c b/src/engine/engine.c index 9f2945b8..2540b6db 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -7,16 +7,9 @@ #include "engine.h" -#include "display/primitives/quad.h" -#include "display/texture.h" -#include "display/shader.h" -#include "world/chunklist.h" -#include "file/asset.h" - camera_t *camera; shader_t *shader; -primitive_t *primitive; -texture_t *texture; +primitive_t *cube; engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { // Create the engine instance. @@ -42,23 +35,25 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { } shader = assetShaderLoad("shaders/test.vert", "shaders/test.frag"); - camera = cameraCreate(); cameraLookAt(camera, 3, 3, 3, 0, 0, 0 ); - cameraPerspective(camera, 45.0f, 1920.0f/1080.0f, 3.0f, 100.0f); + cameraPerspective(camera, 45.0f, 1920.0f/1080.0f, 0.5f, 100.0f); shaderUseCamera(shader, camera); + // cube = quadCreate(0, 0, 0, 0, 1, 1, 1, 1); + cube = cubeCreate(1, 1, 1); + return engine; } uint32_t engineUpdate(engine_t *engine) { shaderUse(shader); renderFrame(engine->render); - - primitiveDraw(primitive, 0, 6); + + primitiveDraw(cube, 0, cube->indiceCount); inputUpdate(engine->input); return 0; diff --git a/src/engine/engine.h b/src/engine/engine.h index 285a358e..6dd39b2e 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -12,6 +12,11 @@ #include "file/asset.h" #include "display/shader.h" #include "display/camera.h" +#include "world/world.h" + +#include "display/primitives/cube.h" +#include "display/primitives/quad.h" + /** Information about the current engine context. */ typedef struct { diff --git a/src/engine/world/chunk.c b/src/engine/world/chunk.c index 924c29a4..34f99609 100644 --- a/src/engine/world/chunk.c +++ b/src/engine/world/chunk.c @@ -6,10 +6,28 @@ */ #include "chunk.h" -#include +void chunkCreate(chunk_t *chunk) { + int32_t count, x, y, z, i; + + count = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH; + chunk->primitive = primitiveCreate(count * 4, count * 6); + + i = 0; + for(y = 0; y < CHUNK_HEIGHT; y++) { + for(x = 0; x < CHUNK_WIDTH; x++) { + quadBuffer(chunk->primitive, + x, y, 0, 0, + x+1, y+1, 1, 1, + i*4, i*6 + ); + i++; + } + } +} void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) { + } void chunkUnload(chunk_t *chunk) { diff --git a/src/engine/world/chunk.h b/src/engine/world/chunk.h index f97a4bfb..ca324461 100644 --- a/src/engine/world/chunk.h +++ b/src/engine/world/chunk.h @@ -5,11 +5,29 @@ #pragma once #include +#include "../display/primitive.h" +#include "../display/primitives/quad.h" + +#define CHUNK_WIDTH 32 +#define CHUNK_HEIGHT CHUNK_WIDTH +#define CHUNK_DEPTH CHUNK_HEIGHT + +typedef uint8_t tile_t; typedef struct { int32_t x, y, z; + tile_t *tiles; + primitive_t *primitive; } chunk_t; + +/** + * Initializes a chunk for the first time. + * + * @param chunk Pointer to the chunk to initialize. + */ +void chunkCreate(chunk_t *chunk); + /** * Loads a given chunk into the memory specified. * diff --git a/src/engine/world/chunklist.c b/src/engine/world/chunklist.c index 53e1919e..ec7c26eb 100644 --- a/src/engine/world/chunklist.c +++ b/src/engine/world/chunklist.c @@ -9,6 +9,7 @@ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) { chunklist_t *list; + chunk_t *chunk; int32_t i, x, y, z; list = malloc(sizeof(chunklist_t)); @@ -32,12 +33,12 @@ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) { return NULL; } - // Load initial chunks, ZYX order is necessary. + // Load initial chunks, ZYX order is important. i = 0; for(z = 0; z < width; z++) { for(y = 0; y < height; y++) { for(x = 0; x < depth; x++) { - chunk_t *chunk = list->chunks + i; + chunk = list->chunks + i; list->chunkList[i] = chunk; // Load initial coordinates @@ -45,8 +46,8 @@ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) { chunk->y = y; chunk->z = z; + chunkCreate(chunk); chunkLoad(chunk, x, y, z); - i++; } } diff --git a/src/engine/world/chunklist.h b/src/engine/world/chunklist.h index 4aaeb327..9ac0c586 100644 --- a/src/engine/world/chunklist.h +++ b/src/engine/world/chunklist.h @@ -33,7 +33,7 @@ typedef struct { * @param width The width (x axis) of chunks to keep loaded. * @param height The height (y axis) of chunks to keep loaded. * @param depth The depth (z axis) of chunks to keep loaded. - * @returns A new chunk list. + * @return A new chunk list. */ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth); diff --git a/src/engine/world/world.c b/src/engine/world/world.c index ecfe63be..c155dca8 100644 --- a/src/engine/world/world.c +++ b/src/engine/world/world.c @@ -9,6 +9,13 @@ world_t * worldCreate() { world_t *world = malloc(sizeof(world_t)); + if(world == NULL) return NULL; + + world->chunkList = chunkListCreate(3, 3, 1); + if(world->chunkList == NULL) { + free(world); + return NULL; + } return world; } diff --git a/src/engine/world/world.h b/src/engine/world/world.h index 2d27176c..a39572ad 100644 --- a/src/engine/world/world.h +++ b/src/engine/world/world.h @@ -7,8 +7,9 @@ #include "chunklist.h" typedef struct { - uint32_t x; + chunklist_t *chunkList; } world_t; world_t * worldCreate(); + void worldDispose(world_t *world); \ No newline at end of file