   equality of strings problem
  I am in the process of making a sprite engine. I can get everything to load and show to the screen, but currently, the textures are being loaded every frame. I figured this to be an easy problem to fix by checking to see if the filename of the current texture and the filename of the one being loaded are the same, and if so, do nothing. This way, the texture would only be loaded when it is different than the current one. However, even with the string check, the texture is loaded every frame. For some reason (I have no idea why), the string check always returns true in the following function:bool jScreen::LoadSpriteTexture (jSprite *sprite, std::string kFileName){// If a different picture is being loaded than before, // Create the d3d texture for the spriteif (sprite-&gt;FileName != kFileName)    // both operands of type std::string{if (SUCCEEDED (D3DXCreateTextureFromFile (m_d3ddev, kFileName.c_str (), &sprite-&gt;D3DTexture))){sprite-&gt;FileName = kFileName;  // THIS GETS EXECUTED EVERY FRAME! &lt;- PROBLEMreturn true;}}else{ // Do Nothing}return false;}I also ran the code using breakpoints. The first loop shows the string, sprite->FileName holding "" and kFileName "jGameEngineBanner.png". These arguments are evaluated to be unequal. <- CORRECTThe following times the function is run, both strings hold the same values, however the outcome is the same as the first loop. <- INCORRECTI am really confused about this since the compiler actually said they hold the same value when I did a breakpoint on about the 200th frame (even though the program acted differently). I should also mention that this function is called every frame with the same EXACT parameters. Any help would be greatly appreciated.Jacobedited by evolutional - added source tags[Edited by - evolutional on May 28, 2009 7:05:58 AM]
  Use strcmp()   ;Quote:Original post by ChainChiefI can get everything to load and show to the screen, but currently, the textures are being loaded every frame. I figured this to be an easy problem to fix...Why are you even attempting to load the textures every frame? An even easier fix would be to load all textures when your program starts (i.e. before the main input/rendering loop).btw, the tag for posting code is [source][/source]@afsd42: he's using std::strings not C strings   ;  aside from the issue xta pointed out, have a closer look at this line of codeif (sprite->FileName != kFileName) check to make sure you don't have a semi-colin at the end of the if statement i.e.if (sprite->FileName != kFileName);{   // do stuff if strings are different}// is equivalent to if (sprite->FileName != kFileName){   // do nothing}{   // do stuff if strings are different}and you can use  and  to post code too (avoids the big white box, useful for small snippets)   ;Quote:Original post by XTAL256@afsd42: he's using std::strings not C stringsOops I missed that. In that case he can use the compare function.if (sprite->FileName.Compare(kFileName) == 0) // then they are equal   ;Quote:Original post by afsd42Quote:Original post by XTAL256@afsd42: he's using std::strings not C stringsOops I missed that. In that case he can use the compare function.if (sprite->FileName.Compare(kFileName) == 0) // then they are equalIt's actually compare(), with a lower-case 'c'. Also, since only a Boolean result is needed in this case, you can just use operator==(). (For lexicographical comparison, there's also operator<().)   ;  As far as I can see, there's nothing wrong with the code that was posted; it should work.Like XTAL256, I would question the attempt to load a new sprite every frame (what does the code calling LoadSpriteTexture look like?).But I think there's something else going on here, since the code, as posted, should work.   ;  and please, pass your kFileName as a const-reference.so the method declaration should look like this probably:bool LoadSpriteTexture(jSprite* sprite, const std::string& kFileName);best regards   ;  Code tags have square brackets, not angle brackets:[code]int x;[/code]gives:int x;However, if you are posting more than about 10 lines of code, consider using [source][/source] tags instead.   ;  Maybe the strings *are* different every time. Have you tried breaking in there with a debugger and looking at the strings?