1
Graphique / Re: Bug texture sprite
« le: Octobre 13, 2019, 10:41:18 pm »
Ne charge qu'une seul fois la texture et passe la dans le constructeur.
#include <SFML/Graphics.hpp>
#include <iostream>
#include <random>
#define screen sf::Vector2f(1600, 900)
class character :public sf::Drawable
{
public:
sf::Sprite sprite_character;
character(sf::Vector2f pos, sf::Texture& texture)
{
sprite_character.setTexture(texture);
sprite_character.setOrigin((texture.getSize().x / 2), (texture.getSize().y / 2));
sprite_character.setPosition(pos);
}
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(sprite_character, states);
}
};
int main()
{
srand(time(NULL));
std::vector<character> sprites;
sf::RenderWindow window(sf::VideoMode(screen.x, screen.y), "Game");
window.setVerticalSyncEnabled(true);
sf::Event event;
sf::Clock clock;
sf::Texture tex_character;
if (!tex_character.loadFromFile("Content/player.png")) // ????????????????
return EXIT_FAILURE;
while (window.isOpen())
{
sf::Time time = clock.getElapsedTime();
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
{
if (time.asSeconds() >= 1)
{
time = sf::Time::Zero;
clock.restart();
int pos_x = rand() % (int)screen.x;
int pos_y = rand() % (int)screen.y;
sprites.push_back(character(sf::Vector2f(pos_x, pos_y), tex_character));
}
}
window.clear(sf::Color(0, 0, 0));
for (unsigned int i = 0; i < sprites.size(); ++i)
window.draw(sprites[i]);
window.display();
}
return 0;
}
#include <iostream>
#include <random>
#define screen sf::Vector2f(1600, 900)
class character :public sf::Drawable
{
public:
sf::Sprite sprite_character;
character(sf::Vector2f pos, sf::Texture& texture)
{
sprite_character.setTexture(texture);
sprite_character.setOrigin((texture.getSize().x / 2), (texture.getSize().y / 2));
sprite_character.setPosition(pos);
}
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(sprite_character, states);
}
};
int main()
{
srand(time(NULL));
std::vector<character> sprites;
sf::RenderWindow window(sf::VideoMode(screen.x, screen.y), "Game");
window.setVerticalSyncEnabled(true);
sf::Event event;
sf::Clock clock;
sf::Texture tex_character;
if (!tex_character.loadFromFile("Content/player.png")) // ????????????????
return EXIT_FAILURE;
while (window.isOpen())
{
sf::Time time = clock.getElapsedTime();
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
{
if (time.asSeconds() >= 1)
{
time = sf::Time::Zero;
clock.restart();
int pos_x = rand() % (int)screen.x;
int pos_y = rand() % (int)screen.y;
sprites.push_back(character(sf::Vector2f(pos_x, pos_y), tex_character));
}
}
window.clear(sf::Color(0, 0, 0));
for (unsigned int i = 0; i < sprites.size(); ++i)
window.draw(sprites[i]);
window.display();
}
return 0;
}