【问题标题】:Try to rewrite engine from SDL to SFML, wrong scope of sf::RenderWindow?尝试将引擎从 SDL 重写为 SFML,sf::RenderWindow 的范围错误?
【发布时间】:2016-04-26 00:00:44
【问题描述】:

我的显示图像有问题(我尝试将引擎从 SDL 重写为 SFML 2.0;引擎下载自: http://gamedevgeek.com/tutorials/managing-game-states-in-c/ )

我对 introstate.cpp 中的那部分代码有疑问。

程序编译并创建窗口(仅一秒钟)然后消失,没有任何反应,也没有渲染任何东西(它应该显示图像)。

我认为这与对象 sf::RenderWindow MarioClone 的范围有关。我的意思是它在几个标题中声明并用于各种方法,所以我认为指向创建的特定窗口存在误解。我应该在某处使用“extern”关键字还是什么?

我留下了 github 的链接,因为代码在很多文件中,甚至一个文件也包含很多代码,不想在此处粘贴(很难阅读)。 https://github.com/shahar23/MarioClone (是的 - 代码已经对之前的原始 SDL 进行了注释,以便轻松理解应该在方法中添加什么)

【问题讨论】:

    标签: c++ game-engine sfml


    【解决方案1】:

    在您的 gameengine.cpp 文件中,在您的 init 方法中,您创建一个与头文件中声明的变量同名的 local 变量。那不是你想要的。您要更改现有变量:

    void CGameEngine::Init(const char* title, int width, int height, bool fullscreen)
    {
        // This line creates a NEW LOCAL variable of the same name. 
        // Your instance level variable remains unchanged:
        // sf::RenderWindow MarioClone(sf::VideoMode(width, height), title, sf::Style::Default);
    
        // instead, change your class level variable:
        MarioClone.create(sf::VideoMode(width, height), title, sf::Style::Default);
    

    【讨论】:

    • 是的,但是它不能编译。这是因为 introstate.cpp 也使用了该对象 (RenderWindow MarioClone) 并且它没有在那里声明(它看不到在 CGameEngine::Init 中创建的实例)。
    • MarioClone introstate.cpp 需要为 game->MarioClone。但那是基本的 C++,与 SFML 甚至 SDL 无关。
    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2018-01-07
    相关资源
    最近更新 更多