From d59aa56318da2ecc516fe853c877ce1af26b6e6f Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 8 Apr 2023 23:40:52 -0500 Subject: [PATCH] 3D and 2D sorting settled. --- Faceball2030/Editor.cpp | 11 +++++++++++ Faceball2030/Editor.h | 1 + Faceball2030/main.cpp | 9 +-------- Faceball2030/pixelGameEngine.h | 20 +++++++++++++++++++- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Faceball2030/Editor.cpp b/Faceball2030/Editor.cpp index b44503f..f410cfa 100644 --- a/Faceball2030/Editor.cpp +++ b/Faceball2030/Editor.cpp @@ -7,6 +7,17 @@ extern FaceBall* game; void Editor::Update(float fElapsedTime){ LoadLevelHandling(); + vf2d center = { -(float)MAP_SIZE.x / 2 * GRID_SIZE.x + game->ScreenWidth()/2 , -(float)MAP_SIZE.y / 2 * GRID_SIZE.y + game->ScreenHeight()/2 }; + for (int y = 0; y < MAP_SIZE.y; y++) { + for (int x = 0; x < MAP_SIZE.x; x++) { + vf2d squarePos = vf2d{ (float)x,(float)y }*GRID_SIZE + center; + if (game->GetMouseX() >= squarePos.x && game->GetMouseX() <= squarePos.x + GRID_SIZE.x && + game->GetMouseY() >= squarePos.y && game->GetMouseY() <= squarePos.y + GRID_SIZE.y) { + game->FillRectDecal(squarePos, GRID_SIZE, { 0,0,255,64 }); + } + game->DrawRectDecal(squarePos,GRID_SIZE); + } + } } void Editor::LoadLevelHandling() { diff --git a/Faceball2030/Editor.h b/Faceball2030/Editor.h index 5d5200c..b214bc3 100644 --- a/Faceball2030/Editor.h +++ b/Faceball2030/Editor.h @@ -47,6 +47,7 @@ class Editor { vi2d MAP_SIZE; bool reEnableTextEntry; //We must set this to true to cause Text Entry to reappear due to how the callback works. std::vector> map; + const vi2d GRID_SIZE = { 32,32 }; public: Editor() {} void Update(float fElapsedTime); diff --git a/Faceball2030/main.cpp b/Faceball2030/main.cpp index 633ce46..8977741 100644 --- a/Faceball2030/main.cpp +++ b/Faceball2030/main.cpp @@ -452,6 +452,7 @@ void FaceBall::RenderWorld() { //std::sort(vecTrianglesToRaster.begin(),vecTrianglesToRaster.end(),[](triangle&t1,triangle&t2){return (t1.p[0].z+t1.p[1].z+t1.p[2].z)/3.0f>(t2.p[0].z+t2.p[1].z+t2.p[2].z)/3.0f;}); ClearBuffer(BLACK, true); int triRenderCount = 0; + for (auto& triToRaster : vecTrianglesToRaster) { Triangle clipped[2]; @@ -491,8 +492,6 @@ void FaceBall::RenderWorld() { nNewTriangles = listTriangles.size(); } - std::mapcoords; - for (auto& t : listTriangles) { // Rasterize triangle SetDecalStructure(DecalStructure::LIST); @@ -519,12 +518,6 @@ void FaceBall::RenderWorld() { SetDecalStructure(DecalStructure::FAN); triRenderCount++; } - - for (const auto& key : coords) { - vf2d coord = key.first; - vf2d val = key.second; - DrawStringDecal({ coord.x,coord.y }, val.str()); - } } SetDecalMode(DecalMode::NORMAL); diff --git a/Faceball2030/pixelGameEngine.h b/Faceball2030/pixelGameEngine.h index f19c135..1b1e0e6 100644 --- a/Faceball2030/pixelGameEngine.h +++ b/Faceball2030/pixelGameEngine.h @@ -865,6 +865,7 @@ namespace olc olc::DecalMode mode = olc::DecalMode::NORMAL; olc::DecalStructure structure = olc::DecalStructure::FAN; uint32_t points = 0; + bool depthEnabled = false; }; struct LayerDesc @@ -1090,6 +1091,7 @@ namespace olc // Draws a multiline string as a decal, with tiniting and scaling void DrawStringDecal(const olc::vf2d& pos, const std::string& sText, const Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f }); void DrawStringPropDecal(const olc::vf2d& pos, const std::string& sText, const Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f }); + void DrawRectDecal(const olc::vf2d& pos, const olc::vf2d& size, const olc::Pixel col = olc::WHITE); // Draws a single shaded filled rectangle as a decal void FillRectDecal(const olc::vf2d& pos, const olc::vf2d& size, const olc::Pixel col = olc::WHITE); // Draws a corner shaded rectangle as a decal @@ -1169,6 +1171,7 @@ namespace olc std::string sAppName; private: // Inner mysterious workings + bool depthEnabled = false; olc::Sprite* pDrawTarget = nullptr; Pixel::Mode nPixelMode = Pixel::NORMAL; float fBlendFactor = 1.0f; @@ -2698,6 +2701,7 @@ namespace olc di.pos.resize(elements); di.uv.resize(elements); di.w.resize(elements); + di.z.resize(elements); di.tint.resize(elements); di.points = elements; for (uint32_t i = 0; i < elements; i++) @@ -2756,6 +2760,7 @@ namespace olc } di.mode = nDecalMode; di.structure = nDecalStructure; + di.depthEnabled = true; vLayers[nTargetLayer].vecDecalInstance.push_back(di); } @@ -2849,7 +2854,18 @@ namespace olc di.mode = olc::DecalMode::WIREFRAME; vLayers[nTargetLayer].vecDecalInstance.push_back(di); } + void PixelGameEngine::DrawRectDecal(const olc::vf2d& pos, const olc::vf2d& size, const olc::Pixel col) + { + auto m = nDecalMode; + SetDecalMode(olc::DecalMode::WIREFRAME); + olc::vf2d vNewSize = size;// (size - olc::vf2d(0.375f, 0.375f)).ceil(); + std::array points = { { {pos}, {pos.x, pos.y + vNewSize.y}, {pos + vNewSize}, {pos.x + vNewSize.x, pos.y} } }; + std::array uvs = { {{0,0},{0,0},{0,0},{0,0}} }; + std::array cols = { {col, col, col, col} }; + DrawExplicitDecal(nullptr, points.data(), uvs.data(), cols.data(), 4); + SetDecalMode(m); + } void PixelGameEngine::FillRectDecal(const olc::vf2d& pos, const olc::vf2d& size, const olc::Pixel col) { olc::vf2d vNewSize = (size - olc::vf2d(0.375f, 0.375f)).ceil(); @@ -4164,7 +4180,9 @@ namespace olc } else { - glEnable(GL_DEPTH_TEST); + if (decal.depthEnabled) { + glEnable(GL_DEPTH_TEST); + } glDepthFunc(GL_LESS); if (nDecalMode == DecalMode::WIREFRAME) glBegin(GL_LINE_LOOP);