jtmm2/src/main.c

107 lines
2.1 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 */
play_level(0);
/* return to menu */
return 1;
}
int play_level(uint level_id)
{
/* create player */
Player player = {
.pos = {16 * VEC_PRECISION, 16 * VEC_PRECISION},
.hbox = {7 * VEC_PRECISION, 7 * VEC_PRECISION},
.vbox = {7, 7},
.origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION}
};
/* set level */
const Level *level;
level_set(&level, level_id);
/* create camera */
Camera camera = {
.pos = {DWIDTH * VEC_PRECISION, DHEIGHT * VEC_PRECISION},
.target = &player.pos,
.speed = 0.02
};
/* create input manager */
Input input;
input_init(&input);
/* UPS control */
volatile int has_ticked = 1;
timer_start(timer_setup(TIMER_ANY, 1000000/UPS, callback, &has_ticked));
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);
}
/* draw event just draws stuff */
draw_event(&player, level, &camera, &input, step);
}
return 1;
}
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)
{
//getkey();
input_step(input);
player_step(player, input);
level_step(level);
camera_step(camera);
}
void draw_event(Player *player, const Level *level, Camera *camera, Input *input, uint step)
{
dclear(C_WHITE);
level_draw(level, camera);
player_draw(player, camera);
#ifdef DEBUG
camera_draw_debug(camera);
//input_draw_debug(input);
player_draw_debug(player, step);
#endif
dupdate();
}