11
« on: March 09, 2011, 01:25:28 am »
So i'm using
sf::Image Image;
if(Image.LoadFromFile("sprite.png"))
to try to load an image, but I keep getting an error that it can't load the image. I included it in the project file. Heres the code if you need it/
// sfml specific headers required
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
int main()
{
//create a sfml window
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "SFML Blank Window");
//creat the sfml event handler
sf::Event Event;
//Creat a SFML shape
sf::Shape box = sf::Shape::Rectangle(0, 0, 50, 50,sf::Color(127, 0, 100, 255));
//set the box position
box.SetPosition(100, 100);
//creat the image buffer
sf::Image Image;
if(Image.LoadFromFile("sprite.png"))
return EXIT_FAILURE;
// create SFML sprite
sf::Sprite sprite;
//set the sprite image
sprite.SetImage(Image);
// set the sprites posisiton
sprite.SetPosition(400, 400);
// game loop
while(Game.IsOpened())
{
// event handler loop
while(Game.GetEvent(Event))
{
//if x'd out close the game window
if(Event.Type == sf::Event::Closed)
Game.Close();
}
//clear the game window
Game.Clear(sf::Color(100, 149, 237));
// closes game when esc is pressed
if(Game.GetInput().IsKeyDown(sf::Key::Escape))
Game.Close();
// if left mouse and A or D is down move box
if(Game.GetInput().IsMouseButtonDown(sf::Mouse::Left))
{
if(Game.GetInput().IsKeyDown(sf::Key::A))
{
box.Move(-1, 0);
}
else if(Game.GetInput().IsKeyDown(sf::Key::D))
{
box.Move(1, 0);
}
}
//if the right mouse button is pressed and a or d is down move box
else if(Game.GetInput().IsMouseButtonDown(sf::Mouse::Right))
{
if(Game.GetInput().IsKeyDown(sf::Key::A))
{
sprite.Move(-1,0);
}
else if(Game.GetInput().IsKeyDown(sf::Key::D))
{
sprite.Move(1, 0);
}
}
//draw the shape
Game.Draw(box);
// draw the sprite
Game.Draw(sprite);
//display sfml graphics to the game window
Game.Display();
}
return EXIT_SUCCESS;
}