chaos-drop/src/main.cc

270 lines
6.7 KiB
C++

#define __BSD_VISIBLE 1
#include <azur/azur.h>
#include <string.h>
#include <math.h>
#include "chaos-drop.h"
#if defined(AZUR_TOOLKIT_SDL)
#include <SDL2/SDL.h>
#include <azur/gl/gl.h>
#include <memory>
#include "backend/linux/programs.h"
static uint16_t vram[VWIDTH * VHEIGHT];
std::unique_ptr<ProgramTexture> shader_texture = nullptr;
static GLuint tex_vram;
static void view_update(void)
{
SDL_Window *window = azur_sdl_window();
int width, height;
SDL_GetWindowSize(window, &width, &height);
/* Transform from pixel coordinates within the winfow to GL coordinates */
glm::mat3 tr_pixel2gl(
2.0f / width, 0.0f, 0.0f,
0.0f, -2.0f / height, 0.0f,
-1.0f, 1.0f, 1.0f);
glUseProgram(shader_texture->prog);
shader_texture->set_uniform("u_transform", tr_pixel2gl);
}
static void init(void)
{
shader_texture = std::make_unique<ProgramTexture>();
view_update();
memset(vram, 0x55, sizeof vram);
glGenTextures(1, &tex_vram);
glBindTexture(GL_TEXTURE_2D, tex_vram);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VWIDTH, VHEIGHT, 0, GL_RGB,
GL_UNSIGNED_SHORT_5_6_5, vram);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
static void quit(void)
{
}
static int platform_update(struct input *input)
{
SDL_Event e;
*input = (struct input){};
while(SDL_PollEvent(&e)) {
// ImGui_ImplSDL2_ProcessEvent(&e);
// render_needed = std::max(render_needed, 1);
if(e.type == SDL_QUIT)
return 1;
if(e.type == SDL_WINDOWEVENT &&
e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
view_update();
}
}
Uint8 const *state = SDL_GetKeyboardState(NULL);
input->up = state[SDL_SCANCODE_UP];
input->down = state[SDL_SCANCODE_DOWN];
input->left = state[SDL_SCANCODE_LEFT];
input->right = state[SDL_SCANCODE_RIGHT];
return 0;
}
static void platform_render(void)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VWIDTH, VHEIGHT, 0, GL_RGB,
GL_UNSIGNED_SHORT_5_6_5, vram);
SDL_Window *window = azur_sdl_window();
shader_texture->vertices.clear();
shader_texture->add_texture(0, 0, 3*VWIDTH, 3*VHEIGHT);
shader_texture->draw();
SDL_GL_SwapWindow(window);
}
#elif defined(AZUR_TOOLKIT_GINT)
#include <azur/gint/render.h>
#include <gint/gint.h>
#include <gint/keyboard.h>
#include <gint/display.h>
#include <gint/drivers/r61524.h>
#include <libprof.h>
#define vram gint_vram
static int platform_update(struct input *input)
{
key_event_t e;
while((e = pollevent()).type != KEYEV_NONE) {
if(e.type == KEYEV_UP)
continue;
if(e.key == KEY_EXIT)
return 1;
if(e.key == KEY_MENU)
gint_osmenu();
if(e.key == KEY_OPTN)
input->OPTN = true;
}
input->left = keydown(KEY_LEFT);
input->right = keydown(KEY_RIGHT);
input->up = keydown(KEY_UP);
input->down = keydown(KEY_DOWN);
return 0;
}
static void platform_render(void)
{
azrp_update();
}
static void init(void)
{
prof_init();
azrp_config_scale(2);
cd_raytrace_configure();
}
static void quit(void)
{
prof_quit();
}
#endif
mat3 operator *(mat3 const &A, mat3 const &B)
{
mat3 C;
C.x11 = A.x11 * B.x11 + A.x12 * B.x21 + A.x13 * B.x31;
C.x12 = A.x11 * B.x12 + A.x12 * B.x22 + A.x13 * B.x32;
C.x13 = A.x11 * B.x13 + A.x12 * B.x23 + A.x13 * B.x33;
C.x21 = A.x21 * B.x11 + A.x22 * B.x21 + A.x23 * B.x31;
C.x22 = A.x21 * B.x12 + A.x22 * B.x22 + A.x23 * B.x32;
C.x23 = A.x21 * B.x13 + A.x22 * B.x23 + A.x23 * B.x33;
C.x31 = A.x31 * B.x11 + A.x32 * B.x21 + A.x33 * B.x31;
C.x32 = A.x31 * B.x12 + A.x32 * B.x22 + A.x33 * B.x32;
C.x33 = A.x31 * B.x13 + A.x32 * B.x23 + A.x33 * B.x33;
return C;
}
vec3 operator * (mat3 const &M, vec3 const &u)
{
vec3 v;
v.x = M.x11 * u.x + M.x12 * u.y + M.x13 * u.z;
v.y = M.x21 * u.x + M.x22 * u.y + M.x23 * u.z;
v.z = M.x31 * u.x + M.x32 * u.y + M.x33 * u.z;
return v;
}
static struct camera *camera = NULL;
static bool debug = false;
void render(void)
{
#ifdef AZUR_TOOLKIT_GINT
azrp_perf_clear();
cd_raytrace(camera);
platform_render();
if(debug) {
drect(0, DHEIGHT-20, DWIDTH-1, DHEIGHT-1, C_WHITE);
dprint(4, 209, C_BLACK, "render:%4d+%4dus",
prof_time(azrp_perf_render) - prof_time(azrp_perf_r61524),
prof_time(azrp_perf_r61524));
r61524_display(gint_vram, DHEIGHT-20, 20, R61524_DMA_WAIT);
}
#else
render_fragment(camera, vram, 0, VHEIGHT);
platform_render();
#endif
}
int update(void)
{
struct input input = {};
if(platform_update(&input))
return 1;
/* Yes I'm aware I'm always changing the angles */
bool changed = false;
static num const mv_speed = 0.35;
static num const wall = WORLD_SIZE * 0.45; /* margin */
static float const rot_speed = 0.01;
static float const rot_snap = 0.6;
static float const rot_max = 0.2;
static num const neon_speed = 2;
if(input.left) {
camera->pos.x = max(camera->pos.x - mv_speed, -wall);
camera->yaw = fmaxf(camera->yaw - rot_speed, -rot_max);
changed = true;
}
else if(input.right) {
camera->pos.x = min(camera->pos.x + mv_speed, wall);
camera->yaw = fminf(camera->yaw + rot_speed, rot_max);
changed = true;
}
else {
camera->yaw *= rot_snap;
changed = true;
}
if(input.up) {
camera->pos.z = min(camera->pos.z + mv_speed, wall);
camera->pitch = fmaxf(camera->pitch - rot_speed, -rot_max);
changed = true;
}
else if(input.down) {
camera->pos.z = max(camera->pos.z - mv_speed, -wall);
camera->pitch = fminf(camera->pitch + rot_speed, rot_max);
changed = true;
}
else {
camera->pitch *= rot_snap;
changed = true;
}
if(changed)
camera_update_angles(camera);
camera->neon_position =
(camera->neon_position + num(32) - neon_speed) % num(32);
if(input.OPTN)
debug = !debug;
return 0;
}
int main(void)
{
if(azur_init("Chaos Drop!", 3*VWIDTH, 3*VHEIGHT))
return 1;
struct camera c = {};
camera = &c;
/* TODO: Why do I need such a low FOV?! */
camera_set_fov(camera, 80.0);
camera_update_angles(camera);
init();
int rc = azur_main_loop(render, 30, update, -1, AZUR_MAIN_LOOP_TIED);
quit();
return rc;
}