Add tilecoords and unit test.
Disable most projects because the build is broken. A lot.
This commit is contained in:
parent
5b027914d5
commit
6aa4d15381
6 changed files with 165 additions and 4 deletions
|
@ -26,6 +26,8 @@ target_include_directories(${PROJECT_NAME}
|
|||
|
||||
#add_subdirectory(lib/sprout)
|
||||
add_subdirectory(src)
|
||||
add_subdirectory(test)
|
||||
add_subdirectory(game)
|
||||
add_subdirectory(tools/mapconv)
|
||||
#add_subdirectory(test)
|
||||
#add_subdirectory(game)
|
||||
#add_subdirectory(tools/mapconv)
|
||||
add_subdirectory(test/gtest-1.7.0)
|
||||
add_subdirectory(test/unit)
|
||||
|
|
38
include/doorkeeper/components/tilecoords.hpp
Normal file
38
include/doorkeeper/components/tilecoords.hpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
#ifndef id753CAC41D3AE48A1A1D1EC399FD3DADF
|
||||
#define id753CAC41D3AE48A1A1D1EC399FD3DADF
|
||||
|
||||
#include "doorkeeper/primitivetypes.hpp"
|
||||
#include "doorkeeper/implem/vector.hpp"
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
|
||||
namespace dk {
|
||||
template <uint32_t D>
|
||||
class TileCoords {
|
||||
public:
|
||||
typedef Vector<D> coords;
|
||||
|
||||
TileCoords ( void ) = default;
|
||||
TileCoords ( const TileCoords& ) = default;
|
||||
explicit TileCoords ( const coords& parSize );
|
||||
TileCoords ( const coords& parValue, const coords& parSize );
|
||||
|
||||
TileCoords& operator++ ( void ); //pre
|
||||
TileCoords operator++ ( int ); //post
|
||||
TileCoords& operator-- ( void ); //pre
|
||||
TileCoords operator-- ( int ); //post
|
||||
const TileCoords& operator+= ( CoordinateScalarType parValue );
|
||||
const TileCoords& operator-= ( CoordinateScalarType parValue );
|
||||
bool operator== ( const TileCoords& parOther ) const;
|
||||
bool operator!= ( const TileCoords& parOther ) const;
|
||||
TileCoords& operator= ( const TileCoords& ) = default;
|
||||
|
||||
private:
|
||||
coords m_pos;
|
||||
coords m_size;
|
||||
};
|
||||
} //namespace dk
|
||||
|
||||
#include "doorkeeper/implem/tilecoords.inl"
|
||||
|
||||
#endif
|
|
@ -10,7 +10,7 @@
|
|||
#if !defined(DK_COORD_SCALAR_TYPE)
|
||||
/* this type represent tiles' coordinates, so it should be an integer type */
|
||||
/* so it's not the tile position in your game world */
|
||||
# define DK_COORD_SCALAR_TYPE int
|
||||
# define DK_COORD_SCALAR_TYPE int32_t
|
||||
#endif
|
||||
#if !defined(DK_COORD_DISTANCE_TYPE)
|
||||
# define DK_COORD_DISTANCE_TYPE int64_t
|
||||
|
|
81
include/doorkeeper/implem/tilecoords.inl
Normal file
81
include/doorkeeper/implem/tilecoords.inl
Normal file
|
@ -0,0 +1,81 @@
|
|||
namespace dk {
|
||||
template <uint32_t D>
|
||||
TileCoords<D>::TileCoords (const coords& parSize) :
|
||||
m_pos(CoordinateScalarType(0)),
|
||||
m_size(parSize)
|
||||
{
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TileCoords<D>::TileCoords (const coords& parValue, const coords& parSize) :
|
||||
m_pos(parValue),
|
||||
m_size(parSize)
|
||||
{
|
||||
assert(parValue < parSize);
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TileCoords<D>& TileCoords<D>::operator++ () {
|
||||
const coords lower(0);
|
||||
for (CoordinateDistType d = 0; d < D - 1; ++d) {
|
||||
++m_pos[d];
|
||||
if (m_pos[d] >= m_size[d])
|
||||
m_pos[d] = lower[d];
|
||||
else
|
||||
return *this;
|
||||
}
|
||||
++m_pos[D - 1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TileCoords<D> TileCoords<D>::operator++ (int) {
|
||||
TileCoords<D> retval(*this);
|
||||
++(*this);
|
||||
return retval;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TileCoords<D>& TileCoords<D>::operator-- () {
|
||||
const coords lower(0);
|
||||
for (CoordinateDistType d = 0; d < D; ++d) {
|
||||
if (m_pos[d] > lower[d]) {
|
||||
--m_pos[d];
|
||||
return;
|
||||
}
|
||||
else {
|
||||
m_pos[d] = m_size[d];
|
||||
}
|
||||
}
|
||||
++m_pos[D - 1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TileCoords<D> TileCoords<D>::operator-- (int) {
|
||||
TileCoords<D> retval(*this);
|
||||
--(*this);
|
||||
return retval;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
const TileCoords<D>& TileCoords<D>::operator+= (CoordinateScalarType parValue) {
|
||||
CoordinateScalarType acc(parValue);
|
||||
CoordinateScalarType div(1);
|
||||
for (uint32_t z = 0; z < D; ++z) {
|
||||
m_pos[z] = (m_pos[z] + acc / div) % m_size[z];
|
||||
div *= m_size[z];
|
||||
acc += m_pos[z];
|
||||
}
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
bool TileCoords<D>::operator== (const TileCoords& parOther) const {
|
||||
return m_pos == parOther.m_pos and m_size == parOther.m_size;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
bool TileCoords<D>::operator!= (const TileCoords& parOther) const {
|
||||
return not(*this == parOther);
|
||||
}
|
||||
} //namespace dk
|
19
test/unit/CMakeLists.txt
Normal file
19
test/unit/CMakeLists.txt
Normal file
|
@ -0,0 +1,19 @@
|
|||
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
|
||||
project(dkunit CXX)
|
||||
|
||||
include_directories(
|
||||
.
|
||||
)
|
||||
include_directories(SYSTEM
|
||||
../gtest-1.7.0/include
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../gtest-1.7.0/src/gtest_main.cc"
|
||||
tilecoords.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
doorkeeper
|
||||
gtest
|
||||
)
|
21
test/unit/tilecoords.cpp
Normal file
21
test/unit/tilecoords.cpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include "doorkeeper/components/tilecoords.hpp"
|
||||
|
||||
TEST(increment, tilecoords) {
|
||||
typedef dk::TileCoords<2> tcoords2;
|
||||
typedef dk::TileCoords<2>::coords coords2;
|
||||
|
||||
{
|
||||
const coords2 max_coords(150, 200);
|
||||
tcoords2 test(max_coords);
|
||||
EXPECT_EQ(tcoords2(coords2(0), max_coords), test);
|
||||
|
||||
for (auto z = coords2(0).x(); z < max_coords.x(); ++z) {
|
||||
EXPECT_EQ(tcoords2(coords2(z, 0), max_coords), test);
|
||||
++test;
|
||||
}
|
||||
auto test_old = test++;
|
||||
EXPECT_EQ(tcoords2(coords2(1, 1), max_coords), test);
|
||||
EXPECT_EQ(tcoords2(coords2(0, 1), max_coords), test_old);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue