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

Author Topic: Is the alpha of default colors different in debug and release configurations?  (Read 1238 times)

0 Members and 1 Guest are viewing this topic.

Tess

  • Newbie
  • *
  • Posts: 2
    • View Profile
Because I've been struggling to understand for hours why text would display in "Release" and not at all in "Debug" configs (in Visual Studio, static linked SFML 2.6.1).
And it only showed up when instead of sf::Color::Black, I used sf::Color(0,0,0,255). I just wanted it to be in black to show up clearer and I didn't think that would cause problems.
Removing the set color line did make it display in white, but also testing other colors and blue had the same problem. 0 alpha in debug, 255 in release.

So, whilst for my game this is now fixed, I have a headache and i'm still not sure if I configured something wrong to have that happen?
« Last Edit: December 28, 2023, 12:10:44 am by Tess »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
In visual studio, one difference between debug and release is uninitialised local variables will be filled with the value 0xcd in every byte in debug builds, but left as whatever was already in memory (so potentially random) in release builds.

How are you using the colour? Black is defined as const Color Color::Black(0, 0, 0); and the color constructor defaults the alpha to 255 when you only give three values, so Black and Color(0,0,0,255) should always be identical.

Tess

  • Newbie
  • *
  • Posts: 2
    • View Profile
After looking into it further. I was having any predefined sf::Color become 0,0,0,0 regardless of what it was.
so sf::Color test = sf::Color::Black would have values (r=0,g=0,b=0,a=0)
BUT only in places where I happened to be initalising before main().

To test I put, above main(), this:
Quote
sf::Color mainTest = sf::Color::Black; //results in 0,0,0,0
sf::Color mainTest2 = sf::Color(0, 0, 0); //results in 0,0,0,255

And I knew about these kind of ordering issues, but I'd never expect it with predefined values of something as simple a a color. And it was extremely weird that it would work in release but not in debug.

My confusion now is that I don't know what bad assumption I made, I thought the two lines I wrote above would always be identical, and yet they are not?

Anyway hope this helps someone else avoid a headache from this!

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Ah static initialisation order fiasco. https://en.cppreference.com/w/cpp/language/siof

The predefined colours are static members of the Color class. They are initialised in color.cpp.
The problem is the order of global variables and static variables is only reliable within a single cpp (it goes top to bottom). But with multiple cpps you don't know which will be initialised first.
So in your one (when it fails) your cpp is trying to read Black before the color.cpp has set the values in Black. But calling the color constructor directly will give you the values immediately.