SFML community forums

Help => Graphics => Topic started by: Nawy on January 14, 2014, 08:24:36 pm

Title: Error load Image
Post by: Nawy 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?

(http://s30.postimg.org/5nbdqhulr/atas.png)
Title: Re: Error load Image
Post by: Nexus on January 14, 2014, 08:27:51 pm
You might have the wrong working directory.

Always check the return type of loadFromFile()!
Title: Re: Error load Image
Post by: FRex 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. ;)
Title: Re: Error load Image
Post by: math1992 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");
Title: Re: Error load Image
Post by: Nawy 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.
(http://i39.tinypic.com/14wvdb9.jpg)
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.
(http://i39.tinypic.com/dy3lh1.jpg)
Title: Re: Error load Image
Post by: dabbertorres 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++.  :)
Title: Re: Error load Image
Post by: Nawy 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:
I use different files:
Compiler indicates at following line:
texture.loadFromFile("D:/Project/CPP/Game_Engine/Debug/sprites/first.bmp");
More precisely, Program crashes on this line.
Title: Re: Error load Image
Post by: Nawy 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
(http://i43.tinypic.com/spd4x5.jpg)

(http://i41.tinypic.com/2e5v137.jpg)
code above is also crashes. Where I could go wrong?
Title: Re: Error load Image
Post by: eXpl0it3r on January 15, 2014, 08:28:30 am
(http://i43.tinypic.com/spd4x5.jpg)
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. :)
Title: Re: Error load Image
Post by: Nawy 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!