Refactored.
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
@ -7,61 +7,52 @@
|
||||
|
||||
#include "game.h"
|
||||
|
||||
game_t * gameInit(platform_t *platform) {
|
||||
// Create the game
|
||||
game_t *game = malloc(sizeof(game_t));
|
||||
if(game == NULL) return NULL;
|
||||
game_t GAME_STATE;
|
||||
|
||||
// Load the game engine
|
||||
game->engine = engineInit(platform, GAME_NAME, GAME_INPUT_COUNT);
|
||||
if(game->engine == NULL) {
|
||||
free(game);
|
||||
return NULL;
|
||||
}
|
||||
bool gameInit() {
|
||||
// Init the game
|
||||
GAME_STATE.name = GAME_NAME;
|
||||
|
||||
// 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);
|
||||
// Init the renderer.
|
||||
renderInit();
|
||||
inputInit();
|
||||
worldInit();
|
||||
|
||||
// Load the world
|
||||
game->world = worldLoad("testworld/");
|
||||
playerCreate(&game->world->entities, 0);
|
||||
|
||||
// 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, game->camera,
|
||||
game->engine->input
|
||||
// Load the world shader.
|
||||
GAME_STATE.shaderWorld = assetShaderLoad(
|
||||
"shaders/test.vert", "shaders/test.frag"
|
||||
);
|
||||
|
||||
return 0;
|
||||
// Init the input manger.
|
||||
return true;
|
||||
}
|
||||
|
||||
void gameDispose(game_t *game) {
|
||||
worldDispose(game->world);
|
||||
shaderDipose(game->shader);
|
||||
cameraDispose(game->camera);
|
||||
engineDispose(game->engine);
|
||||
free(game);
|
||||
bool gameUpdate() {
|
||||
renderFrameStart();
|
||||
inputUpdate();
|
||||
|
||||
// Attach the world shader
|
||||
shaderUse(GAME_STATE.shaderWorld);
|
||||
|
||||
// Set up the camera.
|
||||
int32_t d = 50;
|
||||
cameraLookAt(&GAME_STATE.cameraWorld, d, d, d, 0, 0, 0);
|
||||
cameraPerspective(&GAME_STATE.cameraWorld, 45.0f,
|
||||
((float)RENDER_STATE.width) / ((float)RENDER_STATE.height),
|
||||
0.5f, 500.0f
|
||||
);
|
||||
shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraWorld);
|
||||
|
||||
// Render the game scene.
|
||||
worldRender();
|
||||
|
||||
if(inputIsPressed(INPUT_NULL)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
engine_t * gameGetEngine(game_t *game) {
|
||||
return game->engine;
|
||||
void gameDispose() {
|
||||
shaderDispose(GAME_STATE.shaderWorld);
|
||||
worldDispose();
|
||||
inputDispose();
|
||||
renderDispose();
|
||||
}
|
@ -1,62 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// 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 <dawn/dawn.h>
|
||||
#include "../display/render.h"
|
||||
#include "../display/camera.h"
|
||||
#include "../display/shader.h"
|
||||
#include "../file/asset.h"
|
||||
#include "../input/input.h"
|
||||
#include "../world/world.h"
|
||||
|
||||
#include "../world/entity/player.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 game_t {
|
||||
/** 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.
|
||||
* @return True if successful, otherwise false.
|
||||
*/
|
||||
game_t * gameInit(platform_t *platform);
|
||||
bool gameInit();
|
||||
|
||||
/**
|
||||
* Start the main game loop.
|
||||
* Tick 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.
|
||||
* @return True if successful, false if safe exit requested..
|
||||
*/
|
||||
uint32_t gameUpdate(game_t *game);
|
||||
bool gameUpdate();
|
||||
|
||||
/**
|
||||
* Cleanup a previously constructed.
|
||||
* @param game The game to cleanup.
|
||||
* Cleanup the game instance.
|
||||
*/
|
||||
void gameDispose(game_t *game);
|
||||
void gameDispose();
|
Reference in New Issue
Block a user