Ok, alors deja le code : (Déplacement d'un cube avec le temps en référence)
main.cpp#include <SFML/Graphics.hpp>
#include <iostream>
#include "Cube.h"
using namespace sf;
int main()
{
RenderWindow Window(VideoMode(800, 600, 32), "SFML Sample Application");
Window.setVerticalSyncEnabled(true);
Window.setFramerateLimit(0);
Cube *cube(0);
cube=new Cube("bloc.png");
Clock horloge;
horloge.restart();
while (Window.isOpen())
{
Event Event;
while (Window.pollEvent(Event))
{
switch (Event.type)
{
case Event::Closed:
Window.close();
break;
default:
break;
}
}
Window.clear(Color(0, 0, 0));
if (Keyboard::isKeyPressed(Keyboard::D))
{
cube->horizontal(300, horloge.getElapsedTime());
}
if (Keyboard::isKeyPressed(Keyboard::Q))
{
cube->horizontal(-300, horloge.getElapsedTime());
}
if (Keyboard::isKeyPressed(Keyboard::S))
{
cube->vertical(300, horloge.getElapsedTime());
}
if (Keyboard::isKeyPressed(Keyboard::Z))
{
cube->vertical(-300, horloge.getElapsedTime());
}
horloge.restart();
Window.draw(cube->sprite());
Window.display();
}
return 0;
}
Cube.h#ifndef CUBE_H
#define CUBE_H
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
sf::Color const fond(255,255,255,255);
class Cube
{
public:
Cube(std::string);
virtual ~Cube();
float x() const;
float y() const;
sf::Sprite sprite() const;
void horizontal(float vitesse,sf::Time t);
void vertical(float vitesse, sf::Time t);
protected:
private:
sf::Image *m_image;
sf::Texture *m_texture;
sf::Sprite *m_sprite;
};
#endif // CUBE_H
Cube.cpp#include "Cube.h"
using namespace sf;
// ------------------ Constructeurs, Destructeurs ------------------ //
Cube::Cube(std::string adresse) : m_image(0), m_texture(0), m_sprite(0)
{
m_image=new Image;
m_image->loadFromFile(adresse);
m_image->createMaskFromColor(fond,0);
m_texture=new Texture;
m_texture->loadFromImage(*m_image);
m_sprite=new Sprite;
m_sprite->setTexture(*m_texture);
}
Cube::~Cube()
{
delete m_image;
delete m_texture;
delete m_sprite;
}
// --------------------------- Accesseurs -------------------------- //
float Cube::x() const
{
return m_sprite->getPosition().x;
}
float Cube::y() const
{
return m_sprite->getPosition().y;
}
Sprite Cube::sprite() const
{
return *m_sprite;
}
// ---------------------------- Methodes --------------------------- //
void Cube::horizontal(float vitesse, Time t)
{
m_sprite->setPosition(x()+vitesse*t.asSeconds(), y());
}
void Cube::vertical(float vitesse, Time t)
{
m_sprite->setPosition(x(), y()+vitesse*t.asSeconds());
}
// --------------------------- Operateurs -------------------------- //
Carte Graphique : Radeon HD 7950 Vapor-X - OC With Boost - 3 Go.
Drivers : A jour, puis je n'ai de soucis dans aucun jeu ni programme.
Version SFML : 2.0 (affiché dans le titre).
OS : Windows seven.