using common implementation avoiding out-of-order redraws

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77031 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Stefan Csomor
2014-08-09 16:14:22 +00:00
parent 780b9d119e
commit 50d539499b

View File

@@ -27,6 +27,7 @@
#include "wx/wfstream.h" #include "wx/wfstream.h"
#include "wx/filedlg.h" #include "wx/filedlg.h"
#include "wx/stockitem.h" #include "wx/stockitem.h"
#include "wx/dcbuffer.h"
#include "life.h" #include "life.h"
#include "game.h" #include "game.h"
@@ -436,11 +437,20 @@ void LifeFrame::OnMenu(wxCommandEvent& event)
m_running = true; m_running = true;
m_topspeed = true; m_topspeed = true;
UpdateUI(); UpdateUI();
const long YIELD_INTERVAL = 1000 / 30;
wxMilliClock_t lastyield = 0, now;
while (m_running && m_topspeed) while (m_running && m_topspeed)
{ {
OnStep(); OnStep();
if ( (now=wxGetLocalTimeMillis()) - lastyield > YIELD_INTERVAL)
{
wxYield(); wxYield();
lastyield = now;
} }
}
break; break;
} }
} }
@@ -589,12 +599,13 @@ void LifeFrame::OnStop()
void LifeFrame::OnStep() void LifeFrame::OnStep()
{ {
if (m_life->NextTic()) if (m_life->NextTic())
{
m_tics++; m_tics++;
m_canvas->Refresh();
UpdateInfoText();
}
else else
OnStop(); OnStop();
m_canvas->DrawChanged();
UpdateInfoText();
} }
@@ -791,13 +802,9 @@ void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, wxDC &dc)
} }
} }
// draw all changed cells // draw all changed cells, currently not in use
void LifeCanvas::DrawChanged() void LifeCanvas::DrawChanged()
{ {
#ifdef __WXOSX__
// we should not do out of band redraws on OSX, let things happen in the event loop
Refresh();
#else
wxClientDC dc(this); wxClientDC dc(this);
size_t ncells; size_t ncells;
@@ -812,14 +819,14 @@ void LifeCanvas::DrawChanged()
if (m_cellsize == 1) if (m_cellsize == 1)
{ {
dc.SetPen(*wxBLACK_PEN); dc.SetPen(*wxWHITE_PEN);
} }
else else
{ {
dc.SetPen(*wxTRANSPARENT_PEN); dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(*wxBLACK_BRUSH); dc.SetBrush(*wxWHITE_BRUSH);
} }
dc.SetLogicalFunction(wxINVERT); dc.SetLogicalFunction(wxXOR);
while (!done) while (!done)
{ {
@@ -828,13 +835,12 @@ void LifeCanvas::DrawChanged()
for (size_t m = 0; m < ncells; m++) for (size_t m = 0; m < ncells; m++)
DrawCell(cells[m].i, cells[m].j, dc); DrawCell(cells[m].i, cells[m].j, dc);
} }
#endif
} }
// event handlers // event handlers
void LifeCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) void LifeCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
{ {
wxPaintDC dc(this); wxAutoBufferedPaintDC dc(this);
wxRect rect = GetUpdateRegion().GetBox(); wxRect rect = GetUpdateRegion().GetBox();
wxCoord x, y, w, h; wxCoord x, y, w, h;
wxInt32 i0, j0, i1, j1; wxInt32 i0, j0, i1, j1;