Commits

Toni Ruža committed d1dc9bf

Added background drawing test.

  • Participants
  • Parent commits 1a9aaef

Comments (0)

Files changed (5)

File glutton_tester.py

         event.Skip()
 
 
-def test(test_class):
+def test(test_class, maximize=False):
     app = wx.App(False)
     app.TopWindow = f = TestFrame(test_class)
+    f.Maximize(maximize)
     f.Show()
     app.MainLoop()

File sierpinski_triangle.py

+
+def construct_sierpinski_triangle(points, size, count):
+    import time
+    from random import choice
+
+    vertices = ((0.0, 0.0), (10.0, 0.0), (5.0, 10.0))
+    x, y = 5.0, 5.0
+    start = time.clock()
+
+    for c in xrange(size):
+        vx, vy = choice(vertices)
+        i = 2 * c
+        points[i] = x = (x + vx) / 2.0
+        points[i + 1] = y = (y + vy) / 2.0
+        if c % 100 == 0:
+            count.value = c
+
+    print "time taken:", time.clock() - start

File test_background_drawing_multiprocessing.py

+def main():
+    import glutton_tester
+    import test_background_drawing_threads as test
+    test.MULTIPROCESSING = True 
+    glutton_tester.test(test.TestCanvas, maximize=True)
+
+if __name__ == "__main__":
+    main()

File test_background_drawing_multiprocessing_pypy.py

+if __name__ == "__main__":
+    import multiprocessing as mp
+    mp.set_executable(r"c:\pypy-1.8\pypy.exe")
+
+    from test_background_drawing_multiprocessing import main
+    main()

File test_background_drawing_threads.py

+import threading
+import multiprocessing as mp
+from ctypes import c_float, c_int32
+
+import wx
+from OpenGL.GL import *
+import numpy as np
+
+import glutton_tester
+from glutton import canvas
+from glutton.mixins.navigate2d import Navigate2D
+
+from sierpinski_triangle import construct_sierpinski_triangle
+
+
+NUMBER_OF_POINTS = 1000000
+MULTIPROCESSING = False
+
+
+class TestCanvas(canvas.Canvas, Navigate2D):
+    def __init__(self, parent):
+        canvas.Canvas.__init__(self, parent, enforce_fps=60)
+
+    def start_background_drawing(self):
+        self.raw_points = mp.Array(c_float, 2 * NUMBER_OF_POINTS)
+        self.points = np.frombuffer(self.raw_points.get_obj(), np.float32)
+        
+        self.points_count = mp.Value(c_int32)
+        self.points_count.value = 0
+        
+        Worker = mp.Process if MULTIPROCESSING else threading.Thread
+        self.worker = Worker(
+            target=construct_sierpinski_triangle, 
+            args=(self.raw_points, NUMBER_OF_POINTS, self.points_count)
+        )
+        self.worker.start()
+
+    def init_context(self):
+        glColor3ub(255, 255, 255)
+        wx.CallAfter(self.pan, 5, 5)
+        
+        self.start_background_drawing()
+        
+        self.Parent.Bind(wx.EVT_CLOSE, self.OnExit)
+
+    def OnExit(self, event):
+        self.worker.join()
+        event.Skip()
+
+    def render_scene(self):
+        glClear(GL_COLOR_BUFFER_BIT)
+        glEnableClientState(GL_VERTEX_ARRAY)
+        glVertexPointer(2, GL_FLOAT, 0, self.points)
+        glDrawArrays(GL_POINTS, 0, self.points_count.value)
+        glDisableClientState(GL_VERTEX_ARRAY)
+
+
+if __name__ == "__main__":
+    glutton_tester.test(TestCanvas, maximize=True)