Compare commits

...

2 Commits

Author SHA1 Message Date
Asuro
2ff08d7258 show log in game 2025-03-27 17:36:55 +01:00
Asuro
24a724a021 fix mesh indexing 2025-03-27 16:52:34 +01:00
7 changed files with 79 additions and 15 deletions

View File

@@ -24,7 +24,8 @@ namespace Game
{
void EntityRenderData::Render(const Model* models, const Material* materials)
{
if (!Generated::IsValid(ModelH) || MaterialHandle == UINT16_MAX) return;
if (models == nullptr || materials == nullptr) return;
if (!Generated::IsValid(ModelH) || MaterialHandle == EMaterial::UNDEFINED) return;
if (!Visible) return;
auto& rendering = GameRendering::Get();
@@ -32,7 +33,11 @@ namespace Game
bgfx::setTransform(Transform.M.M);
const Model& currentModel = models[ModelH.ModelIdx];
const Material& currentMaterial = materials[MaterialHandle];
const Material& currentMaterial = materials[(uint16_t)MaterialHandle];
if (!isValid(currentModel.IndexBuffer) || !isValid(currentModel.VertexBuffer)) return;
if (!isValid(currentMaterial.Shader)) return;
bgfx::setVertexBuffer(0, currentModel.VertexBuffer);
bgfx::setIndexBuffer(currentModel.IndexBuffer);
bgfx::setState(currentMaterial.State);
@@ -79,7 +84,6 @@ namespace Game
needReset |= PuzzleTiles.Setup(storagePtr, needReset);
needReset |= UIQuads.Setup(storagePtr, needReset);
UIQuads.IsEnabled = false;
Tests.IsEnabled = false;
Cubes.IsEnabled = false;
@@ -269,6 +273,8 @@ namespace Game
bgfx::setViewTransform(viewId, player.PlayerCamTransform.M.M, proj);
}
bgfx::touch(viewId);
Cubes.Render(models, materials);
Tests.Render(models, materials);
PuzzleTiles.Render(models, materials);
@@ -277,7 +283,7 @@ namespace Game
void Cube::Setup()
{
EData.MaterialHandle = 0;
EData.MaterialHandle = EMaterial::Default;
EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/cube.gltf");
}
@@ -300,7 +306,7 @@ namespace Game
void TestEntity::Setup()
{
EData.MaterialHandle = 0;
EData.MaterialHandle = EMaterial::Default;
EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/zurg.gltf");
EData.Transform.Position = {0.0f, 0.0f, 0.0f};
@@ -313,8 +319,11 @@ namespace Game
{
TileHandles[i] = level.PuzzleTiles.New();
auto& tile = level.PuzzleTiles.Get(TileHandles[i]);
tile.EData.MaterialHandle = 0;
tile.EData.MaterialHandle = EMaterial::Default;
UIPlacedCards[i] = level.UIQuads.New();
auto& quad = level.UIQuads.Get(UIPlacedCards[i]);
quad.EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/plane.glb");
quad.EData.MaterialHandle = EMaterial::Default;
}
IsSetup = true;
LOG("finished setup!");
@@ -352,8 +361,9 @@ namespace Game
tile.EData.Transform.SetPosition(cardPos);
bx::mtxRotateY(tile.EData.Transform.Rotation.M, card.Rotation * bx::kPi * 0.5f);
Vec3 fw = {0, -1, 0};
quad.EData.Transform.SetPosition(fw);
Vec3 fw = {0, 0, 0};
Vec3 pos = {camTransform.Position.x, camTransform.Position.y, camTransform.Position.z};
quad.EData.Transform.SetPosition(pos);
quad.EData.Transform.Rotation = {};
}
}

View File

@@ -20,7 +20,7 @@ namespace Game
Vec4 TestColor{1.0f, 1.0f, 1.0f, 1.0f};
Vec4 BaseColor{0.0f, 0.0f, 0.0f, 1.0f};
Transform Transform;
uint16_t MaterialHandle = UINT16_MAX;
EMaterial MaterialHandle = EMaterial::UNDEFINED;
Generated::ModelHandle ModelH;
bool Visible = true;

View File

