finally compiling and running, text still needs rework
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
#include "twn_loop.h"
|
||||
#include "townengine/context.h"
|
||||
#include "townengine/rendering.h"
|
||||
#include "townengine/input/internal_api.h"
|
||||
#include "townengine/util.h"
|
||||
#include "townengine/twn_game_object.h"
|
||||
#include "townengine/audio/internal_api.h"
|
||||
#include "townengine/textures/internal_api.h"
|
||||
#include "townengine/rendering/internal_api.h"
|
||||
#include "twn_engine_context_c.h"
|
||||
#include "twn_input_c.h"
|
||||
#include "twn_util.h"
|
||||
#include "twn_game_object_c.h"
|
||||
#include "twn_audio_c.h"
|
||||
#include "twn_textures_c.h"
|
||||
#include "twn_rendering.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <physfs.h>
|
||||
@ -28,12 +27,12 @@
|
||||
static void poll_events(void) {
|
||||
SDL_Event e;
|
||||
|
||||
ctx.window_size_has_changed = false;
|
||||
ctx.game.window_size_has_changed = false;
|
||||
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_QUIT:
|
||||
ctx.is_running = false;
|
||||
ctx.game.is_running = false;
|
||||
break;
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
@ -42,7 +41,7 @@ static void poll_events(void) {
|
||||
|
||||
switch (e.window.event) {
|
||||
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||
ctx.window_size_has_changed = true;
|
||||
ctx.game.window_size_has_changed = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -93,7 +92,7 @@ static void main_loop(void) {
|
||||
int64_t current_frame_time = SDL_GetPerformanceCounter();
|
||||
int64_t delta_time = current_frame_time - ctx.prev_frame_time;
|
||||
ctx.prev_frame_time = current_frame_time;
|
||||
ctx.delta_time = delta_time;
|
||||
ctx.game.delta_time = delta_time;
|
||||
|
||||
/* handle unexpected timer anomalies (overflow, extra slow frames, etc) */
|
||||
if (delta_time > ctx.desired_frametime * 8) { /* ignore extra-slow frames */
|
||||
@ -159,28 +158,28 @@ static void main_loop(void) {
|
||||
|
||||
/* finally, let's get to work */
|
||||
int frames = 0;
|
||||
while (ctx.frame_accumulator >= ctx.desired_frametime * ctx.update_multiplicity) {
|
||||
while (ctx.frame_accumulator >= ctx.desired_frametime * ctx.game.update_multiplicity) {
|
||||
frames += 1;
|
||||
for (size_t i = 0; i < ctx.update_multiplicity; ++i) {
|
||||
for (size_t i = 0; i < ctx.game.update_multiplicity; ++i) {
|
||||
/* TODO: disable rendering pushes on not-last ? */
|
||||
render_queue_clear();
|
||||
|
||||
poll_events();
|
||||
|
||||
if (ctx.window_size_has_changed) {
|
||||
if (ctx.game.window_size_has_changed) {
|
||||
t_vec2 size;
|
||||
SDL_GetWindowSize(ctx.window, &size.x, &size.y);
|
||||
ctx.window_w = size.x;
|
||||
ctx.window_h = size.y;
|
||||
ctx.game.window_w = size.x;
|
||||
ctx.game.window_h = size.y;
|
||||
}
|
||||
|
||||
input_state_update(&ctx.input);
|
||||
input_state_update(&ctx.game.input);
|
||||
|
||||
game_object_tick();
|
||||
|
||||
ctx.frame_accumulator -= ctx.desired_frametime;
|
||||
ctx.tick_count = (ctx.tick_count % ULLONG_MAX) + 1;
|
||||
ctx.initialization_needed = false;
|
||||
ctx.game.tick_count = (ctx.game.tick_count % ULLONG_MAX) + 1;
|
||||
ctx.game.initialization_needed = false;
|
||||
|
||||
}
|
||||
}
|
||||
@ -202,9 +201,9 @@ static bool initialize(void) {
|
||||
/* debug mode _defaults_ to being enabled on debug builds. */
|
||||
/* you should be able to enable it at runtime on any build */
|
||||
#ifndef NDEBUG
|
||||
ctx.debug = true;
|
||||
ctx.game.debug = true;
|
||||
#else
|
||||
ctx.debug = false;
|
||||
ctx.game.debug = false;
|
||||
#endif
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
@ -215,7 +214,7 @@ static bool initialize(void) {
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
if (ctx.debug)
|
||||
if (ctx.game.debug)
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
|
||||
else
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_NO_ERROR);
|
||||
@ -279,8 +278,8 @@ static bool initialize(void) {
|
||||
|
||||
/* TODO: */
|
||||
// SDL_GetRendererOutputSize(ctx.renderer, &ctx.window_w, &ctx.window_h);
|
||||
ctx.window_w = RENDER_BASE_WIDTH;
|
||||
ctx.window_h = RENDER_BASE_HEIGHT;
|
||||
ctx.game.window_w = RENDER_BASE_WIDTH;
|
||||
ctx.game.window_h = RENDER_BASE_HEIGHT;
|
||||
|
||||
/* audio initialization */
|
||||
{
|
||||
@ -312,11 +311,11 @@ static bool initialize(void) {
|
||||
}
|
||||
|
||||
/* you could change this at runtime if you wanted */
|
||||
ctx.update_multiplicity = 1;
|
||||
ctx.game.update_multiplicity = 1;
|
||||
|
||||
#ifndef EMSCRIPTEN
|
||||
/* hook up opengl debugging callback */
|
||||
if (ctx.debug) {
|
||||
if (ctx.game.debug) {
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glDebugMessageCallback(opengl_log, NULL);
|
||||
}
|
||||
@ -325,18 +324,18 @@ static bool initialize(void) {
|
||||
/* random seeding */
|
||||
/* SDL_GetPerformanceCounter returns some platform-dependent number. */
|
||||
/* it should vary between game instances. i checked! random enough for me. */
|
||||
ctx.random_seed = SDL_GetPerformanceCounter();
|
||||
srand((unsigned int)ctx.random_seed);
|
||||
stbds_rand_seed(ctx.random_seed);
|
||||
ctx.game.random_seed = SDL_GetPerformanceCounter();
|
||||
srand((unsigned int)ctx.game.random_seed);
|
||||
stbds_rand_seed(ctx.game.random_seed);
|
||||
|
||||
/* main loop machinery */
|
||||
ctx.is_running = true;
|
||||
ctx.game.is_running = true;
|
||||
ctx.resync_flag = true;
|
||||
ctx.clocks_per_second = SDL_GetPerformanceFrequency();
|
||||
ctx.prev_frame_time = SDL_GetPerformanceCounter();
|
||||
ctx.desired_frametime = ctx.clocks_per_second / TICKS_PER_SECOND;
|
||||
ctx.frame_accumulator = 0;
|
||||
ctx.tick_count = 0;
|
||||
ctx.game.tick_count = 0;
|
||||
|
||||
/* delta time averaging */
|
||||
ctx.delta_averager_residual = 0;
|
||||
@ -353,7 +352,7 @@ static bool initialize(void) {
|
||||
text_cache_init(&ctx.text_cache);
|
||||
|
||||
/* input */
|
||||
input_state_init(&ctx.input);
|
||||
input_state_init(&ctx.game.input);
|
||||
|
||||
/* scripting */
|
||||
/*
|
||||
@ -376,7 +375,7 @@ static void clean_up(void) {
|
||||
scripting_deinit(ctx);
|
||||
*/
|
||||
|
||||
input_state_deinit(&ctx.input);
|
||||
input_state_deinit(&ctx.game.input);
|
||||
|
||||
/* if you're gonna remove this, it's also being done in rendering.c */
|
||||
for (size_t i = 0; i < arrlenu(ctx.render_queue_2d); ++i) {
|
||||
@ -395,7 +394,7 @@ static void clean_up(void) {
|
||||
|
||||
|
||||
static void reset_state(void) {
|
||||
input_reset_state(&ctx.input);
|
||||
input_reset_state(&ctx.game.input);
|
||||
textures_reset_state();
|
||||
}
|
||||
|
||||
@ -420,11 +419,11 @@ int enter_loop(int argc, char **argv) {
|
||||
game_object_load();
|
||||
|
||||
ctx.was_successful = true;
|
||||
ctx.initialization_needed = true;
|
||||
ctx.game.initialization_needed = true;
|
||||
|
||||
while (ctx.is_running) {
|
||||
while (ctx.game.is_running) {
|
||||
if (game_object_try_reloading()) {
|
||||
ctx.initialization_needed = true;
|
||||
ctx.game.initialization_needed = true;
|
||||
reset_state();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user