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

Author Topic: Divide By Zero When Drawing  (Read 4348 times)

0 Members and 1 Guest are viewing this topic.

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Divide By Zero When Drawing
« on: July 05, 2012, 04:54:15 pm »
I don't think it's normal to get a divide by zero error when drawing a sprite. Haha.

Check this out...
for (unsigned i = 0; i < tiles.size(); ++i)
        if (tiles[i]->GetSprites().size() > 0) {
                cout << "Error ";
                cout << tiles[i]->GetSprites()[0].getPosition().x;
                _window->draw(tiles[i]->GetSprites().at(0));
                /// The only thing Tile::GetSprites() does is return the vector of sprites for the tile.
                /// I'm only using the first sprite though.
                cout << " Here!" << endl;
        }

I ran through the code a lot and isolated the error to this section of the code.... drawing my sprites. The output of the program into the console shows that its the drawing that causes the error.
The output is as follows (verbatum):
Quote
Error 32 Here!
Error 64 Here!
Error 96 Here!
Error 128 Here!
Error 160 Here!
Error 192 Here!
Error 224 Here!
Error 256

Unhandled exception at 0x0f6d2382 in TheReverendGUI.exe: 0xC0000094: Integer division by zero.
As you can see, it successfully accesses the sprites in the vector well enough to output their position, but I get this error only on ONE of these sprites... Any idea what this might be?

Windows 7
SFML 2.0

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Divide By Zero When Drawing
« Reply #1 on: July 05, 2012, 05:00:09 pm »
Is the sprite's texture valid?
Laurent Gomila - SFML developer

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Divide By Zero When Drawing
« Reply #2 on: July 05, 2012, 05:17:49 pm »
I'm pretty sure it is. I don't see anything that could be wrong with the algorithm that pulls the texture from the sprite sheet. The algorithm worked in 1.6, all I did was port it by changing things to sf::Image and sf::Texture appropriately. Maybe I don't adequately understand how to use each one and how each one is related to the other.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Divide By Zero When Drawing
« Reply #3 on: July 05, 2012, 06:19:50 pm »
Any idea what this might be?

A devision by zero... ;)

If never seen anyone run into such a problem with SFML, thus I have to assume that you did something wrong in the initialization of the sprite or texture.
You should post a minimal example that reproduces the problem.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Divide By Zero When Drawing
« Reply #4 on: July 05, 2012, 06:25:08 pm »
Smart Alec. XD

I'll post up a minimal code of the algorithm I used. :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Divide By Zero When Drawing
« Reply #5 on: July 05, 2012, 06:49:48 pm »
Quote
I'm pretty sure it is
Never trust yourself :P

You can easily check:
std::cout << tiles[i]->GetSprites().at(0).getTexture()->getSize().x;
Should be enough.
Laurent Gomila - SFML developer

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Divide By Zero When Drawing
« Reply #6 on: July 05, 2012, 07:34:58 pm »
Oh Lord! Thank God for you guys. I tried your texture size thing and the sizes come up in the millions and then some are 0. I checked the validity of the sprite, but it didn't occur to me to check the texture.

I just don't see when I went wrong with the loading of the texture. I can't get my minimal to reproduce the problem. It keeps working. XD

// Load the tileset into memory
sf::Image* tileset_image = new sf::Image;
tileset_image->loadFromFile(tileset->GetImage()->GetSource());

// Create a temporary image to manipulate
sf::Image *tile_image = new sf::Image;
tile_image->create((int)tile_dimensions.x, (int)tile_dimensions.y);
// Copy the tile image into the sf::Image
tile_image->copy(*tileset_image, 0, 0,
        sf::IntRect(tileset->GetMargin() + (col - 1) * tileset->GetSpacing() + (col - 1) * tileset->GetTileWidth(),
        tileset->GetMargin() + (row - 1) * tileset->GetSpacing() + (row - 1) * tileset->GetTileHeight(),
        tileset->GetMargin() + col * tileset->GetSpacing() + col * tileset->GetTileWidth(),
        tileset->GetMargin() + row * tileset->GetSpacing() + row * tileset->GetTileHeight()));

// Create a temporary texture to prepare for loading
sf::Texture *tile_texture = new sf::Texture;
// Load image into texture
tile_texture->loadFromImage(*tile_image); delete tile_image;
// Add texture into the texture
tiles.back()->AddTexture(tile_texture); delete tile_texture;
delete tileset_image;

This is a small portion of the code for loading textures from the sprite sheet. The original that presents problems.

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Divide By Zero When Drawing
« Reply #7 on: July 05, 2012, 07:47:03 pm »
Oh wow! I found it.
tiles.back()->AddTexture(tile_texture); delete tile_texture;
I just don't understand why. I transferred the texture into the Tile object...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Divide By Zero When Drawing
« Reply #8 on: July 05, 2012, 08:54:54 pm »
What does AddTexture does? If it doesn't copy the texture (only the pointer), then you mustn't destroy it.
Laurent Gomila - SFML developer

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Divide By Zero When Drawing
« Reply #9 on: July 05, 2012, 08:58:21 pm »
void Tile::AddTexture(sf::Texture* _texture) {
        textures.push_back(_texture);
        AddSprite(sf::Sprite(*textures.back()));
        sprites.back().setPosition(global_coords);
}

How do I copy the texture rather than the pointer?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Divide By Zero When Drawing
« Reply #10 on: July 05, 2012, 09:10:38 pm »
How do I copy the texture rather than the pointer?
Interesting that you don't know what a pointer is... ???

You'll also have to change the vector declaretion for textures to take objects instead of pointers.

std::vector<sf::Texture> textures;
//...
void Tile::AddTexture(sf::Texture _texture) {
        textures.push_back(_texture);
        AddSprite(sf::Sprite(textures.back()));
        sprites.back().setPosition(global_coords);
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Divide By Zero When Drawing
« Reply #11 on: July 05, 2012, 09:22:58 pm »
I do know what a pointer is... -__-
I don't, however, claim to know everything.

Thanks for the fix.