2013-02-26 56 views
-1

正如标题所说,我已成功编译,链接并运行Linux Mint 14上的sfml-1.6中创建的程序。我使用g ++进行编译。然而,只要我将源文件移动到Windows并尝试编译到Windows的等效sfml库,它就会崩溃。该程序编译和链接就好,但它在达到主要之前崩溃。我确定我没有任何全局对象/变量在我自己的.h文件中。但我无法为sfml库做到这一点。我怀疑这可能是因为库被破坏,所以我移植了sfml 2.0的代码,问题依然存在。任何线索我做错了什么?我也确保我不使用任何平台特定的标题。当移植到窗口时程序崩溃

这里是我的main.cpp

#include <SFML/System.hpp> 
#include <SFML/Window.hpp> 
#include <SFML/Graphics.hpp> 
#include "World.h" 
#include <sstream> 
#include <vector> 
#include <zip.h> 

enum states 
{ 
    MENU, 
    GAME, 
    PAUSE, 
    EXIT, 
    DIE 
}; 
player plr; 
sf::Clock timer; 
void getKeys(const sf::Input& input); 
std::string floatToString(float flt); 

int main(int argc, char** argv) 
{ 
    sf::RenderWindow window(sf::VideoMode(1024, 768), "sfTest"); 
    window.SetFramerateLimit(60); 
    sf::Music startSong; 
    sf::Music endSong; 
    sf::Music s1, s2, s3; 
    sf::String musicBy; 
    musicBy.SetText("Music by Kevin McLeod: www.incompetech.com"); 
    if(!s1.OpenFromFile("Batty McFaddin.ogg")) 
     return -1; 
    if(!s2.OpenFromFile("Merry Go.ogg")) 
     return -1; 
    if(!s3.OpenFromFile("One-eyed Maestro.ogg")) 
     return -1; 
    if(!endSong.OpenFromFile("One-eyed Maestro.ogg")) 
     return -10; 


    if(!startSong.OpenFromFile("Comedic Juggernaut.ogg")) 
     return -15; 

    endSong.SetVolume(50); 

    std::vector<sf::Music*> playList; 
    std::vector<sf::Music*>::iterator mIt; 
    s1.SetVolume(25); 
    s2.SetVolume(25); 
    s3.SetVolume(25); 
    playList.push_back(&s2); 
    playList.push_back(&s3); 
    playList.push_back(&s1);  

    startSong.SetLoop(true); 
    startSong.SetVolume(50); 
    const sf::Input& input = window.GetInput(); 

    plr.setSpeed(.5); 
    plr.setMaxSpeed(15.0); 
    plr.setJumpForce(.4f); 
    plr.setJumpSpeed(16.f); 

    int state = MENU; 
    sf::String str; 
    str.SetText("Welcome to fatrunning!"); 
    sf::String str2; 
    str2.SetText("Developed by Echoes Entertainment"); 
    str2.Move(0, 100); 
    sf::String cont; 
    cont.SetText("Please press enter to play..."); 
    cont.Move(0, 30); 
    sf::String spd; 

    sf::String menuHelp; 
    menuHelp.SetText("Stand on the block equivalent to the level (0-15) and press TAB"); 
    menuHelp.Move(0, 30); 
    float endTime = 60*5; 
    float curTime = 0; 

    sf::Sprite menu; 
    sf::Image mim; 
    mim.LoadFromFile("MenuBackground.png"); 
    menu.SetImage(mim); 

    sf::String scr; 
    sf::String thanks("Thanks for playing! Your score was: "); 
    //sf::String tarantulas("        Tarantual tarantulas   Everybody loves tarantulas   if there's just fuzz where your hamster was   it's propably because of tarantulas            They're so soft   and they're so furry   And they're so cute   if your vision's blurry   all of mine got free   but dont you worry   though they're crawling up your wall in a big hurry         Tarantual tarantulas   Everybody loves tarantulas   if there's just fuzz where your hamster was   it's propably because of tarantulas            Don't look now but i have a feeling   there's one above you on your ceiling   but when they crawl they never fall     unless the person under them is nervous at all       Tarantual tarantulas   Everybody loves tarantulas   if there's just fuzz where your hamster was    it's propably because of tarantulas     \t  \t     can you feel that itch on the top of your head   it could be one of them crawling instead   but it wont bite unless it senses fear   so just stay calm til' it's gone in a year    \t\t        Tarantual tarantulas   Everybody loves tarantulas   if there's just fuzz where your hamster was   it's propably because of tarantulas   Tarantual tarantulas   Everybody loves tarantulas   if there's just fuzz where your hamster was   it's propably because of tarantulas   if there's just fuzz where your hamster was   it's propably because of tarantulas   Tarantulas tarantulas   it's propably because of tarantulas"); 
    //.tarantulas.Move(3000, 0); 
    thanks.Move(0, 30); 
    scr.Move(500, 30); 

    World world; 
    world.addLevel("lvl1", "lvl1.txt"); 
    world.addLevel("lvl2", "lvl2.txt"); 
    world.addLevel("lvl3", "lvl3.txt"); 
    world.addLevel("lvl4", "lvl4.txt"); 
    world.addLevel("lvl5", "lvl5.txt"); 
    world.addLevel("lvl6", "lvl6.txt"); 
    world.addLevel("lvl7", "lvl7.txt"); 
    world.addLevel("lvl8", "lvl8.txt"); 
    world.addLevel("lvl9", "lvl9.txt"); 
    world.addLevel("lvl10", "lvl10.txt"); 
    world.addLevel("lvl11", "lvl11.txt"); 
    world.addLevel("lvl12", "lvl12.txt"); 
    world.addLevel("lvl13", "lvl13.txt"); 
    world.addLevel("lvl14", "lvl14.txt"); 
    world.addLevel("lvl15", "lvl15.txt"); 
    world.setMenuLevel("menu.txt"); 
    world.regPlr(plr); 
    bool once = false; 
    bool unodos = false; 
    float finalScore = 0; 

    while (state != DIE) 
    { 
     sf::Event e; 
     while (window.GetEvent(e)) 
     { 
      if (e.Type == sf::Event::Closed) 
      { 
       window.Close(); 
       state = EXIT; 
      } 
      if(e.Type == sf::Event::KeyPressed) 
      { 
       if(e.Key.Code == sf::Key::Return) 
       { 
        if(!unodos) 
        { 
         state = GAME; 
         unodos = true; 
        } 
       } 
      } 
     } 
     getKeys(input); 
     window.Clear(); 
     switch(state) 
     { 
      case MENU: 
       if(startSong.GetStatus() == sf::Sound::Stopped) 
        startSong.Play(); 
       window.Draw(menu); 
       window.Draw(str); 
       window.Draw(str2); 
       window.Draw(cont); 
       break; 
      case GAME: 
       if(startSong.GetStatus() == sf::Sound::Playing) 
       { 
        mIt = playList.begin(); 
        (*mIt)->Play(); 
        startSong.Stop(); 
       } 
       if((*mIt)->GetStatus() == sf::Sound::Stopped) 
       { 
        mIt++; 
        if(mIt == playList.end()) 
         mIt = playList.begin(); 
        (*mIt)->Play(); 
       } 
       timer.Reset(); 
       spd.SetText(floatToString(endTime-plr.time)); 
       world.drawWorld(window); 
       world.update(input); 
       plr.update(); 
       window.Draw(plr.getSprite()); 
       window.Draw(spd); 
       plr.time+=timer.GetElapsedTime(); 
       if(plr.time >= endTime || world.quit) 
       { 
        state = EXIT; 
        plr.time = endTime; 
       } 
       if(world.atMenu) 
        state = PAUSE; 
       break; 
      case PAUSE: 
       if(startSong.GetStatus() == sf::Sound::Playing) 
        startSong.Stop(); 
       spd.SetText(floatToString(endTime-plr.time)); 
       plr.update(); 
       world.drawWorld(window); 
       world.update(input); 
       window.Draw(plr.getSprite()); 
       window.Draw(spd); 
       window.Draw(menuHelp); 
       if(!world.atMenu) 
        state = GAME; 
       if(world.quit) 
        state = EXIT; 
       break; 
      case EXIT: 
       if(!once) 
       { 
        endSong.Play(); 
        (*mIt)->Stop(); 
        world.addLevel("end", "end.txt"); 
        world.setLevel("end"); 
        once = true; 
        finalScore = plr.score; 
        finalScore += (endTime-plr.time); 
       } 
       //tarantulas.Move(-3.5, 0); 
       plr.update(); 
       world.drawWorld(window); 
       window.Draw(plr.getSprite()); 
       scr.SetText(floatToString(finalScore)); 
       //window.Draw(tarantulas); 
       window.Draw(musicBy); 
       window.Draw(thanks); 
       window.Draw(scr); 
       if(world.curLvl->atEnd) 
        state = DIE; 
       break; 
      case DIE: 
       window.Close(); 
       break; 
      default: 
       break; 
     } 
     window.Display(); 
    } 

    return 0; 
} 

