mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-21 20:06:40 +00:00
Deduplicate BuildTextureRGBA functions
All of them contain the same loop.
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
#ifndef INCLUDED_TERRAINOVERLAY
|
#ifndef INCLUDED_TERRAINOVERLAY
|
||||||
#define INCLUDED_TERRAINOVERLAY
|
#define INCLUDED_TERRAINOVERLAY
|
||||||
|
|
||||||
|
#include "graphics/SColor.h"
|
||||||
#include "graphics/ShaderTechniquePtr.h"
|
#include "graphics/ShaderTechniquePtr.h"
|
||||||
#include "lib/code_annotation.h"
|
#include "lib/code_annotation.h"
|
||||||
#include "lib/posix/posix_types.h"
|
#include "lib/posix/posix_types.h"
|
||||||
@@ -212,6 +213,24 @@ public:
|
|||||||
*/
|
*/
|
||||||
static SColor4ub GetColor(std::size_t idx, std::uint8_t alpha);
|
static SColor4ub GetColor(std::size_t idx, std::uint8_t alpha);
|
||||||
|
|
||||||
|
static void OverwriteEachTile(std::uint8_t* data, const std::size_t w, const std::size_t h,
|
||||||
|
const auto generate)
|
||||||
|
{
|
||||||
|
static_assert(std::is_invocable_r_v<SColor4ub, decltype(generate), int, int>);
|
||||||
|
|
||||||
|
for (int i{0}; i != static_cast<int>(h); ++i)
|
||||||
|
{
|
||||||
|
for (int j{0}; j != static_cast<int>(w); ++j)
|
||||||
|
{
|
||||||
|
SColor4ub color{generate(i, j)};
|
||||||
|
*data++ = color.R;
|
||||||
|
*data++ = color.G;
|
||||||
|
*data++ = color.B;
|
||||||
|
*data++ = color.A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void RenderAfterWater(
|
void RenderAfterWater(
|
||||||
Renderer::Backend::IDeviceCommandContext* deviceCommandContext, int cullGroup) final;
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext, int cullGroup) final;
|
||||||
|
|||||||
@@ -78,28 +78,18 @@ REGISTER_COMPONENT_TYPE(Pathfinder)
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
void BuildTextureRGBA(const CCmpPathfinder& pathfinder, std::uint8_t* data,
|
void BuildTextureRGBA(const CCmpPathfinder& pathfinder, std::uint8_t* data, std::size_t w, std::size_t h)
|
||||||
std::size_t w, std::size_t h)
|
|
||||||
{
|
{
|
||||||
// Render navcell passability, based on the terrain-only grid
|
// Render navcell passability, based on the terrain-only grid
|
||||||
u8* p = data;
|
TerrainTextureOverlay::OverwriteEachTile(data, w, h, [&](const int i, const int j)
|
||||||
for (size_t j = 0; j < h; ++j)
|
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < w; ++i)
|
if (!IS_PASSABLE(pathfinder.m_TerrainOnlyGrid->get(j, i),
|
||||||
|
pathfinder.m_OverlayPassClass))
|
||||||
{
|
{
|
||||||
SColor4ub color(0, 0, 0, 0);
|
return SColor4ub{255, 0, 0, 127};
|
||||||
if (!IS_PASSABLE(pathfinder.m_TerrainOnlyGrid->get(static_cast<int>(i),
|
|
||||||
static_cast<int>(j)), pathfinder.m_OverlayPassClass))
|
|
||||||
{
|
|
||||||
color = SColor4ub(255, 0, 0, 127);
|
|
||||||
}
|
|
||||||
|
|
||||||
*p++ = color.R;
|
|
||||||
*p++ = color.G;
|
|
||||||
*p++ = color.B;
|
|
||||||
*p++ = color.A;
|
|
||||||
}
|
}
|
||||||
}
|
return SColor4ub{0, 0, 0, 0};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1091,3 +1081,4 @@ ICmpObstruction::EFoundationCheck CCmpPathfinder::CheckBuildingPlacement(const I
|
|||||||
|
|
||||||
return ICmpObstruction::FOUNDATION_CHECK_SUCCESS;
|
return ICmpObstruction::FOUNDATION_CHECK_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,21 +77,14 @@ namespace
|
|||||||
{
|
{
|
||||||
constexpr bool DISABLE_TERRITORY_OVERLAY{true};
|
constexpr bool DISABLE_TERRITORY_OVERLAY{true};
|
||||||
|
|
||||||
void BuildTextureRGBA(Grid<u8>*& territories, u8* data, size_t w, size_t h)
|
void BuildTextureRGBA(Grid<u8>*& territories, std::uint8_t* data, const int w, const int h)
|
||||||
{
|
{
|
||||||
for (size_t j = 0; j < h; ++j)
|
TerrainTextureOverlay::OverwriteEachTile(data, w, h, [&](const int i, const int j)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < w; ++i)
|
const u8 id{static_cast<std::uint8_t>(territories->get(j, i) &
|
||||||
{
|
ICmpTerritoryManager::TERRITORY_PLAYER_MASK)};
|
||||||
SColor4ub color;
|
return TerrainTextureOverlay::GetColor(id, 64);
|
||||||
u8 id = (territories->get((int)i, (int)j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK);
|
});
|
||||||
color = TerrainTextureOverlay::GetColor(id, 64);
|
|
||||||
*data++ = color.R;
|
|
||||||
*data++ = color.G;
|
|
||||||
*data++ = color.B;
|
|
||||||
*data++ = color.A;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +157,7 @@ public:
|
|||||||
m_DebugOverlay = DISABLE_TERRITORY_OVERLAY? nullptr :
|
m_DebugOverlay = DISABLE_TERRITORY_OVERLAY? nullptr :
|
||||||
new TerrainTextureOverlay{static_cast<float>(Pathfinding::NAVCELLS_PER_TERRAIN_TILE) /
|
new TerrainTextureOverlay{static_cast<float>(Pathfinding::NAVCELLS_PER_TERRAIN_TILE) /
|
||||||
ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE,
|
ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE,
|
||||||
std::bind_front(BuildTextureRGBA, std::ref(this->m_Territories))};
|
std::bind_front(BuildTextureRGBA, std::ref(m_Territories))};
|
||||||
m_BoundaryLinesDirty = true;
|
m_BoundaryLinesDirty = true;
|
||||||
m_TriggerEvent = true;
|
m_TriggerEvent = true;
|
||||||
m_EnableLineDebugOverlays = false;
|
m_EnableLineDebugOverlays = false;
|
||||||
|
|||||||
@@ -53,30 +53,18 @@ void BuildTextureRGBA(HierarchicalPathfinder& pathfinderHier, std::uint8_t* data
|
|||||||
std::size_t h)
|
std::size_t h)
|
||||||
{
|
{
|
||||||
ENSURE(h <= std::numeric_limits<u16>::max() && w <= std::numeric_limits<u16>::max());
|
ENSURE(h <= std::numeric_limits<u16>::max() && w <= std::numeric_limits<u16>::max());
|
||||||
u16 height = static_cast<u16>(h);
|
|
||||||
u16 width = static_cast<u16>(w);
|
|
||||||
pass_class_t passClass = pathfinderHier.GetPassabilityClass("default");
|
pass_class_t passClass = pathfinderHier.GetPassabilityClass("default");
|
||||||
|
|
||||||
for (u16 j = 0; j < height; ++j)
|
TerrainTextureOverlay::OverwriteEachTile(data, w, h, [&](const int i, const int j)
|
||||||
{
|
{
|
||||||
for (u16 i = 0; i < width; ++i)
|
HierarchicalPathfinder::RegionID rid = pathfinderHier.Get(static_cast<std::uint16_t>(j),
|
||||||
{
|
static_cast<std::uint16_t>(i), passClass);
|
||||||
SColor4ub color;
|
if (rid.r == 0)
|
||||||
|
return SColor4ub{0, 0, 0, 0};
|
||||||
HierarchicalPathfinder::RegionID rid = pathfinderHier.Get(i, j, passClass);
|
if (rid.r == 0xFFFF)
|
||||||
if (rid.r == 0)
|
return SColor4ub{255, 0, 255, 255};
|
||||||
color = SColor4ub(0, 0, 0, 0);
|
return TerrainTextureOverlay::GetColor(rid.r + rid.ci*5 + rid.cj*7, 127);
|
||||||
else if (rid.r == 0xFFFF)
|
});
|
||||||
color = SColor4ub(255, 0, 255, 255);
|
|
||||||
else
|
|
||||||
color = TerrainTextureOverlay::GetColor(rid.r + rid.ci*5 + rid.cj*7, 127);
|
|
||||||
|
|
||||||
*data++ = color.R;
|
|
||||||
*data++ = color.G;
|
|
||||||
*data++ = color.B;
|
|
||||||
*data++ = color.A;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,37 +48,23 @@ void BuildTextureRGBA(LongPathfinder& pathfinder, std::uint8_t* data, std::size_
|
|||||||
pathfinder.GetDebugData(steps, time, debugGrid);
|
pathfinder.GetDebugData(steps, time, debugGrid);
|
||||||
|
|
||||||
// Render navcell passability
|
// Render navcell passability
|
||||||
u8* p = data;
|
TerrainTextureOverlay::OverwriteEachTile(data, w, h, [&](const int i, const int j)
|
||||||
for (size_t j = 0; j < h; ++j)
|
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < w; ++i)
|
if (debugGrid.m_W && debugGrid.m_H)
|
||||||
{
|
{
|
||||||
SColor4ub color(0, 0, 0, 0);
|
if (pathfinder.m_Debug.Goal.NavcellContainsGoal(i, j))
|
||||||
if (!IS_PASSABLE(pathfinder.m_Grid->get(static_cast<int>(i), static_cast<int>(j)),
|
return SColor4ub(0, 0, 255, 127);
|
||||||
pathfinder.m_Debug.PassClass))
|
|
||||||
{
|
|
||||||
color = SColor4ub(255, 0, 0, 127);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (debugGrid.m_W && debugGrid.m_H)
|
const u8 n{debugGrid.get(i, j)};
|
||||||
{
|
if (n == 1)
|
||||||
u8 n = debugGrid.get((int)i, (int)j);
|
return SColor4ub(255, 255, 0, 127);
|
||||||
|
if (n == 2)
|
||||||
if (n == 1)
|
return SColor4ub(0, 255, 0, 127);
|
||||||
color = SColor4ub(255, 255, 0, 127);
|
|
||||||
else if (n == 2)
|
|
||||||
color = SColor4ub(0, 255, 0, 127);
|
|
||||||
|
|
||||||
if (pathfinder.m_Debug.Goal.NavcellContainsGoal(i, j))
|
|
||||||
color = SColor4ub(0, 0, 255, 127);
|
|
||||||
}
|
|
||||||
|
|
||||||
*p++ = color.R;
|
|
||||||
*p++ = color.G;
|
|
||||||
*p++ = color.B;
|
|
||||||
*p++ = color.A;
|
|
||||||
}
|
}
|
||||||
}
|
if (!IS_PASSABLE(pathfinder.m_Grid->get(j, i), pathfinder.m_Debug.PassClass))
|
||||||
|
return SColor4ub(255, 0, 0, 127);
|
||||||
|
return SColor4ub{0, 0, 0, 0};
|
||||||
|
});
|
||||||
|
|
||||||
// Render the most recently generated path
|
// Render the most recently generated path
|
||||||
if (pathfinder.m_Debug.Path && !pathfinder.m_Debug.Path->m_Waypoints.empty())
|
if (pathfinder.m_Debug.Path && !pathfinder.m_Debug.Path->m_Waypoints.empty())
|
||||||
|
|||||||
Reference in New Issue
Block a user