glerminal/examples/towers.cpp

67 lines
1.7 KiB
C++
Raw Normal View History

#include <glerminal.h>
#include <cstdlib>
#include <cmath>
namespace
{
void init()
{
2024-05-26 03:16:23 +00:00
srand(0);
2024-05-20 02:46:56 +00:00
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;
2024-05-17 04:51:45 +00:00
glerminal_layer_color(i, j);
glerminal_layer_scale(i, i / static_cast<float>(LAYER_COUNT) + 1);
}
2024-05-20 02:46:56 +00:00
for (int i = 0; i < GRID_WIDTH; i++)
2024-05-20 02:46:56 +00:00
{
for (int j = 0; j < GRID_HEIGHT; j++)
2024-05-20 02:46:56 +00:00
{
const int c = rand() % (LAYER_COUNT * 3 / 4) + LAYER_COUNT / 4;
2024-05-20 02:46:56 +00:00
for (int k = 0; k < c; k++)
{
glerminal_set(i, j, k, 1);
}
}
}
}
void mainloop(float dt)
{
2024-05-20 02:46:56 +00:00
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)
{
2024-05-30 14:57:36 +00:00
glerminal_run(init, mainloop, nullptr, nullptr);
}