diff --git a/TODO.md b/TODO.md index ef013b8..3b94154 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ # TODO ## Before releasing v0.1.0 - - [ ] In BoardCanvas search for a workaround of the dynamic allocation of adata.buffer (on canvas resize) + - [x] In BoardCanvas search for a workaround of the dynamic allocation of adata.buffer (on canvas resize) - [ ] Bind the chess game editor settings to EditorPrefs.hpp - [ ] Ask before closing MainWindow/Tabs if anything is not saved - [ ] Disable the save button in GameTab after saving (and re-enable it on new changes) diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 1043d86..9eb54ee 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -21,8 +21,7 @@ BoardCanvas::BoardCanvas(wxFrame *parent) adata.duration_fast=80; adata.fps=30; adata.buffer=new wxBitmap(500,500,32); - adata.reuseBuffer=false; - adata.buffer=nullptr; + adata.animate=false; // Init game state gs.is_black_turn=false; gs.board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR"; @@ -41,7 +40,6 @@ BoardCanvas::BoardCanvas(wxFrame *parent) Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this); - Bind(wxEVT_SIZE, &BoardCanvas::OnResize, this); // Mouse events: Bind(wxEVT_MOTION, &BoardCanvas::MouseEvent, this); Bind(wxEVT_LEFT_DOWN, &BoardCanvas::MouseEvent, this); @@ -51,16 +49,6 @@ BoardCanvas::BoardCanvas(wxFrame *parent) Bind(wxEVT_RIGHT_DCLICK, &BoardCanvas::MouseEvent, this); } -void BoardCanvas::OnResize(wxSizeEvent &e){ - wxSize size=e.GetSize(); - if(size.x>100 && size.y>100){ - // Setup buffer (use for animations) - if(adata.buffer!=nullptr) - free(adata.buffer); - adata.buffer=new wxBitmap(size.x,size.y,32); - } -} - BoardCanvas::~BoardCanvas() { delete t; delete t_captures; @@ -82,7 +70,7 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { dc.SetBackground(*wxWHITE_BRUSH); dc.Clear(); - if(!adata.reuseBuffer){ + if(!adata.animate){ // Setting up required attributes REFRESH_MOUSE_LOCATION(); square_width = t->GetSquaresSizes(); @@ -94,14 +82,10 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { boardX = 0; if (boardY > canvas_size.y) boardY = 0; - wxMemoryDC memDC(*adata.buffer); - memDC.SetBackground(*wxWHITE_BRUSH); - memDC.Clear(); - DrawBoard(memDC); - dc.Blit(0,0,canvas_size.x,canvas_size.y,(wxDC*)&memDC,0,0); + DrawBoard(dc); } else { - // Otherwise reuse buffer and animate + // Reuse buffer and animate dc.DrawBitmap(*adata.buffer, 0, 0, true); double percent=adata.frame/adata.frames; // Draw moving piece @@ -155,9 +139,17 @@ void BoardCanvas::Animate(const GameState &new_gs, const std::string &src, const std::uint8_t prank = src[1]-'1'; adata.piece_moved = gs.board[pfile + 8 * (7-prank)]; // Piece to move - // Now remove the piece that will be moved + // Now init adata.buffer and remove the piece that will be moved gs.board[pfile + 8 * (7-prank)]=' '; + wxSize buffer_size=adata.buffer->GetSize(); + if(canvas_size!=buffer_size){ + wxLogDebug("Allocate a new animation buffer"); + free(adata.buffer); + adata.buffer=new wxBitmap(canvas_size.x,canvas_size.y,32); + } wxMemoryDC memDC(*adata.buffer); + memDC.SetBackground(*wxWHITE_BRUSH); + memDC.Clear(); DrawBoard(memDC); // Now compute piece start position and translation vector @@ -183,7 +175,7 @@ void BoardCanvas::Animate(const GameState &new_gs, const std::string &src, const adata.transVect.y=y-adata.src.y+centrer_offset; // Start animation: - adata.reuseBuffer=true; + adata.animate=true; int duration_backup=adata.duration; adata.duration=faster ? adata.duration_fast : adata.duration; int frame_duration=(1000/adata.fps); @@ -201,7 +193,7 @@ void BoardCanvas::Animate(const GameState &new_gs, const std::string &src, const sw.Start(0); } adata.duration=faster ? duration_backup : duration_backup; - adata.reuseBuffer=false; + adata.animate=false; SetupBoard(new_gs); } diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 6ef2957..0576f1d 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -42,8 +42,8 @@ typedef std::tuple ClockTime; typedef struct AnimState { /// @brief Temporary buffer to reduce latency wxBitmap *buffer; - /// @brief Should *buffer be used? - bool reuseBuffer; + /// @brief Should animation be played on refresh? + bool animate; /// @brief Current animated frame int frame; /// @brief Total number of frames for the animation