]> git.saurik.com Git - wxWidgets.git/commitdiff
Optimized drawing code in OnMouse() for freehand drawing.
authorGuillermo Rodriguez Garcia <guille@iies.es>
Thu, 17 Feb 2000 03:02:14 +0000 (03:02 +0000)
committerGuillermo Rodriguez Garcia <guille@iies.es>
Thu, 17 Feb 2000 03:02:14 +0000 (03:02 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6102 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

demos/life/life.cpp

index 2bb95263937dfb03d389217dcb5be118e1e04399..4dba59107b2bcdeb0e0d0cba3f91b19359c2a957 100644 (file)
@@ -543,12 +543,10 @@ void LifeCanvas::DrawChanged()
 
     if (m_cellsize == 1)
     {
-        // drawn using DrawPoint
         dc.SetPen(*wxBLACK_PEN);
     }
     else
     {
-        // drawn using DrawRectangle
         dc.SetPen(*wxTRANSPARENT_PEN);
         dc.SetBrush(*wxBLACK_BRUSH);
     }
@@ -666,6 +664,14 @@ void LifeCanvas::OnMouse(wxMouseEvent& event)
     }
     else if ((m_mi != i) || (m_mj != j))
     {
+        bool alive = (m_status == MOUSE_DRAWING);
+
+        // prepare DC and pen + brush to optimize drawing
+        wxClientDC dc(this);
+        dc.SetPen(alive? *wxBLACK_PEN : *wxWHITE_PEN);
+        dc.SetBrush(alive? *wxBLACK_BRUSH : *wxWHITE_BRUSH);
+        dc.BeginDrawing();
+
         // draw a line of cells using Bresenham's algorithm
         wxInt32 d, ii, jj, di, ai, si, dj, aj, sj;
         di = i - m_mi;
@@ -685,8 +691,8 @@ void LifeCanvas::OnMouse(wxMouseEvent& event)
                
             while (ii != i)
             {
-                m_life->SetCell(ii, jj, m_status == MOUSE_DRAWING);
-                DrawCell(ii, jj, m_status == MOUSE_DRAWING);
+                m_life->SetCell(ii, jj, alive);
+                DrawCell(ii, jj, dc);
                 if (d >= 0)
                 {
                     jj += sj;
@@ -703,8 +709,8 @@ void LifeCanvas::OnMouse(wxMouseEvent& event)
 
             while (jj != j)
             {
-                m_life->SetCell(ii, jj, m_status == MOUSE_DRAWING);
-                DrawCell(ii, jj, m_status == MOUSE_DRAWING);
+                m_life->SetCell(ii, jj, alive);
+                DrawCell(ii, jj, dc);
                 if (d >= 0)
                 {
                     ii += si;
@@ -716,10 +722,12 @@ void LifeCanvas::OnMouse(wxMouseEvent& event)
         }
 
         // last cell
-        m_life->SetCell(ii, jj, m_status == MOUSE_DRAWING);
-        DrawCell(ii, jj, m_status == MOUSE_DRAWING);
+        m_life->SetCell(ii, jj, alive);
+        DrawCell(ii, jj, dc);
         m_mi = ii;
         m_mj = jj;
+
+        dc.EndDrawing();
     }
 
     GET_FRAME()->UpdateInfoText();