Commits

Laurens Rodriguez committed 522a0de

Updated Mandelbrot Fractal viewer
(If you want to run the tetris sample follow the README instructions)

  • Participants
  • Parent commits 03cf827
  • Branches default

Comments (0)

Files changed (2)

File bin/windows/game/README.txt

+In order to test the simple tetris clone change this line in config.kia:
+
+    t.main = "mandelbrot"       // The main script to load (without kia extension)
+
+to:
+
+    t.main = "main"       	// The main script to load (without kia extension)

File bin/windows/game/mandelbrot.kia

 var m_zoomGridY
 
 var m_zoomIsVisible
+var m_isDrawingFractal
 
 // Fractal zones history.
 // Each element is an array with the zone coordinates: [x1, y1, x2, y2]
 // Some nice palette colors
 var m_palette
 
+var m_x
+var m_dx
+var m_dy
+
 //-----------------------------------------------------------------------------
 function drawZoomZone() {
 
     }
 }
 
+function drawSegmentMandelbrot() {
+    var px, py
+    var steps
+    var fx, fy
+    var temp
+
+    love.graphics.setCanvas(m_bitmapBack)
+
+    for (var y = 0; y < m_height; y += 1) {
+        // Point in fractal zone
+        px = m_fractalZoneX1 + m_x * m_dx
+        py = m_fractalZoneY2 - y * m_dy
+
+        // Iterate fractal computation.
+        steps = 0
+        fx = 0.0
+        fy = 0.0
+
+        while (true) {
+            // Mandelbrot recurrence:
+            // ---------------------
+            // F(n+1) = F(n)*F(n) + (px + i*py)
+            temp = fx*fx - fy*fy + px
+            fy = 2*fx*fy + py
+            fx = temp
+
+            steps += 1
+
+            // F(z) belongs to Mandelbrot set if |F(z)| < 2
+            // We give up if we passed the limit of number of iterations.
+            if ((steps >= m_steps) || (fx*fx + fy*fy >= 4.0)) {
+                break;
+            }
+        }
+
+        if (steps < m_steps) {
+            // We found that: |F(z)| >= 2 (the point doesn't belong to the Mandelbrot set)
+            var indexColor = (steps - 1) % 28 + 1
+            if (indexColor > 15) {
+                indexColor = 30 - indexColor
+            }
+            love.graphics.setColor(m_colors[indexColor][0], m_colors[indexColor][1], m_colors[indexColor][2])
+            love.graphics.point(m_x, y)
+        }
+        else {
+            // We suspect this point belongs to the Mandelbrot set.
+            // We can't be sure because the point can belong to the set if the number
+            // of iteration steps is increased. Leave this pixel in black.
+            love.graphics.setColor(0, 0, 0)
+            love.graphics.point(m_x, y)
+        }
+    }
+    m_x += 1
+
+    love.graphics.setCanvas()
+    love.graphics.setColor(255, 255, 255)
+}
+
 function drawMandelbrot(initializeZone) {
 
     if (m_zoomIsVisible) {
         m_fractalZoneY2 = 1.2
     }
 
-    var dx = (m_fractalZoneX2 - m_fractalZoneX1) / (m_width - 1)
-    var dy = (m_fractalZoneY2 - m_fractalZoneY1) / (m_height - 1)
-
-    love.graphics.setCanvas(m_bitmapBack)
-    m_bitmapBack::clear(0, 0, 0)
-
-    var px, py
-    var steps
-    var fx, fy
-    var temp
-
-    // Draw fractal zone
-    for (var x = 0; x < m_width; x += 1) {
-        for (var y = 0; y < m_height; y += 1) {
-            // Point in fractal zone
-            px = m_fractalZoneX1 + x * dx
-            py = m_fractalZoneY2 - y * dy
-
-            // Iterate fractal computation.
-            steps = 0
-            fx = 0.0
-            fy = 0.0
-
-            while (true) {
-                // Mandelbrot recurrence:
-                // ---------------------
-                // F(n+1) = F(n)*F(n) + (px + i*py)
-                temp = fx*fx - fy*fy + px
-                fy = 2*fx*fy + py
-                fx = temp
-
-                steps += 1
-
-                // F(z) belongs to Mandelbrot set if |F(z)| < 2
-                // We give up if we passed the limit of number of iterations.
-                if ((steps >= m_steps) || (fx*fx + fy*fy >= 4.0)) {
-                    break;
-                }
-            }
-
-            if (steps < m_steps) {
-                // We found that: |F(z)| >= 2 (the point doesn't belong to the Mandelbrot set)
-                var indexColor = (steps - 1) % 28 + 1
-                if (indexColor > 15) {
-                    indexColor = 30 - indexColor
-                }
-                love.graphics.setColor(m_colors[indexColor][0], m_colors[indexColor][1], m_colors[indexColor][2])
-                love.graphics.point(x, y)
-            }
-            else {
-                // We suspect this point belongs to the Mandelbrot set.
-                // We can't be sure because the point can belong to the set if the number
-                // of iteration steps is increased. Leave this pixel in black.
-            }
-        }
-    }
-    love.graphics.setCanvas()
-    love.graphics.setColor(255, 255, 255)
+    m_dx = (m_fractalZoneX2 - m_fractalZoneX1) / (m_width - 1)
+    m_dy = (m_fractalZoneY2 - m_fractalZoneY1) / (m_height - 1)
+    m_x = 0
+    m_isDrawingFractal = true
+    
+    drawSegmentMandelbrot()
 }
 
 function redraw() {
 
 function initializeColorData() {
     m_palette = [
-        [0, 4, 62, 5, 31, 3],  [7, 4, 4, 4, 42, 5],   [8, 0, 55, 4, 4, 4],   [8, 5, 8, 4, 8, 1],    [12, 4, 44, 2, 46, 3],
-        [17, 4, 35, 5, 41, 4], [20, 5, 43, 4, 57, 3], [20, 5, 58, 5, 21, 2], [21, 2, 35, 4, 59, 0], [24, 4, 53, 2, 54, 3],
-        [25, 2, 36, 2, 50, 2], [25, 5, 52, 5, 0, 5],  [27, 3, 19, 3, 31, 5], [27, 3, 35, 4, 39, 2], [29, 5, 63, 2, 34, 2],
-        [32, 5, 58, 5, 33, 2], [33, 5, 61, 5, 34, 3], [35, 2, 16, 5, 22, 0], [35, 4, 2, 0, 10, 3],  [36, 4, 43, 4, 35, 2],
-        [38, 4, 63, 3, 55, 5], [39, 5, 8, 2, 48, 2],  [39, 5, 59, 3, 7, 2],  [40, 3, 6, 0, 61, 5],  [40, 5, 58, 5, 25, 2],
-        [41, 2, 49, 5, 52, 3], [41, 5, 59, 5, 0, 3],  [43, 5, 56, 3, 43, 4], [44, 2, 11, 3, 54, 4], [44, 4, 61, 4, 13, 2],
-        [45, 3, 61, 3, 10, 1], [45, 4, 63, 5, 6, 3],  [45, 5, 46, 5, 11, 0], [46, 3, 5, 5, 17, 3],  [47, 1, 30, 4, 14, 0],
-        [48, 5, 58, 5, 17, 2], [48, 5, 63, 4, 6, 4],  [48, 5, 63, 5, 6, 4],  [49, 3, 17, 4, 38, 2], [50, 2, 63, 5, 57, 3],
-        [51, 5, 62, 3, 37, 0], [53, 1, 56, 5, 13, 1], [53, 3, 57, 2, 49, 1], [53, 3, 56, 5, 44, 2], [54, 4, 1, 0, 33, 3],
-        [54, 5, 53, 4, 45, 2], [55, 4, 13, 0, 4, 4],  [55, 4, 40, 5, 34, 2], [55, 5, 57, 2, 56, 2], [57, 2, 14, 3, 20, 0],
-        [58, 1, 15, 5, 9, 2],  [58, 3, 38, 4, 13, 4], [59, 0, 48, 5, 6, 2],  [59, 3, 3, 0, 6, 3],   [59, 5, 4, 4, 60, 0],
-        [59, 5, 52, 3, 5, 0],  [60, 1, 51, 5, 0, 3],  [60, 5, 14, 2, 24, 3], [61, 5, 42, 5, 24, 3], [63, 4, 14, 3, 0, 5]
+        [0, 4, 62, 5, 31, 3],  [7, 4, 4, 4, 42, 5],   [8, 0, 55, 4, 4, 4],  
+        [8, 5, 8, 4, 8, 1],    [12, 4, 44, 2, 46, 3], [17, 4, 35, 5, 41, 4],
+        [20, 5, 43, 4, 57, 3], [20, 5, 58, 5, 21, 2], [21, 2, 35, 4, 59, 0],
+        [24, 4, 53, 2, 54, 3], [25, 2, 36, 2, 50, 2], [25, 5, 52, 5, 0, 5], 
+        [27, 3, 19, 3, 31, 5], [27, 3, 35, 4, 39, 2], [29, 5, 63, 2, 34, 2],
+        [32, 5, 58, 5, 33, 2], [33, 5, 61, 5, 34, 3], [35, 2, 16, 5, 22, 0],
+        [35, 4, 2, 0, 10, 3],  [36, 4, 43, 4, 35, 2], [38, 4, 63, 3, 55, 5],
+        [39, 5, 8, 2, 48, 2],  [39, 5, 59, 3, 7, 2],  [40, 3, 6, 0, 61, 5],  
+        [40, 5, 58, 5, 25, 2], [41, 2, 49, 5, 52, 3], [41, 5, 59, 5, 0, 3], 
+        [43, 5, 56, 3, 43, 4], [44, 2, 11, 3, 54, 4], [44, 4, 61, 4, 13, 2],
+        [45, 3, 61, 3, 10, 1], [45, 4, 63, 5, 6, 3],  [45, 5, 46, 5, 11, 0],
+        [46, 3, 5, 5, 17, 3],  [47, 1, 30, 4, 14, 0], [48, 5, 58, 5, 17, 2],
+        [48, 5, 63, 4, 6, 4],  [48, 5, 63, 5, 6, 4],  [49, 3, 17, 4, 38, 2],
+        [50, 2, 63, 5, 57, 3], [51, 5, 62, 3, 37, 0], [53, 1, 56, 5, 13, 1], 
+        [53, 3, 57, 2, 49, 1], [53, 3, 56, 5, 44, 2], [54, 4, 1, 0, 33, 3],
+        [54, 5, 53, 4, 45, 2], [55, 4, 13, 0, 4, 4],  [55, 4, 40, 5, 34, 2],
+        [55, 5, 57, 2, 56, 2], [57, 2, 14, 3, 20, 0], [58, 1, 15, 5, 9, 2], 
+        [58, 3, 38, 4, 13, 4], [59, 0, 48, 5, 6, 2],  [59, 3, 3, 0, 6, 3], 
+        [59, 5, 4, 4, 60, 0],  [59, 5, 52, 3, 5, 0],  [60, 1, 51, 5, 0, 3],
+        [60, 5, 14, 2, 24, 3], [61, 5, 42, 5, 24, 3], [63, 4, 14, 3, 0, 5]
     ]
 
     m_colorSteps = [
     // Set window title
     love.graphics.setCaption("Mandelbrot: " .. global._VERSION)
 
-    // Set window size
-    assert(love.graphics.setMode(m_width, m_height, false, true, 1), "Could not set screen mode")
-
     love.graphics.setPoint(1, "rough")
     love.graphics.setLine(1, "rough")
 
 
     m_steps = INIT_STEPS
     m_zoomIsVisible = false
+    m_isDrawingFractal = false
     m_zones = []
 
     // Zoom zone init position
 }
 
 function love.draw() {
+    if (m_isDrawingFractal) {
+        drawSegmentMandelbrot()
+        drawSegmentMandelbrot()
+        if (m_x >= m_width) {
+            m_isDrawingFractal = false
+        }
+    }
     love.graphics.draw(m_bitmapBack, 0, 0)
     love.graphics.draw(m_bitmapFront, 0, 0)
 }