Improve animation consistency

This commit is contained in:
Loic Guegan 2022-12-29 11:52:24 +01:00
parent 3efabf1c33
commit 756c888145

View file

@ -17,7 +17,7 @@ BoardCanvas::BoardCanvas(wxFrame *parent)
SetBackgroundStyle(wxBG_STYLE_PAINT); SetBackgroundStyle(wxBG_STYLE_PAINT);
duration=200; duration=200;
duration_fast=100; duration_fast=100;
fps=180; fps=30;
Bind(wxEVT_KEY_DOWN, &BoardCanvas::OnKeyEvent, this); Bind(wxEVT_KEY_DOWN, &BoardCanvas::OnKeyEvent, this);
Bind(wxEVT_KEY_UP, &BoardCanvas::OnKeyEvent, this); Bind(wxEVT_KEY_UP, &BoardCanvas::OnKeyEvent, this);
@ -121,9 +121,9 @@ void BoardCanvas::Animate(std::string board, bool is_black_turn, std::map<char,
// Now remove the piece that will be moved // Now remove the piece that will be moved
this->board[pfile + 8 * (7-prank)]=' '; this->board[pfile + 8 * (7-prank)]=' ';
SetupBoard(this->board,this->is_black_turn,this->captures); wxMemoryDC memDC(*buffer);
Update(); // Since refresh in SetupBoard is not synchronous, this call wait the end of Refresh() DrawBoard(memDC);
// Now compute piece start position and translation vector (Copy paste from DrawBoard()) // Now compute piece start position and translation vector (Copy paste from DrawBoard())
std::uint32_t piece_width = t->GetPiecesSizes(); std::uint32_t piece_width = t->GetPiecesSizes();
std::uint32_t centrer_offset = (square_width - piece_width) / 2; std::uint32_t centrer_offset = (square_width - piece_width) / 2;
@ -153,10 +153,16 @@ void BoardCanvas::Animate(std::string board, bool is_black_turn, std::map<char,
duration=faster ? duration_fast : duration; duration=faster ? duration_fast : duration;
frame=0; frame=0;
int frames=duration/(1000/fps); // total number of frames int frames=duration/(1000/fps); // total number of frames
int time_per_frame=duration/frames;
wxStopWatch sw;
for(int i=frames;i>0;i--){ for(int i=frames;i>0;i--){
Refresh(); Refresh();
Update(); Update();
wxMilliSleep((1000/(fps*2))); int delay=sw.Time()-time_per_frame;
if(delay>10){
wxMilliSleep(delay);
}
sw.Start(0);
} }
duration=faster ? duration_backup : duration_backup; duration=faster ? duration_backup : duration_backup;
reuseBuffer=false; reuseBuffer=false;