• Advertisement
Sign in to follow this  

SFML fail to load image as texture

This topic is 1673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone,

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), "Breaking 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)

	// Hide mouse cursor

	// Limit 30 frames per sec; the minimum for all games
	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)
						// Create sprite and add features before putting it into container
						sf::Sprite sprite(texture);
						sf::Vector2u textSize = texture.getSize();

						sprite.setPosition(sf::Vector2f(mousePos.x-textSize.x/2.0f, mousePos.y - textSize.y/2.0f));


				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)

			// Move all sprites synchronously
			for (std::list<sf::Sprite>::iterator sprite = spriteContainer.begin(); sprite != spriteContainer.end(); ++sprite)
				sprite->move(offsetX, offsetY);


        // clear the window with black color

        // draw everything here...
        // window.draw(...);
		// Draw all sprites in the container
		for (std::list<sf::Sprite>::iterator sprite = spriteContainer.begin(); sprite != spriteContainer.end(); ++sprite)

        // end the current frame

    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.

Edited by Cdrandin

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement