Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

SFML fail to load image as texture

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Cdrandin   Members   


Posted 26 June 2013 - 02:05 PM

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.

Attached Thumbnails

  • tile.png

Edited by Cdrandin, 26 June 2013 - 02:51 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.