std::string floatToString(float flt) 
{ 
    std::stringstream ss; 
    ss << flt; 
    return ss.str(); 
} 

void getKeys(const sf::Input& input) 
{ 
    if(input.IsKeyDown(sf::Key::D)) 
    { 
     plr.isAcc = true; 
     plr.right(); 
    }   
    else if(input.IsKeyDown(sf::Key::A)) 
    { 
     plr.isAcc = true; 
     plr.left(); 
    } 
    if(input.IsKeyDown(sf::Key::Space)) 
    { 
     plr.isJump = false; 
     plr.jump(); 
    } 
    if(!input.IsKeyDown(sf::Key::D) && !input.IsKeyDown(sf::Key::A)) 
    { 
     plr.isAcc = false; 
    } 
    if(!input.IsKeyDown(sf::Key::Space)) 
     plr.isJump = true; 
} 

这是整个main.cpp中,如果你需要看其他来源,问我。但程序崩溃之前,它达到主要。它甚至没有达到枚举定义。

+0

你有一个调用堆栈?它指向什么? – 2013-02-26 13:51:38

+1

你说它崩溃。那是什么意思?有错误讯息?你在调试器中运行它吗? – 2013-02-26 13:51:42

+0

当你说它崩溃时,它会抛出一个崩溃后的错误或类似的东西吗? – 2013-02-26 13:51:44

