RogueLife/src/main.c

168 lines
4.4 KiB
C

#include "map.h"
#include "level.h"
#include "render.h"
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#include <gint/kprint.h>
#include <gint/cpu.h>
#include <gint/timer.h>
#include <stdlib.h>
static bool getkey_global_shortcuts(key_event_t e)
{
if(usb_is_open() && e.key == KEY_OPTN && !e.shift && !e.alpha) {
usb_fxlink_screenshot(true);
return true;
}
return false;
}
#define _ C_BLACK,
#define R C_RED,
#define G C_GREEN,
#define B C_BLUE,
#define Y C_RGB(16,16,16),
struct level lv_demo = { 0, {
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ G G G G G G G _ _ _ _ _ _ _ _ _
_ _ _ Y Y G G G G G G G G G G G G G G G _ _ _ _
_ _ _ Y _ _ _ G G _ _ _ G G G _ _ G _ G _ _ _ _
_ _ _ R _ _ _ _ Y Y Y Y Y _ G Y _ G Y G _ _ _ _
_ _ R R _ _ Y Y Y _ _ Y _ _ _ Y _ _ Y _ _ _ _ _
_ R R R R R Y _ Y Y Y Y Y Y _ Y _ _ Y Y Y B _ _
_ R R R R R Y _ _ Y Y Y Y Y Y Y _ _ Y _ _ B _ _
_ _ R R R _ Y _ Y Y Y Y Y Y Y Y Y _ B _ _ B B _
_ _ R R R _ Y Y Y Y Y Y Y Y Y _ Y B B B _ B B _
_ _ R R _ _ _ _ Y Y _ _ Y Y _ _ _ B B B B B B _
_ _ _ Y Y Y _ _ _ _ _ _ Y Y Y Y B B B B B B _ _
_ _ _ _ Y Y Y Y Y Y Y Y Y _ _ _ _ _ _ _ B B _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
}};
__attribute__((noreturn))
void panic(char const *message)
{
dclear(C_WHITE);
dtext(1, 1, C_BLACK, message);
dupdate();
getkey();
exit(EXIT_FAILURE);
}
int main(void)
{
getkey_set_feature_function(getkey_global_shortcuts);
kprint_enable_fp();
usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL };
usb_open(interfaces, GINT_CALL_NULL);
//---
// Load map
//---
struct map demo_map = { .width = 24, .height = 14 };
struct map *m = &demo_map;
m->tiles = malloc(m->width * m->height * sizeof *m->tiles);
if(!m->tiles) panic("Bruh go fix your malloc");
for(int y = 0; y < m->height; y++)
for(int x = 0; x < m->width; x++) {
struct tile *t = map_tile(m, x, y);
uint16_t color = lv_demo.colors[y * m->width + x];
t->solid = (color == C_BLACK);
t->color = color;
}
struct camera camera;
struct camera *c = &camera;
camera_init(c, m);
//---
// Main loop
//---
volatile int frame_tick = 1;
int timer_id = timer_configure(TIMER_ANY, 1000000 / FRAME_RATE,
GINT_CALL_SET(&frame_tick));
if(timer_id >= 0)
timer_start(timer_id);
/* Lock inputs for (+) and (-) until release */
bool lock_plus = false;
bool lock_minus = false;
bool lock_optn = false;
while(1) {
while(!frame_tick) sleep();
/* Assume the frame is not late */
fixed_t dt = fix(1) / FRAME_RATE;
dclear(C_BLACK);
render_map(m, c);
// Debugging stuff
map_coord_t top, left, bottom, right;
camera_screen2map(c, 0, 0, &left, &top);
camera_screen2map(c, DWIDTH, DHEIGHT, &right, &bottom);
dprint(1, 1, C_WHITE, "Corners, as map: %g..%g %g..%g",
f2double(left), f2double(right), f2double(top), f2double(bottom));
int x0, y0;
camera_map2screen(c, fix(0), fix(0), &x0, &y0);
dprint(1, 14, C_WHITE, "Coords of start: %d, %d", x0, y0);
// ~
dupdate();
clearevents();
if(keydown(KEY_MENU))
gint_osmenu();
if(keydown(KEY_EXIT))
break;
/* Camera speed */
fixed_t vx = CAMERA_SPEED_X;
fixed_t vy = CAMERA_SPEED_Y;
/* Camera movement for this frame */
if(keydown(KEY_4))
camera_move(c, -fmul(dt, vx), 0);
if(keydown(KEY_6))
camera_move(c, fmul(dt, vx), 0);
if(keydown(KEY_8))
camera_move(c, 0, -fmul(dt, vy));
if(keydown(KEY_2))
camera_move(c, 0, fmul(dt, vy));
if(keydown(KEY_PLUS)) {
if(!lock_plus) camera_zoom(c, c->zoom + 1);
lock_plus = true;
}
else lock_plus = false;
if(keydown(KEY_MINUS)) {
if(!lock_minus) camera_zoom(c, c->zoom - 1);
lock_minus = true;
}
else lock_minus = false;
if(keydown(KEY_OPTN)) {
if(!lock_optn && usb_is_open()) usb_fxlink_screenshot(true);
lock_optn = true;
}
else lock_optn = false;
}
timer_stop(timer_id);
free(m->tiles);
usb_close();
return EXIT_SUCCESS;
}