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

Author Topic: Unhandled exception: Integer division by zero.  (Read 5295 times)

0 Members and 1 Guest are viewing this topic.

Hellgast23

  • Newbie
  • *
  • Posts: 6
    • View Profile
Unhandled exception: Integer division by zero.
« on: February 11, 2013, 04:47:26 pm »
Hello, I've been searching for a solution on this error for some time now, but I don't seem to be able to find a fix.
I'm using SFML 2.0, visual C++ 2010, Release mode, dynamic libs.

The program first displays a startmenu window, after pressing 'S' it displays the game window.
Then it should show the 4 spEddStill sprites in a loop, each for 200 milliseconds. But after the first loop or the 4 sprites, it gives an error:

Unhandled exception at 0x0fdba57f in The Next Project.exe: 0xC0000094: Integer division by zero.

Here is the Full code:

The "animation" is at line 126, the error is at line 160

// Headers die nodig zijn
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

// Functie verklaringen



int main()
{
        // Een variable die zal beslissen bij welk deel het spel zit
        int WelkDeel = 0; // (0 - Start Menu) (1 - Speel wereld) (2 - Game over menu )
        bool PlayGame = true;

        // Maak een het StartMenuWindow
        sf::RenderWindow wStartMenu(sf::VideoMode(800, 600), "Edd's Adventure Start Menu!");

        // Loop die zorgt dat het spel controleer welk kader er moet geopent worden
        while (PlayGame)
        {
                switch (WelkDeel) //Bepaald welk kader er moet gemaakt worden
                {
                case 0:// Start Menu
                       
                        // Start Menu frame loop
                        while (wStartMenu.isOpen())
                        {
                                sf::Event evStartMenu;
                                while (wStartMenu.pollEvent(evStartMenu))
                                {
                                        if (evStartMenu.type == sf::Event::Closed) // Close Button:  Stop game
                                        {
                                                PlayGame = false;
                                                wStartMenu.close();
                                        }
                                        if ((evStartMenu.type == sf::Event::KeyPressed) && (evStartMenu.key.code == sf::Keyboard::Escape)) // Escape Button:  Stop game
                                        {
                                                PlayGame = false;
                                                wStartMenu.close();
                                        }
                                        if ((evStartMenu.type == sf::Event::KeyPressed) && (evStartMenu.key.code == sf::Keyboard::S)) // S Button: Start Spel
                                        {
                                                WelkDeel = 1;
                                                wStartMenu.close();

                                        }
                                }//End event loop

                                //Clears the previous frame
                                wStartMenu.clear(sf::Color(100,0,0,255));

                                //Draws the new frame

                                //Displays the new rendered fram
                                wStartMenu.display();

                        }//end StartMenu frame Loop
                        break;

                case 1: //Speel wereld
                        //Variabelen
                        bool EdStandStill = true; // Stored is edd is standing still or not
       
                        //Geef de textures een bestand
                        sf::Texture txEddStill[3];
                        txEddStill[0].loadFromFile("EddStandStill1.png");
                        txEddStill[1].loadFromFile("EddStandStill2.png");
                        txEddStill[2].loadFromFile("EddStandStill3.png");
                        txEddStill[3].loadFromFile("EddStandStill4.png");

                        //Zet de afbeeldingen om naar een sprite
                        sf::Sprite spEddStill[3];
                        spEddStill[0].setTexture(txEddStill[0]);
                        spEddStill[1].setTexture(txEddStill[1]);
                        spEddStill[2].setTexture(txEddStill[2]);
                        spEddStill[3].setTexture(txEddStill[3]);

                        //Zet de sprites op een basis plaats
                        spEddStill[0].setPosition(100,100);
                        spEddStill[1].setPosition(100,100);
                        spEddStill[2].setPosition(100,100);
                        spEddStill[3].setPosition(100,100);

                        //Zet de sprites op de juiste groote
                        sf::Vector2f EddStillScale(0.3f,0.3f);
                        spEddStill[0].setScale(EddStillScale);
                        spEddStill[1].setScale(EddStillScale);
                        spEddStill[2].setScale(EddStillScale);
                        spEddStill[3].setScale(EddStillScale);

                        //Maak een pointer die het adress zal opslaan van het te tekenen standstill texture
                        sf::Sprite* pEddStill;

                        // Maak het Speel window aan
                        sf::RenderWindow wSpeel(sf::VideoMode(800, 600), "Ed's Adventure Game!");

                        //Een klok die zal meten hoeland de standstill animatie duurd
                        sf::Clock StandStillClock;
                        //Een klok die de frame tijd meet
                        sf::Clock clSpeel;

                        //Start Speel frame loop
                        while (wSpeel.isOpen())
                        {
                                // Measure the time of a frame
                                sf::Time ElapsedTimeSpeel1 = clSpeel.getElapsedTime();

                                //capture the inputs
                                sf::Event evSpeel;
                                while (wSpeel.pollEvent(evSpeel))
                                {
                                        if (evSpeel.type == sf::Event::Closed) // Close Button:  Stop game
                                        {
                                                PlayGame = false;
                                                wSpeel.close();
                                        }
                                        if ((evSpeel.type == sf::Event::KeyPressed) && (evSpeel.key.code == sf::Keyboard::Escape)) // Escape Button:  Stop game
                                        {
                                                PlayGame = false;
                                                wSpeel.close();
                                        }
                                }

                                //Make the standstill animation by changing the pointer
                                if (EdStandStill)
                                {
                                        sf::Time StandStillTime = StandStillClock.getElapsedTime();
                                        int StandStillTimeInt = StandStillTime.asMilliseconds();
                                        int TimeBetweenStandStillAnimations = 200;
                                        if (StandStillTimeInt < TimeBetweenStandStillAnimations)
                                        {
                                                pEddStill = &spEddStill[0];
                                        }else if (StandStillTimeInt < TimeBetweenStandStillAnimations * 2){
                                                pEddStill = &spEddStill[1];
                                        }else if(StandStillTimeInt < TimeBetweenStandStillAnimations * 3){
                                                pEddStill = &spEddStill[2];
                                        }else if(StandStillTimeInt < TimeBetweenStandStillAnimations * 4){
                                                pEddStill = &spEddStill[3];
                                        }else{
                                                StandStillClock.restart();
                                        }
                                }

                               
                                //if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))   // left key is pressed: Move edd left
                                //{
                                        //*pEddStill
                        //      }


                                //Clears the previous frame
                                wSpeel.clear(sf::Color(0,100,0,255));

                                //Draws the new frame
                                sf::Sprite temp1 = *pEddStill;
                                wSpeel.draw(temp1);

                                //Displays the new rendered fram
                                wSpeel.setFramerateLimit(30);
                                wSpeel.display();

                        }//End Speel frame loop
                        break;

                }//end Main Switch

        }//end Main Loop

    return EXIT_SUCCESS;
}
« Last Edit: February 11, 2013, 05:04:05 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Unhandled exception: Integer division by zero.
« Reply #1 on: February 11, 2013, 05:05:01 pm »
Your sprites and textures arrays have 3 elements, but you use 4. The last one (xxx[3]) doesn't exist.
Laurent Gomila - SFML developer

Hellgast23

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Unhandled exception: Integer division by zero.
« Reply #2 on: February 11, 2013, 05:09:00 pm »
Thank you for the quick reply,
Made me feel dumb tho  ::)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Unhandled exception: Integer division by zero.
« Reply #3 on: February 11, 2013, 08:03:57 pm »
If you use std::array or std::vector, such errors are immediately visible in debug mode. The STL containers can save you a lot of trouble (and std::array is not slower).

With a C++11 conforming compiler, there is absolutely no reason to use raw C arrays anymore.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: