I'm making a game on C++ using SFML 2.4.1 for GCC SJLJ (CodeBlocks with MinGW). When it starts, I want my game to draw the SFML logo (like a splash screen) and, after 3 seconds, draw the title screen's background. Sounds simple, but after displaying the background, after a time it displays the SFML logo again, thus making a loop rather than making the image static. Could you guys help me? This is my code:
#include <Windows.h>
#include <SFML/Graphics.hpp>
int main()
{
// Setting up resources and the window
sf::RenderWindow screen(sf::VideoMode(816, 624), "Elkiria");
sf::Texture tx_sfml;
tx_sfml.loadFromFile("sfml.png");
sf::Sprite sp_sfml;
sp_sfml.setTexture(tx_sfml);
sf::Texture tx_titlebg;
tx_titlebg.loadFromFile("skybg.png");
sf::Sprite sp_titlebg;
sp_titlebg.setTexture(tx_titlebg);
// While the window is opened, do stuff.
while (screen.isOpen())
{
// Create game's event and event's types
sf::Event main_ev;
while (screen.pollEvent(main_ev))
{
if (main_ev.type == sf::Event::Closed) {
screen.close();
}
}
// Operate with the window
screen.clear();
screen.draw(sp_sfml);
screen.display();
Sleep(3000);
screen.draw(sp_titlebg);
screen.display();
}
return 0;
}
you only need call clear() and display() once with all your drawing in between (from back to front).
screen.clear();
screen.draw(sp_titlebg);
screen.draw(sp_sfml);
screen.display();
you only need call clear() and display() once with all your drawing in between (from back to front).
screen.clear();
screen.draw(sp_titlebg);
screen.draw(sp_sfml);
screen.display();
Actually, what I want to do is display only the sfml logo and after 3 seconds, display the background, not one behind another.
Don't know if this is any help. Or if it even matters. But you haven't declared anything for your sprite? Position, size, etc.
Well you can declare a sprite without properties, but it'll be on the top of the screen.
I would say that it would be better to use a clock rather than blocking the window for three seconds. Something like this, maybe:
sf::Clock clock;
while (screen.isOpen())
{
// Create game's event and event's types
sf::Event main_ev;
while (screen.pollEvent(main_ev))
{
if (main_ev.type == sf::Event::Closed) {
screen.close();
}
}
// Operate with the window
screen.clear();
if (clock.getElapsedTime() < sf::seconds(3))
screen.draw(sp_sfml);
else
screen.draw(sp_titlebg);
screen.display();
}
Remember that the whole loop should be constantly and regularly looping with every attempt at processing events as soon as they arrive/happen.