Recent

Author Topic: help with SDL2 mandelbrot code  (Read 1486 times)

ron77

  • New Member
  • *
  • Posts: 22
help with SDL2 mandelbrot code
« on: August 25, 2021, 10:38:32 am »
hello.

I'm trying to make a Mandelbrot set to display on the window screen using the SDL2 library in Lazarus. i made a program (console) and copied a code originally made in C++ and tried to convert it to free pascal. the code compiles and runs but you get a black screen with nothing on it... anyone cares to help me find out what I'm doing wrong? I'm new to Lazarus or free pascal and to use the SDL2 library.

here is the code:
Code: Pascal  [Select][+][-]
  1. program mandelbrot;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses SDL2;
  6.   //{$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   //cthreads,
  8.   //{$ENDIF}{$ENDIF}
  9.   //Classes
  10.   //{ you can add units after this };
  11.  
  12. var
  13.    //window:SDL_Window@
  14.    // Renderer:SDL_Renderer@;
  15.    // Event:SDL_Event@;
  16.    WIDTH:integer = 1000;
  17.    HEIGHT:integer = 1000;
  18.  
  19.    min_num:currency = -2.84;
  20.    max_num:currency = 1.0;
  21.    factor:currency = 1;
  22.    MAX_ITERATIONS:integer = 200;
  23.    x, y, i, n:integer;
  24.    a,b,ai,bi,a1,b1:currency;
  25.     counter, bright,red,green,blue:integer;
  26.      window: PSDL_Window;
  27.      Renderer: PSDL_Renderer;
  28.  
  29. function map( value, in_min, in_max, out_min, out_max:currency):currency;
  30. begin
  31.     map:= (value - in_min) * (out_max - out_min) / (in_max - out_max) + out_min;
  32. end;
  33.  
  34.  
  35.  
  36.  
  37. procedure main;
  38. begin
  39. //var
  40. //
  41.  
  42.     SDL_Init(SDL_INIT_EVERYTHING);
  43.  
  44.  
  45.      // full set up
  46.   Window := SDL_CreateWindow('Window', 50, 50, 500, 500, SDL_WINDOW_SHOWN);
  47.   if Window = nil then Halt;
  48.  
  49.   Renderer := SDL_CreateRenderer(Window, -1, 0);
  50.   if Renderer = nil then Halt;
  51.  
  52.  
  53.  
  54.   if SDL_CreateWindowAndRenderer(1920, 1080, SDL_WINDOW_RESIZABLE, @Window, @Renderer) <> 0
  55.     then Halt;
  56.  
  57.  
  58.  
  59.  
  60.     SDL_RenderSetLogicalSize(Renderer, WIDTH, HEIGHT);
  61.  
  62.     counter := 0;
  63.  
  64.     while True do
  65.  
  66.         max_num -= 0.1 * factor;
  67.         min_num += 0.15 * factor;
  68.         factor *= 0.9349;
  69.         MAX_ITERATIONS += 5;
  70.  
  71.  
  72.  
  73.         SDL_RenderPresent(Renderer);
  74.  
  75.         for x:= 0 to WIDTH do
  76.             for y := 0 to WIDTH do
  77.  
  78.  
  79.  
  80.  
  81.                 a := map(x, 0, WIDTH, min_num, max_num);
  82.                  b := map(y, 0, WIDTH, min_num, max_num);
  83.  
  84.                  ai := a;
  85.                  bi := b;
  86.  
  87.                 n := 0;
  88.  
  89.  
  90.  
  91.                 for i := 0 to MAX_ITERATIONS do
  92.                     begin
  93.                     a1 := a * a - b * b;
  94.                     b1 := 2 * a * b;
  95.  
  96.                     a := a1 + ai;
  97.                     b := b1 + bi;
  98.  
  99.                     if ((a + b) > 2) then
  100.                         begin
  101.                         break;
  102.                         end;
  103.  
  104.  
  105.  
  106.                     n := n +1;
  107.  
  108.                  bright := Trunc(map(n, 0, MAX_ITERATIONS, 0, 255));
  109.  
  110.                 if ((n = MAX_ITERATIONS)) then
  111.                    begin
  112.                    bright := 0;
  113.                    end;
  114.  
  115.  
  116.  
  117.                 red := Trunc(map(bright * bright, 0, 6502, 0, 255));
  118.                 green := bright;
  119.                 blue := Trunc(map(sqrt(bright), 0, sqrt(255), 0, 255));
  120.  
  121.                 SDL_SetRenderDrawColor(Renderer, red, green, blue, 255);
  122.                 SDL_RenderDrawPoint(Renderer, x, y);
  123.  
  124.             end;
  125.  
  126.  
  127.         end;
  128. end;
  129.  
  130.  begin
  131.  main;
  132.  end.
  133.  
  134.  
  135.  
  136.  

here is the original code in C++ that works

