pawengine/main_layer.c

100 lines
3.5 KiB
C
Raw Permalink Normal View History

2025-04-19 13:36:13 +02:00
#include "src/kitty.h"
#include "src/matrix.h"
#include "src/types.h"
#include "src/util.h"
#include "src/wayland.h"
#include <string.h>
#include <time.h>
#define INSTANCE_COUNT 1000
#define TRIG_VERTEX_COUNT 6
struct Vertex trig_vertices[TRIG_VERTEX_COUNT] = {
(struct Vertex){(struct Vec2){.x = -0.5f, .y = -0.5f},
(struct Vec3){1.0f, 0.0f, 1.0f}},
(struct Vertex){(struct Vec2){.x = 0.5f, .y = -0.5f},
(struct Vec3){1.0f, 0.0f, 0.0f}},
(struct Vertex){(struct Vec2){.x = 0.5f, .y = 0.5f},
(struct Vec3){1.0f, 0.0f, 1.0f}},
(struct Vertex){(struct Vec2){.x = -0.5f, .y = -0.5f},
(struct Vec3){1.0f, 0.0f, 1.0f}},
(struct Vertex){(struct Vec2){.x = 0.5f, .y = 0.5f},
(struct Vec3){1.0f, 0.0f, 1.0f}},
(struct Vertex){(struct Vec2){.x = -0.5f, .y = 0.5f},
(struct Vec3){0.0f, 0.0f, 1.0f}},
};
struct TrigUBO {
struct mat3x3 view;
struct mat3x3 proj;
};
int main() {
struct Vk *vk;
struct cat_Wl *state = cat_init_wl_layer("meooow", &vk);
struct Vec2 vel[INSTANCE_COUNT];
struct Vec2 pos[INSTANCE_COUNT];
float rot[INSTANCE_COUNT];
struct Kitty *kitty = kitty_make(NULL);
kitty_set_vertex_shader(kitty, "./res/shaders/vert.spv");
kitty_set_fragment_shader(kitty, "./res/shaders/frag.spv");
kitty_set_vertex_buffer(kitty, trig_vertices, TRIG_VERTEX_COUNT,
sizeof(struct Vertex));
kitty_add_vertex_buffer_format(kitty, VK_FORMAT_R32G32_SFLOAT);
kitty_add_vertex_buffer_format(kitty, VK_FORMAT_R32G32B32_SFLOAT);
kitty_attatch_ubo(kitty, sizeof(struct TrigUBO));
kitty_attatch_ubo(kitty, sizeof(struct mat3x3) * INSTANCE_COUNT);
kitty_attatch_image(kitty, "./res/images/neocat.png");
uint32_t instance_buffer[INSTANCE_COUNT] = {0};
for (int i = 0; i < INSTANCE_COUNT; i++) {
instance_buffer[i] = i;
rot[i] = randf() * 2.0f * PI;
pos[i] = (struct Vec2){randf(), randf()};
vel[i] = vec2_mul(
vec2_normalize((struct Vec2){randf() - 0.5f, randf() - 0.5f}), 0.2f);
/* trig->vel[i] = (struct Vec2){1.0f, 0.0f}; */
}
kitty_add_instance_buffer(kitty, instance_buffer, INSTANCE_COUNT,
sizeof(uint32_t));
kitty_add_instance_buffer_format(kitty, VK_FORMAT_R32_UINT);
kitty_finalise(vk, kitty);
float delta_secs = 1.0f / 1000.0f;
while (!cat_wl_should_close(state)) {
struct TrigUBO ubo = {0};
ubo.view = multiply3x3(translate3x3((struct Vec2){-1.5f, -0.5f}),
scale3x3((struct Vec2){1.0f, 1.0f}));
ubo.proj =
scale3x3((struct Vec2){1.f, (float)vk->width / (float)vk->heigh});
kitty_set_next_ubo(vk, kitty, 0, &ubo);
struct mat3x3 *model = kitty_get_next_ubo_pointer(vk, kitty, 1);
for (int i = 0; i < INSTANCE_COUNT; i++) {
pos[i] = vec2_add(pos[i], vec2_mul(vel[i], delta_secs));
rot[i] += delta_secs;
if (pos[i].x < 0 || pos[i].x > 2.5) {
vel[i].x = -vel[i].x;
}
if (pos[i].y < -0.5 || pos[i].y > 2) {
vel[i].y = -vel[i].y;
}
model[i] = IDENT3x3;
model[i] = multiply3x3(model[i], translate3x3(pos[i]));
model[i] = multiply3x3(model[i], rotate3x3(rot[i]));
model[i] = multiply3x3(model[i], scale3x3((struct Vec2){0.1f, 0.1f}));
}
cat_wl_draw(state);
struct timespec t = {
.tv_sec = 0,
.tv_nsec = 10000000,
};
nanosleep(&t, NULL);
}
kitty_free(vk, kitty);
cat_uninit_wl(state);
state = NULL;
vk = NULL;
}