Skip to content

Commit

Permalink
factorize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Krozark committed Apr 16, 2015
1 parent b4d16d6 commit fa13630
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 303 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ Only one parameter as been change in the json file to change the grid geometry:
"img" :"media/img/ground2.png", //texture to apply
"x" : 0, //start point x
"y" : 0, //start point y
"width" : 100, //number of lines
"height" : 100 //number of collumns
"width" : 100, //number of lines (default is 1)
"height" : 100 //number of collumns (default is 1)
}
//... other textures can be specify if needed
]
Expand Down
2 changes: 1 addition & 1 deletion examples/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name" : "test",
"geometry" :
{
"name" :"SquareIso",
"name" :"SquareStaggered",
"size" : 50.0
},
"layers" :
Expand Down
12 changes: 10 additions & 2 deletions include/SFML-utils/map/Map.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ namespace sfutils
int tex_x = texture["x"].as_int();
int tex_y = texture["y"].as_int();
int height = std::max<int>(0,texture["height"].as_int());
int width = std::max<int>(0,texture["width"].as_int());
int height = 1;
try {
height = std::max<int>(0,texture["height"].as_int());
} catch(...){}

int width = 1;
try {
width = std::max<int>(0,texture["width"].as_int());
}catch (...){}

std::string img = texture["img"];

sf::Texture& tex = _textures.getOrLoad(img,img);
Expand Down
120 changes: 120 additions & 0 deletions include/SFML-utils/map/tileShapes/BaseTile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef SFUTILS_GEOMETRY_BASETILE_HPP
#define SFUTILS_GEOMETRY_BASETILE_HPP

#include <SFML/Graphics.hpp>
#include <cmath>

namespace sfutils
{
namespace geometry
{
template<typename T>
struct BaseTile
{
static const sf::ConvexShape& getShape() {return T::_shape;};
protected:
static sf::ConvexShape _shape;
};

#define SFML_UTILS_BASE_TILE_INIT(klass) \
template <> sf::ConvexShape BaseTile<klass>::_shape = sf::ConvexShape();

template<typename T>
struct _BaseSquare : public BaseTile<T>
{
static sf::Vector2i round(float x,float y)
{
if(x>=0)
x+=0.5;
else
x-=0.5;

if(y>=0)
y+=0.5;
else
y-=0.5;

return sf::Vector2i(x,y);
}


static sf::IntRect getTextureRect(int x,int y,float scale)
{
sf::Vector2f pos = T::mapCoordsToPixel(x,y,scale);
sf::IntRect res(pos.x,
pos.y,
T::width * scale,
T::height * scale);
return res;
}

static int distance(int x1,int y1, int x2,int y2)
{
float x = x1 - x2;
x = x*x;

float y = y1 - y2;
y = y*y;

return std::ceil(sqrt(x + y));
}
};

template<typename T>
struct BaseSquare : public _BaseSquare<T>
{
static constexpr float height = 2;
static constexpr float width = height;

protected:
static void init()
{
T::_shape.setPointCount(4);
T::_shape.setPoint(0,sf::Vector2f(0,0));
T::_shape.setPoint(1,sf::Vector2f(0,T::height));
T::_shape.setPoint(2,sf::Vector2f(T::height,T::height));
T::_shape.setPoint(3,sf::Vector2f(T::height,0));

T::_shape.setOrigin(T::height/2.0,T::height/2.0);
}

static struct _Initiatiser {
_Initiatiser(){
T::init();
};
} _initiatiser_;
};
#define SFML_UTILS_BASE_SQUARE_INIT(klass) \
SFML_UTILS_BASE_TILE_INIT(klass);\
template <> BaseSquare<klass>::_Initiatiser BaseSquare<klass>::_initiatiser_ = BaseSquare<klass>::_Initiatiser();

template<typename T>
struct BaseSquareIso : public _BaseSquare<T>
{
static constexpr float height = 1;
static constexpr float width = 2;

protected:
static void init()
{
T::_shape.setPointCount(4);
T::_shape.setPoint(0,sf::Vector2f(T::width/2,0));
T::_shape.setPoint(1,sf::Vector2f(T::width,T::height/2.0));
T::_shape.setPoint(2,sf::Vector2f(T::width/2.0,T::height));
T::_shape.setPoint(3,sf::Vector2f(0,T::height/2.0));

T::_shape.setOrigin(T::width/2.0,T::height/2.0);
}
static struct _Initiatiser {
_Initiatiser(){
T::init();
};
} _initiatiser_;
};

#define SFML_UTILS_BASE_SQUARE_ISO_INIT(klass) \
SFML_UTILS_BASE_TILE_INIT(klass);\
template <> BaseSquareIso<klass>::_Initiatiser BaseSquareIso<klass>::_initiatiser_ = BaseSquareIso<klass>::_Initiatiser();
}
}
#endif
19 changes: 2 additions & 17 deletions include/SFML-utils/map/tileShapes/Square.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
#ifndef SFUTILS_GEOMETRY_SQUARE_HPP
#define SFUTILS_GEOMETRY_SQUARE_HPP

#include <SFML/Graphics.hpp>
#include <SFML-utils/map/tileShapes/BaseTile.hpp>

namespace sfutils
{
namespace geometry
{
struct Square
struct Square : BaseSquare<Square>
{
static const sf::ConvexShape& getShape();
static sf::Vector2f mapCoordsToPixel(int x,int y,float scale);
static sf::Vector2i mapPixelToCoords(float x,float y,float scale);
static sf::IntRect getTextureRect(int x,int y,float scale);
static sf::Vector2i round(float x,float y);
static int distance(int x1,int y1, int x2,int y2);


private:
static sf::ConvexShape _shape;
static void init();

static struct __Initiatiser {
__Initiatiser(){
Square::init();
};
} __initiatiser__;
};
}
}
Expand Down
18 changes: 2 additions & 16 deletions include/SFML-utils/map/tileShapes/SquareIso.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
#ifndef SFUTILS_GEOMETRY_SQUAREISO_HPP
#define SFUTILS_GEOMETRY_SQUAREISO_HPP

#include <SFML/Graphics.hpp>
#include <SFML-utils/map/tileShapes/BaseTile.hpp>

namespace sfutils
{
namespace geometry
{
struct SquareIso
struct SquareIso : public BaseSquareIso<SquareIso>
{
static const sf::ConvexShape& getShape();
static sf::Vector2f mapCoordsToPixel(int x,int y,float scale);
static sf::Vector2i mapPixelToCoords(float x,float y,float scale);
static sf::IntRect getTextureRect(int x,int y,float scale);
static sf::Vector2i round(float x,float y);
static int distance(int x1,int y1, int x2,int y2);


private:
static sf::ConvexShape _shape;
static void init();

static struct __Initiatiser {
__Initiatiser(){
SquareIso::init();
};
} __initiatiser__;
};
}
}
Expand Down
19 changes: 3 additions & 16 deletions include/SFML-utils/map/tileShapes/SquareIsoStaggered.hpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
#ifndef SFUTILS_GEOMETRY_SQUAREISOSTAGGERED_HPP
#define SFUTILS_GEOMETRY_SQUAREISOSTAGGERED_HPP

#include <SFML/Graphics.hpp>
#include <SFML-utils/map/tileShapes/BaseTile.hpp>

namespace sfutils
{
namespace geometry
{
struct SquareIsoStaggered
struct SquareIsoStaggered : public BaseSquareIso<SquareIsoStaggered>
{
static const sf::ConvexShape& getShape();
static sf::Vector2f mapCoordsToPixel(int x,int y,float scale);
static sf::Vector2i mapPixelToCoords(float x,float y,float scale);
static sf::IntRect getTextureRect(int x,int y,float scale);
static sf::Vector2i round(float x,float y);
static int distance(int x1,int y1, int x2,int y2);


private:
static sf::ConvexShape _shape;
static void init();

static struct __Initiatiser {
__Initiatiser(){
SquareIsoStaggered::init();
};
} __initiatiser__;
};

}
}
#endif
18 changes: 2 additions & 16 deletions include/SFML-utils/map/tileShapes/SquareStaggered.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
#ifndef SFUTILS_GEOMETRY_SQUARESTAGGERED_HPP
#define SFUTILS_GEOMETRY_SQUARESTAGGERED_HPP

#include <SFML/Graphics.hpp>
#include <SFML-utils/map/tileShapes/BaseTile.hpp>

namespace sfutils
{
namespace geometry
{
struct SquareStaggered
struct SquareStaggered : public BaseSquare<SquareStaggered>
{
static const sf::ConvexShape& getShape();
static sf::Vector2f mapCoordsToPixel(int x,int y,float scale);
static sf::Vector2i mapPixelToCoords(float x,float y,float scale);
static sf::IntRect getTextureRect(int x,int y,float scale);
static sf::Vector2i round(float x,float y);
static int distance(int x1,int y1, int x2,int y2);


private:
static sf::ConvexShape _shape;
static void init();

static struct __Initiatiser {
__Initiatiser(){
SquareStaggered::init();
};
} __initiatiser__;
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/SFML-utils/map/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(FILES_HEADER
${INCROOT}/VLayer.hpp
${INCROOT}/VMap.hpp

${INCROOT}/tileShapes/BaseTile.hpp
${INCROOT}/tileShapes/HexaIso.hpp
${INCROOT}/tileShapes/Hexa.hpp
${INCROOT}/tileShapes/Square.hpp
Expand Down
59 changes: 1 addition & 58 deletions src/SFML-utils/map/tileShapes/Square.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
#include <SFML-utils/map/tileShapes/Square.hpp>
#include <cmath>

namespace sfutils
{
namespace geometry
{
sf::ConvexShape Square::_shape;
Square::__Initiatiser Square::__initiatiser__;

const float height = 2;

const sf::ConvexShape& Square::getShape()
{
return _shape;
}
SFML_UTILS_BASE_SQUARE_INIT(Square);

sf::Vector2f Square::mapCoordsToPixel(int X,int Y,float scale)
{
Expand All @@ -26,53 +17,5 @@ namespace sfutils
return round(X/scale/height,
Y/scale/height);
}

sf::Vector2i Square::round(float x, float y)
{
if(x>=0)
x+=0.5;
else
x-=0.5;

if(y>=0)
y+=0.5;
else
y-=0.5;

return sf::Vector2i(x,y);
}

sf::IntRect Square::getTextureRect(int x,int y,float scale)
{
sf::Vector2f pos = mapCoordsToPixel(x,y,scale);
sf::IntRect res(pos.x,
pos.y,
height * scale,
height * scale);
return res;
}

int Square::distance(int x1,int y1, int x2,int y2)
{
float x = x1 - x2;
x = x*x;

float y = y1 - y2;
y = y*y;

return ceil(sqrt(x + y));
}

void Square::init()
{
_shape.setPointCount(4);
_shape.setPoint(0,sf::Vector2f(0,0));
_shape.setPoint(1,sf::Vector2f(0,height));
_shape.setPoint(2,sf::Vector2f(height,height));
_shape.setPoint(3,sf::Vector2f(height,0));

_shape.setOrigin(height/2,height/2);
}

}
}
Loading

0 comments on commit fa13630

Please sign in to comment.