diff --git a/examples/example1/build.bat b/examples/example1/build.bat index b3da528..2fcac26 100644 --- a/examples/example1/build.bat +++ b/examples/example1/build.bat @@ -6,10 +6,10 @@ mkdir build git submodule update --init -REM pushd ".\external\raylib\" -REM cmake -S . -B build -REM cmake --build build --config Release -REM popd +pushd ".\external\raylib\" +cmake -S . -B build -DBUILD_EXAMPLES=Off +cmake --build build +popd 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 diff --git a/examples/example1/example1.c b/examples/example1/example1.c index 9ff570a..05641a9 100644 --- a/examples/example1/example1.c +++ b/examples/example1/example1.c @@ -5,6 +5,8 @@ #include #include #include "external/raylib/src/raylib.h" +#include "external/raylib/src/rlgl.h" +#include "external/raylib/src/raymath.h" // Component structs typedef struct { @@ -46,10 +48,12 @@ void SpawnParticle(BECS_ECS *ecs, BECS_ComponentID posID, BECS_ComponentID velID data->lifetime = 2.0f; } -void LifetimeSystem(BECS_ECS *ecs, float dt) { - ParticleData *data = (ParticleData *)ecs->componentPools[3].denseArray; - BECS_Entity *entity = (BECS_Entity *)ecs->componentPools[3].denseEntityMapping; - for (uint32_t i = 0; i < ecs->entityCount; i++) { +void LifetimeSystem(BECS_ECS *ecs, float dt, ParticleData *data, BECS_Entity *entity) { + // ParticleData *data = (ParticleData *)ecs->componentPools[3].denseArray; + // BECS_Entity *entity = (BECS_Entity *)ecs->componentPools[3].denseEntityMapping; + // 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; if (data[i].lifetime <= 0.0f) { BECS_EntityDestroy(ecs, entity[i]); @@ -57,12 +61,13 @@ void LifetimeSystem(BECS_ECS *ecs, float dt) { } } -void PhysicsSystem(BECS_ECS *ecs, float dt) { - Position *pos = ((Position *)ecs->componentPools[0].denseArray); - Velocity *vel = ((Velocity *)ecs->componentPools[1].denseArray); - Acceleration *acc = ((Acceleration *)ecs->componentPools[2].denseArray); - for (uint32_t i = 0; i < ecs->entityCount; i++) - { +void PhysicsSystem(BECS_ECS *ecs, float dt, Position *pos, Velocity *vel, Acceleration *acc) { + //Position *pos = ((Position *)ecs->componentPools[0].denseArray); + //Velocity *vel = ((Velocity *)ecs->componentPools[1].denseArray); + // Acceleration *acc = ((Acceleration *)ecs->componentPools[2].denseArray); + // 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].vx += acc[i].ax * 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) { - Position *pos = ((Position *)ecs->componentPools[0].denseArray); - ParticleData *data = ((ParticleData *)ecs->componentPools[3].denseArray); - for (uint32_t i = 0; i < ecs->entityCount; i++) - { +void RenderSystem(BECS_ECS *ecs, float dt, Position *pos, ParticleData *data) { + // Position *pos = ((Position *)ecs->componentPools[0].denseArray); + // ParticleData *data = ((ParticleData *)ecs->componentPools[3].denseArray); + // 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; Color color = {255, 255, 255, (unsigned char)(alpha * 255)}; 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 ;icomponentPools[0].count;//Use correct count + for(uint32_t i=0; ilifetime -= dt; - // if (data->lifetime <= 0.0f) { - // BECS_EntityDestroy(&ecs, entity); - // } - // } - // } - LifetimeSystem(&ecs, dt); + LifetimeSystem(&ecs, dt, dataData, entityData); // Spawn particle on mouse click if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) { 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); } } - // Physics system: update position and velocity - // 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); + PhysicsSystem(&ecs, dt, posData, velData, accData); BeginDrawing(); ClearBackground(BLACK); - // Render system: draw particles - // 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, 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); + // RenderSystem(&ecs, dt, posData, dataData); + RenderSystemBatched(&ecs, dt, posData, dataData, posArray, radArray, colArray); 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(); } CloseWindow(); free(memory); + free(posArray); + free(colArray); + free(radArray); return 0; }