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

Author Topic: Divide by 0 when Drawing a texture. Attempting to animate objects  (Read 1981 times)

0 Members and 1 Guest are viewing this topic.

MephilesTH

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hi, I get an error that says it's dividing by 0 when I attempt to have the images drawn on screen.

Oddly enough there was no issue in a prior test a made with the animation class.

So to start off, I'm gonna show you some of the sample code I've got that works in the main.cpp file.
Quote
sf::Texture left1, left2, left3; //11 *23 = 23 + 230 = 253
   left1.loadFromFile("chao.png",sf::IntRect(220, 25,21,25)); //stand
   left2.loadFromFile("chao.png", sf::IntRect(240,25,21,25)); //left foot
   left3.loadFromFile("chao.png",sf::IntRect(280,25,21,25)); //right foot

   anim1.addFrameTexture(left1);
   anim1.addFrameTexture(left2);
   anim1.addFrameTexture(left1);
   anim1.addFrameTexture(left3);
   anim1.Loop();
   anim1.setDelay((long)750);
   anim1.setScale(2,2);
   anim1.setPosition(0,50);
        ...
        while(window.isOpen()) {
        ...
        window.draw(anim1.getCurrentSprite());
        }

However, I have this other class that is an actor in the scene... but when I attempt to have it move around. 1) it shows a blank box on all other animations. 2) when it attempts to play the rightward animation, it crashes giving the error I spoke of.

Quote
Chao::Chao() {
       ...
       sf::Texture right1, right2, right3;
   right1.loadFromFile("chao.png", sf::IntRect(220,25,21,25));
   right2.loadFromFile("chao.png", sf::IntRect(280,25,21,25));
   right3.loadFromFile("chao.png", sf::IntRect(240,25,21,25));
   sf::Sprite rSprite1, rSprite2, rSprite3;
   rSprite1.setTexture(right1);
   rSprite2.setTexture(right2);
   rSprite3.setTexture(right3);
   animations[2].addFrame(rSprite1);
   animations[2].addFrame(rSprite2);
   animations[2].addFrame(rSprite1);
   animations[2].addFrame(rSprite3);
        ....
        for(size_t i = 0; i < animations.size(); i++) {
      animations.setPosition(xCor, yCor);
      animations.setVisible(false);
      animations.setScale(xScale,yScale);
      animations.setDelay(frameSpeed);
      animations.Loop();
   }
        animations[2].setScale(-2.f,2.f);
}

sf::Sprite & Chao::getCurrentSprite() {
   return animations[currentAnimation].getCurrentSprite();
}

animation and chao classes both respectively have an update class as well.  Chao's update class is suppose to decide on a direction and a speed for the x and y variables and start playing and showing the animations respectively.

The animation class' update takes the clock_t time and based on a set delay, changes the frame.

Can you help me figure out why SFML is dividing by 0? and why I'm getting a blank box for the textures coming out of the chao class?
« Last Edit: June 10, 2014, 07:23:30 pm by MephilesTH »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Divide by 0 when Drawing a texture. Attempting to animate objects
« Reply #1 on: June 10, 2014, 08:14:08 pm »
Please write a complete and minimal example that shows the problem. Chances are it is your code and not SFML that is causing the issue, not to mention the code you showed us doesn't even include division.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Divide by 0 when Drawing a texture. Attempting to animate objects
« Reply #2 on: June 10, 2014, 08:22:10 pm »
To know what "minimal complete example" means, please read this post. It is crucial that you read it carefully.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MephilesTH

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Divide by 0 when Drawing a texture. Attempting to animate objects
« Reply #3 on: June 11, 2014, 04:25:23 am »
Oookay... well here is the Call Stack I have been getting:
Quote
>   msvcp110d.dll!std::_Debug_message(const wchar_t * message, const wchar_t * file, unsigned int line) Line 15   C++
    SFML Test 2.exe!std::vector<sf::Sprite,std::allocator<sf::Sprite> >::operator[](unsigned int _Pos) Line 1140   C++
    SFML Test 2.exe!Animation::getCurrentSprite() Line 167   C++
    SFML Test 2.exe!Chao::getCurrentSprite() Line 170   C++
    SFML Test 2.exe!main() Line 113   C++
    SFML Test 2.exe!__tmainCRTStartup() Line 536   C
    SFML Test 2.exe!mainCRTStartup() Line 377   C
    kernel32.dll!7602338a()   Unknown
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]   
    ntdll.dll!77669f72()   Unknown
    ntdll.dll!77669f45()   Unknown

As for the methods that would apply, this is the update methods for

Animation:
Quote
void Animation::update(clock_t currentTime) {
   if(running == true) {
      if( (long)(currentTime - lastChanged) >= frameSpeedDelay) {
         currentFrame++;
         lastChanged = currentTime;
      }

      if((size_t)currentFrame >= sprites.size() && loop == true) {
         currentFrame = 0;
      } else if((size_t)currentFrame >= sprites.size() && loop == false) {
         currentFrame = sprites.size()-1;
         stop();
      }

   } else {
      lastChanged = currentTime;
   }
}

