Hi. I have a RectangleShape that I'm trying to make gradually change to white as I hold a key. Actually, I've succeeded in this, but now I'd like a different key to gradually turn it black. It seems, however, that the minus operator will not work with sf:Color like the plus operator does.
To change my shape to white, I've used this code in the game loop.
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sf::Color shapeColor = shape.getFillColor();
if(shapeColor != sf::Color::White)
shape.setFillColor(shapeColor + sf::Color(1, 1, 1));
}
I could only imagine this very same code with a "-" in place of the "+" (and sf::Color::Black instead of White, and a different Key) should do the opposite.
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
sf::Color shapeColor = shape.getFillColor();
if(shapeColor != sf::Color::Black)
shape.setFillColor(shapeColor - sf::Color(1, 1, 1));
}
However, that only gives me compiler errors that I don't entirely understand (I will post them if needed). Could somebody tell me why this won't work?
tbh part of the reason I didn't post the compiler errors anyway is because I wasn't sure how to copy them to the clipboard haha
..turns out Code::Blocks has a right click option just especially for that, so here you go.
||=== Build: Debug in SFML01 (compiler: GNU GCC Compiler) ===|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML01\main.cpp||In function 'int main()':|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML01\main.cpp|166|error: no match for 'operator-' (operand types are 'sf::Color' and 'sf::Color')|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML01\main.cpp|166|note: candidates are:|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|101|note: template<class T> sf::Vector3<T> sf::operator-(const sf::Vector3<T>&, const sf::Vector3<T>&)|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|101|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML01\main.cpp|166|note: 'sf::Color' is not derived from 'const sf::Vector3<T>'|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|61|note: template<class T> sf::Vector3<T> sf::operator-(const sf::Vector3<T>&)|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|61|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML01\main.cpp|166|note: 'sf::Color' is not derived from 'const sf::Vector3<T>'|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|96|note: template<class T> sf::Vector2<T> sf::operator-(const sf::Vector2<T>&, const sf::Vector2<T>&)|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|96|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML01\main.cpp|166|note: 'sf::Color' is not derived from 'const sf::Vector2<T>'|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|58|note: template<class T> sf::Vector2<T> sf::operator-(const sf::Vector2<T>&)|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|58|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML01\main.cpp|166|note: 'sf::Color' is not derived from 'const sf::Vector2<T>'|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Time.hpp|268|note: sf::Time sf::operator-(sf::Time, sf::Time)|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Time.hpp|268|note: no known conversion for argument 1 from 'sf::Color' to 'sf::Time'|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Time.hpp|232|note: sf::Time sf::operator-(sf::Time)|
C:\Users\Nick\Documents\CodeBlocks\Libraries\SFML-2.1\include\SFML\System\Time.hpp|232|note: candidate expects 1 argument, 2 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
I'm baffled by why it is mentioning sf::Vector2 or sf::Time at all.
To be clear, the code that produces that error is exactly like the working "+" code aside from the fact that it uses a "-" instead (and a different key and sf::Color::Black, but I've already changed these to the same and can confirm the only issue is the "-" symbol).
It's really weird that it works for you, so maybe I should try it in a fresher project and see what happens.
edit: Just tried a fresh project, but I get the same errors when trying to minus.
||=== Build: Debug in SFML02 (compiler: GNU GCC Compiler) ===|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML02\main.cpp||In function 'int main()':|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML02\main.cpp|29|error: no match for 'operator-' (operand types are 'sf::Color' and 'sf::Color')|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML02\main.cpp|29|note: candidates are:|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|101|note: template<class T> sf::Vector3<T> sf::operator-(const sf::Vector3<T>&, const sf::Vector3<T>&)|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|101|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML02\main.cpp|29|note: 'sf::Color' is not derived from 'const sf::Vector3<T>'|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|61|note: template<class T> sf::Vector3<T> sf::operator-(const sf::Vector3<T>&)|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector3.inl|61|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML02\main.cpp|29|note: 'sf::Color' is not derived from 'const sf::Vector3<T>'|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|96|note: template<class T> sf::Vector2<T> sf::operator-(const sf::Vector2<T>&, const sf::Vector2<T>&)|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|96|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML02\main.cpp|29|note: 'sf::Color' is not derived from 'const sf::Vector2<T>'|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|58|note: template<class T> sf::Vector2<T> sf::operator-(const sf::Vector2<T>&)|
..\..\Libraries\SFML-2.1\include\SFML\System\Vector2.inl|58|note: template argument deduction/substitution failed:|
C:\Users\Nick\Documents\CodeBlocks\Projects\SFML02\main.cpp|29|note: 'sf::Color' is not derived from 'const sf::Vector2<T>'|
..\..\Libraries\SFML-2.1\include\SFML\System\Time.hpp|268|note: sf::Time sf::operator-(sf::Time, sf::Time)|
..\..\Libraries\SFML-2.1\include\SFML\System\Time.hpp|268|note: no known conversion for argument 1 from 'sf::Color' to 'sf::Time'|
..\..\Libraries\SFML-2.1\include\SFML\System\Time.hpp|232|note: sf::Time sf::operator-(sf::Time)|
..\..\Libraries\SFML-2.1\include\SFML\System\Time.hpp|232|note: candidate expects 1 argument, 2 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
I grabbed the newest github SFML source (https://github.com/SFML/SFML/archive/master.zip) and built it and replaced my old one. There's no error anymore, but, strangely, the minus operation now turns the rectangle invisible. However, it appears to be changing the rectangle black despite this, as when I start holding the key to turn it white (which functions perfectly fine), it becomes visible again and is at a darker shade from before it turned invisible. ???
(I've made sure it wasn't just blending in with my black background. Even on blue, it still completely disappears when I press the key that should darken the shape.)
Is this yet another issue with SFML as it is now at its current version, or have I made a mistake?
Does this code behave properly on your end?
//Outside game loop
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(100,100));
rect.setFillColor(sf::Color(100,100,100));
...
//Inside game loop
if(sf::Keyboard::isKeyPressed(sf::Keyboard::O))
{
sf::Color rectColor = rect.getFillColor();
if(rectColor != sf::Color::White)
rect.setFillColor(rectColor + sf::Color(1, 1, 1));
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::P))
{
sf::Color rectColor = rect.getFillColor();
if(rectColor != sf::Color::Black)
rect.setFillColor(rectColor - sf::Color(1, 1, 1));
}