回答

2

这可能是由于在具有静态生命期的对象的构造函数中存在任何未定义或未指定的行为。然而,频率最高的原因之一是初始化 问题的顺序。使用静态生命周期的一些对象在其构造函数中使用静态生命期的其他 对象?例如, 播放器的构造函数是否使用在 世界中定义的静态对象?甚至是来自SFML的东西? (看一眼就显示了一些 对象与库静态寿命。你不应该使用 任何这些在你的任何对象的构造函数的静态 寿命。)

+0

当我想到它时,也许我想我有一个解释另一个类的构造函数的类。我看看它 – Alx101 2013-02-26 16:31:33

0

这是可能的崩溃是由于图书馆如何EXE被编译,以及如何编译...检查你的代码生成标志,字符集之间的一些不兼容,等等

+3

在C++中,'main()'之前的崩溃可能来自全局对象的构造函数。我会看那里。 – 2013-02-26 14:00:44

+0

正如你可以在我的文章中看到的,我没有全局对象。不过,我也尝试重新编译整个库以及使用预编译的库。 – Alx101 2013-02-26 14:07:17

+3

什么是那些如果不是全球性的对象:球员plr; sf ::时钟定时器; – 2013-02-26 14:10:19

1

在调试器中运行程序(windbg.exe),它应该告诉你发生异常的位置,并在输出窗口中提供有用的进度文本,介绍迄今为止已加载的内容以及任何“吞食”异常,并且可以从此处取出异常。