glerminal/examples/towers.cpp
2024-05-25 23:16:23 -04:00

67 lines
1.6 KiB
C++

#include <glerminal.h>
#include <cstdlib>
#include <cmath>
namespace
{
void init()
{
srand(0);
glerminal_load_sprites_file("resources/towers.png");
for (int i = 0; i < LAYER_COUNT; i++)
{
constexpr unsigned char c = 16;
const unsigned char v = (255 - c) * powf((256 / LAYER_COUNT * i - 1) / 256.0f, 2.0f) + c;
const unsigned int j = (0xFF << 24) | (v << 16) | (v << 8) | v;
glerminal_layer_color(i, j);
glerminal_layer_scale(i, i / static_cast<float>(LAYER_COUNT) + 1);
}
for (int i = 0; i < GRID_WIDTH; i++)
{
for (int j = 0; j < GRID_HEIGHT; j++)
{
const int c = rand() % (LAYER_COUNT * 3 / 4) + LAYER_COUNT / 4;
for (int k = 0; k < c; k++)
{
glerminal_set(i, j, k, 1);
}
}
}
}
void mainloop(float dt)
{
static float time = 0;
time += dt;
const float cx = (GRID_WIDTH / 2.0f) * cosf(time / 3.1415f) + (GRID_WIDTH / 2.0f);
const float cy = (GRID_HEIGHT / 2.0f) * sinf(time / 3.1415f) + (GRID_HEIGHT / 2.0f);
for (int i = 0; i < GRID_WIDTH; i++)
{
for (int j = 0; j < GRID_WIDTH; j++)
{
for (int k = 0; k < LAYER_COUNT; k++)
{
const float ox = (256.0f / LAYER_COUNT) * 0.0025f * k * (i - cx);
const float oy = (256.0f / LAYER_COUNT) * 0.0025f * k * (j - cy);
const float sx = (1 + (256.0f / LAYER_COUNT) * 0.002f * k) * cosf(i + k * 6.2832f / 128.0f + 3.1415f * time / 1.5f);
const float sy = (1 + (256.0f / LAYER_COUNT) * 0.002f * k) * sinf(j + k * 6.2832f / 128.0f + 3.1415f * time / 1.5f);
glerminal_offset(i, j, k, ox + sx, oy + sy);
}
}
}
glerminal_flush();
}
}
int main(int argc, char** argv)
{
glerminal_run(init, mainloop);
}