glerminal/examples/rogue.cpp

87 lines
1.7 KiB
C++

#include <glerminal.h>
#include <cmath>
namespace
{
enum : unsigned char
{
empty,
player,
floor,
wall
};
constexpr unsigned int WALL_LAYERS = LAYER_COUNT - 1;
void init()
{
glerminal_load_sprites_file("resources/rogue.png");
for (int i = 0; i < WALL_LAYERS; i++)
{
const unsigned char v = (i + 1) * (256 / WALL_LAYERS) - 1;
const unsigned int j = (0xFF << 24) | (v << 16) | (v << 8) | v;
glerminal_layer_color(i, j);
glerminal_layer_scale(i, (i / static_cast<float>(WALL_LAYERS)) + 1);
}
for (int i = 0; i < GRID_WIDTH; i++)
{
for (int j = 0; j < GRID_HEIGHT; j++)
{
for (int k = 0; k < WALL_LAYERS; k++)
{
if (i == 0 || j == 0 || i == GRID_WIDTH - 1 || j == GRID_HEIGHT - 1)
{
glerminal_set(i, j, k, floor);
}
else
{
glerminal_set(i, j, k, wall);
}
}
}
}
}
void mainloop(float dt)
{
static float time = 0;
time += dt;
const float cx = GRID_WIDTH / 2.0f * cosf(time / 2) + GRID_WIDTH / 2.0f;
const float cy = GRID_HEIGHT / 2.0f * sinf(time / 2) + GRID_HEIGHT / 2.0f;
for (int i = 0; i < GRID_WIDTH; i++)
{
for (int j = 0; j < GRID_HEIGHT; j++)
{
for (int k = 0; k < WALL_LAYERS; k++)
{
const float dx = (i - cx);
const float dy = (j - cy);
const float dist = sqrtf(dx * dx + dy * dy);
const float angle = atan2f(dy, dx);
const float ox = dist * k / WALL_LAYERS * cosf(angle);
const float oy = dist * k / WALL_LAYERS * sinf(angle);
glerminal_offset(i, j, k, ox * 2, oy * 2);
}
glerminal_set(i, j, WALL_LAYERS, empty);
}
}
glerminal_set(cx, cy, WALL_LAYERS, player);
glerminal_flush();
}
}
int main(int argc, char** argv)
{
glerminal_run(init, mainloop);
}