Reshuffled
This commit is contained in:
		@@ -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
 | 
			
		||||
@@ -20,11 +20,10 @@ primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) {
 | 
			
		||||
  free(buffer);
 | 
			
		||||
  
 | 
			
		||||
  //Buffer an empty set of data then buffer each component
 | 
			
		||||
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
 | 
			
		||||
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeIndices, 0, GL_DYNAMIC_DRAW);
 | 
			
		||||
 | 
			
		||||
  glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
 | 
			
		||||
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
 | 
			
		||||
  glBufferData(GL_ARRAY_BUFFER, sizePositions+sizeCoordinates, 0, GL_DYNAMIC_DRAW);
 | 
			
		||||
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeIndices, 0, GL_DYNAMIC_DRAW);
 | 
			
		||||
 | 
			
		||||
  size_t offset = 0;
 | 
			
		||||
 | 
			
		||||
@@ -32,13 +31,11 @@ primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) {
 | 
			
		||||
  glVertexAttribPointer(0, PRIMITIVE_POSITIONS_PER_VERTICE, GL_FLOAT,
 | 
			
		||||
    GL_FALSE, 0, (void *)offset
 | 
			
		||||
  );
 | 
			
		||||
  glEnableVertexAttribArray(0);
 | 
			
		||||
  offset += sizePositions;
 | 
			
		||||
 | 
			
		||||
  glVertexAttribPointer(1, PRIMITIVE_COORDINATES_PER_VERTICE, GL_FLOAT,
 | 
			
		||||
    GL_FALSE, 0, (void *)offset
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  glEnableVertexAttribArray(0);
 | 
			
		||||
  glEnableVertexAttribArray(1);
 | 
			
		||||
 | 
			
		||||
  return primitive;
 | 
			
		||||
@@ -98,8 +95,23 @@ void primitiveBufferIndices(primitive_t *primitive,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void primitiveDraw(primitive_t *primitive, int32_t start, int32_t count) {
 | 
			
		||||
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
 | 
			
		||||
  // Re-Bind the buffers
 | 
			
		||||
  glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
 | 
			
		||||
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
 | 
			
		||||
 | 
			
		||||
  // Re-Calculate the attrib pointers.
 | 
			
		||||
  size_t offset = 0;
 | 
			
		||||
  glVertexAttribPointer(0, PRIMITIVE_POSITIONS_PER_VERTICE, GL_FLOAT,
 | 
			
		||||
    GL_FALSE, 0, (void *)offset
 | 
			
		||||
  );
 | 
			
		||||
  glEnableVertexAttribArray(0);
 | 
			
		||||
  offset += sizeof(float) * primitive->verticeCount * PRIMITIVE_POSITIONS_PER_VERTICE;
 | 
			
		||||
  glVertexAttribPointer(1, PRIMITIVE_COORDINATES_PER_VERTICE, GL_FLOAT,
 | 
			
		||||
    GL_FALSE, 0, (void *)offset
 | 
			
		||||
  );
 | 
			
		||||
  glEnableVertexAttribArray(1);
 | 
			
		||||
 | 
			
		||||
  // Render the elements.
 | 
			
		||||
  glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void *)(
 | 
			
		||||
    sizeof(indice_t)*start
 | 
			
		||||
  ));
 | 
			
		||||
@@ -1,47 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright (c) 2021 Dominic Msters
 | 
			
		||||
 * 
 | 
			
		||||
 * This software is released under the MIT License.
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include "../engine.h"
 | 
			
		||||
#include "../platform.h"
 | 
			
		||||
 | 
			
		||||
/** Information about the current game context. */
 | 
			
		||||
#ifndef GAMETYPE_T
 | 
			
		||||
  #define GAMETYPE_T void
 | 
			
		||||
#endif
 | 
			
		||||
typedef GAMETYPE_T game_t;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Initialize the game context.
 | 
			
		||||
 * 
 | 
			
		||||
 * @return The game instance context.
 | 
			
		||||
 */
 | 
			
		||||
game_t * gameInit(platform_t *platform);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Start the main game loop.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param game The game to start the loop for.
 | 
			
		||||
 * @return Refer to engineUpdate. 0 for loop continue, 1 for safe exit.
 | 
			
		||||
 */
 | 
			
		||||
uint32_t gameUpdate(game_t *game);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Cleanup a previously constructed.
 | 
			
		||||
 * @param game The game to cleanup.
 | 
			
		||||
 */
 | 
			
		||||
void gameDispose(game_t *game);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Because games are anonymously typed we simply request that they allow other
 | 
			
		||||
 * parts of the software to access the engine directly.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param game The game context
 | 
			
		||||
 * @return The engine context attached to the running game.
 | 
			
		||||
 */
 | 
			
		||||
engine_t * gameGetEngine(game_t *game);
 | 
			
		||||
@@ -11,7 +11,6 @@
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <malloc.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#include "../display/shader.h"
 | 
			
		||||
#include "../display/texture.h"
 | 
			
		||||
 | 
			
		||||
@@ -5,7 +5,7 @@
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "dawngame.h"
 | 
			
		||||
#include "game.h"
 | 
			
		||||
 | 
			
		||||
game_t * gameInit(platform_t *platform) {
 | 
			
		||||
  // Create the game
 | 
			
		||||
@@ -45,7 +45,7 @@ uint32_t gameUpdate(game_t *game) {
 | 
			
		||||
  // Prepare for rendering
 | 
			
		||||
  shaderUse(game->shader);
 | 
			
		||||
  shaderUseCamera(game->shader, game->camera);
 | 
			
		||||
  worldRender(game->world, game->shader);
 | 
			
		||||
  worldRender(game->world, game->shader, game->camera);
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										59
									
								
								src/game/game.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								src/game/game.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright (c) 2021 Dominic Msters
 | 
			
		||||
 * 
 | 
			
		||||
 * This software is released under the MIT License.
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include "../engine.h"
 | 
			
		||||
#include "../platform.h"
 | 
			
		||||
#include "../display/camera.h"
 | 
			
		||||
#include "../display/shader.h"
 | 
			
		||||
#include "../world/world.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
 | 
			
		||||
 | 
			
		||||
/////////////////////////////// TYPE DEFINITIONS ///////////////////////////////
 | 
			
		||||
/** Information about the current game context. */
 | 
			
		||||
typedef struct {
 | 
			
		||||
  /** The engine context for the game */
 | 
			
		||||
  engine_t *engine;
 | 
			
		||||
 | 
			
		||||
  /** Rendering items */
 | 
			
		||||
  camera_t *camera;
 | 
			
		||||
  shader_t *shader;
 | 
			
		||||
  world_t *world;
 | 
			
		||||
} game_t;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Initialize the game context.
 | 
			
		||||
 * 
 | 
			
		||||
 * @return The game instance context.
 | 
			
		||||
 */
 | 
			
		||||
game_t * gameInit(platform_t *platform);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Start the main game loop.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param game The game to start the loop for.
 | 
			
		||||
 * @return Refer to engineUpdate. 0 for loop continue, 1 for safe exit.
 | 
			
		||||
 */
 | 
			
		||||
uint32_t gameUpdate(game_t *game);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Cleanup a previously constructed.
 | 
			
		||||
 * @param game The game to cleanup.
 | 
			
		||||
 */
 | 
			
		||||
void gameDispose(game_t *game);
 | 
			
		||||
@@ -107,7 +107,7 @@ void inputBind(input_t *input, inputbind_t bind, inputsource_t source);
 | 
			
		||||
void inputUnbind(input_t *input, inputbind_t bind, inputsource_t source);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Is the current input "down", not being pressed, being moved, not in a state
 | 
			
		||||
 * Is the current input "down", being pressed, being moved, not in a state
 | 
			
		||||
 * of rest.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param state The input state to check against.
 | 
			
		||||
@@ -42,11 +42,12 @@ int32_t main() {
 | 
			
		||||
  if(runningGame == NULL) return 1;
 | 
			
		||||
 | 
			
		||||
  // Update the window title.
 | 
			
		||||
  engine_t *engine = gameGetEngine(runningGame);
 | 
			
		||||
  glfwSetWindowTitle(window, engine->name);
 | 
			
		||||
  glfwSetWindowTitle(window, runningGame->engine->name);
 | 
			
		||||
 | 
			
		||||
  // Bind inputs
 | 
			
		||||
  inputBind(engine->input, INPUT_NULL, (inputsource_t *)GLFW_KEY_ESCAPE);
 | 
			
		||||
  inputBind(runningGame->engine->input, INPUT_NULL,
 | 
			
		||||
    (inputsource_t *)GLFW_KEY_ESCAPE
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  // Main Render Loop
 | 
			
		||||
  while(!glfwWindowShouldClose(window)) {
 | 
			
		||||
@@ -67,10 +68,9 @@ int32_t main() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) {
 | 
			
		||||
  engine_t *engine = gameGetEngine(runningGame);
 | 
			
		||||
  engine->platform->screenWidth = width;
 | 
			
		||||
  engine->platform->screenWidth = height;
 | 
			
		||||
  renderSetResolution(engine->render, width, height);
 | 
			
		||||
  runningGame->engine->platform->screenWidth = width;
 | 
			
		||||
  runningGame->engine->platform->screenWidth = height;
 | 
			
		||||
  renderSetResolution(runningGame->engine->render, width, height);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void glfwOnKey(GLFWwindow *window,
 | 
			
		||||
@@ -78,10 +78,9 @@ void glfwOnKey(GLFWwindow *window,
 | 
			
		||||
) {
 | 
			
		||||
  if(runningGame == NULL) return;
 | 
			
		||||
 | 
			
		||||
  engine_t *engine = gameGetEngine(runningGame);
 | 
			
		||||
  if(action == GLFW_PRESS) {
 | 
			
		||||
    engine->input->buffer[key] = 1;
 | 
			
		||||
    runningGame->engine->input->buffer[key] = 1;
 | 
			
		||||
  } else if(action == GLFW_RELEASE) {
 | 
			
		||||
    engine->input->buffer[key] = 0;
 | 
			
		||||
    runningGame->engine->input->buffer[key] = 0;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -5,16 +5,17 @@
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#define PLATFORMINPUT_T uint32_t
 | 
			
		||||
 | 
			
		||||
// I load GLAD and GLFW Here because they need to be included in specific orders
 | 
			
		||||
#include <glad/glad.h>
 | 
			
		||||
#include <GLFW/glfw3.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include "../../engine/platform.h"
 | 
			
		||||
#include "../../engine/game/game.h"
 | 
			
		||||
#include "../../engine/input/input.h"
 | 
			
		||||
#include "../../engine/display/render.h"
 | 
			
		||||
 | 
			
		||||
#define PLATFORMINPUT_T uint32_t
 | 
			
		||||
 | 
			
		||||
#include "../../platform.h"
 | 
			
		||||
#include "../../game/game.h"
 | 
			
		||||
#include "../../input/input.h"
 | 
			
		||||
#include "../../display/render.h"
 | 
			
		||||
 | 
			
		||||
#define WINDOW_WIDTH_DEFAULT 480
 | 
			
		||||
#define WINDOW_HEIGHT_DEFAULT 270
 | 
			
		||||
@@ -29,7 +30,6 @@ extern game_t *runningGame;
 | 
			
		||||
 */
 | 
			
		||||
int32_t main();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Resize callbacks.
 | 
			
		||||
 * 
 | 
			
		||||
@@ -39,7 +39,6 @@ int32_t main();
 | 
			
		||||
 */
 | 
			
		||||
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Keyboard Input callbacks.
 | 
			
		||||
 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -2,8 +2,8 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include "worldtypes.h"
 | 
			
		||||
#include "tile.h"
 | 
			
		||||
#include "../../engine/file/asset.h"
 | 
			
		||||
#include "../../engine/util/string.h"
 | 
			
		||||
#include "../file/asset.h"
 | 
			
		||||
#include "../util/string.h"
 | 
			
		||||
 | 
			
		||||
/** When loading a chunk, how many chars to offset (ASCII char to byte) */
 | 
			
		||||
#define CHUNK_TILE_LOAD_ASCII 48
 | 
			
		||||
							
								
								
									
										35
									
								
								src/world/entity/player.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/world/entity/player.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright (c) 2021 Dominic Masters
 | 
			
		||||
 * 
 | 
			
		||||
 * This software is released under the MIT License.
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "player.h"
 | 
			
		||||
 | 
			
		||||
void playerCreate(world_t *world, entity_t *entity) {
 | 
			
		||||
  // Setup the update state.
 | 
			
		||||
  entity->entityUpdate = &playerUpdate;
 | 
			
		||||
  entity->entityDispose = &playerDispose;
 | 
			
		||||
 | 
			
		||||
  world->entityPrimitives[entity->id] = quadCreate(0,
 | 
			
		||||
    0, 0, 0, 0,
 | 
			
		||||
    1, 1, 1, 1
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void playerUpdate(world_t *world, entity_t *entity, shader_t *shader,
 | 
			
		||||
  camera_t *camera
 | 
			
		||||
) {
 | 
			
		||||
  shaderUsePosition(shader, 0, 0, 0, 0, 0, 0);
 | 
			
		||||
  primitiveDraw(world->entityPrimitives[entity->id],
 | 
			
		||||
    0, world->entityPrimitives[entity->id]->indiceCount
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void playerDispose(world_t *world, entity_t *entity) {
 | 
			
		||||
  primitiveDispose(world->entityPrimitives[entity->id]);
 | 
			
		||||
 | 
			
		||||
  entity->entityDispose = NULL;
 | 
			
		||||
  entity->entityUpdate = NULL;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								src/world/entity/player.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/world/entity/player.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright (c) 2021 Dominic Masters
 | 
			
		||||
 * 
 | 
			
		||||
 * This software is released under the MIT License.
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "../worldtypes.h"
 | 
			
		||||
#include "../../display/primitives/quad.h"
 | 
			
		||||
#include "../../input/input.h"
 | 
			
		||||
 | 
			
		||||
void playerCreate(world_t *world, entity_t *entity);
 | 
			
		||||
void playerUpdate(world_t *world, entity_t *entity, shader_t *shader,
 | 
			
		||||
  camera_t *camera
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
void playerDispose(world_t *world, entity_t *entity);
 | 
			
		||||
@@ -7,9 +7,9 @@
 | 
			
		||||
#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"
 | 
			
		||||
#include "../display/tileset.h"
 | 
			
		||||
#include "../display/primitives/quad.h"
 | 
			
		||||
#include "../display/primitives/cube.h"
 | 
			
		||||
 | 
			
		||||
/** The tile id that represents a NULL tile */
 | 
			
		||||
#define TILE_NULL (tileid_t)0
 | 
			
		||||
@@ -13,6 +13,7 @@ world_t * worldLoad(char *assetWorldDirectory) {
 | 
			
		||||
  char *reading, *version, *textureFilename, *temp;
 | 
			
		||||
  world_t *world;
 | 
			
		||||
  chunk_t *chunk;
 | 
			
		||||
  entity_t *entity;
 | 
			
		||||
  tiledef_t *tileDef;
 | 
			
		||||
  int32_t i, x, y, z;
 | 
			
		||||
 | 
			
		||||
@@ -28,6 +29,14 @@ world_t * worldLoad(char *assetWorldDirectory) {
 | 
			
		||||
  world->assetWorldDirectory = assetWorldDirectory;
 | 
			
		||||
  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
 | 
			
		||||
  // we are using.
 | 
			
		||||
  reading = worldData;
 | 
			
		||||
@@ -93,15 +102,18 @@ world_t * worldLoad(char *assetWorldDirectory) {
 | 
			
		||||
  return world;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void worldRender(world_t *world, shader_t *shader) {
 | 
			
		||||
void worldRender(world_t *world, shader_t *shader, camera_t *camera) {
 | 
			
		||||
  int32_t i, j, tx, ty, tz;
 | 
			
		||||
  float x, y, z;
 | 
			
		||||
  chunk_t *chunk;
 | 
			
		||||
  tile_t *tile;
 | 
			
		||||
  tiledef_t *tileDef;
 | 
			
		||||
  entity_t *entity;
 | 
			
		||||
 | 
			
		||||
  // Bind world texture
 | 
			
		||||
  shaderUseTexture(shader, world->texture);
 | 
			
		||||
 | 
			
		||||
  // Render each chunk.
 | 
			
		||||
  for(i = 0; i < WORLD_CHUNK_COUNT; i++) {
 | 
			
		||||
    chunk = world->chunks + i;
 | 
			
		||||
    if(chunk->primitive == NULL) continue;
 | 
			
		||||
@@ -113,6 +125,14 @@ void worldRender(world_t *world, shader_t *shader) {
 | 
			
		||||
    shaderUsePosition(shader, x, y, z, 0, 0, 0);
 | 
			
		||||
    primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Tick entities
 | 
			
		||||
  for(i = 0; i < WORLD_ENTITY_COUNT; i++) {
 | 
			
		||||
    entity = world->entities + i;
 | 
			
		||||
    if(entity->entityUpdate == NULL) break;
 | 
			
		||||
 | 
			
		||||
    entity->entityUpdate(world, entity, shader, camera);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void worldDispose(world_t *world) {
 | 
			
		||||
@@ -9,13 +9,14 @@
 | 
			
		||||
#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"
 | 
			
		||||
#include "../display/shader.h"
 | 
			
		||||
#include "../display/camera.h"
 | 
			
		||||
#include "../display/primitive.h"
 | 
			
		||||
#include "../display/tileset.h"
 | 
			
		||||
#include "../display/texture.h"
 | 
			
		||||
#include "../file/asset.h"
 | 
			
		||||
#include "../util/string.h"
 | 
			
		||||
#include "../util/math.h"
 | 
			
		||||
 | 
			
		||||
/** Token in the world data file to split on. */
 | 
			
		||||
#define WORLD_LOAD_TOKEN ";"
 | 
			
		||||
@@ -33,7 +34,7 @@ world_t * worldLoad(char *fileName);
 | 
			
		||||
 * @param world The world to render.
 | 
			
		||||
 * @param shader The shader to render to.
 | 
			
		||||
 */
 | 
			
		||||
void worldRender(world_t *world, shader_t *shader);
 | 
			
		||||
void worldRender(world_t *world, shader_t *shader, camera_t *camera);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Cleans up a previously created world.
 | 
			
		||||
@@ -8,17 +8,35 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <malloc.h>
 | 
			
		||||
#include "../../engine/display/primitive.h"
 | 
			
		||||
#include "../../engine/display/texture.h"
 | 
			
		||||
#include "../../engine/display/tileset.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
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
/** Entity ID */
 | 
			
		||||
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;
 | 
			
		||||
@@ -43,7 +61,6 @@ typedef uint8_t tileid_t;
 | 
			
		||||
typedef struct {
 | 
			
		||||
  /** ID of the tile */
 | 
			
		||||
  tileid_t id;
 | 
			
		||||
 | 
			
		||||
  /** Rendering indice and vertice offsets for the tile. */
 | 
			
		||||
  int32_t indiceStart, verticeStart;
 | 
			
		||||
} tile_t;
 | 
			
		||||
@@ -80,13 +97,11 @@ typedef struct {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/** Representation of a chunk, a group of tiles that can be buffered around. */
 | 
			
		||||
typedef struct chunk_t {
 | 
			
		||||
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;
 | 
			
		||||
@@ -105,11 +120,14 @@ typedef struct chunk_t {
 | 
			
		||||
/** 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 {
 | 
			
		||||
typedef struct world_t {
 | 
			
		||||
  /** Asset subdir name */
 | 
			
		||||
  char *assetWorldDirectory;
 | 
			
		||||
 | 
			
		||||
  // Tiles
 | 
			
		||||
  /** Tileset texture */
 | 
			
		||||
  texture_t *texture;
 | 
			
		||||
  /** Tileset predivided */
 | 
			
		||||
@@ -117,10 +135,16 @@ typedef struct {
 | 
			
		||||
  /** 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