C ++ SFML图像加载失败

时间:2017-01-19 09:02:06

标签: c++ sfml stdmap

我是C ++的新手,为了更好地学习它(我从中学习)我已经在GitHub上找到的示例C ++游戏中重写了代码,我打算改变代码片段来扩展一旦我使用它就改变图形,以便确切知道它是什么。我不是简单地复制和粘贴现有代码,而是确保自己编写代码,我相信重写代码比简单地复制和粘贴更能理解。

我尝试为小型游戏加载图片作为C ++测试,当尝试从IDE执行代码时,它一直在if(iter == _images.end())附近失败我尝试了我自己能想到的所有内容,如更改_images的类型,更改filename的制作方式,添加支票,删除支票,我在谷歌和StackOverflow上花了最近3天尝试我遇到的每个修复提到了类似的问题。到目前为止,没有任何帮助。

我会在这里发布我认为与此相关的代码部分,请查看并指出我在何处或如何解决此问题。

首先,它向我显示错误:

Exception thrown: read access violation.
std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,sf::Texture,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,sf::Texture> >,0> >::_Root(...) returned 0x4.

If there is a handler for this exception, the program may be safely continued.

从我收集的内容来看,这与它试图读/写的内存有关。

调用堆栈

FileName.exe!std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,sf::Texture,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,sf::Texture> >,0> >::_Lbound<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Keyval) Line 2060    C++
FileName.exe!std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,sf::Texture,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,sf::Texture> >,0> >::lower_bound(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Keyval) Line 1538   C++
FileName.exe!std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,sf::Texture,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,sf::Texture> >,0> >::find(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Keyval) Line 1481  C++
FileName.exe!VisibleGameObject::Load(std::basic_string<char,std::char_traits<char>,std::allocator<char> > filename) Line 17 C++
FileName.exe!Player::Player() Line 7    C++
FileName.exe!`dynamic initializer for 'speler''() Line 30   C++
[External Code] 

VisibleGameObject.cpp

void VisibleGameObject::Load(std::string filename) {
    filename = "textures/" + filename;

    //Basic algorithm for images
    auto iter = _images.find(filename);
    if (iter == _images.end()) {
        sf::Texture texture;
        if (!texture.loadFromFile(filename)) {
            std::cout << "[ERROR] Image failed to load!" << std::endl;
            assert(false);
        }
        auto img = _images.insert(std::pair<std::string, sf::Texture>(filename, texture)).first;
        std::cout << "[DEBUG] " + filename + ": Loaded and added to cache." << std::endl;
        _sprite.setTexture(img->second);
        _isLoaded = true;
    }
    else {
        _sprite.setTexture(iter->second);
        _isLoaded = true;
    }
    _imageKey = filename;
}

VisibleGameObject.h

#pragma once
#include "stdafx.h"

class VisibleGameObject {
public:
    VisibleGameObject();
    virtual ~VisibleGameObject();

    virtual void Load(std::string filename);
    virtual void Draw(sf::RenderWindow & window);
    virtual void Update(float elapsedTime) = 0;

    virtual void SetPosition(float x, float y);
    virtual sf::Vector2f GetPosition() const;
    virtual float GetWidth() const;
    virtual float GetHeight() const;

    virtual sf::Rect<float> GetBoundingRect() const;
    virtual bool IsLoaded() const;

    virtual bool ShouldPersist() const;
    virtual void ToggleVisibility(bool isVisible);
    virtual bool ShouldDraw() const;

protected:
    sf::Sprite& GetSprite();

private:
    sf::Sprite _sprite;
    std::string _imageKey;
    bool _isLoaded;
    bool _shouldDraw;
    static std::map<std::string, sf::Texture> _images;
};

stdafx.h中

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>

#include <map>
#include <iostream>
#include <cassert>

对VisibleGameObject.cpp的调用来自Game.cpp

void Game::Start() {
    _mainWindow.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Window PlaceHolder Title");

    Background *background1 = new Background(1);
    background1->SetPosition(0.f, -768.f);
    _gameObjectManager.Add("bg1", background1);

    Background *background2 = new Background(2);
    background2->SetPosition(0.f, -768.f);
    _gameObjectManager.Add("bg2", background2);

    Background *background4 = new Background(4);
    background4->SetPosition(0.f, -768.f);
    _gameObjectManager.Add("bg4", background4);

    Player *player = new Player();
    player->ResetStartingPosition();
    _gameObjectManager.Add("player", player);

    MainHUD *hud = new MainHUD();
    _gameObjectManager.Add("hud", hud);

    GameOverHUD *gameOver = new GameOverHUD();
    _gameObjectManager.Add("gameover", gameOver);

    while (!IsExiting()) {
        GameLoop();
    }

    _mainWindow.close();
}

如果遗失任何内容或我添加了可以排除的内容,请告诉我,我会编辑问题。

0 个答案:

没有答案