Answer the question
In order to leave comments, you need to log in
How to solve the problem with not displaying sprites in SFML?
Sprites are not displayed in SFML. Here:
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;
};
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question