Quick dirty implementation of a world grid+viewport+iterator.
This commit makes the program quit immediately. Prints debug info about iterator. Incomplete, no actual tile being iterated.
This commit is contained in:
parent
92a1069324
commit
b5a6b9f9bc
|
@ -55,7 +55,10 @@ add_executable(${PROJECT_NAME}
|
|||
src/sizenotifiable.cpp
|
||||
src/sizeratio.cpp
|
||||
src/gamescenebase.cpp
|
||||
src/worldgrid.cpp
|
||||
src/worldviewport.cpp
|
||||
src/inputbag.cpp
|
||||
src/tileiterator.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} SYSTEM
|
||||
|
|
|
@ -1,19 +1,27 @@
|
|||
#include "ingamescene.hpp"
|
||||
#include "sdlmain.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace curry {
|
||||
IngameScene::IngameScene (cloonel::SDLMain* parSDLMain) :
|
||||
GameSceneBase(parSDLMain)
|
||||
GameSceneBase(parSDLMain),
|
||||
m_world(vec2us(64), vec2us(100)),
|
||||
m_viewport(&m_world, vec2i(parSDLMain->WidthHeight()))
|
||||
{
|
||||
m_viewport.set_position(vec2i(38));
|
||||
}
|
||||
|
||||
IngameScene::~IngameScene() noexcept = default;
|
||||
|
||||
void IngameScene::on_prepare() {
|
||||
std::cout << "game prepare\n";
|
||||
|
||||
for (auto& tile : m_viewport) {
|
||||
}
|
||||
}
|
||||
|
||||
void IngameScene::on_update() {
|
||||
set_wants_to_quit();
|
||||
std::cout << "game exec\n";
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include "gamescenebase.hpp"
|
||||
#include "worldgrid.hpp"
|
||||
#include "worldviewport.hpp"
|
||||
|
||||
namespace cloonel {
|
||||
class SDLMain;
|
||||
|
@ -16,5 +18,9 @@ namespace curry {
|
|||
virtual void on_prepare() override;
|
||||
virtual void on_destroy() noexcept override;
|
||||
virtual void on_update() override;
|
||||
|
||||
private:
|
||||
WorldGrid m_world;
|
||||
WorldViewport m_viewport;
|
||||
};
|
||||
} //namespace curry
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
#include "tileiterator.hpp"
|
||||
#include "worldviewport.hpp"
|
||||
#include "worldgrid.hpp"
|
||||
#include <cassert>
|
||||
#include <ciso646>
|
||||
|
||||
namespace curry {
|
||||
TileIterator::TileIterator (WorldViewport* parViewport, bool parEnd) :
|
||||
m_viewport(parViewport),
|
||||
m_index(parEnd ? -1 : 0)
|
||||
{
|
||||
assert(m_viewport);
|
||||
}
|
||||
|
||||
void TileIterator::increment() {
|
||||
assert(m_index != vec2i(-1));
|
||||
|
||||
const WorldGrid& world = *m_viewport->world();
|
||||
vec2i tile_size(world.tile_size());
|
||||
vec2i pos(tile_size * m_index - m_viewport->position());
|
||||
|
||||
if (pos.x() + tile_size.x() < m_viewport->size().x()) {
|
||||
++m_index.x();
|
||||
}
|
||||
else if (pos.y() + tile_size.y() < m_viewport->size().y()) {
|
||||
m_index.x() = 0;
|
||||
++m_index.y();
|
||||
}
|
||||
else {
|
||||
m_index = vec2i(-1);
|
||||
}
|
||||
std::cout << "got index " << m_index << " pixel " <<
|
||||
m_index * tile_size - m_viewport->position() << '\n';
|
||||
}
|
||||
|
||||
Tile& TileIterator::dereference() const {
|
||||
static int mic = 10;
|
||||
return mic;
|
||||
}
|
||||
|
||||
bool TileIterator::equal (const TileIterator& parOther) const {
|
||||
return m_viewport == parOther.m_viewport and
|
||||
m_index == parOther.m_index
|
||||
;
|
||||
}
|
||||
} //namespace curry
|
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include "vector.hpp"
|
||||
#include <boost/iterator/iterator_facade.hpp>
|
||||
#include <ciso646>
|
||||
|
||||
namespace curry {
|
||||
typedef int Tile;
|
||||
class WorldViewport;
|
||||
|
||||
class TileIterator : public boost::iterator_facade<TileIterator, Tile, boost::bidirectional_traversal_tag> {
|
||||
typedef boost::iterator_facade<TileIterator, Tile, boost::bidirectional_traversal_tag> base_class;
|
||||
friend class boost::iterator_core_access;
|
||||
public:
|
||||
TileIterator (WorldViewport* parViewport, bool parEnd);
|
||||
|
||||
private:
|
||||
Tile& dereference() const;
|
||||
void increment();
|
||||
bool equal (const TileIterator& parOther) const;
|
||||
|
||||
WorldViewport* m_viewport;
|
||||
vec2i m_index;
|
||||
};
|
||||
} //namespace curry
|
|
@ -3,40 +3,43 @@
|
|||
#include "vectorwrapper/vectorwrapper.hpp"
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#if !defined(NDEBUG)
|
||||
# include <iostream>
|
||||
#endif
|
||||
|
||||
#define SPECIALIZE_ARRAY_VECTOR(TYPE, DIM) \
|
||||
template <> \
|
||||
struct VectorWrapperInfo<std::array<TYPE, DIM>> { \
|
||||
enum { dimensions = DIM }; \
|
||||
typedef TYPE scalar_type; \
|
||||
typedef std::array<scalar_type, dimensions> vector_type; \
|
||||
static scalar_type& get_at (size_t parIndex, vector_type& parVector) { \
|
||||
return parVector[parIndex]; \
|
||||
} \
|
||||
}
|
||||
|
||||
namespace vwr {
|
||||
template <>
|
||||
struct VectorWrapperInfo<std::array<float, 2>> {
|
||||
enum {
|
||||
dimensions = 2
|
||||
};
|
||||
SPECIALIZE_ARRAY_VECTOR(float, 2);
|
||||
SPECIALIZE_ARRAY_VECTOR(uint16_t, 2);
|
||||
SPECIALIZE_ARRAY_VECTOR(int32_t, 2);
|
||||
|
||||
typedef float scalar_type;
|
||||
typedef std::array<scalar_type, 2> vector_type;
|
||||
|
||||
static scalar_type& get_at (size_t parIndex, vector_type& parVector) {
|
||||
return parVector[parIndex];
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct VectorWrapperInfo<std::array<uint16_t, 2>> {
|
||||
enum {
|
||||
dimensions = 2
|
||||
};
|
||||
|
||||
typedef uint16_t scalar_type;
|
||||
typedef std::array<scalar_type, 2> vector_type;
|
||||
|
||||
static scalar_type& get_at (size_t parIndex, vector_type& parVector) {
|
||||
return parVector[parIndex];
|
||||
}
|
||||
};
|
||||
#if !defined(NDEBUG)
|
||||
template <typename V>
|
||||
std::ostream& operator<< (std::ostream& parStream, const Vec<V>& parVec) {
|
||||
parStream << '<';
|
||||
for (std::size_t z = 0; z < Vec<V>::dimensions - 1; ++z) {
|
||||
std::cout << parVec[z] << ", ";
|
||||
}
|
||||
std::cout << parVec[Vec<V>::dimensions - 1] << '>';
|
||||
return parStream;
|
||||
}
|
||||
#endif
|
||||
} //namespace vwr
|
||||
|
||||
namespace curry {
|
||||
using vec2f = vwr::Vec<std::array<float, 2>>;
|
||||
using vec2us = vwr::Vec<std::array<uint16_t, 2>>;
|
||||
using vec2i = vwr::Vec<std::array<int32_t, 2>>;
|
||||
} //namespace curry
|
||||
|
||||
//make stuff from CloonelJump compile happily
|
||||
|
@ -44,3 +47,5 @@ namespace cloonel {
|
|||
using ushort2 = vwr::Vec<std::array<uint16_t, 2>>;
|
||||
using float2 = vwr::Vec<std::array<float, 2>>;
|
||||
} //namespace cloonel
|
||||
|
||||
#undef SPECIALIZE_ARRAY_VECTOR
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#include "worldgrid.hpp"
|
||||
|
||||
namespace curry {
|
||||
WorldGrid::WorldGrid (vec2us parTileSize, vec2us parWorldSize) :
|
||||
m_tile_size(parTileSize),
|
||||
m_world_size(parWorldSize)
|
||||
{
|
||||
}
|
||||
|
||||
const vec2us& WorldGrid::tile_size() const {
|
||||
return m_tile_size;
|
||||
}
|
||||
} //namespace curry
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include "vector.hpp"
|
||||
|
||||
namespace curry {
|
||||
class WorldGrid {
|
||||
public:
|
||||
WorldGrid (vec2us parTileSize, vec2us parWorldSize);
|
||||
|
||||
const vec2us& tile_size() const;
|
||||
|
||||
private:
|
||||
vec2us m_tile_size;
|
||||
vec2us m_world_size;
|
||||
};
|
||||
} //namespace curry
|
|
@ -0,0 +1,37 @@
|
|||
#include "worldviewport.hpp"
|
||||
#include "worldgrid.hpp"
|
||||
#include <cassert>
|
||||
|
||||
namespace curry {
|
||||
WorldViewport::WorldViewport (WorldGrid* parWorld, vec2i parSize) :
|
||||
m_position(0),
|
||||
m_size(parSize),
|
||||
m_world(parWorld)
|
||||
{
|
||||
assert(m_world);
|
||||
}
|
||||
|
||||
const WorldGrid* WorldViewport::world() const {
|
||||
return m_world;
|
||||
}
|
||||
|
||||
const vec2i& WorldViewport::position() const {
|
||||
return m_position;
|
||||
}
|
||||
|
||||
const vec2i& WorldViewport::size() const {
|
||||
return m_size;
|
||||
}
|
||||
|
||||
auto WorldViewport::begin() -> iterator {
|
||||
return iterator(this, false);
|
||||
}
|
||||
|
||||
auto WorldViewport::end() -> iterator {
|
||||
return iterator(this, true);
|
||||
}
|
||||
|
||||
void WorldViewport::set_position (const vec2i& parPos) {
|
||||
m_position = parPos; //TODO: assert validity
|
||||
}
|
||||
} //namespace curry
|
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include "vector.hpp"
|
||||
#include "tileiterator.hpp"
|
||||
|
||||
namespace curry {
|
||||
class WorldGrid;
|
||||
|
||||
class WorldViewport {
|
||||
public:
|
||||
typedef TileIterator iterator;
|
||||
|
||||
WorldViewport (WorldGrid* parWorld, vec2i parSize);
|
||||
const WorldGrid* world() const;
|
||||
const vec2i& position() const;
|
||||
const vec2i& size() const;
|
||||
|
||||
void set_position (const vec2i& parPos);
|
||||
|
||||
iterator begin();
|
||||
iterator end();
|
||||
|
||||
private:
|
||||
vec2i m_position;
|
||||
vec2i m_size;
|
||||
WorldGrid* m_world;
|
||||
};
|
||||
} //namespace curry
|
Loading…
Reference in New Issue