-1

As the title says, I have successfully compiled, linked and ran a program made in sfml-1.6 on Linux Mint 14. I used g++ for compiling. However as soon as I moved the source files to Windows and tried to compile to the Windows equivalent of the sfml libraries, it crashes. The program compiles and links just fine but it crashes BEFORE it reaches main. I have made sure I dont have any global objects/variables in my own .h files. But I cannot do it for the sfml library. I suspected that it might be because the library was broken so I ported the code for sfml 2.0., and the problem persists. Any clues on what I am doing wrong? I have also made sure I dont use any platform specific headers.

Here is my 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;
}

That is the whole main.cpp, if you need to look at the other sources, ask me. But the program crashes BEFORE it reaches main. It doesn't even reach the enum define.

bash.d
  • 13,029
  • 3
  • 29
  • 42
LoKat
  • 1,179
  • 10
  • 18

3 Answers3

2

This could be due to any undefined or unspecified behavior in the constructors of objects with static lifetime. One of the most frequence causes, however, is order of initialization issues. Do some of your objects with static lifetime use other objects with static lifetime in their constructor? For example, does the constructor for player use a static object defined in World? Or even something from SFML? (A quick glance shows some objects with static lifetime in the library. You shouldn't use any of these in any of your constructors of objects with static lifetime.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • When i come to think about it, maybe, i think i have a class that interprets another class's constructor. Ill have a look at it – LoKat Feb 26 '13 at 16:31
1

Run the program in the debugger (windbg.exe), that should show you where the exception occurs, with helpful progress text in the output window about what's been loaded so far, any 'swallowed' exceptions, and you can take it from there.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
0

It's possible the crash is due to some incompatibility between how the EXE was compiled and how the library was compiled... check your code-generation flags, character sets, etc.

mark
  • 5,269
  • 2
  • 21
  • 34
  • 3
    In C++ a crash before `main()` can originate from constructors of global objects. I'd look there. – Alexey Frunze Feb 26 '13 at 14:00
  • As you can read in my post, i have no global objects. However i have also tried to recompile the whole library as well as use ready-compiled ones. – LoKat Feb 26 '13 at 14:07
  • 3
    What are those if not global objects: player plr; sf::Clock timer; – J_D Feb 26 '13 at 14:10
  • @user1935069: You did say, "I cannot do it for the sfml library", so it is not unfair to suppose that your program has global objects somewhere even if you did not declare them. `std::cin`, is a global object, for instance. But, as @J_D pointed out, `plr` and `timer` ***are global*** and in your code snippet. I'm afraid you'll have to look harder. – johnsyweb Feb 26 '13 at 14:10
  • I will take a look at the constructors though. The code above has changed in my windows version where i took out all of the global objects i made. – LoKat Feb 26 '13 at 14:11
  • 1
    The issue isn't global; it's static lifetime. – James Kanze Feb 26 '13 at 14:43