JacksEscape/app.c

90 lines
2.1 KiB
C
Raw Normal View History

2024-03-04 13:41:01 +08:00
2024-03-05 11:40:42 +08:00
#include "app.h"
2024-03-08 14:51:29 +08:00
#include "camera.h"
2024-03-05 11:40:42 +08:00
#include "entity.h"
2024-04-22 05:11:11 +08:00
#include "gametime.h"
2024-03-05 11:40:42 +08:00
#include "input.h"
#include "particle.h"
2024-03-05 11:40:42 +08:00
#include "physics.h"
#include "player.h"
#include "types.h"
2024-03-28 14:04:43 +08:00
#include "render_bundle.h"
2024-03-30 22:05:49 +08:00
#include "render_component.h"
#include "mapper_misc.h"
2024-04-24 21:30:28 +08:00
#include "ui.h"
2024-03-04 13:41:01 +08:00
#include <stdlib.h>
2024-03-04 16:13:43 +08:00
#include <stdio.h>
2024-04-24 22:23:01 +08:00
#ifndef RGB
#define RGB(r, g, b) ((r) | ((g) << 8) | ((b) << 16))
#endif
2024-03-04 13:41:01 +08:00
App *app_NewApp() {
2024-03-28 14:04:43 +08:00
render_LoadBundle("bundles.txt");
2024-04-02 15:42:04 +08:00
App *app = zero_malloc(sizeof(App));
2024-03-04 13:41:01 +08:00
app->input = input_NewSystem(app);
app->physics = physics_NewSystem(app);
app->player = player_NewSystem(app);
app->entity = entity_NewSystem(app);
app->camera = camera_NewSystem(app);
app->particle = particle_NewSystem(app);
2024-04-22 05:11:11 +08:00
app->time = gametime_NewSystem(app);
2024-04-24 21:30:28 +08:00
app->ui = ui_NewSystem(app);
ui_RebuildUI(app->ui);
2024-04-24 22:23:01 +08:00
ui_PushState(app->ui, ui_TitleMenu);
2024-03-04 13:41:01 +08:00
2024-04-02 15:42:04 +08:00
app->switch_level = NULL;
2024-04-22 05:46:05 +08:00
app->timescale = 1.0;
2024-04-24 22:23:01 +08:00
app->clear_color = RGB(40, 40, 40);
2024-04-02 15:42:04 +08:00
app->wantQuit = false;
2024-04-22 05:28:50 +08:00
app->paused = false;
2024-03-04 15:05:21 +08:00
app_QueueLoadLevel(app, "title.txt");
2024-03-04 16:13:43 +08:00
2024-03-04 13:41:01 +08:00
return app;
}
void app_DeleteApp(App *app) {
input_DeleteSystem(app->input);
entity_DeleteSystem(app->entity);
2024-03-04 13:41:01 +08:00
physics_DeleteSystem(app->physics);
player_DeleteSystem(app->player);
2024-03-08 14:51:29 +08:00
camera_DeleteSystem(app->camera);
2024-04-22 05:11:11 +08:00
particle_DeleteSystem(app->particle);
gametime_DeleteSystem(app->time);
2024-04-24 21:30:28 +08:00
ui_DeleteSystem(app->ui);
2024-03-04 13:41:01 +08:00
free(app);
}
void app_Advance(App *app, Duration deltaTime) {
2024-04-02 15:42:04 +08:00
// Limit deltaTime to 20ms (1/50 second)
if (deltaTime.microseconds > 20000)
deltaTime.microseconds = 20000;
if (app->switch_level != NULL)
_app_SwitchLevel(app);
2024-03-04 13:41:01 +08:00
input_Advance(app->input);
2024-04-22 05:28:50 +08:00
2024-04-24 21:30:28 +08:00
Duration delta_game = deltaTime;
2024-04-22 05:46:05 +08:00
if (1.0 - app->timescale > EPS)
2024-04-24 21:30:28 +08:00
delta_game.microseconds = delta_game.microseconds * app->timescale;
2024-04-22 05:46:05 +08:00
2024-04-22 05:28:50 +08:00
if (!app->paused) {
2024-04-24 21:30:28 +08:00
gametime_Advance(app->time, delta_game);
particle_Advance(app->particle, delta_game);
player_Advance(app->player, delta_game);
physics_Advance(app->physics, delta_game);
entity_Advance(app->entity, delta_game);
camera_Advance(app->camera, delta_game);
2024-04-22 05:28:50 +08:00
}
2024-04-24 21:30:28 +08:00
ui_Advance(app->ui, deltaTime);
2024-03-04 13:41:01 +08:00
}