From 8d32ccfa2e853fead77b56985a403df98ec18f8c Mon Sep 17 00:00:00 2001 From: Dmitry Brant Date: Sun, 26 Jan 2025 15:57:39 -0500 Subject: [PATCH] Add Mandelbrot graphics demo. --- src/Demo/Graphics/Mandel.ZC | 219 ++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 src/Demo/Graphics/Mandel.ZC diff --git a/src/Demo/Graphics/Mandel.ZC b/src/Demo/Graphics/Mandel.ZC new file mode 100644 index 00000000..2220a62c --- /dev/null +++ b/src/Demo/Graphics/Mandel.ZC @@ -0,0 +1,219 @@ +// Mandelbrot set explorer (multi-core). +// +// - Click-and drag to move around. +// - PGUP/PGDN to zoom +// - HOME/END to increase/decrease iterations +// - ESC to exit + +I64 width = GR_WIDTH; +I64 height = GR_HEIGHT; +I64 iterations = 24; +F64 xCenter = -0.5, yCenter = 0.0; +F64 xExtent = 3.0; + + +I64 numCores = mp_count - 1; + +// ------------------- + +CDC* image; +I64 rowsPerCore = height / numCores; +I64 currentSequence; + + +U0 MandelFrame(I64 core) +{ +// CDC* dc = DCAlias(image); + I64 mySequence = currentSequence; + + I64 startX = 0; + I64 maxX = startX + width; + I64 startY = core * rowsPerCore; + I64 maxY = startY + rowsPerCore; + + F64 xmin = xCenter - (xExtent / 2.0); + F64 xmax = xmin + xExtent; + F64 aspect = ToF64(width) / ToF64(height); + F64 ymin = yCenter - (xExtent / aspect / 2.0); + F64 ymax = ymin + (xExtent / aspect); + + F64 x, y, x0, y0, x2, y2; + F64 xscale = (xmax - xmin) / ToF64(width); + F64 yscale = (ymax - ymin) / ToF64(height); + I64 iteration; + I64 px, py; + + for (py = startY; py < maxY; py++) { + y0 = ymin + ToF64(py) * yscale; + if (mySequence != currentSequence) + break; + + for (px = startX; px < maxX; px++) { + x = 0.0; y = 0.0; + x2 = 0.0; y2 = 0.0; + iteration = 0; + x0 = xmin + ToF64(px) * xscale; + + while ((x2 + y2) < 4.0) { + y = (2.0 *x * y) + y0; + x = x2 - y2 + x0; + x2 = x * x; + y2 = y * y; + if (++iteration > iterations) { + break; + } + } + +/* + if (iteration >= iterations) { + dc->color = BLACK; + } else { + dc->color = iteration & 15; + } + GrPlot0(dc, px, py); +*/ + + // Instead of getting a DC handle + // every time, we can just poke + // into the body of the DC. + // (is that ok?) + if (iteration >= iterations) + image->body[py*width+px] = 15; + else + image->body[py*width+px] = iteration % 15; + + + } + } +// DCDel(dc); +} + +U0 DrawIt(CTask *task, CDC *dc) { + image->flags |= DCF_NO_TRANSPARENTS; + GrBlot(dc, 0, 0, image); +} + +U0 Mandel() { + SettingsPush; + AutoComplete; + WinBorder; + WinMax; + + DocClear; + DCFill; + + Fs->win_inhibit = WIG_TASK_DEFAULT-WIF_SELF_FOCUS-WIF_SELF_BORDER; + + I64 msg_code, arg1, arg2; + I64 i, j, lastx, lasty; + F64 aspect; + Bool moving = FALSE; + Bool renderFrame = TRUE; + + + // Make our own palette, if we like. + CBGR48 bgr; + for (i=0; idraw_it = &DrawIt; + + while(TRUE) { + + if (renderFrame) { + renderFrame = FALSE; + + currentSequence++; + for (i=0; i