Added some time functions.
This commit is contained in:
@ -7,7 +7,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define GAMETIME_STEP 0.016
|
||||
#define GAMETIME_FIXED_STEP 0.016
|
||||
#define GAMETIME_SMALLEST_STEP 0.001
|
||||
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
|
@ -14,8 +14,12 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
// Windows Fixes
|
||||
# define strtok_r strtok_s
|
||||
# define sleep(n) _sleep(n)
|
||||
#else
|
||||
|
||||
#endif
|
@ -30,8 +30,8 @@ bool gameInit() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gameUpdate() {
|
||||
gameTimeUpdate();
|
||||
bool gameUpdate(float platformDelta) {
|
||||
gameTimeUpdate(platformDelta);
|
||||
renderFrameStart();
|
||||
inputUpdate();
|
||||
|
||||
|
@ -24,9 +24,10 @@ bool gameInit();
|
||||
/**
|
||||
* Tick the main game loop.
|
||||
*
|
||||
* @param platformDelta The platform tick delta between the last render.
|
||||
* @return True if successful, false if safe exit requested..
|
||||
*/
|
||||
bool gameUpdate();
|
||||
bool gameUpdate(float platformDelta);
|
||||
|
||||
/**
|
||||
* Cleanup the game instance.
|
||||
|
@ -10,13 +10,18 @@
|
||||
gametime_t TIME_STATE;
|
||||
|
||||
void gameTimeInit() {
|
||||
TIME_STATE.delta = GAMETIME_STEP;
|
||||
TIME_STATE.last = GAMETIME_STEP;
|
||||
TIME_STATE.current = GAMETIME_STEP + GAMETIME_STEP;
|
||||
TIME_STATE.delta = GAMETIME_FIXED_STEP;
|
||||
TIME_STATE.last = GAMETIME_FIXED_STEP;
|
||||
TIME_STATE.current = GAMETIME_FIXED_STEP + GAMETIME_FIXED_STEP;
|
||||
}
|
||||
|
||||
void gameTimeUpdate() {
|
||||
void gameTimeUpdate(float platformDelta) {
|
||||
platformDelta = mathMax(
|
||||
mathMin(platformDelta, GAMETIME_FIXED_STEP),
|
||||
0
|
||||
);
|
||||
|
||||
TIME_STATE.last = TIME_STATE.current;
|
||||
TIME_STATE.current = TIME_STATE.current + GAMETIME_STEP;
|
||||
TIME_STATE.current = TIME_STATE.current + platformDelta;
|
||||
TIME_STATE.delta = TIME_STATE.current - TIME_STATE.last;
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "../util/math.h"
|
||||
|
||||
/**
|
||||
* Initializes the gametime global time tracking.
|
||||
@ -15,5 +16,7 @@ void gameTimeInit();
|
||||
|
||||
/**
|
||||
* Ticks the current game time.
|
||||
*
|
||||
* @param platformDelta The delta step between frames from the platform engine.
|
||||
*/
|
||||
void gameTimeUpdate();
|
||||
void gameTimeUpdate(float platformDelta);
|
@ -21,6 +21,7 @@ int32_t main() {
|
||||
|
||||
// Load GLAD
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSwapInterval(0);
|
||||
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
|
||||
// Setup window listeners
|
||||
@ -46,12 +47,21 @@ int32_t main() {
|
||||
// Update the window title.
|
||||
glfwSetWindowTitle(window, GAME_STATE.name);
|
||||
|
||||
// Main Render Loop
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
if(!gameUpdate()) break;
|
||||
double time = 0;
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
// Main Render Loop
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
glfwPollEvents();
|
||||
|
||||
// Determine the delta.
|
||||
double newTime = glfwGetTime();
|
||||
float fDelta = (float)(newTime - time);
|
||||
time = newTime;
|
||||
|
||||
// Tick the engine.
|
||||
if(!gameUpdate(fDelta)) break;
|
||||
glfwSwapBuffers(window);
|
||||
sleep(0);
|
||||
}
|
||||
|
||||
// Game has finished running, cleanup.
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <dawn/dawn.h>
|
||||
#include "../../display/render.h"
|
||||
#include "../../game/game.h"
|
||||
#include "../../game/gametime.h"
|
||||
#include "../../input/input.h"
|
||||
|
||||
#define WINDOW_WIDTH_DEFAULT 480
|
||||
|
@ -42,7 +42,7 @@ void entityCommonRender(entityid_t id, entity_t *entity) {
|
||||
|
||||
// Render sprite
|
||||
spriteBatchQuad(ENTITY_STATE.spriteBatch, -1,
|
||||
entity->positionX, entity->positionY, entity->positionZ,
|
||||
entity->positionX, entity->positionY, entity->positionZ + 0.16,
|
||||
1, 1,
|
||||
0, 0, 1, 1
|
||||
);
|
||||
|
@ -15,6 +15,7 @@ void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) {
|
||||
chunk->tiles[0] = 1;
|
||||
chunk->tiles[1] = 1;
|
||||
chunk->tiles[16] = 1;
|
||||
chunk->tiles[17] = 1;
|
||||
|
||||
// Start by loading the tiles and figuring out how big we need to make the
|
||||
// primitive that the chunk uses.
|
||||
|
Reference in New Issue
Block a user