dimanche 5 juillet 2015

C++ SFML integer not working in if / else statement

So here's the breakdown: I'm learning SFML, and I encounter the method getSize().x that you call on a texture / image (texture.getSize().x in the code) that returns the width as an integer, pretty basic. The issue that I encountered was in the code below. Basically, this code is in a while loop that makes the variable "source.x" continually change from 0 to 1 to 2 to 1 to 0 then repeat endlessly (it's purpose doesn't matter). No matter what I did the code below would endlessly decrease the value of source.x, as it would execute the first part of the if/else statement every time that it looped through, even when the value of source.x was far below 0. For some reason it clicked to me to convert the pTexture.getSize().x to an integer just to double check that it was one, and then the code magically worked. What is stumping me is why if, for some weird reason, pTexture.getSize().x isn't an integer (which it is whenever I print it out, and i tried returning it in a method of type int for extra sureness), why wouldn't I get a compiler error for comparing incompatible types, and why on top of that would it always assume that the condition was true. Also, the value of pTexture.getSize().x is 96 for reference. Thanks in advance and sorry for how long this post is.

source.x += posNeg;
if(source.x*32 >= (int)pTexture.getSize().x){ 
    posNeg = -1;
    source.x -= 2;
    std::cout << "if called" << std::endl;
} else if(source.x < 0){
    posNeg = 1;
    source.x += 2;
    std::cout << "else called" << std::endl;
}

Aucun commentaire:

Enregistrer un commentaire