Update: Permanant change to new Systems, change rendering system to rlgl implementation with batched rendering
This commit is contained in:
parent
c88484a794
commit
1f2f2f9541
|
|
@ -6,10 +6,10 @@ mkdir build
|
||||||
|
|
||||||
git submodule update --init
|
git submodule update --init
|
||||||
|
|
||||||
REM pushd ".\external\raylib\"
|
pushd ".\external\raylib\"
|
||||||
REM cmake -S . -B build
|
cmake -S . -B build -DBUILD_EXAMPLES=Off
|
||||||
REM cmake --build build --config Release
|
cmake --build build
|
||||||
REM popd
|
popd
|
||||||
|
|
||||||
pushd ".\build"
|
pushd ".\build"
|
||||||
cl /Zi raylib.lib user32.lib opengl32.lib gdi32.lib winmm.lib msvcrt.lib shell32.lib "..\example1.c" -Ic:"\external\raylib\build\raylib\include" /link /libpath:c:"..\external\raylib\build\raylib\Release" /NODEFAULTLIB:libcmt /NODEFAULTLIB:msvcrt /NODEFAULTLIB:libcmtd
|
cl /Zi raylib.lib user32.lib opengl32.lib gdi32.lib winmm.lib msvcrt.lib shell32.lib "..\example1.c" -Ic:"\external\raylib\build\raylib\include" /link /libpath:c:"..\external\raylib\build\raylib\Release" /NODEFAULTLIB:libcmt /NODEFAULTLIB:msvcrt /NODEFAULTLIB:libcmtd
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "external/raylib/src/raylib.h"
|
#include "external/raylib/src/raylib.h"
|
||||||
|
#include "external/raylib/src/rlgl.h"
|
||||||
|
#include "external/raylib/src/raymath.h"
|
||||||
|
|
||||||
// Component structs
|
// Component structs
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
@ -46,10 +48,12 @@ void SpawnParticle(BECS_ECS *ecs, BECS_ComponentID posID, BECS_ComponentID velID
|
||||||
data->lifetime = 2.0f;
|
data->lifetime = 2.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LifetimeSystem(BECS_ECS *ecs, float dt) {
|
void LifetimeSystem(BECS_ECS *ecs, float dt, ParticleData *data, BECS_Entity *entity) {
|
||||||
ParticleData *data = (ParticleData *)ecs->componentPools[3].denseArray;
|
// ParticleData *data = (ParticleData *)ecs->componentPools[3].denseArray;
|
||||||
BECS_Entity *entity = (BECS_Entity *)ecs->componentPools[3].denseEntityMapping;
|
// BECS_Entity *entity = (BECS_Entity *)ecs->componentPools[3].denseEntityMapping;
|
||||||
for (uint32_t i = 0; i < ecs->entityCount; i++) {
|
// for (uint32_t i = 0; i < ecs->entityCount; i++) {
|
||||||
|
uint32_t entityCount = ecs->entityCount;
|
||||||
|
for (uint32_t i = 0; i < entityCount; i++) {
|
||||||
data[i].lifetime -= dt;
|
data[i].lifetime -= dt;
|
||||||
if (data[i].lifetime <= 0.0f) {
|
if (data[i].lifetime <= 0.0f) {
|
||||||
BECS_EntityDestroy(ecs, entity[i]);
|
BECS_EntityDestroy(ecs, entity[i]);
|
||||||
|
|
@ -57,12 +61,13 @@ void LifetimeSystem(BECS_ECS *ecs, float dt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsSystem(BECS_ECS *ecs, float dt) {
|
void PhysicsSystem(BECS_ECS *ecs, float dt, Position *pos, Velocity *vel, Acceleration *acc) {
|
||||||
Position *pos = ((Position *)ecs->componentPools[0].denseArray);
|
//Position *pos = ((Position *)ecs->componentPools[0].denseArray);
|
||||||
Velocity *vel = ((Velocity *)ecs->componentPools[1].denseArray);
|
//Velocity *vel = ((Velocity *)ecs->componentPools[1].denseArray);
|
||||||
Acceleration *acc = ((Acceleration *)ecs->componentPools[2].denseArray);
|
// Acceleration *acc = ((Acceleration *)ecs->componentPools[2].denseArray);
|
||||||
for (uint32_t i = 0; i < ecs->entityCount; i++)
|
// for (uint32_t i = 0; i < ecs->entityCount; i++) {
|
||||||
{
|
uint32_t entityCount = ecs->entityCount;
|
||||||
|
for (uint32_t i = 0; i < entityCount; i++) {
|
||||||
vel[i].vy += acc[i].ay * dt;
|
vel[i].vy += acc[i].ay * dt;
|
||||||
vel[i].vx += acc[i].ax * dt;
|
vel[i].vx += acc[i].ax * dt;
|
||||||
pos[i].y += vel[i].vy * dt;
|
pos[i].y += vel[i].vy * dt;
|
||||||
|
|
@ -70,22 +75,62 @@ void PhysicsSystem(BECS_ECS *ecs, float dt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RenderSystem(BECS_ECS *ecs, float dt) {
|
void RenderSystem(BECS_ECS *ecs, float dt, Position *pos, ParticleData *data) {
|
||||||
Position *pos = ((Position *)ecs->componentPools[0].denseArray);
|
// Position *pos = ((Position *)ecs->componentPools[0].denseArray);
|
||||||
ParticleData *data = ((ParticleData *)ecs->componentPools[3].denseArray);
|
// ParticleData *data = ((ParticleData *)ecs->componentPools[3].denseArray);
|
||||||
for (uint32_t i = 0; i < ecs->entityCount; i++)
|
// for (uint32_t i = 0; i < ecs->entityCount; i++) {
|
||||||
{
|
uint32_t entityCount = ecs->entityCount;
|
||||||
|
for (uint32_t i = 0; i < entityCount; i++) {
|
||||||
float alpha = data[i].lifetime / 2.0;
|
float alpha = data[i].lifetime / 2.0;
|
||||||
Color color = {255, 255, 255, (unsigned char)(alpha * 255)};
|
Color color = {255, 255, 255, (unsigned char)(alpha * 255)};
|
||||||
DrawCircle(pos[i].x, pos[i].y, data[i].radius, color);
|
DrawCircle(pos[i].x, pos[i].y, data[i].radius, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DrawCircleBatched(Vector2 *positions, float *radii, Color *colors, uint32_t count, uint32_t segments) {
|
||||||
|
rlBegin(RL_TRIANGLES);
|
||||||
|
for(uint32_t i=0 ;i<count; i++){
|
||||||
|
Vector2 center=positions[i];
|
||||||
|
float radius=radii[i];
|
||||||
|
Color color=colors[i];
|
||||||
|
rlColor4ub(color.r,color.g,color.b,color.a);
|
||||||
|
|
||||||
|
//Approximatecirclewithtriangles
|
||||||
|
for(uint32_t j=0; j<segments; j++){
|
||||||
|
float angle1=(j*2.0f*PI)/segments;
|
||||||
|
float angle2=((j+1)*2.0f*PI)/segments;
|
||||||
|
|
||||||
|
Vector2 v1={center.x+radius*cosf(angle1),center.y+radius*sinf(angle1)};
|
||||||
|
Vector2 v2={center.x+radius*cosf(angle2),center.y+radius*sinf(angle2)};
|
||||||
|
|
||||||
|
rlVertex2f(center.x,center.y);
|
||||||
|
rlVertex2f(v1.x,v1.y);
|
||||||
|
rlVertex2f(v2.x,v2.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rlEnd();
|
||||||
|
printf("Drawing %d circles\n", count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderSystemBatched(BECS_ECS *ecs, float dt, Position *pos, ParticleData *data, Vector2 *posArray, float *radArray, Color *colArray) {
|
||||||
|
uint32_t batchCount = 0;
|
||||||
|
uint32_t entityCount = ecs->componentPools[0].count;//Use correct count
|
||||||
|
for(uint32_t i=0; i<entityCount; i++){
|
||||||
|
posArray[batchCount]=(Vector2){pos[i].x,pos[i].y};
|
||||||
|
radArray[batchCount]=data[i].radius;
|
||||||
|
float alpha=data[i].lifetime/2.0f;
|
||||||
|
colArray[batchCount]=(Color){255,255,255,(unsigned char)(alpha*255)};
|
||||||
|
batchCount++;
|
||||||
|
}
|
||||||
|
printf("entityCount %u, batchCount: %u\n", entityCount, batchCount);
|
||||||
|
DrawCircleBatched(posArray, radArray, colArray, batchCount, 16);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
// Initialize ECS
|
// Initialize ECS
|
||||||
size_t componentSizes[4] = {sizeof(Position), sizeof(Velocity), sizeof(Acceleration), sizeof(ParticleData)};
|
size_t componentSizes[4] = {sizeof(Position), sizeof(Velocity), sizeof(Acceleration), sizeof(ParticleData)};
|
||||||
size_t memSize = BECS_GetMinMemoryArenaSize(componentSizes, 4);
|
size_t memSize = BECS_GetMinMemoryArenaSize(componentSizes, 4);
|
||||||
printf("%lu\n", memSize);
|
printf("%llu\n", memSize);
|
||||||
char *memory = (char *)malloc(memSize);
|
char *memory = (char *)malloc(memSize);
|
||||||
BECS_ECS ecs = BECS_InitECS(memory, memSize);
|
BECS_ECS ecs = BECS_InitECS(memory, memSize);
|
||||||
|
|
||||||
|
|
@ -95,73 +140,61 @@ int main(void) {
|
||||||
BECS_ComponentID accID = BECS_ComponentRegister(&ecs, sizeof(Acceleration), BECS_MAX_ENTITIES);
|
BECS_ComponentID accID = BECS_ComponentRegister(&ecs, sizeof(Acceleration), BECS_MAX_ENTITIES);
|
||||||
BECS_ComponentID dataID = BECS_ComponentRegister(&ecs, sizeof(ParticleData), BECS_MAX_ENTITIES);
|
BECS_ComponentID dataID = BECS_ComponentRegister(&ecs, sizeof(ParticleData), BECS_MAX_ENTITIES);
|
||||||
|
|
||||||
|
Position *posData = (Position *)ecs.componentPools[posID].denseArray;
|
||||||
|
Velocity *velData = (Velocity *)ecs.componentPools[velID].denseArray;
|
||||||
|
Acceleration *accData = (Acceleration *)ecs.componentPools[accID].denseArray;
|
||||||
|
ParticleData *dataData = (ParticleData *)ecs.componentPools[dataID].denseArray;
|
||||||
|
BECS_Entity *entityData = (BECS_Entity *)ecs.componentPools[dataID].denseEntityMapping;
|
||||||
|
|
||||||
InitWindow(1600, 800, "2D Particle System with BECS");
|
InitWindow(1600, 800, "2D Particle System with BECS");
|
||||||
|
|
||||||
|
Vector2 *posArray = malloc(BECS_MAX_ENTITIES * sizeof(Vector2));
|
||||||
|
float *radArray = malloc(BECS_MAX_ENTITIES * sizeof(float));
|
||||||
|
Color *colArray = malloc(BECS_MAX_ENTITIES * sizeof(Color));
|
||||||
|
|
||||||
|
rlMatrixMode(RL_PROJECTION);
|
||||||
|
rlLoadIdentity();
|
||||||
|
rlOrtho(0,GetScreenWidth(),GetScreenHeight(),0, 0.0f, 1.0f);
|
||||||
|
rlMatrixMode(RL_MODELVIEW);
|
||||||
|
rlLoadIdentity();
|
||||||
|
rlSetBlendMode(RL_BLEND_ALPHA);
|
||||||
|
rlDisableBackfaceCulling();
|
||||||
|
rlDisableDepthTest();
|
||||||
|
rlDisableTexture();
|
||||||
|
rlDrawRenderBatchActive();
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
float dt = GetFrameTime();
|
float dt = GetFrameTime();
|
||||||
|
|
||||||
// Update lifetimes and despawn
|
LifetimeSystem(&ecs, dt, dataData, entityData);
|
||||||
// for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
|
||||||
// if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
|
||||||
// if (BECS_ComponentHas(&ecs, entity, dataID)) {
|
|
||||||
// ParticleData *data = (ParticleData *)BECS_ComponentGet(&ecs, entity, dataID);
|
|
||||||
// data->lifetime -= dt;
|
|
||||||
// if (data->lifetime <= 0.0f) {
|
|
||||||
// BECS_EntityDestroy(&ecs, entity);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
LifetimeSystem(&ecs, dt);
|
|
||||||
|
|
||||||
// Spawn particle on mouse click
|
// Spawn particle on mouse click
|
||||||
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
|
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
|
||||||
Vector2 mousePos = GetMousePosition();
|
Vector2 mousePos = GetMousePosition();
|
||||||
for (uint32_t i = 0; i < 10000; i++) {
|
for (uint32_t i = 0; i < 60; i++) {
|
||||||
SpawnParticle(&ecs, posID, velID, accID, dataID, mousePos.x, mousePos.y);
|
SpawnParticle(&ecs, posID, velID, accID, dataID, mousePos.x, mousePos.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Physics system: update position and velocity
|
PhysicsSystem(&ecs, dt, posData, velData, accData);
|
||||||
// for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
|
||||||
// if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
|
||||||
// if (BECS_ComponentHas(&ecs, entity, posID) &&
|
|
||||||
// BECS_ComponentHas(&ecs, entity, velID) &&
|
|
||||||
// BECS_ComponentHas(&ecs, entity, accID)) {
|
|
||||||
// Position *pos = (Position *)BECS_ComponentGet(&ecs, entity, posID);
|
|
||||||
// Velocity *vel = (Velocity *)BECS_ComponentGet(&ecs, entity, velID);
|
|
||||||
// Acceleration *acc = (Acceleration *)BECS_ComponentGet(&ecs, entity, accID);
|
|
||||||
//
|
|
||||||
// vel->vx += acc->ax * dt;
|
|
||||||
// vel->vy += acc->ay * dt;
|
|
||||||
// pos->x += vel->vx * dt;
|
|
||||||
// pos->y += vel->vy * dt;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
PhysicsSystem(&ecs, dt);
|
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
|
|
||||||
// Render system: draw particles
|
// RenderSystem(&ecs, dt, posData, dataData);
|
||||||
// for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
RenderSystemBatched(&ecs, dt, posData, dataData, posArray, radArray, colArray);
|
||||||
// if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
|
||||||
// if (BECS_ComponentHas(&ecs, entity, posID) && BECS_ComponentHas(&ecs, entity, dataID)) {
|
|
||||||
// Position *pos = (Position *)BECS_ComponentGet(&ecs, entity, posID);
|
|
||||||
// ParticleData *data = (ParticleData *)BECS_ComponentGet(&ecs, entity, dataID);
|
|
||||||
// float alpha = data->lifetime / 2.0f;
|
|
||||||
// Color color = {255, 255, 255, (unsigned char)(alpha * 255)};
|
|
||||||
// DrawCircle((int)pos->x, (int)pos->y, data->radius, color);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
RenderSystem(&ecs, dt);
|
|
||||||
|
|
||||||
DrawText(TextFormat("Particles: %u", ecs.entityCount), 10, 10, 20, WHITE);
|
DrawText(TextFormat("Particles: %u", ecs.entityCount), 10, 10, 20, WHITE);
|
||||||
|
DrawText(TextFormat("Time: %f", dt * 1000), 10, 30, 20, WHITE);
|
||||||
|
DrawText(TextFormat("FPS: %f", 1/dt), 10, 50, 20, WHITE);
|
||||||
|
DrawText(TextFormat("Screen Res: %d x %d", GetScreenWidth(), GetScreenHeight()), 10, 70, 20, WHITE);
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
free(memory);
|
free(memory);
|
||||||
|
free(posArray);
|
||||||
|
free(colArray);
|
||||||
|
free(radArray);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue