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

Author Topic: Error load Image  (Read 4371 times)

0 Members and 4 Guests are viewing this topic.

Nawy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Error load Image
« on: January 14, 2014, 08:24:36 pm »
I have follow code:
TApp.h
#pragma once
#include <SFML\Graphics.hpp>


class TApp
{
public:
   TApp();

   int DoExecute();

private: /*MAIN FUNCTION*/
   void DoInputHandle();
   void DoUpdate();
   void DoRender();

private:
   sf::RenderWindow m_Window;
   sf::Sprite m_sprite;
};

TApp.cpp
#include "TApp.h"

TApp::TApp()
: m_Window(sf::VideoMode(640, 480), "SFML Game Engine")
{
   sf::Texture texture;
   texture.loadFromFile("sprites/first.bmp");
   m_sprite.setTexture(texture);
}

int TApp::DoExecute()
{
   while (m_Window.isOpen())
   {
      DoInputHandle();
      DoUpdate();
      DoRender();
   }

   return 0;
}

int main(int argc, char* argv[])
{
   TApp application;
   return application.DoExecute();
}

DoInputHandle.cpp
#include "TApp.h"

void TApp::DoInputHandle()
{
   sf::Event event;

   while (m_Window.pollEvent(event))
   {
      switch (event.type)
      {
      case sf::Event::Closed:
         m_Window.close();
         break;

      default:
         break;
      }
   }
}

DoRender.cpp
#include "TApp.h"

void TApp::DoRender()
{
   m_Window.clear();
   m_Window.draw(m_sprite);
   m_Window.display();
}

DoUpdate.cpp
#include "TApp.h"

void TApp::DoUpdate()
{
}

It's very very simple. But haven't work. :(
Where may be an error?


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Error load Image
« Reply #1 on: January 14, 2014, 08:27:51 pm »
You might have the wrong working directory.

Always check the return type of loadFromFile()!
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Error load Image
« Reply #2 on: January 14, 2014, 09:12:33 pm »
Check if you didn't link to release libraries(in debug you must link to dlls that have -d in names).

You might have the wrong working directory.

Always check the return type of loadFromFile()!
That'd not cause Windows to intervene with 'Program stopped working' message box.

As for return type of loadFromFile - it was a bool last time I checked. ;)
Back to C++ gamedev with SFML in May 2023

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Error load Image
« Reply #3 on: January 15, 2014, 01:23:13 am »
TApp.h

TApp::TApp()
: m_Window(sf::VideoMode(640, 480), "SFML Game Engine")
{
   sf::Texture texture;            //Problem here!
   texture.loadFromFile("sprites/first.bmp");
   m_sprite.setTexture(texture);
}


A common mistake. Once the TAPP() end, your texture is destroyed. However, a sprite use a pointer to the texture to draw, which does not exist anymore. Result, the sprite can not draw your texture.

You should add in your header:

sf::Texture m_Texture;

And in TAPP() use m_Texture.loadFromFile("sprites/first.bmp");
« Last Edit: January 15, 2014, 01:30:05 am by math1992 »

Nawy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Error load Image
« Reply #4 on: January 15, 2014, 05:59:24 am »
Okay. In this way, I tried to correct following:
TApp.h
#pragma once
#include <SFML\Graphics.hpp>


class TApp
{
public:
        TApp();

        int DoExecute();

private: /*MAIN FUNCTION*/
        void DoInputHandle();
        void DoUpdate();
        void DoRender();

private:
        sf::RenderWindow m_Window;
        sf::Texture m_texture;
        sf::Sprite m_sprite;
};

DoRender.cpp
#include "TApp.h"
#include <assert.h>
void TApp::DoRender()
{
        bool m = m_texture.loadFromFile("D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp");
        assert(m == false);
        m_sprite.setTexture(m_texture);
        m_sprite.setPosition(100.0f, 100.0f);


        m_Window.clear();
        m_Window.draw(m_sprite);
        m_Window.display();
}
Not working! I remembered a relation between sf::Image - sf::Texture - sf::Sprite, and I thought that program is right now working, but not.
A few days ago I wrote following code:
main.cpp
#include <SFML\Graphics.hpp>
#include <boost\lexical_cast.hpp>
#include <iostream>
#include <string>

class Game
{
public:
        Game();
        void run();

private:
        void processEvent();
        void update(sf::Time deltaTime);
        void render();

private:
        sf::RenderWindow        mWindow;
};

int main()
{
        Game game;
        game.run();
        return 0;
}

Game::Game()
: mWindow(sf::VideoMode::getDesktopMode(), "SFML Window", sf::Style::Fullscreen)
{
}

void Game::run()
{
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        const sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
        while (mWindow.isOpen())
        {
                processEvent();
                timeSinceLastUpdate += clock.restart();
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;
                        processEvent();
                        update(TimePerFrame);
                }
                render();
        }
}

void Game::processEvent()
{
        sf::Event event;
        while (mWindow.pollEvent(event))
        {
                switch (event.type)
                {

                case sf::Event::Closed:
                        mWindow.close();
                        break;

                default:
                        break;
                }
        }
}

void Game::update(sf::Time deltaTime)
{
}

void Game::render()
{

        sf::Texture texture;
        texture.loadFromFile("D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp");
       
        sf::Sprite sprite(texture);
        sprite.setPosition(100.f, 100.f);

        mWindow.clear();
        mWindow.draw(sprite);
        mWindow.display();
}
It's working. But why?? I confused.

I tried use also following:
#include "TApp.h"
#include <assert.h>
void TApp::DoRender()
{
        sf::Texture texture;
        bool m = texture.loadFromFile("D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp");
        assert(m == false);
        sf::Sprite sprite(texture);
        sprite.setPosition(100.0f, 100.0f);


        m_Window.clear();
        m_Window.draw(sprite);
        m_Window.display();
}
 
Same error.
« Last Edit: January 15, 2014, 06:07:28 am by Nawy »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Error load Image
« Reply #5 on: January 15, 2014, 06:48:27 am »
Given this line:
assert(m == false);
Your program will fail at runtime if the texture was loaded correctly (That's what you're telling the program to do with the above line).

Also, "#include <assert.h>"is the C way. For C++, you would do: "#include <cassert>".

That is, if you want to do it in C++.  :)

Nawy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Error load Image
« Reply #6 on: January 15, 2014, 07:22:11 am »
If even I use following:
DoRender.cpp
#include "TApp.h"

void TApp::DoRender()
{
        sf::Texture texture;
        texture.loadFromFile("D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp");

        sf::Sprite sprite(texture);
        sprite.setPosition(100.0f, 100.0f);


        m_Window.clear();
        m_Window.draw(sprite);
        m_Window.display();
}
occurs only a crash program. I tried use different kinds of path:
  • D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp
  • D:\\Project\\CPP\\Game_Engine\\Debug\\sprites\\first.bmp
  • d:\\Project\\CPP\\Game_Engine\\Debug\\sprites\\first.bmp
I use different files:
  • D:/Project/Sprites/bt.png
  • D:/Project/Sprites/anim.bmp
  • D:/Project/Sprites/boy.jpg
Compiler indicates at following line:
texture.loadFromFile("D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp");
More precisely, Program crashes on this line.
« Last Edit: January 15, 2014, 07:24:30 am by Nawy »

Nawy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Error load Image
« Reply #7 on: January 15, 2014, 07:45:32 am »
#include <SFML\Graphics.hpp>
#include <iostream>

int main(int argc, char* argv[])
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Render");
        sf::Image image;
        sf::Texture texture;
        sf::Sprite sprite;

        image.loadFromFile("D:/Project/Sprites/bt1.png");
        texture.loadFromImage(image);
        sprite.setTexture(texture);
        sprite.setPosition(100.0f, 100.0f);

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

                window.clear();
                window.draw(sprite);
                window.display();
        }

        return 0;
}

My configurations



code above is also crashes. Where I could go wrong?
« Last Edit: January 15, 2014, 08:16:30 am by Nawy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Error load Image
« Reply #8 on: January 15, 2014, 08:28:30 am »
As stated in the tutorials, you should not mix debug and release modes, i.e. you're linking the release libraries (without -d suffix) in debug mode. Change all the suffix to for example sfml-graphics-d.lib and it should work. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nawy

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Error load Image
« Reply #9 on: January 15, 2014, 09:32:23 am »
As stated in the tutorials, you should not mix debug and release modes, i.e. you're linking the release libraries (without -d suffix) in debug mode. Change all the suffix to for example sfml-graphics-d.lib and it should work. :)
You're right! It works!