Недавно я начал изучать SFML по книге и столкнулся с проблемой: при загрузке изображения вместо этого появляется большой белый квадрат.
У меня есть класс TextureHolder для управления игровыми ресурсами, такими как текстуры, спрайты и т.п. (еще не доработано). Я пытался применить к нему подход RAII, но теперь он не работает (я учусь по книге)
Texture.hpp
#pragma once
#include
#include
#include
namespace Textures {
enum class ID { Landscape, Airplane, Missile };
}
class TextureHolder final {
public:
void load(Textures::ID id, const std::string& filename);
sf::Texture& get(Textures::ID id);
const sf::Texture& get(Textures::ID id) const;
private:
std::map m_TextureMap;
};
Texture.cpp
#include
#include "Textures.hpp"
void TextureHolder::load(Textures::ID id, const std::string &filename) {
std::unique_ptr texture(new sf::Texture());
if (!texture->loadFromFile(filename)) {
std::cerr TimePerFrame) {
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
void Game::processEvents() {
sf::Event event;
while (m_window.pollEvent(event)) {
switch (event.type) {
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case (sf::Event::Closed):
m_window.close();
break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed) {
if (key == sf::Keyboard::W)
m_isMoving[Up] = isPressed;
else if (key == sf::Keyboard::S)
m_isMoving[Down] = isPressed;
else if (key == sf::Keyboard::A)
m_isMoving[Left] = isPressed;
else if (key == sf::Keyboard::D)
m_isMoving[Right] = isPressed;
}
void Game::update(const sf::Time deltaTime) {
sf::Vector2f movement(0.f, 0.f);
if (m_isMoving[Up])
movement.y -= PLAYER_SPEED;
if (m_isMoving[Down])
movement.y += PLAYER_SPEED;
if (m_isMoving[Left])
movement.x -= PLAYER_SPEED;
if (m_isMoving[Right])
movement.x += PLAYER_SPEED;
m_spritePlane.move(movement * deltaTime.asSeconds());
}
void Game::render() {
m_window.clear();
m_window.draw(m_spritePlane);
m_window.display();
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... tual-image