and Chao:
Quote
void Chao::update(clock_t currentTime) {
   if( (currentTime - lastMoveDecision) >= 5000) {
      int random = rand() % animations.size();
      animations[currentAnimation].stop();
      animations[currentAnimation].hide();
      switch(random) {
      case 0: //stand still
         currentAnimation = 0;
         xSpeed = 0;
         ySpeed = 0;
         break;
      case 1: //mostly leftward movement
         currentAnimation = 1;
         setTwoSpeedsOneLesser(xSpeed, ySpeed, false, randBool());
         break;
      case 2: //mostly rightward movement
         currentAnimation = 2;
         setTwoSpeedsOneLesser(xSpeed, ySpeed, true, randBool());
         break;
      case 3: //mostly upward movement
         setTwoSpeedsOneLesser(ySpeed, xSpeed, false, randBool());
         currentAnimation = 3;
         break;
      case 4: //mostly downward movement
         setTwoSpeedsOneLesser(ySpeed, xSpeed, true, randBool());
         currentAnimation = 4;
         break;
      default: //stand still
         currentAnimation = 0;
         xSpeed = 0;
         ySpeed = 0;
         break;
      }
      animations[currentAnimation].start(currentTime);
      animations[currentAnimation].show();
      lastMoveDecision = currentTime;
   } else {
      float newX = xCor + xSpeed;
      float newY = yCor + ySpeed;
      animations[currentAnimation].update(currentTime);
      if(newX + (22 * xScale) >= endXWall) {
         newX = endXWall - (22 * xScale);
      } else if(newX < 0) {
         newX = 0;
      }
      if(newY + (25 *yScale) >= endYWall) {
         newY = endYWall - (25*yScale);
      } else if(newY < 0) {
         newY = 0;
      }
      xCor = newX;
      yCor = newY;
      keepImagesSamePosScale();
   }
   cout << "Current Animation: " << currentAnimation << "\n";

}

I have since originally posting fixed a small part of the problem, the error is now a "vector subscript out of range" and still breaks into SFML.

But still, it either A) shows white boxes from the images drawn out of the chao class... or B) crashes and gives me the as mentioned error.

as for the code I've left out in chao and main classes, as your "minimal complete" guide, I was attempting to minimalize how much code was shown, as much of it could be considered repeat with slightly different parameters; and thus could be overwhelming.

If you would like me to include the initilization in it's complete form, I can.

The two methods that the chao update class calls within itself are:

keepImageSamePosScale():
Quote
void Chao::keepImagesSamePosScale() {
   for(size_t i = 0; i < animations.size(); i++) {
      animations.setPosition(xCor, yCor);
      animations.setScale(xScale, yScale);
      animations.setDelay(frameSpeed);
   }
}
This method is intended to keep all the animations with the same position, scale and delay speed.

setTwoSpeedsOneLesser:
Quote
void Chao::setTwoSpeedsOneLesser(float& greater, float& lesser, bool greaterPos, bool lesserPos) {
   greater = (float)(1 + (rand() % 2));
   if(greater > 0) {
      lesser = (float)(rand() % (int)greater);
   } else {
      lesser = 0;
   }
   if(greaterPos == false) {
      greater = greater * -1;
   } if(lesserPos == false) {
      lesser = lesser * -1;
   }
}
which is suppose to generate a random value for both the x and y speeds.

and then randBool() is just suppose to generate one of two numbers and create a true false value.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Divide by 0 when Drawing a texture. Attempting to animate objects
« Reply #4 on: June 11, 2014, 08:05:28 am »
I don't see in the callstack how it would crash in SFML. All I see is a some calls originating from the animation class.

What compiler do you use? Where did you get SFML from?

Also the posted code is not considered a minimal and complete example.

At best you just run a debugger and track all the relevant variables to see which ones are invalid.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MephilesTH

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Divide by 0 when Drawing a texture. Attempting to animate objects
« Reply #5 on: June 11, 2014, 05:37:08 pm »
Well like I said, I found where it was for that first problem, so I was giving an update... Originally the call stack was only bringing me to a bunch of SFML files and so on, which was what was confusing me.

I am using Visual Studio 2012.

and it still stands that the chao sprite is only showing me a white box and crashing.

It looks like for whatever reason, that it is not adding in anything to the vectors after the initialization; practically its as if I never did it at all, despite the fact that I've clearly put in the code to add it in.

So, I'm gonna keep looking through this, and I have fixed the original problem myself, it appears.

I've corrected some more things with it, and now the call stack looks like this:
Quote
>   SFML Test 2.exe!main() Line 114   C++
    SFML Test 2.exe!__tmainCRTStartup() Line 536   C
    SFML Test 2.exe!mainCRTStartup() Line 377   C
    kernel32.dll!74e7338a()   Unknown
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]   
    ntdll.dll!77309f72()   Unknown
    ntdll.dll!77309f45()   Unknown

the error now being "Unhandled exception at 0x00BF5F04 in SFML Test 2.exe: 0xC0000094: Integer division by zero."
« Last Edit: June 11, 2014, 05:44:25 pm by MephilesTH »

 

anything