painfull-success-cg/src/main.c

79 lines
1.9 KiB
C

/* SPDX-License-Identifier: MIT
* Copyright (c) 2021 KikooDX
* This file is part of
* [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg),
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#include "conf.h"
#include "input.h"
#include "lazyint.h"
#include "level.h"
#include "player.h"
#include <gint/clock.h>
#include <gint/display.h>
#include <gint/timer.h>
#include <stdbool.h>
int
callback(volatile void *arg)
{
volatile bool *has_ticked = arg;
*has_ticked = true;
return 0;
}
int
main(void)
{
/* Initialize player. */
Player player;
player_init(&player);
/* Initialize level. */
Level level = (Level){};
u8 level_id = 0;
level_load(&level, &player, level_id);
/* Initialize input. */
Input input = (Input){};
input_init(&input);
/* Initalize y offset. */
i16 y_offset = DRAW_OFFSET_Y_DEFAULT;
/* UPS control. */
volatile bool has_ticked = true;
int timer =
timer_setup(TIMER_ANY, 1000000 / TARGET_UPS, callback, &has_ticked);
timer_start(timer);
/* Core loop. */
while (!input_is_down(input, K_EXIT)) {
/* Repeat step event so the UPS is constant. */
for (u8 i = 0; i < TARGET_UPS / TARGET_FPS; i += 1) {
/* UPS control. */
while (!has_ticked)
sleep();
has_ticked = false;
/* Update. */
input_update(&input);
player_update(&player, &level, input, &level_id);
/* Y offset. */
if (input_is_pressed(input, K_CAM_UP) &&
y_offset < DRAW_OFFSET_Y_MAX)
y_offset += DRAW_OFFSET_Y_STEP;
if (input_is_pressed(input, K_CAM_DOWN) &&
y_offset > DRAW_OFFSET_Y_MIN)
y_offset -= DRAW_OFFSET_Y_STEP;
}
/* Draw. */
dclear(C_BLACK);
level_draw(level, y_offset);
player_draw(player, y_offset);
/* Black borders. */
drect(0, 0, DRAW_OFFSET_X - 1, DHEIGHT, C_BLACK);
drect(DRAW_OFFSET_X + LEVEL_WIDTH_PX, 0, DWIDTH, DHEIGHT,
C_BLACK);
dupdate();
}
return 1;
}