Here is the MINIMAL code:
main.cpp
#include "DisplaySfml.hh"
#define WIDTH (desktop.width())
#define HEIGHT (desktop.height())
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget desktop;
IDisplay* Win = new DisplaySfml();
Win->InitWindow(WIDTH, HEIGHT, "Air Type");
// Win->loadImage("Ship", "./Resources/ShipNormal.png");
// Win->loadImage("ShipForward", "./Resources/ShipForward.png");
// Win->loadImage("ShipBack", "./Resources/ShipBack.png");
// Win->loadImage("ShipLeft", "./Resources/ShipLeft.png");
// Win->loadImage("ShipRight", "./Resources/ShipRight.png");
Win->loadImage("Background", "./Resources/Background.jpg");
// Win->loadImage("FrontStars", "./Resources/FrontStars.png");
// Win->loadImage("BackStars", "./Resources/BackStars.png");
// Win->loadImage("DoubleStars", "./Resources/FrontStars.png");
// Win->getSprite("Ship")->SetPosition(WIDTH / 2, HEIGHT / 2);
Win->DisplayWindow();
return EXIT_SUCCESS;
}
DisplaySfml.cpp
#include "DisplaySfml.hh"
DisplaySfml::DisplaySfml()
:Speed_(0.01f)
{
turnSendDirection_[sf::Keyboard::Down] = &moveRight;
turnSendDirection_[sf::Keyboard::Up] = &moveLeft;
turnSendDirection_[sf::Keyboard::Left] = &moveBack;
turnSendDirection_[sf::Keyboard::Right] = &moveForward;
turnSendDirection_[sf::Keyboard::Space] = &moveSpace;
}
DisplaySfml::~DisplaySfml()
{
for (std::vector<sf::RenderTexture*>::iterator it = saveTexture_.begin(); it != saveTexture_.end(); ++it)
delete *it;
for (std::map<std::string, Sprite>::iterator it = displayImage_.begin(); it != displayImage_.end(); ++it)
delete (it->second).sprite_;
delete Window_;
}
void DisplaySfml::InitWindow(int Width, int Height, const std::string & Title)
{
Width_ = Width;
Height_ = Height;
Window_ = new sf::RenderWindow(sf::VideoMode(Width_, Height_), Title.c_str(), sf::Style::None);
Window_->EnableVerticalSync(true);
}
void DisplaySfml::loadImage(std::string const &name, std::string const & pathImage)
{
sf::RenderTexture *renderTexture = new sf::RenderTexture;
sf::Texture texture;
if (!(texture.LoadFromFile(pathImage)))
std::cerr << "Image [" << pathImage << "] is missing" << std::endl;
sf::Sprite spriteTmp(texture);
spriteTmp.Scale(Width_ / 1920.f, Height_ / 1200.f);
if (!renderTexture->Create(texture.GetWidth() * (Width_ / 1920.f), texture.GetHeight() * (Height_ / 1200.f)))
std::cerr << "RenderTexture [" << pathImage << "] failed to create" << std::endl;
renderTexture->Clear(sf::Color::Transparent);
renderTexture->Draw(spriteTmp);
renderTexture->Display();
displayImage_[name].sprite_ = new sf::Sprite(renderTexture->GetTexture());
displayImage_[name].width_ = renderTexture->GetWidth();
displayImage_[name].height_ = renderTexture->GetHeight();
displayImage_[name].loop_ = 0;
displayImage_[name].screenWidth_ = 0;
saveTexture_.push_back(renderTexture);
}
sf::Sprite *DisplaySfml::getSprite(std::string const &name)
{
return displayImage_[name].sprite_;
}
int DisplaySfml::getXSprite(std::string const &name)
{
return displayImage_[name].sprite_->GetPosition().x;
}
int DisplaySfml::getYSprite(std::string const &name)
{
return displayImage_[name].sprite_->GetPosition().y;
}
void DisplaySfml::drawImage(std::string const &name, int const &x, int const &y)
{
displayImage_[name].sprite_->SetPosition(x, y);
Window_->Draw(*(displayImage_[name].sprite_));
}
void DisplaySfml::DisplayWindow()
{
x = 0;
y = 0;
Clock_.Reset();
Window_->SetFramerateLimit(60);
while (Window_->IsOpen())
{
event();
ElapsedTime_ = Clock_.GetElapsedTime();
if (ElapsedTime_ > 17)
{
Window_->Clear();
drawImage("Background", 0, 0);
// loopSprite("Background", 3);
// loopSprite("FrontStars", 2);
// loopSprite("BackStars", 1);
// drawImage("Ship", getXSprite("Ship") + x, getYSprite("Ship") + y);
// drawImage("Ship", 500, 500);
// loopSprite("DoubleStars", 6);
Window_->Display();
Clock_.Reset();
}
}
}
void DisplaySfml::event()
{
sf::Event event;
while (Window_->PollEvent(event))
{
if (event.Type == sf::Event::KeyPressed)
{
if ((event.Key.Code == sf::Keyboard::Escape))
{
Window_->Close();
break;
}
for (std::map<sf::Keyboard::Key, setNewPos>::iterator it = turnSendDirection_.begin(); it != turnSendDirection_.end(); ++it)
{
if (sf::Keyboard::IsKeyPressed(it->first))
turnSendDirection_[it->first](x, y);
}
}
}
}
void DisplaySfml::loopSprite(const std::string name, int const speed)
{
if ((displayImage_[name]).screenWidth_ >= Width_)
(displayImage_[name]).screenWidth_ = 0;
if ((displayImage_[name]).loop_ >= (displayImage_[name]).width_)
(displayImage_[name]).loop_ = 0;
sf::IntRect leftRect((displayImage_[name]).loop_, 0,
Width_ - (displayImage_[name]).screenWidth_, Height_);
(displayImage_[name]).sprite_->SetTextureRect(leftRect);
drawImage(name, 0, 0);
if ((displayImage_[name]).loop_ + Width_ >= (displayImage_[name]).width_)
{
sf::IntRect rightRect(0 ,0, (displayImage_[name]).screenWidth_, Height_);
(displayImage_[name]).sprite_->SetTextureRect(rightRect);
drawImage(name, Width_ - (displayImage_[name]).screenWidth_, 0);
(displayImage_[name]).screenWidth_ += speed;
}
(displayImage_[name]).loop_ += speed;
}
Any help ?