This repository has been archived on 2022-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
jtmm2-old/src/main.c

97 lines
2.0 KiB
C

#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include <gint/std/string.h>
#include <stdbool.h>
#include "conf.h"
#include "main.h"
#include "debug.h"
#include "init.h"
#include "vec.h"
#include "player.h"
#include "level.h"
#include "camera.h"
#include "input.h"
#include "gen_levels.h"
int main(void) {
init(); /* initialize gint */
/* main game loop */
while (play_level(1)) {};
/* return to menu */
return 1;
}
int play_level(uint level_id) {
/* set level */
const Level *level;
level_set(&level, level_id);
/* create player */
Player player;
player_init(&player, level);
/* create camera */
Camera camera;
camera_init(&camera, &player, level);
/* create input manager */
Input input;
input_init(&input);
/* UPS control */
volatile int has_ticked = 1;
int timer = timer_setup(TIMER_ANY, 1000000/UPS, callback, &has_ticked);
timer_start(timer);
uint step = 0;
while (!input_is_down(&input, K_EXIT)) {
/* repeat step event so the UPS is constant regardless of FPS */
for (int i = 0; i < UPS / FPS; ++i) {
/* UPS control */
while(!has_ticked) sleep();
has_ticked = 0;
/* step event is where all the logic happens */
step += 1;
step_event(&player, level, &camera, &input, step);
/* player death check */
if(player.dead) {
timer_stop(timer);
return 1;
}
}
/* draw event just draws stuff */
draw_event(&player, level, &camera, &input, step);
}
timer_stop(timer);
return 0;
}
int callback(volatile void *arg) {
volatile int *has_ticked = arg;
*has_ticked = 1;
return 0;
}
void step_event(Player *player, const Level *level, Camera *camera, Input *input, uint step) {
input_step(input, step);
player_step(player, input, level, step);
level_step(level);
camera_step(camera);
}
void draw_event(Player *player, const Level *level, Camera *camera, Input *input, uint step) {
dclear(level->bg_color);
level_draw(level, camera);
player_draw(player, camera);
#ifdef DEBUG
/* put your debug code here */
#endif
dupdate();
}