End credits: only start scrolling when the window is actually drawn

It turned out that "credits start halfway through" wasn't caused by my
changes. The cause was that, in debug builds, the initial draw of the
window took more than three seconds and therefore the window had already
scrolled when it appeared.

This commit fixes the issue.
This commit is contained in:
Jyrki Vesterinen 2017-05-06 18:26:20 +03:00
parent 495c744cf1
commit 5e86da67b6
3 changed files with 24 additions and 4 deletions

View file

@ -71,10 +71,12 @@ static void parse_about_tag(const config& cfg, std::stringstream& ss)
void end_credits::pre_show(window& window)
{
timer_id_ = add_timer(10, std::bind(&end_credits::timer_callback, this), true);
// Delay a little before beginning the scrolling
last_scroll_ = SDL_GetTicks() + 3000;
window.set_callback_next_draw([this]()
{
timer_id_ = add_timer(10, std::bind(&end_credits::timer_callback, this), true);
// Delay a little before beginning the scrolling
last_scroll_ = SDL_GetTicks() + 3000;
});
connect_signal_pre_key_press(window, std::bind(&end_credits::key_press_callback, this, _5));

View file

@ -840,6 +840,11 @@ void window::draw()
std::vector<widget*> call_stack;
populate_dirty_list(*this, call_stack);
assert(dirty_list_.empty());
if(callback_next_draw_ != nullptr) {
callback_next_draw_();
callback_next_draw_ = nullptr;
}
}
void window::undraw()

View file

@ -28,6 +28,7 @@
#include "gui/core/window_builder.hpp"
#include "gui/widgets/panel.hpp"
#include <functional>
#include <map>
#include <memory>
#include <string>
@ -490,6 +491,17 @@ public:
};
}
/**
* Sets a callback that will be called after the window is drawn next time.
* The callback is automatically removed after calling it once.
* Useful if you need to do something after the window is drawn for the first time
* and it's timing-sensitive (i.e. pre_show is too early).
*/
void set_callback_next_draw(std::function<void()> func)
{
callback_next_draw_ = func;
}
private:
/** Needed so we can change what's drawn on the screen. */
CVideo& video_;
@ -797,6 +809,7 @@ private:
bool& handled);
std::function<bool(window&)> exit_hook_ = [](window&)->bool { return true; };
std::function<void()> callback_next_draw_;
};
// }---------- DEFINITION ---------{