BosonX/src/main.cpp

188 lines
5.3 KiB
C++

#include "level.h"
#include "generator.h"
#include "render.h"
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/cpu.h>
#include <azur/gint/render.h>
#include <gint/drivers/r61524.h>
#include <fxlibc/printf.h>
#define RGB24(hex) \
(((hex & 0xf80000) >> 8) | \
((hex & 0x00fc00) >> 5) | \
((hex & 0x0000f8) >> 3))
constexpr int bg = RGB24(0x49759f);
int main(void)
{
__printf_enable_fp();
prof_init();
azrp_config_scale(1);
azrp_shader_clear_configure();
// azrp_shader_image_rgb16_configure();
// azrp_shader_image_p8_configure();
// azrp_shader_image_p4_configure();
azrp_shader_triangle_configure();
render_init();
level_t level = level_create(3);
int volatile need_frame = 1;
int last_frame_us = 0;
int timer = timer_configure(TIMER_ANY, 33000, GINT_CALL_SET(&need_frame));
timer_start(timer);
num t = 0.0;
num player_z = 0.0;
num level_z = 0.0;
num player_speed = 5.0;
int sections_passed = 0;
bool show_footer = false;
struct camera camera;
camera.pos = vec3(0, RENDER_CAMERA_DEPTH, 0);
camera.platform = 0;
camera.rot_direction = 0;
camera.rot_t = 0;
bool game_run = true;
while(game_run) {
while(!need_frame) sleep();
need_frame = 0;
num dt = 1.0 / 30;
t += dt;
prof_t perf_frame = prof_make();
prof_enter_norec(perf_frame);
level_update(&level);
camera_set_angle(&camera, camera_compute_platform_angle(&camera));
//---
// Rendering
//---
azrp_perf_clear();
azrp_clear(bg);
vec2 screen_size(DWIDTH, DHEIGHT);
prof_t perf_comp = prof_make();
prof_enter_norec(perf_comp);
for(int depth = RENDER_SECTION_DISTANCE - 1; depth >= 0; depth--) {
num z = depth * RENDER_SECTION_LENGTH - level_z;
struct section *s = &level.section_buffer[depth];
for(int i = 0; i < PLATFORM_COUNT; i++) {
/* Set this to get the full tunnel test */
constexpr bool full_tunnel = false;
int color = C_WHITE;
if(full_tunnel) {
int gray = ((i + depth + sections_passed) % PLATFORM_COUNT)
* 31 / PLATFORM_COUNT;
color = C_RGB(gray, gray, gray);
}
else {
if(s->platforms[i].type != PLATFORM_WHITE) continue;
}
struct prect p = render_platform_position(i, z);
/* Near plane clipping */
if(p.fl.z <= num(0.2)) continue;
if(p.nl.z <= num(0.2)) p.nl.z = num(0.2);
if(p.nr.z <= num(0.2)) p.nr.z = num(0.2);
camera_project_prect(&camera, &p, screen_size);
render_triangle(&p.nl, &p.fr, &p.fl, color);
render_triangle(&p.fr, &p.nl, &p.nr, color);
}
}
prof_leave_norec(perf_comp);
azrp_update();
if(show_footer) {
drect(0, DHEIGHT-20, DWIDTH-1, DHEIGHT-1, C_WHITE);
dprint(4, 209, C_BLACK, "render:%4d+%4dus comp:%4dus FPS:%02d",
prof_time(azrp_perf_render) - prof_time(azrp_perf_r61524),
prof_time(azrp_perf_r61524),
prof_time(perf_comp),
last_frame_us ? 1000000 / last_frame_us : 0);
r61524_display(gint_vram, DHEIGHT-20, 20, R61524_DMA_WAIT);
}
//---
// Input
//---
key_event_t ev;
while((ev = pollevent()).type != KEYEV_NONE) {
if(ev.type == KEYEV_UP)
continue;
if(ev.key == KEY_EXIT || ev.key == KEY_MENU)
game_run = false;
if(ev.key == KEY_OPTN)
show_footer = !show_footer;
if(ev.key == KEY_F1) {
level = level_create(1);
level_update(&level);
}
if(ev.key == KEY_F2) {
level = level_create(2);
level_update(&level);
}
if(ev.key == KEY_F3) {
level = level_create(3);
level_update(&level);
}
if(ev.key == KEY_LEFT && !camera.rot_direction) {
camera.rot_direction = -1;
camera.rot_t = 0;
}
if(ev.key == KEY_RIGHT && !camera.rot_direction) {
camera.rot_direction = 1;
camera.rot_t = 0;
}
}
if(!game_run) break;
//---
// Simulation
//---
player_z += player_speed * dt;
level_z += player_speed * dt;
while(level_z > num(RENDER_SECTION_LENGTH)) {
level_advance(&level);
level_z -= RENDER_SECTION_LENGTH;
sections_passed++;
}
if(camera.rot_direction != 0) {
camera.rot_t += dt;
if(camera.rot_t > CAMERA_ROTATION_DURATION) {
int p = camera.platform + camera.rot_direction;
p = (p + PLATFORM_COUNT) % PLATFORM_COUNT;
camera.platform = p;
camera.rot_direction = 0;
camera.rot_t = 0;
}
}
//---
prof_leave_norec(perf_frame);
last_frame_us = prof_time(perf_frame);
}
timer_stop(timer);
return 0;
}