1. hi again Mandelbrot Set program (Win32Lib)
- Posted by ron77 3 weeks ago
- 159 views
Hello again, this time I have a Mandelbrot Set (GUI?) program using win32lib for Windows mandelbrot.exw
include win32lib.ew -- Constants constant WIDTH = 800, HEIGHT = 800, MIN_VAL = -2.84, MAX_VAL = 1.0, MAX_ITERATIONS_START = 200, COUNT_LIMIT = 30, TIMER_ID = 1 -- Global variables atom count, min_val, max_val, factor, max_iterations sequence bitmap_data integer animation_running -- Window ID with no background brush to prevent clearing constant MainWindow = create(Window, "Mandelbrot Set", 0, Default, Default, WIDTH + 16, HEIGHT + 39, 0) -- Set window class to not erase background procedure setup_window() atom hwnd = getHandle(MainWindow) atom user32 = open_dll("user32.dll") atom setClassLong_id = define_c_func(user32, "SetClassLongA", {C_POINTER, C_INT, C_LONG}, C_LONG) -- Remove background brush (GCL_HBRBACKGROUND = -10) atom result = c_func(setClassLong_id, {hwnd, -10, 0}) end procedure -- Initialize variables procedure init_vars() count = 0 min_val = MIN_VAL max_val = MAX_VAL factor = 1.0 max_iterations = MAX_ITERATIONS_START animation_running = 1 bitmap_data = repeat(repeat(0, WIDTH), HEIGHT) end procedure -- Map function to convert values between ranges function map_value(atom value, atom in_min, atom in_max, atom out_min, atom out_max) return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min end function -- Create bitmap for fast pixel manipulation procedure create_bitmap() -- We'll use a simpler approach with win32lib -- Create a bitmap data array that we'll draw pixel by pixel bitmap_data = repeat(repeat(0, WIDTH), HEIGHT) end procedure -- Calculate Mandelbrot set and update bitmap procedure calculate_mandelbrot() atom a, b, ai, bi, a1, b1 integer n integer bright, red, green, blue, color -- Calculate Mandelbrot set for x = 0 to WIDTH - 1 do for y = 0 to HEIGHT - 1 do a = map_value(x, 0, WIDTH, min_val, max_val) b = map_value(y, 0, HEIGHT, min_val, max_val) ai = a bi = b n = 0 for idx = 1 to max_iterations do a1 = a * a - b * b b1 = 2 * a * b a = a1 + ai b = b1 + bi if (a + b) > 2 then exit end if n += 1 end for bright = floor(map_value(n, 0, max_iterations, 0, 255)) if n = max_iterations or bright < 20 then bright = 0 end if red = floor(map_value(bright * bright, 0, 65025, 0, 255)) green = bright blue = floor(map_value(sqrt(bright), 0, 15.97, 0, 255)) -- Store color in bitmap data array color = rgb(red, green, blue) bitmap_data[y+1][x+1] = color end for end for end procedure -- Paint event handler - simple, no clearing procedure onPaint_MainWindow(integer self, integer event, sequence params) calculate_mandelbrot() -- Draw pixels directly over previous image for y = 0 to HEIGHT - 1 do for x = 0 to WIDTH - 1 do setPixel(MainWindow, x, y, bitmap_data[y+1][x+1]) end for end for end procedure -- Animation update procedure procedure update_animation() max_val -= 0.1 * factor min_val += 0.15 * factor factor *= 0.9349 max_iterations += 5 if count > COUNT_LIMIT then max_iterations = floor(max_iterations * 1.02) end if count += 1 repaintWindow(MainWindow) end procedure -- Timer callback using Windows API procedure timer_callback() if animation_running then update_animation() end if end procedure -- Start Windows timer procedure start_timer() atom user32, setTimer_id user32 = open_dll("user32.dll") setTimer_id = define_c_func(user32, "SetTimer", {C_POINTER, C_UINT, C_UINT, C_POINTER}, C_UINT) if c_func(setTimer_id, {getHandle(MainWindow), TIMER_ID, 50, 0}) = 0 then message_box("Failed to create timer", "Error", 0) end if end procedure -- Handle timer message procedure onTimer_MainWindow(integer self, integer event, sequence params) if params[1] = TIMER_ID then timer_callback() end if end procedure -- Key event handler procedure onKeyDown_MainWindow(integer self, integer event, sequence params) if upper(params[1]) = 'Q' then animation_running = 0 closeWindow(MainWindow) elsif upper(params[1]) = ' ' then -- Spacebar to toggle animation animation_running = not animation_running if animation_running then repaintWindow(MainWindow) end if end if end procedure -- Close event handler procedure onClose_MainWindow(integer self, integer event, sequence params) animation_running = 0 closeWindow(MainWindow) end procedure -- Set event handlers setHandler(MainWindow, w32HKeyDown, routine_id("onKeyDown_MainWindow")) setHandler(MainWindow, w32HPaint, routine_id("onPaint_MainWindow")) setHandler(MainWindow, w32HClose, routine_id("onClose_MainWindow")) setHandler(MainWindow, w32HTimer, routine_id("onTimer_MainWindow")) -- Initialize and start init_vars() create_bitmap() setup_window() -- Disable background clearing start_timer() -- Initial paint repaintWindow(MainWindow) -- Main message loop WinMain(MainWindow, Normal)
2. Re: hi again Mandelbrot Set program (Win32Lib)
- Posted by ChrisB (moderator) 3 weeks ago
- 154 views
Hi
Both these are very nice - nice to see eu being played with!
Cheers
Chris