mandelbrot-fb.c (1199B)
1 // Copyright 2022, Brian Swetland <swetland@frotz.net> 2 // Licensed under the Apache License, Version 2.0 3 4 #include <string.h> 5 #include <stdint.h> 6 7 #include <hw/debug.h> 8 #include <hw/platform.h> 9 #include <hw/litex.h> 10 #include <gfx/gfx.h> 11 12 #define FB_WIDTH 640 13 #define FB_HEIGHT 480 14 15 uint16_t colors[12] = { 16 0x1111, 0x2222, 0x3333, 0x4444, 17 0x5555, 0x6666, 0x7777, 0x8888, 18 0x9999, 0xAAAA, 0xBBBB, 0xCCCC, 19 }; 20 21 void start(void) { 22 xputs("Hello, Mandelbrot!\n"); 23 24 gfx_surface_t gs; 25 gfx_init_display(&gs); 26 gfx_clear(&gs, C_BLUE); 27 28 gfx_puts(&gs, 0, gs.height - 17, "Hello, Mandelbrot!"); 29 30 for (int py = 0; py < FB_HEIGHT; py++) { 31 int y0 = 1300 - (2600 * py) / FB_HEIGHT; 32 for (int px = 0; px < FB_WIDTH; px++) { 33 int x0 = -2500 + (3500 * px) / FB_WIDTH; 34 int x = 0, y = 0; 35 for (int i = 0; i < 1000; i++) { 36 int x2 = x * x / 1000; 37 int y2 = y * y / 1000; 38 if ((x2 + y2) > 4000) { 39 gs.fgcolor = colors[(i > 11) ? 11 : i]; 40 gfx_plot(&gs, px, py); 41 goto done; 42 } 43 y = 2 * x * y / 1000 + y0; 44 x = x2 - y2 + x0; 45 } 46 gs.fgcolor = 0; 47 gfx_plot(&gs, px, py); 48 done: 49 ; 50 } 51 gs.fgcolor = 0xFFFF; 52 } 53 54 gfx_puts(&gs, 0, gs.height - 16, "Hello, Mandelbrot!"); 55 }