Y
Y
Yaroslav Ryazanov2016-10-01 09:19:44
C++ / C#
Yaroslav Ryazanov, 2016-10-01 09:19:44

How to solve the problem with not displaying sprites in SFML?

Sprites are not displayed in SFML. Here:
783bd8d3994c4988b0eb0811409af94e.PNG
Here is the code:

#include "SFML\Graphics.hpp"


using namespace sf;

int main()
{
  // Создаем главное окно приложения;
  RenderWindow Window(VideoMode(600, 600), "Pong");

  // Объект;
  Image Player;
  // Загружаем в него файл;
  Player.loadFromFile("hero.png");

  // Текстура;
  Texture Player_texture;
  // Передаем в него объект 'Image';
  Player_texture.loadFromImage(Player);

  // Спрайт
  Sprite Player_sprite;
  // Передаем в него оюъект текстуры;
  Player_sprite.setTexture(Player_texture);
  // Определенные координаты из картинки;
  Player_sprite.setTextureRect(IntRect(0, 192, 96, 96));
  // Начальные координаты появления спрайта;
  Player_sprite.setPosition(50, 25);


  //Главный цикл приложения;
  while (Window.isOpen())
  {
    // Обработка событий в цикле;
    Event Event;

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

    // X,Y; Идем вверх;
    if (Keyboard::isKeyPressed(Keyboard::W)) { Player_sprite.move(0, -0.1); Player_sprite.setTextureRect(IntRect(0, 288, 96, 96)); };
    // Идем вниз;
    if (Keyboard::isKeyPressed(Keyboard::S)) { Player_sprite.move(0, 0.1); Player_sprite.setTextureRect(IntRect(0, 0, 96, 96)); };
    // Идем влево;
    if (Keyboard::isKeyPressed(Keyboard::A)) { Player_sprite.move(-0.1, 0); Player_sprite.setTextureRect(IntRect(0, 96, 96, 96)); };
    // Идем вправо;
    if (Keyboard::isKeyPressed(Keyboard::D)) { Player_sprite.move(0, 0.1); Player_sprite.setTextureRect(IntRect(0, 0, 96, 96)); };


    // Очистка;
    Window.clear();
    // Рисуем спрайт;
    Window.draw(Player_sprite);


    // Отрисовка;
    Window.display();
  };




  return 0;
};

I seem to have connected everything:
1. Additional directories of included files:
6b3dfe7d777f46e8b0f2592a64a4d2a3.PNG
2. Additional directories of libraries:
7464b2a3ee754a66bb6f2dccef81baba.PNG
3. Set the output file to "SFML_STATIC":
a9ff98339f8d4947b267930f23e3d249.PNG
4. Additional libraries too:
1f3f7dde330a47079449559a3887fd85.PNG
Tileset is present. I installed it near main.cpp and Pong.exe.
But at startup, there is still no sprite, who knows what the problem is?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maaGames, 2016-10-01
@BRUC

run on F5? The working directory is where the sln file is located, unless explicitly reset. For the test, specify the full path to png to exclude all other possible errors.

N
Nexeon, 2016-10-01
@MrNexeon

How do you launch the game and where do you store the texture?
If through Visual Studio, then the hero.png texture should be in the folder with the game's source files:
If through the game's exe file, then the hero.png texture should also be in the appropriate folders next to the .exe file:
Visual Studio Projects -> Solution Name -> Release

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question