窗口拒绝渲染

发布于 2024-10-09 00:49:17 字数 1253 浏览 4 评论 0原文

我最近开始尝试使用 SFML。由于某种原因,我的简单程序不会渲染窗口。我尝试将所有内容都放入 main 中,以查看我的代码中是否存在与多个文件等有关的错误,但无济于事。

我将启动我的程序,但什么也不会出现。

有什么问题吗?

//main.h
#ifndef MAIN_H
#define MAIN_H

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

#include <iostream>
#include <fstream>

using namespace std;
using namespace sf;

class game
{
    public:
    void startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME);
    void log(const string logging);

    game()
    {
        QUIT = false;

        pendingFile.open("Log.txt", ios::out);
        pendingFile << "---Brain Bread Log---";
    }

    ~game()
    {
        pendingFile.close();
    }

    private:
    bool QUIT;
    ofstream pendingFile;
};

#endif


//main.cpp
#include "main.h"

void game::log(const string logging)
{
    pendingFile << logging;
}

void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
    Window Game(VideoMode(SCREEN_W, SCREEN_H, 32), SCREEN_NAME);
    while(QUIT == false)
    {
        Game.Display();
    }
}


int main(int argc, char* argv[])
{
    game gameObj;

    gameObj.startLoop(800, 600, "Brain Bread");

    return 0;
}

I recently got started trying to use SFML. For some reason my simple program will not render the window. I've tried throwing everything into main to see if there was an error in my code that had to do with multiple files etc. but to no avail.

I'll launch my program and nothing will appear.

What's the problem?

//main.h
#ifndef MAIN_H
#define MAIN_H

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

#include <iostream>
#include <fstream>

using namespace std;
using namespace sf;

class game
{
    public:
    void startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME);
    void log(const string logging);

    game()
    {
        QUIT = false;

        pendingFile.open("Log.txt", ios::out);
        pendingFile << "---Brain Bread Log---";
    }

    ~game()
    {
        pendingFile.close();
    }

    private:
    bool QUIT;
    ofstream pendingFile;
};

#endif


//main.cpp
#include "main.h"

void game::log(const string logging)
{
    pendingFile << logging;
}

void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
    Window Game(VideoMode(SCREEN_W, SCREEN_H, 32), SCREEN_NAME);
    while(QUIT == false)
    {
        Game.Display();
    }
}


int main(int argc, char* argv[])
{
    game gameObj;

    gameObj.startLoop(800, 600, "Brain Bread");

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄与心 2024-10-16 00:49:17

我尝试了你的代码,它的行为完全符合我的预期 - 也就是说,弹出一个带有黑色主体的无图标窗口,并且它不响应事件。这就是你得到的吗?如果没有,您可能需要重建 SFML。

您可能想尝试引入事件处理,以便您的 startLoop 看起来更像这样:

void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
    // Init stuff

    while (Game.IsOpened())
    {
        sf::Event newEvent;

        while (Game.GetEvent(newEvent))
        {
            // Process event
        }

        // Do graphics stuff

        Game.Display();
    }
}

I tried your code and it behaves exactly as I expect it to - that is to say that an iconless window with a black body pops up and it doesn't respond to events. Is that what you're getting? If not, you might need to rebuild SFML.

You might want to try introducing event-handling so that your startLoop looks more like this:

void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
    // Init stuff

    while (Game.IsOpened())
    {
        sf::Event newEvent;

        while (Game.GetEvent(newEvent))
        {
            // Process event
        }

        // Do graphics stuff

        Game.Display();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文