Code: C++  [Select][+][-]
  1. #include <iostream>
  2.  
  3. #include <SDL.h>
  4. #include <windows.h>
  5. #include <math.h>
  6.  
  7. int WIDTH = 1000;
  8. int HEIGHT = 1000;
  9.  
  10. long double min_num = -2.84;
  11. long double max_num = 1.0;
  12. long double factor = 1;
  13. int MAX_ITERATIONS = 200;
  14.  
  15.  
  16.  
  17. long double map(long double value, long double in_min, long double in_max, long double out_min, long double out_max)
  18. {
  19.     return (value - in_min) * (out_max - out_min) / (in_max - out_max) + out_min;
  20. }
  21.  
  22.  
  23.  
  24.  
  25. int main(int argc, char* argv[])
  26. {
  27.     SDL_Init(SDL_INIT_EVERYTHING);
  28.  
  29.     SDL_Window* window;
  30.     SDL_Renderer* Renderer;
  31.     SDL_Event Event;
  32.  
  33.  
  34.     SDL_CreateWindowAndRenderer(1920, 1080, SDL_WINDOW_RESIZABLE, &window, &Renderer);
  35.     //    int SDL_CreateWindowAndRenderer(1440, 900, &window, &Renderer);
  36.     SDL_RenderSetLogicalSize(Renderer, WIDTH, HEIGHT);
  37.  
  38.     int counter = 0;
  39.  
  40.     while (1) {
  41.  
  42.         max_num -= 0.1 * factor;
  43.         min_num += 0.15 * factor;
  44.         factor *= 0.9349;
  45.         MAX_ITERATIONS += 5;
  46.  
  47.  
  48.  
  49.         SDL_RenderPresent(Renderer);
  50.         //        SDL_RendererPresent(Renderer);
  51.         for (int x = 0; x < WIDTH; x++) {
  52.             for (int y = 0; y < WIDTH; y++) {
  53.  
  54.                 if (SDL_PollEvent(&Event) && Event.type == SDL_QUIT) {
  55.                     return 0;
  56.                 }
  57.                 if (GetKeyState('Q') & 0x8000) {
  58.                     return 0;
  59.                 }
  60.  
  61.                 long double a = map(x, 0, WIDTH, min_num, max_num);
  62.                 long double b = map(y, 0, WIDTH, min_num, max_num);
  63.  
  64.                 long double ai = a;
  65.                 long double bi = b;
  66.  
  67.                 int n = 0;
  68.  
  69.  
  70.  
  71.                 for (int i = 0; i < MAX_ITERATIONS; i++) {
  72.  
  73.                     long double a1 = a * a - b * b;
  74.                     long double b1 = 2 * a * b;
  75.  
  76.                     a = a1 + ai;
  77.                     b = b1 + bi;
  78.  
  79.                     if ((a + b) > 2) {
  80.  
  81.                         break;
  82.                     }
  83.  
  84.  
  85.  
  86.                     n++;
  87.                 }
  88.                 int bright = map(n, 0, MAX_ITERATIONS, 0, 255);
  89.  
  90.                 if ((n == MAX_ITERATIONS)) {
  91.                     bright = 0;
  92.  
  93.                 }
  94.  
  95.  
  96.                 int red = map(bright * bright, 0, 6502, 0, 255);
  97.                 int green = bright;
  98.                 int blue = map(sqrt(bright), 0, sqrt(255), 0, 255);
  99.  
  100.                 SDL_SetRenderDrawColor(Renderer, red, green, blue, 255);
  101.                 SDL_RenderDrawPoint(Renderer, x, y);
  102.  
  103.             }
  104.  
  105.  
  106.         }
  107.  
  108.         counter++;
  109.     }
  110.  
  111.  
  112.  
  113.     return 0;
  114. }
  115.  

here is an example of the desired output:
« Last Edit: August 25, 2021, 11:09:46 am by ron77 »

nouzi

  • Full Member
  • ***
  • Posts: 229
Re: help with SDL2 mandelbrot code
« Reply #1 on: August 25, 2021, 12:02:39 pm »
Are you looking for something like this?

https://youtu.be/ARFC6Kf6Tnw
my english is  bad
Lazarus 2.0.6 free pascal 3.0.4
Lazarus trunk  free pascal trunk 
System : linux mint 19.3 64bit  windows 7 64bit

ron77

  • New Member
  • *
  • Posts: 22
Re: help with SDL2 mandelbrot code
« Reply #2 on: August 25, 2021, 12:08:57 pm »
no...
I'm looking for something like this:
https://youtu.be/KnCNfBb2ODQ

nouzi

  • Full Member
  • ***
  • Posts: 229
Re: help with SDL2 mandelbrot code
« Reply #3 on: August 25, 2021, 12:27:23 pm »
my english is  bad
Lazarus 2.0.6 free pascal 3.0.4
Lazarus trunk  free pascal trunk 
System : linux mint 19.3 64bit  windows 7 64bit

 

TinyPortal © 2005-2018