Changeset View
Changeset View
Standalone View
Standalone View
source/tools/atlas/GameInterface/Handlers/ElevationHandlers.cpp
/* Copyright (C) 2012 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 18 Lines | |||||
#include "maths/MathUtil.h" | #include "maths/MathUtil.h" | ||||
#include "graphics/RenderableObject.h" | #include "graphics/RenderableObject.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "simulation2/components/ICmpTerrain.h" | #include "simulation2/components/ICmpTerrain.h" | ||||
#include "../Brushes.h" | #include "../Brushes.h" | ||||
#include "../DeltaArray.h" | #include "../DeltaArray.h" | ||||
#include <limits> | |||||
namespace AtlasMessage { | namespace AtlasMessage { | ||||
class TerrainArray : public DeltaArray2D<u16> | class TerrainArray : public DeltaArray2D<u16> | ||||
{ | { | ||||
public: | public: | ||||
void Init() | void Init() | ||||
{ | { | ||||
m_Heightmap = g_Game->GetWorld()->GetTerrain()->GetHeightMap(); | m_Heightmap = g_Game->GetWorld()->GetTerrain()->GetHeightMap(); | ||||
m_VertsPerSide = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide(); | m_VertsPerSide = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide(); | ||||
} | } | ||||
void RaiseVertex(ssize_t x, ssize_t y, int amount) | void RaiseVertex(ssize_t x, ssize_t y, int amount) | ||||
{ | { | ||||
// Ignore out-of-bounds vertices | // Ignore out-of-bounds vertices | ||||
if (size_t(x) >= size_t(m_VertsPerSide) || size_t(y) >= size_t(m_VertsPerSide)) | if (size_t(x) >= size_t(m_VertsPerSide) || size_t(y) >= size_t(m_VertsPerSide)) | ||||
return; | return; | ||||
set(x,y, (u16)clamp(get(x,y) + amount, 0, 65535)); | set(x,y, static_cast<u16>(Clamp<int>(get(x,y) + amount, 0, std::numeric_limits<u16>::max()))); | ||||
Stan: static_cast | |||||
Not Done Inline Actionsnumeric_limit for that 2^16? elexis: numeric_limit for that 2^16? | |||||
Not Done Inline ActionsYeah would be nice. Same below. Stan: Yeah would be nice. Same below. | |||||
Done Inline ActionsIt'd be good if you test that the std::numeric_limits<u16>::max() is correct for you. vladislavbelov: It'd be good if you test that the `std::numeric_limits<u16>::max()` is correct for you. | |||||
Not Done Inline Actionsin source\lib\types.h typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; MSVC c:\Users\dolci\Desktop>cl.exe fine.cpp /EHsc c:\Users\dolci\Desktop>fine.exe std::numeric_limits<u16>::max(): 65535 MINGW c:\Users\dolci\Desktop>mingw32-g++ fine.cpp c:\Users\dolci\Desktop>a.exe std::numeric_limits<u16>::max(): 65535 Stan:
in source\lib\types.h
```lang=cpp
typedef uint8_t u8;
typedef uint16_t u16;
typedef… | |||||
Done Inline ActionsWhy not clamp<u16> ? Stan: Why not clamp<u16> ? | |||||
Done Inline ActionsBecause you clamping a number more than maximum possible value of u16. If we don't use a bigger type (like int) here, we get an integer overflow. vladislavbelov: Because you clamping a number more than maximum possible value of `u16`. If we don't use a… | |||||
} | } | ||||
void MoveVertexTowards(ssize_t x, ssize_t y, int target, int amount) | void MoveVertexTowards(ssize_t x, ssize_t y, int target, int amount) | ||||
{ | { | ||||
if (size_t(x) >= size_t(m_VertsPerSide) || size_t(y) >= size_t(m_VertsPerSide)) | if (size_t(x) >= size_t(m_VertsPerSide) || size_t(y) >= size_t(m_VertsPerSide)) | ||||
return; | return; | ||||
int h = get(x,y); | int h = get(x,y); | ||||
if (h < target) | if (h < target) | ||||
h = std::min(target, h + amount); | h = std::min(target, h + amount); | ||||
else if (h > target) | else if (h > target) | ||||
h = std::max(target, h - amount); | h = std::max(target, h - amount); | ||||
else | else | ||||
return; | return; | ||||
set(x,y, (u16)clamp(h, 0, 65535)); | set(x, y, static_cast<u16>(Clamp<int>(h, 0, std::numeric_limits<u16>::max()))); | ||||
Not Done Inline Actionsstatic_cast Stan: static_cast | |||||
Done Inline ActionsWhy not clamp<u16> ? Stan: Why not clamp<u16> ? | |||||
} | } | ||||
void SetVertex(ssize_t x, ssize_t y, u16 value) | void SetVertex(ssize_t x, ssize_t y, u16 value) | ||||
{ | { | ||||
if (size_t(x) >= size_t(m_VertsPerSide) || size_t(y) >= size_t(m_VertsPerSide)) | if (size_t(x) >= size_t(m_VertsPerSide) || size_t(y) >= size_t(m_VertsPerSide)) | ||||
return; | return; | ||||
set(x,y, value); | set(x,y, value); | ||||
} | } | ||||
u16 GetVertex(ssize_t x, ssize_t y) | u16 GetVertex(ssize_t x, ssize_t y) | ||||
{ | { | ||||
return get(clamp(x, ssize_t(0), ssize_t(m_VertsPerSide-1)), clamp(y, ssize_t(0), ssize_t(m_VertsPerSide-1))); | return get(Clamp(x, static_cast<ssize_t>(0), ssize_t(m_VertsPerSide - 1)), Clamp(y, static_cast<ssize_t>(0), ssize_t(m_VertsPerSide - 1))); | ||||
Not Done Inline Actionsstatic_cast Stan: static_cast | |||||
} | } | ||||
protected: | protected: | ||||
u16 getOld(ssize_t x, ssize_t y) | u16 getOld(ssize_t x, ssize_t y) | ||||
{ | { | ||||
return m_Heightmap[y*m_VertsPerSide + x]; | return m_Heightmap[y*m_VertsPerSide + x]; | ||||
} | } | ||||
void setNew(ssize_t x, ssize_t y, const u16& val) | void setNew(ssize_t x, ssize_t y, const u16& val) | ||||
▲ Show 20 Lines • Show All 319 Lines • ▼ Show 20 Lines | void Do() | ||||
{ | { | ||||
for (ssize_t dx = 0; dx < g_CurrentBrush.m_W; ++dx) | for (ssize_t dx = 0; dx < g_CurrentBrush.m_W; ++dx) | ||||
{ | { | ||||
float b = g_CurrentBrush.Get(dx, dy); | float b = g_CurrentBrush.Get(dx, dy); | ||||
if (b) | if (b) | ||||
{ | { | ||||
float x = (float)dx - ((float)g_CurrentBrush.m_H - 1) / 2.f; | float x = (float)dx - ((float)g_CurrentBrush.m_H - 1) / 2.f; | ||||
float y = (float)dy - ((float)g_CurrentBrush.m_W - 1) / 2.f; | float y = (float)dy - ((float)g_CurrentBrush.m_W - 1) / 2.f; | ||||
float distance = clamp(1 - (float)sqrt(x * x + y * y) / h, 0.01f, 1.0f); | float distance = Clamp(1.0f - sqrtf(x * x + y * y) / h, 0.01f, 1.0f); | ||||
Not Done Inline Actionsstatic_cast Stan: static_cast | |||||
distance *= distance; | distance *= distance; | ||||
m_TerrainDelta.RaiseVertex(x0 + dx, y0 + dy, (int)(amount * distance)); | m_TerrainDelta.RaiseVertex(x0 + dx, y0 + dy, (int)(amount * distance)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
m_i0 = x0 - 1; | m_i0 = x0 - 1; | ||||
m_j0 = y0 - 1; | m_j0 = y0 - 1; | ||||
m_i1 = x0 + g_CurrentBrush.m_W; | m_i1 = x0 + g_CurrentBrush.m_W; | ||||
Show All 28 Lines |
Wildfire Games · Phabricator
static_cast