I have come to a problem with the code below ...
Using SFML 2.0
#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>
int main()
{
float speed = 5.0f;
// create the window
sf::RenderWindow window(sf::VideoMode(sf::VideoMode::getDesktopMode().height - 300, 800), "Bricks");
// Set game window position on the screen
window.setPosition( sf::Vector2i(sf::VideoMode::getDesktopMode().width/4 + sf::VideoMode::getDesktopMode().width/16 , 0) );
// Allow library to accept repeatitive key presses (i.e. holding key)
window.setKeyRepeatEnabled(true);
// Hide mouse cursor
//window.setMouseCursorVisible(false);
// Limit 30 frames per sec; the minimum for all games
window.setFramerateLimit(30);
sf::Texture texture;
if (!texture.loadFromFile("tile.png", sf::IntRect(0, 0, 125, 32)))
{
std::cout<<"Could not load image\n";
return -1;
}
// Empty list of sprites
std::list<sf::Sprite> spriteContainer;
bool gameFocus = true;
// run the program as long as the window is open
while (window.isOpen())
{
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
float offsetX = 0.0f, offsetY = 0.0f;
if(event.type == sf::Event::GainedFocus)
gameFocus = !gameFocus;
else if(event.type == sf::Event::LostFocus)
gameFocus = !gameFocus;
if(event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Space)
{
if(gameFocus)
{
// Create sprite and add features before putting it into container
sf::Sprite sprite(texture);
sprite.scale(.9f,.7f);
sf::Vector2u textSize = texture.getSize();
sprite.setPosition(sf::Vector2f(mousePos.x-textSize.x/2.0f, mousePos.y - textSize.y/2.0f));
spriteContainer.push_front(sprite);
}
}
if(event.key.code == sf::Keyboard::P)
std::cout << spriteContainer.size() << std::endl;
if( event.key.code == sf::Keyboard::W )
offsetY -= speed;
if( event.key.code == sf::Keyboard::A )
offsetX -= speed;
if( event.key.code == sf::Keyboard::S )
offsetY += speed;
if( event.key.code == sf::Keyboard::D )
offsetX += speed;
}
// "close requested" event: we close the window
if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
window.close();
// Move all sprites synchronously
for (std::list<sf::Sprite>::iterator sprite = spriteContainer.begin(); sprite != spriteContainer.end(); ++sprite)
sprite->move(offsetX, offsetY);
//sprite.move(offsetX,offsetY);
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
// window.draw(...);
// Draw all sprites in the container
for (std::list<sf::Sprite>::iterator sprite = spriteContainer.begin(); sprite != spriteContainer.end(); ++sprite)
window.draw(*sprite);
// end the current frame
window.display();
}
return 0;
}
A couple weeks ago it worked flawlessly to my expectation, but now that I come back to it and I am having problems importing the image as a texture "tile.png". I don't understand why this is evening happening and the only message I get via the terminal is "Cannot load image ..." then a bunch of random characters. My libraries are for sure working, but now I am not sure why the image is not loading. My image is in the same directory as with my .h and .cpp files.
This is an irritating problem that keep coming up for some reason and is always a problem to fix it.
I import my libraries via my own directory "locals" which contain many APIs, but I specifically get SFML, and done appropriately as I am able to open a window and many other stuff.