Просмотр исходного кода

Add mandelbrot from https://github.com/xMasterX/all-the-plugins

git-subtree-dir: mandelbrot
git-subtree-mainline: 498fba47822ae51dcb06a6f893a046f50f591202
git-subtree-split: 9d81a28742baeffb109b8a1e62bad04d1810fd7f
Willy-JL 2 лет назад
Родитель
Сommit
b2787a7e76

+ 1 - 0
mandelbrot/.gitsubtree

@@ -0,0 +1 @@
+https://github.com/xMasterX/all-the-plugins dev apps_source_code/mandelbrot

BIN
mandelbrot/Mandelbrot.png


+ 16 - 0
mandelbrot/application.fam

@@ -0,0 +1,16 @@
+App(
+    appid="mandelbrotset",
+    name="Mandelbrot Set",
+    apptype=FlipperAppType.EXTERNAL,
+    entry_point="mandelbrot_app",
+    cdefines=["APP_MANDELBROT_GAME"],
+    requires=["gui"],
+    stack_size=1 * 1024,
+    order=130,
+    fap_icon="Mandelbrot.png",
+    fap_category="Games",
+    fap_author="@Possibly-Matt",
+    fap_weburl="https://github.com/Possibly-Matt",
+    fap_version="1.1",
+    fap_description="The Mandelbrot set is the set of all so-called (complex) numbers that meet Mandelbrots simple arithmetic criterion.",
+)

BIN
mandelbrot/img/1.png


BIN
mandelbrot/img/2.png


+ 172 - 0
mandelbrot/mandelbrot.c

@@ -0,0 +1,172 @@
+#include <furi.h>
+#include <math.h>
+#include <gui/gui.h>
+#include <input/input.h>
+#include <stdlib.h>
+
+typedef enum {
+    EventTypeTick,
+    EventTypeKey,
+} EventType;
+
+typedef struct {
+    EventType type;
+    InputEvent input;
+} PluginEvent;
+
+typedef struct {
+    FuriMutex* mutex;
+    float xZoom;
+    float yZoom;
+    float xOffset;
+    float yOffset;
+    float zoom;
+} PluginState;
+
+bool mandelbrot_pixel(int x, int y, float xZoom, float yZoom, float xOffset, float yOffset) {
+    float ratio = 128.0 / 64.0;
+    //x0 := scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.00, 0.47))
+    float x0 = (((x / 128.0) * ratio * xZoom)) - xOffset;
+    //y0 := scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1.12, 1.12))
+    float y0 = ((y / 64.0) * yZoom) - yOffset;
+    float x1 = 0.0;
+    float y1 = 0.0;
+    float x2 = 0.0;
+    float y2 = 0.0;
+
+    int iteration = 0;
+    int max_iteration = 50;
+
+    while(x2 + y2 <= 4.0 && iteration < max_iteration) {
+        y1 = 2.0 * x1 * y1 + y0;
+        x1 = x2 - y2 + x0;
+        x2 = x1 * x1;
+        y2 = y1 * y1;
+        iteration++;
+    }
+
+    if(iteration > 49) {
+        return true;
+    }
+
+    return false;
+}
+
+static void render_callback(Canvas* const canvas, void* ctx) {
+    furi_assert(ctx);
+    const PluginState* plugin_state = ctx;
+    furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
+    // border around the edge of the screen
+    canvas_draw_frame(canvas, 0, 0, 128, 64);
+
+    for(int y = 0; y < 64; y++) {
+        for(int x = 0; x < 128; x++) {
+            // did you know if you just pass the indivdiual bits of plugin_state instead of plugin_state
+            // you dont get any compiler warnings :)
+            if(mandelbrot_pixel(
+                   x,
+                   y,
+                   plugin_state->xZoom,
+                   plugin_state->yZoom,
+                   plugin_state->xOffset,
+                   plugin_state->yOffset)) {
+                canvas_draw_dot(canvas, x, y);
+            }
+        }
+    }
+
+    furi_mutex_release(plugin_state->mutex);
+}
+
+static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
+    furi_assert(event_queue);
+
+    PluginEvent event = {.type = EventTypeKey, .input = *input_event};
+    furi_message_queue_put(event_queue, &event, FuriWaitForever);
+}
+
+static void mandelbrot_state_init(PluginState* const plugin_state) {
+    plugin_state->xOffset = 3.0;
+    plugin_state->yOffset = 1.12;
+    plugin_state->xZoom = 2.47;
+    plugin_state->yZoom = 2.24;
+    plugin_state->zoom = 1; // this controls the camera when
+}
+
+int32_t mandelbrot_app(void* p) {
+    UNUSED(p);
+
+    FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
+
+    PluginState* plugin_state = malloc(sizeof(PluginState));
+    mandelbrot_state_init(plugin_state);
+    plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
+    if(!plugin_state->mutex) {
+        FURI_LOG_E("mandelbrot", "cannot create mutex\r\n");
+        furi_message_queue_free(event_queue);
+        free(plugin_state);
+        return 255;
+    }
+
+    // Set system callbacks
+    ViewPort* view_port = view_port_alloc();
+    view_port_draw_callback_set(view_port, render_callback, plugin_state);
+    view_port_input_callback_set(view_port, input_callback, event_queue);
+
+    // Open GUI and register view_port
+    Gui* gui = furi_record_open(RECORD_GUI);
+    gui_add_view_port(gui, view_port, GuiLayerFullscreen);
+
+    PluginEvent event;
+    for(bool processing = true; processing;) {
+        FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
+        furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
+
+        if(event_status == FuriStatusOk) {
+            // press events
+            if(event.type == EventTypeKey) {
+                if(event.input.type == InputTypePress) {
+                    switch(event.input.key) {
+                    case InputKeyUp:
+                        plugin_state->yOffset += 0.1 / plugin_state->zoom;
+                        break;
+                    case InputKeyDown:
+                        plugin_state->yOffset += -0.1 / plugin_state->zoom;
+                        break;
+                    case InputKeyRight:
+                        plugin_state->xOffset += -0.1 / plugin_state->zoom;
+                        break;
+                    case InputKeyLeft:
+                        plugin_state->xOffset += 0.1 / plugin_state->zoom;
+                        break;
+                    case InputKeyOk:
+                        plugin_state->xZoom -= (2.47 / 10) / plugin_state->zoom;
+                        plugin_state->yZoom -= (2.24 / 10) / plugin_state->zoom;
+                        // used to make camera control finer the more zoomed you are
+                        // this needs to be some sort of curve
+                        plugin_state->zoom += 0.15;
+                        break;
+                    case InputKeyBack:
+                        processing = false;
+                        break;
+                    default:
+                        break;
+                    }
+                }
+            }
+        }
+
+        furi_mutex_release(plugin_state->mutex);
+        view_port_update(view_port);
+    }
+
+    view_port_enabled_set(view_port, false);
+    gui_remove_view_port(gui, view_port);
+    furi_record_close(RECORD_GUI);
+    view_port_free(view_port);
+    furi_message_queue_free(event_queue);
+    furi_mutex_free(plugin_state->mutex);
+    free(plugin_state);
+
+    return 0;
+}