Duet/src/main.c

135 lines
3.3 KiB
C

#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/gint.h>
#include <gint/cpu.h>
#include <fxlibc/printf.h>
#include <string.h>
#include "duet.h"
level_t level0 = {
.tempo = 1.0,
.blocks = (rectmeta_t []){
{
.time = 0,
.shape = Shape_LongBar,
.position = Position_Left,
.action = Action_Normal
},
{
.time = 1,
.shape = Shape_LongBar,
.position = Position_Middle,
.action = Action_Normal
},
{
.time = 3,
.shape = Shape_LongBar,
.position = Position_Left,
.action = Action_Normal
},
}
};
int main(void)
{
volatile int need_frame = 1;
game_t game;
float dt = 0;
int timer;
__printf_enable_fp();
memset(&game, 0x00, sizeof game);
game.level = &level3;
game.current = game.level->blocks;
game.time = -5.0;
timer = timer_configure(TIMER_ANY, 33000, GINT_CALL_SET(&need_frame));
timer_start(timer);
while (1) {
/* Time management */
while (need_frame == 0) sleep();
need_frame = 0;
dt = (1.0 / 30) * game.level->tempo;
game.time += dt;
/* Input analysis */
bool rotate_left=false, rotate_right=false;
key_event_t e;
while ((e = pollevent()).type != KEYEV_NONE) {
if (e.type == KEYEV_DOWN && e.key == KEY_MENU)
gint_osmenu();
}
if (keydown(KEY_7)) {
rotate_left = true;
}
if (keydown(KEY_0)) {
rotate_right = true;
}
/* Level generation */
// Remove rectangles that have passed their lifetime by 2 seconds
for(int i = 0; i < game.cursor;) {
if(game.time > game.table[i].meta->time + 2)
game.table[i] = game.table[--game.cursor];
else
i++;
}
// Find rectangles that need to be loaded
rectmeta_t const *meta = game.current;
while(meta < game.level->blocks + game.level->block_count) {
if(meta - game.current > RECT_TABLE_SIZE - game.cursor)
break; /* oops, not enough array space left */
if(meta->time - 10 > game.time)
break;
meta++;
}
// Load everything up to meta
while(game.current < meta) {
rect_t *r = &game.table[game.cursor++];
r->meta = game.current++;
rect_load(r, r->meta);
}
/* Physics */
// if(player_collision(&game))
// break;
for(int i = 0; i < game.cursor; i++) {
rect_physics(&game.table[i], game.table[i].meta, game.time);
}
if(rotate_left)
game.player_rota += 2.1 * dt;
if(rotate_right)
game.player_rota -= 2.1 * dt;
/* Rendering */
dclear(player_collision(&game) ? C_RED : C_BLACK);
dprint(0, 0, C_WHITE, "game time: %.2fs", game.time);
dprint(0, 11, C_WHITE, "rectangles loaded: %d", game.cursor);
for(int i = 0; i < game.cursor; i++)
drectoid(&game.table[i], C_WHITE);
render_player(game.player_rota);
dupdate();
}
timer_stop(timer);
return (1);
}