jtmm2/src/main.c

109 lines
2.2 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"
int main(void)
{
init(); /* initialize gint */
/* main game loop */
play_level(0);
/* return to menu */
return 1;
}
int play_level(int level_id)
{
/* create player */
Player player = {
.pos = {16 * VEC_PRECISION, 16 * VEC_PRECISION},
.hbox = {7 * VEC_PRECISION, 7 * VEC_PRECISION},
.origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION}
};
/* create level */
Level level = {
.width = 0,
.height = 0
};
/* create camera */
Camera camera = {
.pos = {DWIDTH * VEC_PRECISION, DHEIGHT * VEC_PRECISION},
.target = &player.pos,
.speed = 0.05
};
/* create input manager */
Input input;
input.keys[K_LEFT] = KEY_LEFT;
input.keys[K_RIGHT] = KEY_RIGHT;
input.keys[K_UP] = KEY_UP;
input.keys[K_DOWN] = KEY_DOWN;
/* FPS control */
volatile int has_ticked = 1;
timer_setup(TIMER_ANY, 1000000/FPS, callback, &has_ticked);
timer_start(0);
while (camera.pos.x / VEC_PRECISION != player.pos.x / VEC_PRECISION ||
camera.pos.y / VEC_PRECISION != player.pos.y / VEC_PRECISION)
{
/* FPS control */
while(!has_ticked) sleep();
has_ticked = 0;
/* repeat step event so the UPS is constant regardless of FPS */
for (int i = 0; i < UPS / FPS; ++i)
{
/* step event is where all the logic happens */
step_event(&player, &level, &camera, &input);
}
/* draw event just draws stuff */
draw_event(&player, &level, &camera, &input);
}
return 0;
}
int callback(volatile void *arg)
{
volatile int *has_ticked = arg;
*has_ticked = 1;
return 0;
}
void step_event(Player *player, Level *level, Camera *camera, Input *input)
{
//getkey();
input_step(input);
player_step(player, input);
level_step(level);
camera_step(camera);
}
void draw_event(Player *player, Level *level, Camera *camera, Input *input)
{
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);
#endif
dupdate();
}