65 lines
1.5 KiB
C
65 lines
1.5 KiB
C
|
|
#include "pxl8_lights.h"
|
||
|
|
#include "pxl8_mem.h"
|
||
|
|
|
||
|
|
#include <stdlib.h>
|
||
|
|
|
||
|
|
struct pxl8_lights {
|
||
|
|
pxl8_light* data;
|
||
|
|
u32 capacity;
|
||
|
|
u32 count;
|
||
|
|
};
|
||
|
|
|
||
|
|
pxl8_lights* pxl8_lights_create(u32 capacity) {
|
||
|
|
if (capacity > PXL8_LIGHTS_MAX) capacity = PXL8_LIGHTS_MAX;
|
||
|
|
|
||
|
|
pxl8_lights* lights = pxl8_calloc(1, sizeof(pxl8_lights));
|
||
|
|
if (!lights) return NULL;
|
||
|
|
|
||
|
|
lights->data = pxl8_calloc(capacity, sizeof(pxl8_light));
|
||
|
|
if (!lights->data) {
|
||
|
|
pxl8_free(lights);
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
lights->capacity = capacity;
|
||
|
|
lights->count = 0;
|
||
|
|
|
||
|
|
return lights;
|
||
|
|
}
|
||
|
|
|
||
|
|
void pxl8_lights_destroy(pxl8_lights* lights) {
|
||
|
|
if (!lights) return;
|
||
|
|
pxl8_free(lights->data);
|
||
|
|
pxl8_free(lights);
|
||
|
|
}
|
||
|
|
|
||
|
|
void pxl8_lights_add(pxl8_lights* lights, f32 x, f32 y, f32 z, u8 r, u8 g, u8 b, u8 intensity, f32 radius) {
|
||
|
|
if (!lights || lights->count >= lights->capacity) return;
|
||
|
|
|
||
|
|
f32 radius_sq = radius * radius;
|
||
|
|
pxl8_light* l = &lights->data[lights->count++];
|
||
|
|
l->position.x = x;
|
||
|
|
l->position.y = y;
|
||
|
|
l->position.z = z;
|
||
|
|
l->r = r;
|
||
|
|
l->g = g;
|
||
|
|
l->b = b;
|
||
|
|
l->intensity = intensity;
|
||
|
|
l->radius = radius;
|
||
|
|
l->radius_sq = radius_sq;
|
||
|
|
l->inv_radius_sq = radius_sq > 0.0f ? 1.0f / radius_sq : 0.0f;
|
||
|
|
}
|
||
|
|
|
||
|
|
void pxl8_lights_clear(pxl8_lights* lights) {
|
||
|
|
if (!lights) return;
|
||
|
|
lights->count = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
u32 pxl8_lights_count(const pxl8_lights* lights) {
|
||
|
|
return lights ? lights->count : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
const pxl8_light* pxl8_lights_data(const pxl8_lights* lights) {
|
||
|
|
return lights ? lights->data : NULL;
|
||
|
|
}
|