mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-07 02:26:29 +02:00
Improve BoardCanvas
This commit is contained in:
parent
749455cf19
commit
7fe3e6dee0
3 changed files with 18 additions and 26 deletions
2
TODO.md
2
TODO.md
|
@ -1,7 +1,7 @@
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
## Before releasing v0.1.0
|
## 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
|
- [ ] Bind the chess game editor settings to EditorPrefs.hpp
|
||||||
- [ ] Ask before closing MainWindow/Tabs if anything is not saved
|
- [ ] 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)
|
- [ ] Disable the save button in GameTab after saving (and re-enable it on new changes)
|
||||||
|
|
|
@ -21,8 +21,7 @@ BoardCanvas::BoardCanvas(wxFrame *parent)
|
||||||
adata.duration_fast=80;
|
adata.duration_fast=80;
|
||||||
adata.fps=30;
|
adata.fps=30;
|
||||||
adata.buffer=new wxBitmap(500,500,32);
|
adata.buffer=new wxBitmap(500,500,32);
|
||||||
adata.reuseBuffer=false;
|
adata.animate=false;
|
||||||
adata.buffer=nullptr;
|
|
||||||
// Init game state
|
// Init game state
|
||||||
gs.is_black_turn=false;
|
gs.is_black_turn=false;
|
||||||
gs.board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR";
|
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_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_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
|
||||||
Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this);
|
Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this);
|
||||||
Bind(wxEVT_SIZE, &BoardCanvas::OnResize, this);
|
|
||||||
// Mouse events:
|
// Mouse events:
|
||||||
Bind(wxEVT_MOTION, &BoardCanvas::MouseEvent, this);
|
Bind(wxEVT_MOTION, &BoardCanvas::MouseEvent, this);
|
||||||
Bind(wxEVT_LEFT_DOWN, &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);
|
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() {
|
BoardCanvas::~BoardCanvas() {
|
||||||
delete t;
|
delete t;
|
||||||
delete t_captures;
|
delete t_captures;
|
||||||
|
@ -82,7 +70,7 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
|
||||||
dc.SetBackground(*wxWHITE_BRUSH);
|
dc.SetBackground(*wxWHITE_BRUSH);
|
||||||
dc.Clear();
|
dc.Clear();
|
||||||
|
|
||||||
if(!adata.reuseBuffer){
|
if(!adata.animate){
|
||||||
// Setting up required attributes
|
// Setting up required attributes
|
||||||
REFRESH_MOUSE_LOCATION();
|
REFRESH_MOUSE_LOCATION();
|
||||||
square_width = t->GetSquaresSizes();
|
square_width = t->GetSquaresSizes();
|
||||||
|
@ -94,14 +82,10 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
|
||||||
boardX = 0;
|
boardX = 0;
|
||||||
if (boardY > canvas_size.y)
|
if (boardY > canvas_size.y)
|
||||||
boardY = 0;
|
boardY = 0;
|
||||||
wxMemoryDC memDC(*adata.buffer);
|
DrawBoard(dc);
|
||||||
memDC.SetBackground(*wxWHITE_BRUSH);
|
|
||||||
memDC.Clear();
|
|
||||||
DrawBoard(memDC);
|
|
||||||
dc.Blit(0,0,canvas_size.x,canvas_size.y,(wxDC*)&memDC,0,0);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Otherwise reuse buffer and animate
|
// Reuse buffer and animate
|
||||||
dc.DrawBitmap(*adata.buffer, 0, 0, true);
|
dc.DrawBitmap(*adata.buffer, 0, 0, true);
|
||||||
double percent=adata.frame/adata.frames;
|
double percent=adata.frame/adata.frames;
|
||||||
// Draw moving piece
|
// 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';
|
std::uint8_t prank = src[1]-'1';
|
||||||
adata.piece_moved = gs.board[pfile + 8 * (7-prank)]; // Piece to move
|
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)]=' ';
|
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);
|
wxMemoryDC memDC(*adata.buffer);
|
||||||
|
memDC.SetBackground(*wxWHITE_BRUSH);
|
||||||
|
memDC.Clear();
|
||||||
DrawBoard(memDC);
|
DrawBoard(memDC);
|
||||||
|
|
||||||
// Now compute piece start position and translation vector
|
// 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;
|
adata.transVect.y=y-adata.src.y+centrer_offset;
|
||||||
|
|
||||||
// Start animation:
|
// Start animation:
|
||||||
adata.reuseBuffer=true;
|
adata.animate=true;
|
||||||
int duration_backup=adata.duration;
|
int duration_backup=adata.duration;
|
||||||
adata.duration=faster ? adata.duration_fast : adata.duration;
|
adata.duration=faster ? adata.duration_fast : adata.duration;
|
||||||
int frame_duration=(1000/adata.fps);
|
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);
|
sw.Start(0);
|
||||||
}
|
}
|
||||||
adata.duration=faster ? duration_backup : duration_backup;
|
adata.duration=faster ? duration_backup : duration_backup;
|
||||||
adata.reuseBuffer=false;
|
adata.animate=false;
|
||||||
SetupBoard(new_gs);
|
SetupBoard(new_gs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,8 +42,8 @@ typedef std::tuple<short, short, short> ClockTime;
|
||||||
typedef struct AnimState {
|
typedef struct AnimState {
|
||||||
/// @brief Temporary buffer to reduce latency
|
/// @brief Temporary buffer to reduce latency
|
||||||
wxBitmap *buffer;
|
wxBitmap *buffer;
|
||||||
/// @brief Should *buffer be used?
|
/// @brief Should animation be played on refresh?
|
||||||
bool reuseBuffer;
|
bool animate;
|
||||||
/// @brief Current animated frame
|
/// @brief Current animated frame
|
||||||
int frame;
|
int frame;
|
||||||
/// @brief Total number of frames for the animation
|
/// @brief Total number of frames for the animation
|
||||||
|
|
Loading…
Add table
Reference in a new issue