diff options
| author | Marc Pervaz Boocha <mboocha@sudomsg.xyz> | 2022-04-03 19:13:29 +0530 |
|---|---|---|
| committer | Marc Pervaz Boocha <mboocha@sudomsg.xyz> | 2022-04-03 19:13:29 +0530 |
| commit | ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22 (patch) | |
| tree | 9f5d7ecc74a206984aee068ce50d5107e8b64f7c | |
| download | mandle-ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22.tar mandle-ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22.tar.gz mandle-ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22.tar.bz2 mandle-ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22.tar.lz mandle-ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22.tar.xz mandle-ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22.tar.zst mandle-ee44ffc271ae7dbe53fd44c8146e5569f8c0ee22.zip | |
Initial commit
Changes to be committed:
new file: .clang-format
new file: .gitignore
new file: license
new file: mandle.c
new file: meson.build
new file: readme.md
new file: subprojects/sdl2.wrap
| -rw-r--r-- | .clang-format | 6 | ||||
| -rw-r--r-- | .gitignore | 25 | ||||
| -rw-r--r-- | license | 22 | ||||
| -rw-r--r-- | mandle.c | 407 | ||||
| -rw-r--r-- | meson.build | 19 | ||||
| -rw-r--r-- | readme.md | 11 | ||||
| -rw-r--r-- | subprojects/sdl2.wrap | 12 |
7 files changed, 502 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a3ef233 --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +BasedOnStyle: LLVM +IndentWidth: 8 +UseTab: Always +BreakBeforeBraces: Linux +AllowShortIfStatementsOnASingleLine: false +IndentCaseLabels: false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..35df969 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +*.o +*.ko +*.obj +*.elf +*.gch +*.pch +*.dll +*.so +*.so.* +*.dylib +*.exe +*.out +*.app +*.pdb +/build*/ +/subprojects/* +!/subprojects/*.wrap +meson-logs +meson-private +meson_benchmark_setup.dat +meson_test_setup.dat +build.ninja +.ninja_deps +.ninja_logs +compile_commands.json @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2022 Marc Pervaz Boocha + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/mandle.c b/mandle.c new file mode 100644 index 0000000..318675f --- /dev/null +++ b/mandle.c @@ -0,0 +1,407 @@ +#include <complex.h> +#include <math.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <SDL2/SDL.h> + +// Some polyfills for complex numbers +#if !defined(CMPLX) +#define CMPLX(x, y) ((double complex)((double)(x) + I * (double)(y))) +#define CMPLXF(x, y) ((float complex)((float)(x) + I * (float)(y))) +#define CMPLXL(x, y) \ + ((long double complex)((long double)(x) + I * (long double)(y))) +#endif + +static uintmax_t mandle(long double complex const c) +{ + const uintmax_t max = UINTMAX_MAX; + if ((pow(creal(c) - 0.25, 2) + pow(cimag(c), 2)) * + (pow(creal(c), 2) + (creal(c) / 2) + pow(cimag(c), 2) - + 0.1875) < + pow(cimag(c), 2) / 4 || + pow(creal(c) + 1, 2) + pow(cimag(c), 2) < 0.0625) { + return 0; + } + long double complex z = c; + long double complex dz = CMPLXL(1, 0); + + for (uintmax_t iteration = 0; iteration <= max; iteration++) { + if (pow(creal(dz), 2) + pow(cimag(dz), 2) * pow(cimag(dz), 2) < + 1e-300) { + break; + } + if (pow(creal(z), 2) + pow(cimag(z), 2) > 4.0) { + return iteration; + } + + z = z * z + c; + dz *= 2 * z; + } + return 0; +} + +static inline void putpixel(SDL_Surface *surface, SDL_Point const point, + SDL_Color color) +{ + if (surface->w <= point.x || surface->h <= point.y) { + return; + } + uint32_t const pixel = + SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a); + int bpp = surface->format->BytesPerPixel; + if (SDL_MUSTLOCK(surface)) { + SDL_LockSurface(surface); + } + + uint8_t *p = (uint8_t *)surface->pixels + point.y * surface->pitch + + point.x * bpp; + + switch (bpp) { + case 1: + *p = pixel; + break; + case 2: + *(uint16_t *)p = pixel; + break; + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + p[0] = pixel >> 16 & UINT8_MAX; + p[1] = pixel >> 8 & UINT8_MAX; + p[2] = pixel & UINT8_MAX; + } else { + p[0] = pixel & UINT8_MAX; + p[1] = (pixel >> 8) & UINT8_MAX; + p[2] = (pixel >> 16) & UINT8_MAX; + } + break; + case 4: + *(uint32_t *)p = pixel; + break; + } + + if (SDL_MUSTLOCK(surface)) { + SDL_UnlockSurface(surface); + } +} + +static inline SDL_Color color_scheme(uintmax_t iteration) +{ + uint8_t color_scale = iteration & UINT8_MAX; + unsigned type = iteration >> 8; + unsigned color_mask = 255 * (type % 5) / 4; + SDL_Color color = {.r = 0, .b = 0, .g = 0, .a = 255}; + switch ((type / 5) % 19) { + case 0: + color = (SDL_Color){ + .r = 0, .b = color_mask, .g = color_scale, .a = 255}; + break; + case 1: + color = (SDL_Color){ + .r = 255, .b = color_mask, .g = color_scale, .a = 255}; + break; + case 2: + color = (SDL_Color){ + .r = color_mask, .b = 0, .g = color_scale, .a = 255}; + break; + case 3: + color = (SDL_Color){ + .r = color_mask, .b = 255, .g = color_scale, .a = 255}; + break; + case 4: + color = (SDL_Color){.r = color_mask, + .b = color_mask, + .g = color_scale, + .a = 255}; + break; + case 5: + color = (SDL_Color){ + .r = 0, .b = color_scale, .g = color_mask, .a = 255}; + break; + case 6: + color = (SDL_Color){ + .r = 255, .b = color_scale, .g = color_mask, .a = 255}; + break; + case 7: + color = (SDL_Color){ + .r = color_mask, .b = color_scale, .g = 0, .a = 255}; + break; + case 8: + color = (SDL_Color){ + .r = color_mask, .b = color_scale, .g = 255, .a = 255}; + break; + case 9: + color = (SDL_Color){.r = color_mask, + .b = color_scale, + .g = color_mask, + .a = 255}; + break; + case 10: + color = (SDL_Color){ + .r = color_scale, .b = 0, .g = color_mask, .a = 255}; + break; + case 11: + color = (SDL_Color){ + .r = color_scale, .b = 255, .g = color_mask, .a = 255}; + break; + case 12: + color = (SDL_Color){ + .r = color_scale, .b = color_mask, .g = 0, .a = 255}; + break; + case 13: + color = (SDL_Color){ + .r = color_scale, .b = color_mask, .g = 255, .a = 255}; + break; + case 14: + color = (SDL_Color){.r = color_scale, + .b = color_mask, + .g = color_mask, + .a = 255}; + break; + case 15: + color = (SDL_Color){.r = color_scale, + .b = color_scale, + .g = color_mask, + .a = 255}; + break; + case 16: + color = (SDL_Color){.r = color_scale, + .b = color_mask, + .g = color_scale, + .a = 255}; + break; + case 17: + color = (SDL_Color){.r = color_mask, + .b = color_scale, + .g = color_scale, + .a = 255}; + break; + case 18: + color = (SDL_Color){.r = color_scale, + .b = color_scale, + .g = color_scale, + .a = 255}; + break; + } + return (type / (5 * 19)) % 2 ? color + : (SDL_Color){.r = ~color.r, + .b = ~color.b, + .g = ~color.g, + .a = color.a}; +} + +struct mandle_thr { + SDL_Surface *surface; + SDL_Point point; + SDL_mutex *mutex; + SDL_cond *cond; + SDL_mutex *cond_mutex; + uint32_t job_id; + char sleep_no; + bool quit; + long double complex centre; + long double scale; +}; + +static int mandle_thr(void *ptr) +{ + struct mandle_thr *data = ptr; + uint32_t id = 0; + SDL_LockMutex(data->cond_mutex); + data->sleep_no++; + SDL_CondWait(data->cond, data->cond_mutex); + data->sleep_no--; + SDL_UnlockMutex(data->cond_mutex); + SDL_LockMutex(data->mutex); + while (true) { + if (data->quit) { + SDL_UnlockMutex(data->mutex); + return 0; + } + if (id != data->job_id) { + id = data->job_id; + } + SDL_Point point = data->point; + if (data->point.x < data->surface->w) { + data->point.x++; + } else if (data->point.y < data->surface->h) { + data->point.y++; + data->point.x = 0; + } else { + SDL_UnlockMutex(data->mutex); + SDL_LockMutex(data->cond_mutex); + data->sleep_no++; + SDL_CondWait(data->cond, data->cond_mutex); |