@@ -1,10 +1,13 @@
#include "Log.h"
#include "bx/handlealloc.h"
#include "bx/hash.h"
#include "bx/timer.h"
#include <bx/string.h>
#define WIN32_LEAN_AND_MEAN
#include "Windows.h"
#include "Instance.h"
#define ESC "\x1B["
#define YELLOW ESC "33m"
#define RED ESC "31m"
@@ -12,9 +15,11 @@
namespace
{
char LineBuffer[1024]{0};
char OutBuffer[1024]{0};
char LineBuffer[LogInternal::MaxLineSize]{0};
char OutBuffer[LogInternal::MaxLineSize]{0};
bx::HandleHashMapT<1024> OnceMap;
LogHistory History;
constexpr char LogFormat[]{"%s\n"};
constexpr char WarnFormat[]{YELLOW "%s" END "\n"};
constexpr char ErrorFormat[]{RED "%s" END "\n"};
@@ -33,6 +38,11 @@ void Log(ELogType logType, const char* file, uint32_t line, const char* format,
bx::vsnprintf(OutBuffer, sizeof(OutBuffer), LineBuffer, args);
va_end(args);
OutputDebugStringA(OutBuffer);
bx::strCopy(&History.LogBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, OutBuffer);
History.WriteTime[History.WriteIdx] = bx::getHPCounter();
++History.WriteIdx;
if (History.WriteIdx >= LogInternal::LogHistorySize) History.WriteIdx = 0;
}
bool WasLogged(ELogType logType, const char* file, uint32_t line, const char* format)
@@ -48,3 +58,8 @@ bool WasLogged(ELogType logType, const char* file, uint32_t line, const char* fo
OnceMap.insert(hash, {});
return false;
}
LogHistory& GetLogHistory()
{
return History;
}

View File

@@ -27,5 +27,19 @@ enum class ELogType
Error,
};
namespace LogInternal
{
constexpr int32_t MaxLineSize = 1024;
constexpr int32_t LogHistorySize = 1024;
} // namespace LogInternal
struct LogHistory
{
char LogBuffer[LogInternal::MaxLineSize * LogInternal::LogHistorySize]{0};
int32_t WriteIdx = 0;
int64_t WriteTime[LogInternal::LogHistorySize]{0};
};
void Log(ELogType logType, const char* file, uint32_t line, const char* format, ...);
bool WasLogged(ELogType logType, const char* file, uint32_t line, const char* format);
LogHistory& GetLogHistory();

View File

@@ -143,7 +143,7 @@ namespace Game
if (LoadMesh(mod, fullPath.getCPtr(), modelFileIsBinary[i]))
{
mod.Handle.Asset.Idx = CrcPath(fullPath.getCPtr());
mod.Handle.ModelIdx = inst.DebugData.AssetCount;
mod.Handle.ModelIdx = writeI;
if (inst.DebugData.AssetCount < inst.DebugData.MaxAssets)
{
inst.DebugData.AssetHandles[inst.DebugData.AssetCount] = mod.Handle.Asset;

View File

@@ -9,6 +9,7 @@
#include "SDL3/SDL_events.h"
#include "backends/imgui_impl_sdl3.h"
#include "bgfx/defines.h"
#include "bgfx/platform.h"
#include "bx/bx.h"
#include "bx/constants.h"
#include "bx/filepath.h"
@@ -343,7 +344,7 @@ namespace Game
bgfx::Init init;
init.type = bgfx::RendererType::Direct3D12;
#ifdef _DEBUG
// init.debug = true;
init.debug = true;
#endif
init.platformData.nwh = shared.Window.Handle;
init.platformData.ndt = nullptr;
@@ -375,7 +376,7 @@ namespace Game
Textures[0].SamplerHandle = DefaultSampler;
LoadModels(Models, ModelCount);
Materials[0] =
Materials[(uint16_t)EMaterial::Default] =
Material::LoadFromShader("vert", "frag", MainViewID, Textures[0].Handle, Textures[0].SamplerHandle);
imguiCreate();
SetImguiStyle();
@@ -448,7 +449,7 @@ namespace Game
bgfx::ProgramHandle newProgram = bgfx::createProgram(vertexShader, fragmentShader, true);
if (isValid(newProgram))
{
Materials[0].Shader = newProgram;
Materials[(uint16_t)EMaterial::Default].Shader = newProgram;
LastShaderLoadTime = GetInstance().Time.Now;
}
else
@@ -473,6 +474,22 @@ namespace Game
debug.DebugCardRotation++;
if (debug.DebugCardRotation >= 4) debug.DebugCardRotation = 0;
}
if (ImGui::Begin("Log"))
{
for (int32_t i = 0; i < bx::min(100, LogInternal::LogHistorySize); ++i)
{
int32_t idx = GetLogHistory().WriteIdx - i - 1;
if (idx < 0) idx += LogInternal::LogHistorySize;
const char* line = &GetLogHistory().LogBuffer[idx * LogInternal::MaxLineSize];
if (line[0] != 0)
{
int64_t timeOffset = GetLogHistory().WriteTime[idx] - GetInstance().Time.StartTime;
double writeTime = (double)timeOffset / bx::getHPFrequency();
ImGui::Text("%.04f: %s", writeTime, line);
}
}
}
ImGui::End();
if (ImGui::Begin("Rendering"))
{
if (LastShaderLoadTime >= 0.0f)
@@ -741,6 +758,7 @@ namespace Game
return Models[i].Handle;
}
}
LOG_WARN("Failed to find model for path %s", path);
return {};
}

View File

@@ -88,6 +88,13 @@ namespace Game
ImTextureID PreviewID = 0;
};
enum class EMaterial : uint16_t
{
Default = 0,
UI = 1,
UNDEFINED = UINT16_MAX
};
class GameRendering
{
public: