1
Graphics / Draw a rotating 'head'
« on: February 22, 2014, 11:03:35 pm »
Hey there! I'm currently working on a Tower Defense type of game and it came to my mind that it would look way cooler if the 'head' of the towers (or the canon or whatever you want to call it) could rotate around and aim at the target. So I create two images : the tower's body and the canon. But here comes the problem : how do I put it in the middle of my sprite? I tried a lot of things : setting the origin to the center of the sprite, calculate the position of the head with the x and y of the body + de width/height but none of that worked...
I should mention that the image representing the head is too small, so I use sprite.scale to grow it, and I think it affects the result of what I'm trying to do.
I attached the cpp to the post. Here's the headers :
Tower.hpp :
BasicTower.cpp :
Game.hpp :
I should mention that the image representing the head is too small, so I use sprite.scale to grow it, and I think it affects the result of what I'm trying to do.
I attached the cpp to the post. Here's the headers :
Tower.hpp :
#ifndef TOWER_HPP
#define TOWER_HPP
#include "Entity.hpp"
#include <functional>
#include <map>
class Tower;
namespace std {
template <typename T>
using add_pointer_ti = typename add_pointer<T>::type;
}
using TowerCreator = std::add_pointer_ti<Tower*()>;
class Tower : public Entity{
public:
virtual ~Tower(){}
void Draw(sf::RenderWindow& win);
void setPosition(float, float);
static Tower* create(sf::Keyboard::Key id);
static void registerToFactory(sf::Keyboard::Key id, TowerCreator func);
//TO ADD : Shoot and ApplyEffect (virtual)
protected :
void LoadSprite();
Tower(float, float);
std::string headFileName;
sf::Texture headTexture;
sf::Sprite headSprite;
int cost;
int upgradeCost;
float range;
private :
static std::map<sf::Keyboard::Key, TowerCreator>& factories();
};
#endif
#define TOWER_HPP
#include "Entity.hpp"
#include <functional>
#include <map>
class Tower;
namespace std {
template <typename T>
using add_pointer_ti = typename add_pointer<T>::type;
}
using TowerCreator = std::add_pointer_ti<Tower*()>;
class Tower : public Entity{
public:
virtual ~Tower(){}
void Draw(sf::RenderWindow& win);
void setPosition(float, float);
static Tower* create(sf::Keyboard::Key id);
static void registerToFactory(sf::Keyboard::Key id, TowerCreator func);
//TO ADD : Shoot and ApplyEffect (virtual)
protected :
void LoadSprite();
Tower(float, float);
std::string headFileName;
sf::Texture headTexture;
sf::Sprite headSprite;
int cost;
int upgradeCost;
float range;
private :
static std::map<sf::Keyboard::Key, TowerCreator>& factories();
};
#endif
BasicTower.cpp :
#include "BasicTower.hpp"
using namespace sf;
class BasicTower::Factory{
public :
Factory(){
Tower::registerToFactory(Keyboard::Q, create);
}
static Tower* create();
};
BasicTower::Factory* BasicTower::factory = new BasicTower::Factory();
Tower* BasicTower::Factory::create(){
return new BasicTower();
}
BasicTower::BasicTower() : Tower(0.0f, 0.0f) {
filename = "Textures/baseTower.png";
headFileName = "Textures/baseTowerHead.png";
LoadSprite();
}
BasicTower::BasicTower(float _x, float _y) : Tower(_x, _y)
{
headFileName = "Textures/baseTowerHead.png";
filename = "Textures/baseTower.png";
LoadSprite();
}
using namespace sf;
class BasicTower::Factory{
public :
Factory(){
Tower::registerToFactory(Keyboard::Q, create);
}
static Tower* create();
};
BasicTower::Factory* BasicTower::factory = new BasicTower::Factory();
Tower* BasicTower::Factory::create(){
return new BasicTower();
}
BasicTower::BasicTower() : Tower(0.0f, 0.0f) {
filename = "Textures/baseTower.png";
headFileName = "Textures/baseTowerHead.png";
LoadSprite();
}
BasicTower::BasicTower(float _x, float _y) : Tower(_x, _y)
{
headFileName = "Textures/baseTowerHead.png";
filename = "Textures/baseTower.png";
LoadSprite();
}
Game.hpp :
#ifndef GAME_HPP
#define GAME_HPP
#include "SFML\Graphics.hpp"
#include <vector>
#include <memory>
#include "Tower.hpp"
#include "BasicTower.hpp"
struct Game {
Game();
~Game();
int Run();
private:
void Update();
void Draw();
void AddTower();
std::unique_ptr<Entity> selectedEntity;
std::unique_ptr<Tower> selectedTowerType;
sf::RenderWindow win;
std::vector<std::unique_ptr<Tower>> towers;
};
#endif
#define GAME_HPP
#include "SFML\Graphics.hpp"
#include <vector>
#include <memory>
#include "Tower.hpp"
#include "BasicTower.hpp"
struct Game {
Game();
~Game();
int Run();
private:
void Update();
void Draw();
void AddTower();
std::unique_ptr<Entity> selectedEntity;
std::unique_ptr<Tower> selectedTowerType;
sf::RenderWindow win;
std::vector<std::unique_ptr<Tower>> towers;
};
#endif