2021-05-30 21:59:09 +02:00
|
|
|
#include "map.h"
|
|
|
|
#include "level.h"
|
|
|
|
#include "render.h"
|
2021-06-01 17:49:29 +02:00
|
|
|
#include "geometry.h"
|
|
|
|
#include "entities.h"
|
2021-05-30 21:59:09 +02:00
|
|
|
|
|
|
|
#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>
|
2021-06-01 17:49:29 +02:00
|
|
|
#include <gint/std/stdlib.h>
|
2021-05-30 21:59:09 +02:00
|
|
|
#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),
|
|
|
|
|
2021-06-02 09:38:59 +02:00
|
|
|
extern level_t lv_demo;
|
2021-05-30 21:59:09 +02:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
getkey_set_feature_function(getkey_global_shortcuts);
|
|
|
|
kprint_enable_fp();
|
2021-06-01 17:49:29 +02:00
|
|
|
srand(0xc0ffee);
|
2021-05-30 21:59:09 +02:00
|
|
|
|
|
|
|
usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL };
|
|
|
|
usb_open(interfaces, GINT_CALL_NULL);
|
|
|
|
|
|
|
|
//---
|
|
|
|
// Load map
|
|
|
|
//---
|
|
|
|
|
2021-06-01 17:49:29 +02:00
|
|
|
struct map *m = malloc(sizeof *m);
|
2021-06-02 09:38:59 +02:00
|
|
|
m->width = lv_demo.width;
|
|
|
|
m->height = lv_demo.height;
|
2021-06-01 17:49:29 +02:00
|
|
|
m->entities = NULL;
|
|
|
|
m->entity_count = 0;
|
2021-05-30 21:59:09 +02:00
|
|
|
m->tiles = malloc(m->width * m->height * sizeof *m->tiles);
|
|
|
|
|
|
|
|
for(int y = 0; y < m->height; y++)
|
|
|
|
for(int x = 0; x < m->width; x++) {
|
|
|
|
struct tile *t = map_tile(m, x, y);
|
2021-06-02 09:38:59 +02:00
|
|
|
t->base = lv_demo.tiles[lv_demo.width * y + x].base;
|
|
|
|
t->decor = lv_demo.tiles[lv_demo.width * y + x].decor;
|
|
|
|
t->solid = (t->base == 0) || (t->base >= 16);
|
2021-05-30 21:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct camera camera;
|
|
|
|
struct camera *c = &camera;
|
|
|
|
camera_init(c, m);
|
|
|
|
|
2021-06-01 17:49:29 +02:00
|
|
|
//---
|
|
|
|
// Load entities on the map
|
|
|
|
//---
|
|
|
|
|
|
|
|
for(int i = 0; i < 10; i++) {
|
|
|
|
struct entity *e = malloc(sizeof *e);
|
|
|
|
if(!e) continue;
|
|
|
|
|
|
|
|
int x=1, y=1;
|
|
|
|
for(int i = 0; i < 1000; i++) {
|
|
|
|
x = rand() % m->width;
|
|
|
|
y = rand() % m->height;
|
|
|
|
|
|
|
|
struct tile *t = map_tile(m, x, y);
|
|
|
|
if(t && !t->solid) break;
|
|
|
|
}
|
|
|
|
e->x = fix(x) + fix(1) / 2;
|
|
|
|
e->y = fix(y) + fix(1) / 2;
|
|
|
|
|
|
|
|
if(rand() % 2) {
|
2021-06-01 21:30:05 +02:00
|
|
|
e->hitbox = (shape_t){ SHAPE_RECT,
|
|
|
|
.rect = { .l=-fix(3)/8, .r=fix(3)/8, .t=-fix(3)/8, .b=fix(3)/8 },
|
2021-06-01 17:49:29 +02:00
|
|
|
.color = C_RED,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2021-06-01 21:30:05 +02:00
|
|
|
e->hitbox = (shape_t){ SHAPE_CIRCLE,
|
|
|
|
.circle = { .x=0, .y=0, .r=fix(3)/8 },
|
2021-06-01 17:49:29 +02:00
|
|
|
.color = C_RED,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
map_add_entity(m, e);
|
|
|
|
}
|
|
|
|
|
2021-06-01 22:06:58 +02:00
|
|
|
struct entity *player = m->entities[5];
|
|
|
|
player->hitbox.color = C_BLUE;
|
|
|
|
|
2021-05-30 21:59:09 +02:00
|
|
|
//---
|
|
|
|
// 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
|
2021-06-01 21:30:05 +02:00
|
|
|
fpoint_t tl = camera_screen2map(c, (ipoint_t){ 0, 0 });
|
|
|
|
fpoint_t br = camera_screen2map(c, (ipoint_t){ DWIDTH, DHEIGHT });
|
2021-06-01 17:49:29 +02:00
|
|
|
dprint(1, 1, C_WHITE, "Visible range: %g..%g %g..%g",
|
2021-06-01 21:30:05 +02:00
|
|
|
f2double(tl.x), f2double(br.x), f2double(tl.y), f2double(br.y));
|
2021-05-30 21:59:09 +02:00
|
|
|
// ~
|
|
|
|
|
|
|
|
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 */
|
2021-06-01 17:49:29 +02:00
|
|
|
if(keydown(KEY_4) || keydown(KEY_7) || keydown(KEY_1))
|
2021-05-30 21:59:09 +02:00
|
|
|
camera_move(c, -fmul(dt, vx), 0);
|
2021-06-01 17:49:29 +02:00
|
|
|
if(keydown(KEY_6) || keydown(KEY_9) || keydown(KEY_3))
|
2021-05-30 21:59:09 +02:00
|
|
|
camera_move(c, fmul(dt, vx), 0);
|
2021-06-01 17:49:29 +02:00
|
|
|
if(keydown(KEY_8) || keydown(KEY_7) || keydown(KEY_9))
|
2021-05-30 21:59:09 +02:00
|
|
|
camera_move(c, 0, -fmul(dt, vy));
|
2021-06-01 17:49:29 +02:00
|
|
|
if(keydown(KEY_2) || keydown(KEY_1) || keydown(KEY_3))
|
2021-05-30 21:59:09 +02:00
|
|
|
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;
|
2021-06-01 22:06:58 +02:00
|
|
|
|
|
|
|
/* Player speed */
|
|
|
|
vx = PLAYER_SPEED_X;
|
|
|
|
vy = PLAYER_SPEED_Y;
|
|
|
|
|
|
|
|
/* Player movement */
|
|
|
|
fixed_t dx = 0;
|
|
|
|
fixed_t dy = 0;
|
|
|
|
if(keydown(KEY_LEFT)) dx -= fmul(dt, vx);
|
|
|
|
if(keydown(KEY_RIGHT)) dx += fmul(dt, vx);
|
|
|
|
if(keydown(KEY_UP)) dy -= fmul(dt, vy);
|
|
|
|
if(keydown(KEY_DOWN)) dy += fmul(dt, vy);
|
|
|
|
|
|
|
|
entity_move(player, dx, dy);
|
|
|
|
if(map_entity_collides(m, player))
|
|
|
|
entity_move(player, -dx, -dy);
|
2021-05-30 21:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
timer_stop(timer_id);
|
2021-06-01 17:49:29 +02:00
|
|
|
map_destroy(m);
|
2021-05-30 21:59:09 +02:00
|
|
|
usb_close();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|