Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Weird Crash  (Read 2504 times)

0 Members and 1 Guest are viewing this topic.

Eragon0605

  • Newbie
  • *
  • Posts: 5
    • View Profile
Weird Crash
« on: June 14, 2010, 06:27:26 am »
When I compile and run the below program and move my mouse around the screen a few times (it also helps a bit if you click around), the program closes. The weird thing is that I get a 0x0 "EXIT_SUCCESS" message when the program exits. It happens more often if you put more commands and event checks and stuff in. (The comments are for my own benefit; I'm not trying to make everybody look like a noob just because I am one)

Code: [Select]

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
    //Creating variables that will define the size of the game window
    int Xres = 1440, Yres = 900;

    // Creates a RenderWindow type window
    sf::RenderWindow App(sf::VideoMode(Xres, Yres, 32), "Awesomely Cool Window", sf::Style::Fullscreen);
    App.UseVerticalSync(true);

    //Creates a font
    sf::Font MyFont;

    //Creates a buffer and loads the sound file
    sf::SoundBuffer Buffer;
    if (!Buffer.LoadFromFile("files/Menu.ogg"))
        return EXIT_FAILURE;

    //Loads the font file
    if (!MyFont.LoadFromFile("files/Achafexp.ttf", 200))
        return EXIT_FAILURE;

    //Creates a sound
    sf::Sound Sound;

    //Creates some text and and all the settings
    sf::String Text("Hello", MyFont, 200);
    Text.SetColor(sf::Color(0, 0, 0));
    Text.SetRotation(0.f);
    Text.SetScale(0.f, 0.f);
    Text.Move(100.f, 200.f);

    //Creates an image and loads the image file
    sf::Image Image;
    if (!Image.LoadFromFile("files/smiley.png"))
        return EXIT_FAILURE;

    // Creates a couple sprites using the image created above
    sf::Sprite CoolSprite(Image);
    sf::Sprite CoolSprite2(Image);

    //All the settings for the sprites
    CoolSprite.SetColor(sf::Color(255, 255, 255, 255));
    CoolSprite.SetPosition(132.f, 42.f);
    CoolSprite.SetCenter(32, 32);

    CoolSprite2.SetColor(sf::Color(255, 0, 0, 255));
    CoolSprite2.SetPosition(42.f, 42.f);
    CoolSprite2.SetCenter(32, 32);

    //Assignes the buffer made above to the sound made above, effectively assigning the sound file to the sound
    Sound.SetBuffer(Buffer);

    //Loops the sound
    Sound.SetLoop(true);

    //Plays the sound
    Sound.Play();

    // Starts the main loop thing
    while (App.IsOpened())
    {
        //Makes the window do stuff when the user does stuff
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if ((Event.Type == sf::Event::Closed) || (Event.Key.Code == sf::Key::Escape) || (Event.Key.Code == sf::Key::F4 && Event.Key.Alt))
                App.Close();

            if (Event.Key.Code == sf::Key::F1)
            {
                sf::Image Screen = App.Capture();
                Screen.SaveToFile("screenshot.jpg");
            }

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::P))
            {
                if (Sound.GetStatus() == sf::Sound::Playing)
                    Sound.Pause();
                else
                    Sound.Play();
            }

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::O))
                Sound.Stop();
        }

        //Gets the FPS
        float ElapsedTime = App.GetFrameTime();

        //Makes the sprites move and spin around when certain keys are pressed
        if (App.GetInput().IsKeyDown(sf::Key::Left))  CoolSprite.Move(-500 * ElapsedTime ,0);
        if (App.GetInput().IsKeyDown(sf::Key::Right)) CoolSprite.Move( 500 * ElapsedTime ,0);
        if (App.GetInput().IsKeyDown(sf::Key::Up))    CoolSprite.Move(0, -500 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Down))  CoolSprite.Move(0,  500 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Add))      CoolSprite.Rotate(-200 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Subtract)) CoolSprite.Rotate(+200 * ElapsedTime);

        if (App.GetInput().IsKeyDown(sf::Key::A))  CoolSprite2.Move(-500 * ElapsedTime ,0);
        if (App.GetInput().IsKeyDown(sf::Key::D)) CoolSprite2.Move( 500 * ElapsedTime ,0);
        if (App.GetInput().IsKeyDown(sf::Key::W))    CoolSprite2.Move(0, -500 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::S))  CoolSprite2.Move(0,  500 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::E))      CoolSprite2.Rotate(-200 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Q)) CoolSprite2.Rotate(+200 * ElapsedTime);

        //Makes the sprites go back to their exact original positions when the spacebar is pressed
        if (App.GetInput().IsKeyDown(sf::Key::Space))
        {
            CoolSprite2.SetPosition(42.f, 42.f);
            CoolSprite.SetPosition(132.f, 42.f);
            CoolSprite.SetRotation(0.f);
            CoolSprite2.SetRotation(0.f);
        }

        //Finds the center of the screen from the resolution the user entered at the beginning of the program
        int xcenter, ycenter;
        xcenter = Xres / 2;
        ycenter = Yres / 2;

        //Set Rectangle lengths in a X1, Y1, X2, Y2, Color, [OutlineSize], [OutlineColor] format.
        sf::Shape Rect = sf::Shape::Rectangle(200, 200, 400, 400, sf::Color::Green, 20, sf::Color::Red);
        Rect.SetColor(sf::Color(255, 255, 255, 200));
        Rect.SetCenter(300, 300);
        Rect.SetPosition(xcenter, ycenter);

        // Clear the screen with a color (R, G, B) format
        App.Clear(sf::Color(255, 255, 225));

        // Draws the sprites, rectangle, and text on the screen
        App.Draw(CoolSprite);
        App.Draw(CoolSprite2);
        App.Draw(Rect);
        App.Draw(Text);

        // Opens the window
        App.Display();
    }

    return EXIT_SUCCESS;
}

Eragon0605

  • Newbie
  • *
  • Posts: 5
    • View Profile
Uhh....
« Reply #1 on: June 14, 2010, 06:53:59 am »
Alright, well, I just ran it and looked at the system monitor and it seems my program takes up 81.6 MB of memory.:shock: Yes, the program you see above takes up over 80 MB of memory. That just doesn't seem right at all. Oh, and it seems that I forgot to put my system specs in the above post, so here goes:

OS: Ubuntu 10.04
CPU: Core-i5 750 @ 2.66 GHz per core (4 cores)
Memory: 2 GB @ 1066 MHz
Compiler/IDE: GCC/Codeblocks

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Weird Crash
« Reply #2 on: June 14, 2010, 07:15:46 am »
Someone else would be able to explain it better so I'll just tell you that you need to check the type of the event before checking what the event is.

This fixed the issue for me.

Code: [Select]
if (Event.Type == sf::Event::Closed)
    App.Close();
else if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape)
    App.Close();
else if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::F4 && Event.Key.Alt)
    App.Close();


Note the "Event.Type == sf::Event::KeyPressed" part.

Why are you checking for Alt+F4? doesn't ubuntu send a close event when you Alt+F4 anyway?

As for the memory usage - not sure. I cut out almost all of the program except the event handling as I don't have the same files as you. How big is the sound you are loading? Maybe the RAM usage isn't being reported correctly or perhaps the system monitor tool is seeing code::blocks memory as well or something. Try run it independently of C::B?

Eragon0605

  • Newbie
  • *
  • Posts: 5
    • View Profile
Weird Crash
« Reply #3 on: June 14, 2010, 07:30:51 am »
Ah, thanks! No more crashing! I would still like to know why my program takes up so much memory (anywhere from 70 to even 100 mb). I also have a Tic-Tac-Toe program that runs at a much more respectable 10-15 mb, but uses %100 of two cores (doesn't touch the other two). Why are these programs taking up so much resources? It doesn't really bug me, but it seems a bit ridiculous. Oh, and it seems Ubuntu doesn't automatically close a window when I press Alt+F4. It seems to be X11 that does it; when I run SFML in an Xlib window, it will automatically close the program with Alt+F4 without me having to say so.

 

anything