Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: For some reason my image isn't loading into my project.  (Read 1115 times)

0 Members and 1 Guest are viewing this topic.

Jackson Kidwell

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • TheBagel
For some reason my image isn't loading into my project.
« on: August 04, 2023, 12:54:13 am »
Here's my code:

#include <iostream>
#include <SFML/Graphics.hpp>

int WIDTH = 500;
int HEIGHT = 500;

int main() {


   sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "SFML Game");
   window.setFramerateLimit(60);
   sf::Event e;

   sf::Texture t1, t2, t3, t4;
   if (t1.loadFromFile("C:/Users/Jackson Kidwell/Downloads/blueBrick.png")) {
      std::cout << "blueBrick.png couldn't load";
   }

   sf::Sprite sBackground(t1);
   sBackground.setPosition(0, 0);
   

   while (window.isOpen()) {
      while (window.pollEvent(e)) {
         if (e.type == sf::Event::Closed) {
            window.close();
         }
      }

      window.clear();
      
      window.draw(sBackground);
      
   }

   return 0;
}

I've tried everything from moving the PNG to changing the file type and it still won't work.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: For some reason my image isn't loading into my project.
« Reply #1 on: August 04, 2023, 02:08:01 am »
Remember that a texture's loadFromFile returns true if it loads successfully.
You're outputting "couldn't load" if it can actually load.

Does it output "blueBrick.png couldn't load"?
If it does, your texture is loading fine. :P
If it doesn't, your texture is failing and SFML should be outputting an error message.

If it's failing, it could be that its format is unsupported. You could try resaving it with only standard features.

So, in the future, remember you need to be testing for false when testing if something fails:
if (!t1.loadFromFile("C:/Users/Jackson Kidwell/Downloads/blueBrick.png"))
    std::cout << "blueBrick.png couldn&#39;t load";
(note the "!" in "!t1")


Anyway, aside from that, I'm going to guess that your texture is loading perfectly as it looks like something else is enough to cause the issue.
You're not "displaying" the window so it'll never show you anything.

Remember: clear, draw, display.

window.clear();
window.draw(sBackground);
window.display();



Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*