未显示 SFML 窗口

SFML Window Doesn't Appear

本文关键字:窗口 SFML 显示      更新时间:2023-10-16

我最近一直在与SFML合作,在一旁创建一个小游戏——直到今晚,一切都出奇地快速和简单。我离开了平时的工作站,在windows8平板电脑/上网本上做了一些清理工作,游戏窗口不再出现。我可以运行程序,它立即完成,并要求我按任意键退出控制台。我做了一些研究,意识到我使用的SFML版本不支持ATI显卡,所以我将程序升级到2.0(目前基本上只是一个框架工作,所以升级只需要几个小时,我认为即使它不能解决所有问题,也不会有什么影响(。

不幸的是,它仍然没有出现。当我运行程序时,控制台会按预期显示,但图形/渲染窗口不会显示,而是在控制台上打印文本"按任意键继续。",就好像程序已经运行完毕一样。按下某个键会导致程序退出,返回值为0。

该程序使用Codelite用C++编写,并使用g++编译。我现在使用的是Windows 8 Professional平板电脑/上网本,虽然在我可以访问另一台电脑之前我无法对其进行测试,但它以前运行良好,我没有理由相信它在当前环境之外已经停止了测试。有什么建议吗?

[更新]:尝试在另一台电脑windows 7上运行它,但出现了一个新错误,即无法在libstdc++-6.dll中找到过程入口点__gxx_personality_v0。整个代码太大,无法发布,但我认为这不是问题所在,因为它似乎甚至没有进入main((。

#include <iostream>
#include <string>
#include <cstdio>
#include <cassert>
#include "sfml.hpp"
#include "angelscript.hpp"
#include "MapGenerator.hpp"
#include "TestState.hpp"
#include "std.hpp"
sf::RenderWindow* initialize();
void TestAngelScript();
void MessageCallback(const asSMessageInfo *msg, void *param);
int main()
{   
    std::cout << "Made it into main!!!" << std::endl;
    TestAngelScript();
    std::cout << "Initializing" << std::endl;
    sf::RenderWindow* App = initialize();
    TextureHandler::Initialize();
    FontHandler::Initialize();
    SoundHandler::Initialize();
    MusicHandler::Initialize();
    InputHandler::Initialize(App);
    StateEngine engine;
    IState* state = new TestState(App);
    engine.AddState(state);
    sf::Clock clock;
    while (App->isOpen())
    {
        sf::Event Event;
        while (App->pollEvent(Event))
        {
            // Window closed
            if (Event.type == sf::Event::Closed)
                App->close();
            // Escape key pressed
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
                App->close();
        }
        // Get elapsed time
        float ElapsedTime = clock.restart().asSeconds();
        engine.Update(ElapsedTime);
        App->clear();
        engine.Draw(App);
        App->display(); 
    }
    return EXIT_SUCCESS;
}
sf::RenderWindow* initialize()
{
    return new sf::RenderWindow(sf::VideoMode(800, 600, 32), "SFML Graphics");
}
// Print the script string to the standard output stream
void print(std::string &msg)
{
  printf("%s", msg.c_str());
}
void TestAngelScript()
{
    // Create the script engine
    asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
    // Set the message callback to receive information on errors in human readable form.
    int r = engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL); assert( r >= 0 );
    // AngelScript doesn't have a built-in string type, as there is no definite standard 
    // string type for C++ applications. Every developer is free to register it's own string type.
    // The SDK do however provide a standard add-on for registering a string type, so it's not
    // necessary to implement the registration yourself if you don't want to.
    RegisterStdString(engine);
    // Register the function that we want the scripts to call 
    r = engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(print), asCALL_CDECL); assert( r >= 0 );  

    // The CScriptBuilder helper is an add-on that loads the file,
    // performs a pre-processing pass if necessary, and then tells
    // the engine to build a script module.
    CScriptBuilder builder;
    r = builder.StartNewModule(engine, "MyModule"); 
    if( r < 0 ) 
    {
      // If the code fails here it is usually because there
      // is no more memory to allocate the module
      printf("Unrecoverable error while starting a new module.n");
      return;
    }
    r = builder.AddSectionFromFile("assets\scripts\test.as");
    if( r < 0 )
    {
      // The builder wasn't able to load the file. Maybe the file
      // has been removed, or the wrong name was given, or some
      // preprocessing commands are incorrectly written.
      printf("Please correct the errors in the script and try again.n");
      return;
    }
    r = builder.BuildModule();
    if( r < 0 )
    {
      // An error occurred. Instruct the script writer to fix the 
      // compilation errors that were listed in the output stream.
      printf("Please correct the errors in the script and try again.n");
      return;
    }
    // Find the function that is to be called. 
    asIScriptModule *mod = engine->GetModule("MyModule");
    asIScriptFunction *func = mod->GetFunctionByDecl("void main()");
    if( func == 0 )
    {
      // The function couldn't be found. Instruct the script writer
      // to include the expected function in the script.
      printf("The script must have the function 'void main()'. Please add it and try again.n");
      return;
    }
    // Create our context, prepare it, and then execute
    asIScriptContext *ctx = engine->CreateContext();
    ctx->Prepare(func);
    r = ctx->Execute();
    if( r != asEXECUTION_FINISHED )
    {
      // The execution didn't complete as expected. Determine what happened.
      if( r == asEXECUTION_EXCEPTION )
      {
        // An exception occurred, let the script writer know what happened so it can be corrected.
        printf("An exception '%s' occurred. Please correct the code and try again.n", ctx->GetExceptionString());
      }
    }
    // Clean up
    ctx->Release();
    engine->Release();
}
// Implement a simple message callback function
void MessageCallback(const asSMessageInfo *msg, void *param)
{
  const char *type = "ERR ";
  if( msg->type == asMSGTYPE_WARNING ) 
    type = "WARN";
  else if( msg->type == asMSGTYPE_INFORMATION ) 
    type = "INFO";
  printf("%s (%d, %d) : %s : %sn", msg->section, msg->row, msg->col, type, msg->message);
}

没有任何内容输出到控制台,请保存执行后的输入提示。程序返回0。

在IDE外运行程序会导致一些关于缺少dll的错误(libstdc++-6.dll和libgcc_s_sjj_1.dll(一旦提供了这些错误,我就会得到关于缺少过程入口点的错误。。。尽管在上网本上,它抱怨SFML音频dll中缺少相同的点。。。。

我知道这是一个老问题,但以防万一你仍在努力解决它。

在我看来,渲染窗口的初始化函数很可能是问题所在。您正在返回一个指向局部变量的指针,这将起作用,但您不会得到您的窗口。

要么你需要像一样在main之外声明你的应用指针

sf::RenderWindow* App;
void initialize() { /* Initialize render window here. */ };
...
int main() ...

或者,您可以选择更简单的解决方案,只在主函数中创建窗口。

...
int main () {
...
sf::RenderWindow App(sf::VideoMode(800, 600), "my window");
...
}

注意,如果这个窗口主要是通过创建的,那么您仍然可以将它传递到您的函数中

InputHandler::Initialize(&App);

其中,初始化函数应使用sf::RenderWindow*作为参数。

关于未出现的sfml窗口:这是一个已知的问题,当您的窗口超过屏幕的分辨率时就会发生。减少渲染窗口的高度和/或宽度通常可以解决这个问题。您也可以使用全屏模式。