• sf::Image lost
    10 replies, posted
Hi I know that somewhere somehow my sf::Image is getting lost but I feel extremely stupid because I can't figure this out. Here is my code [cpp] #include <iostream> #include <SFML/Graphics.hpp> #include <SFML/System.hpp> using namespace std; int main() { // Create the main rendering window sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics"); //Declaring Variables sf::Shape P1 = sf::Shape::Rectangle(10, 300, 20, 400, sf::Color::Green); sf::Image Image; Image.LoadFromFile("ball.png"); sf::Sprite Ball; Ball.SetImage(Image); Ball.SetX(2.f); Ball.SetY(2.f); Ball.Scale(200.f,200.f); // Start game loop while (App.IsOpened()) { int x = App.GetInput().GetMouseX(); int y = App.GetInput().GetMouseY(); // Process events sf::Event Event; while (App.GetEvent(Event)) { float Elapsedtime = App.GetFrameTime(); // Close window : exit if (Event.Type == sf::Event::Closed) App.Close(); if (App.GetInput().IsKeyDown(sf::Key::Q)) { App.Close(); } if (App.GetInput().IsKeyDown(sf::Key::Up)) P1.Move(0,-3000*Elapsedtime); if (App.GetInput().IsKeyDown(sf::Key::Down)) P1.Move(0,3000*Elapsedtime); } // Clear the screen (fill it with black color) App.Clear(); App.Draw(Ball); App.Draw(P1); cout << x << "," << y << endl; // Display window contents on screen App.Display(); } return EXIT_SUCCESS; } [/cpp]
What do you mean by 'getting lost'?
Code seems fine. The image isn't being loaded. Make sure your working directory and output directories are set correctly.
Yeah my image is white. But I know the image is loading because i coded it to throw a error if the image did not load. I took it out after I knew it loaded fine.
Use .bmp
[QUOTE=AtomiC0l;25161855]Use .bmp[/QUOTE] SOIL, the library SFML uses to load images as textures, supports PNG files just fine.
[QUOTE=Mr.123;25159405]Yeah my image is white. But I know the image is loading because i coded it to throw a error if the image did not load. I took it out after I knew it loaded fine.[/QUOTE] A white square means that the image has no been loaded properly. Add some error checking to ensure that your image is loading properly. sf::Image::LoadFromFile() should return a boolean, so if it returns false then we know the image did not load.
I checked to see if it was loading correctly with [cpp] if (!Image.LoadFromFile("ball.png")) EXIT_FAILURE; [/cpp] [editline]09:29PM[/editline] And the program continued to run so I know it loaded
[QUOTE=Mr.123;25168431]I checked to see if it was loading correctly with [cpp] if (!Image.LoadFromFile("ball.png")) EXIT_FAILURE; [/cpp] [editline]09:29PM[/editline] And the program continued to run so I know it loaded[/QUOTE] If I understand you correctly, you should be using return EXIT_FAILURE and not just EXIT_FAILURE. What you are doing now is similar to if (false) 1;
[QUOTE=Mr.123;25168431]I checked to see if it was loading correctly with [cpp] if (!Image.LoadFromFile("ball.png")) EXIT_FAILURE; [/cpp] [editline]09:29PM[/editline] And the program continued to run so I know it loaded[/QUOTE] Lol. What above poster said.
Well that would be the problem then haha. Turns out the image was not loading but now it is. Thank you.
Sorry, you need to Log In to post a reply to this thread.