61
General / Trouble switching states...
« on: February 03, 2014, 02:57:12 am »
Never never never poll after window.display(). Have you read the SFML tutorials?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
...fixed it by placing my music.openFromFile() outside of the window loop, and my first music.play() outside of the window loop.
Another advantage of namespaces is that you can keep them long and expressive:namespace ShockEngine
{
// your library
}
In user code, you can still create aliases, even for nested namespaces:namespace shk = ShockEngine;
namespace si = ShockEngine::Input;
shk::InputManager mgr;
You can also locally "empty out" the namespace (but never do this in headers):using namespace ShockEngine;
You still need _1 as the last argument in std::bind() -- it specifies the thor::ActionContext argument that is forwarded. Furthermore, for member functions you should pass a pointer, not a reference to the object.std::bind(&SE::SInputManager::onTextEntered, this, _1)
If you struggle at understanding std::bind(), just use lambda expressions.
And have you seen my advice concerning "S" prefixes?
Anyway, it would be easier to have a non-static member function, and pass this as the second parameter (first after function name) to std::bind().
There are multiple possibilities what you can pass to connect().
Functor:struct Functor
{
void operator() (const thor::ActionContext<...>& context) const
{
SInputManager::onTextEntered(context, instance);
}
const SInputManager& instance;
...
}
Lambda expression:[&instance] (const thor::ActionContext<...>& context)
{
onTextEntered(context, instance);
}
Bind (_1 is in namespace std::placeholders):std::bind(&SInputManager::onTextEntered, _1, std::cref(instance))
onTextEntered() can also be a non-static member function, you have to adapt the function objects accordingly. I recommend reading more about function objects in C++, they're very powerful.
And don't use those S prefixes for classes, there is really no reason to
// Callback function that is called when text entered event is triggered
void onTextEntered(const thor::ActionContext<MyAction>& context)
{
sf::Uint32 character = context.event->text.unicode;
...
}
// Action map initialization
thor::ActionMap<MyEnum> map;
map[MyTextAction] = thor::Action(sf::Event::TextEntered);
// Callback registering
thor::ActionMap<MyEnum>::CallbackSystem callbacks;
callbacks.connect(MyTextAction, &onTextEntered);
There's a much simpler way to do that. Here's some code that gradually shifts from colour1 at 0 seconds, to colour2 at 3 seconds, to colour3 at 10 seconds;Code: [Select]// use vector2f's for our colours (0=min, 1=max)
sf::Vector2f c1, c2, c3;
sf::Clock clock;
// handy linear interpolation
sf::Vector2f lerp(sf::Vector2f a, sf::Vector2f b, float t){
return a*(1-t) + b*t;
}
// main loop
const double seconds = clock.getElapsedTime.asSeconds();
sf::Vector2f c;
if (seconds <= 3){
double t = seconds / 3; // t goes from 0 to 1
c = lerp(c1, c2, t);
}
else if (seconds <= 10){
double t = (seconds - 3) / (10-3);
c = lerp(c2, c3, t);
}
else {
// after 6 seconds stay as colour 3
c = c3;
}
// transform c into an sf::Color which we can then use to draw something
sf::Color colour = sf::Color(255*c.x, 255*c.y, 255*c.z);
You can also replace the lerp with other easing functions, such as cubic, easeIn, quadratic, etc, to change how the colour shift behaves. See e.g., this page for other easing functions.
Of course for both...
You should really learn how to linking works.