entity system!

This commit is contained in:
Asuro
2025-02-09 19:30:18 +01:00
parent d1270c47a9
commit 90bc5753a9
19 changed files with 306 additions and 67 deletions

View File

@@ -2,6 +2,7 @@
#include "../Log.h"
#include "../../engine/Shared.h"
#include "../Global.h"
#include "../Instance.h"
#include "bgfx/defines.h"
#include "bx/timer.h"
#include <bx/file.h>
@@ -18,18 +19,20 @@ namespace Game
float y;
float z;
uint32_t abgr;
float uv_x;
float uv_y;
};
static PosColorVertex cubeVertices[] =
{
{-1.0f, 1.0f, 1.0f, 0xff000000 },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
{-1.0f, 1.0f, 1.0f, 0xff000000, 0.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff, 0.0f, 0.0f },
{-1.0f, -1.0f, 1.0f, 0xff00ff00, 0.0f, 0.0f },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff, 0.0f, 0.0f },
{-1.0f, 1.0f, -1.0f, 0xffff0000, 0.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff, 0.0f, 0.0f },
{-1.0f, -1.0f, -1.0f, 0xffffff00, 0.0f, 0.0f },
{ 1.0f, -1.0f, -1.0f, 0xffffffff, 0.0f, 0.0f },
};
static const uint16_t cubeTriList[] =
@@ -147,20 +150,28 @@ namespace Game
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
VertLayout.begin()
Models[0].VertLayout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), VertLayout);
IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList)));
Models[0].VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), Models[0].VertLayout);
Models[0].IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList)));
bgfx::ShaderHandle vertexShader = loadShader("vert");
bgfx::ShaderHandle fragmentShader = loadShader("frag");
Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
Materials[0].Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
Materials[0].State = 0
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CW
| BGFX_STATE_MSAA;
if (!GetInstance().IsInitialized)
{
GetInstance().IsInitialized = true;
GetInstance().StartTime = bx::getHPCounter();
}
}
@@ -169,6 +180,7 @@ namespace Game
{
SharedData& shared = GetShared();
// Reload shaders if necessary
FileChangeNotification* shaderChange = nullptr;
if (shared.Dev.ChangedShaderCount > 0)
{
@@ -183,7 +195,7 @@ namespace Game
bgfx::ProgramHandle newProgram = bgfx::createProgram(vertexShader, fragmentShader, true);
if (isValid(newProgram))
{
Shader = newProgram;
Materials[0].Shader = newProgram;
}
else
{
@@ -191,10 +203,9 @@ namespace Game
}
}
}
int64_t tickDelta = bx::getHPCounter() - GetInstance().StartTime;
double time = tickDelta / double(bx::getHPFrequency());
GetInstance().GameLevel.Update();
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
@@ -215,43 +226,25 @@ namespace Game
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
bgfx::IndexBufferHandle ibh = IndexBuffer;
uint64_t state = 0
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CW
| BGFX_STATE_MSAA
;
// Submit 11x11 cubes.
for (uint32_t yy = 0; yy < 11; ++yy)
for (int32_t i = 0; i < GetInstance().GameLevel.Cubes.Count; ++i)
{
for (uint32_t xx = 0; xx < 11; ++xx)
Cube* c = GetInstance().GameLevel.Cubes.Get(i);
if (c)
{
float mtx[16];
bx::mtxRotateXY(mtx, time + xx * 0.21f, time + yy * 0.37f);
mtx[12] = -15.0f + float(xx) * 3.0f;
mtx[13] = -15.0f + float(yy) * 3.0f;
mtx[14] = 0.0f;
bgfx::setTransform(c->Transform.M);
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, VertexBuffer);
bgfx::setIndexBuffer(ibh);
// Set render states.
bgfx::setState(state);
Model& currentModel = Models[c->ModelIdx];
Material& currentMaterial = Materials[c->MaterialIdx];
bgfx::setVertexBuffer(0, currentModel.VertexBuffer);
bgfx::setIndexBuffer(currentModel.IndexBuffer);
bgfx::setState(currentMaterial.State);
// Submit primitive for rendering to view 0.
bgfx::submit(0, Shader);
bgfx::submit(0, currentMaterial.Shader);
}
}
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", time);
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", GetInstance().Now);
bgfx::dbgTextPrintf(1, 2, 0x0f, "Frame: %u", GetInstance().FrameCounter);
bgfx::frame();