# HG changeset patch # User Eris Caffee # Date 1287732112 18000 # Node ID 0684158d38a8f6c940dc0e8c0a85afa5994acf45 # Parent 455406f5f0219b8543834d90ccd00e2b6411fd76 Before cleanup. Still has experimental code in it. diff -r 455406f5f021 -r 0684158d38a8 TODO --- a/TODO Tue Oct 19 03:37:31 2010 -0500 +++ b/TODO Fri Oct 22 02:21:52 2010 -0500 @@ -6,6 +6,27 @@ - Read saved image and continue calculation. +- Zoom feature that zooms in while staying centered on the same spot. - Unzoom feature that enlarges the area while keeping the image cenetered on the same spot. +- Pan feature to let you move side to side while staying at the same zoom level. + +- Windows file handling +- Better file handling in general + +- real GUI interface + - gtk? + +- More color options + +- More factal types + +- Fix the lag of the selection box. It needs to appear instantly and in the exact place the mouse is clicked. + Using a timer callback function and SDL_WaitEvent is noticably better + than my wait_event_timeout() function + +- Clean up the fractal class. No more direct access to fractal settings. - Performance improvment ideas: + - Create a partial rendering mechanism. + OK - Use window events correctly to determine when a redraw is needed! + OK - Use a better way of handling events. A timer callback and an SDL_WaitEvent/ SDL_PollEvent combo. See here http://people.freedesktop.org/~idr/OpenGL_tutorials/01-SDL-intro.html \ No newline at end of file diff -r 455406f5f021 -r 0684158d38a8 include/App.h --- a/include/App.h Tue Oct 19 03:37:31 2010 -0500 +++ b/include/App.h Fri Oct 22 02:21:52 2010 -0500 @@ -40,24 +40,28 @@ // Overrides of EventHandler: void on_event(SDL_Event * event); void on_KeyDown(SDLKey sym, SDLMod mod, Uint16 unicode); + void on_KeyUp(SDLKey sym, SDLMod mod, Uint16 unicode); void on_Exit(); void on_LButtonDown(int mx, int my); void on_LButtonUp(int mx, int my); void on_MouseMove(int mx, int my, int relx, int rely, bool left, bool right, bool middle); + void on_Resize(int w, int h); + void on_Expose(); + int mouse_move_width(int mx, int my); bool create_new_surface(SDL_Surface * &surface); void restore_view(); void set_caption(); - // void png_user_warning(png_structp png, png_const_charp s); - // void png_user_error(png_structp png, png_const_charp s); bool save_image(); + static Uint32 timer_callback(Uint32 interval, void *param); bool running; + bool redraw; SDL_Surface * surf_display; SDL_Surface * surf_selection; @@ -65,16 +69,15 @@ int selection_x, selection_y, selection_width; bool setting_zoom; - bool can_set_julia_K; - bool setting_julia_K; + bool can_set_julia_k; + bool setting_julia_k; struct view { - double Re_min, Im_max, size; + double re_min, im_max, size; }; static std::vector old_views; - Fractal * fractal; }; diff -r 455406f5f021 -r 0684158d38a8 include/EventHandler.h --- a/include/EventHandler.h Tue Oct 19 03:37:31 2010 -0500 +++ b/include/EventHandler.h Fri Oct 22 02:21:52 2010 -0500 @@ -24,6 +24,10 @@ EventHandler(); virtual ~EventHandler(); +#ifndef USE_SDL_WAITEVENT + virtual int wait_event_timeout(SDL_Event * event, Uint32 timeout); +#endif + virtual void on_event(SDL_Event * event); virtual void on_InputFocus(); diff -r 455406f5f021 -r 0684158d38a8 include/Fractal.h --- a/include/Fractal.h Tue Oct 19 03:37:31 2010 -0500 +++ b/include/Fractal.h Fri Oct 22 02:21:52 2010 -0500 @@ -22,20 +22,35 @@ virtual bool calc_set() = 0; virtual bool init(SDL_Surface * surf) = 0; - const char * name(); + void set_re_min(const double r_min); + void set_im_max(const double i_max); + void set_size(const double s); + void set_max_iter(const unsigned int iter); + void inc_max_iter(); + void dec_max_iter(); + void set_calc_needed(); - double Re_min; - double Im_max; - double size; - unsigned int max_iter; + double get_re_min(); + double get_im_max(); + double get_size(); + unsigned int get_max_iter(); + const char * get_name(); + protected: virtual void draw_pixel(int x, int y, Uint32 * color); - std::string the_name; + std::string name; + double re_min; + double im_max; + double size; + unsigned int max_iter; + bool calc_needed; + SDL_Surface * surface; + }; #endif diff -r 455406f5f021 -r 0684158d38a8 include/Julia.h --- a/include/Julia.h Tue Oct 19 03:37:31 2010 -0500 +++ b/include/Julia.h Fri Oct 22 02:21:52 2010 -0500 @@ -26,16 +26,16 @@ bool calc_set(); bool init(SDL_Surface * surf); - void get_K(double & Re, double & Im); - void set_K(double Re, double Im); + void get_k(double & re, double & im); + void set_k(double re, double im); private: void draw_pixel(int x, int y, Uint32 * color); void set_name(); - double K_Re; - double K_Im; + double k_re; + double k_im; }; #endif diff -r 455406f5f021 -r 0684158d38a8 src/App.cpp --- a/src/App.cpp Tue Oct 19 03:37:31 2010 -0500 +++ b/src/App.cpp Fri Oct 22 02:21:52 2010 -0500 @@ -9,10 +9,10 @@ // //////////////////////////////////////////////////////////////////////////////// -#include -#include #include #include +#include +#include #include #include @@ -27,7 +27,7 @@ #include "Julia.h" -#define SAVE_AS_BMP +#define USE_SDL_WAITEVENT std::vector App::old_views; @@ -35,6 +35,7 @@ App::App() : running (true), + redraw (false), surf_display (NULL), surf_selection (NULL), surf_fractal (NULL), @@ -42,8 +43,8 @@ selection_y (-1), selection_width (-1), setting_zoom (false), - can_set_julia_K (false), - setting_julia_K (false) + can_set_julia_k (false), + setting_julia_k (false) { } @@ -69,12 +70,24 @@ while (running) { - while (SDL_PollEvent(&event)) +#ifdef USE_SDL_WAITEVENT + SDL_WaitEvent(&event); + do { on_event(&event); } + while (SDL_PollEvent(&event)); +#else + while (wait_event_timeout(&event, 30) == 1) + { + on_event(&event); + } +#endif + update(); - render(); + + if (redraw) + render(); } cleanup(); @@ -83,10 +96,28 @@ } //////////////////////////////////////////////////////////////////////////////// +// Sample callback from +// http://people.freedesktop.org/~idr/OpenGL_tutorials/01-SDL-intro.html +#ifdef USE_SDL_WAITEVENT +Uint32 App::timer_callback(Uint32 interval, void *param) + { + SDL_Event e; + + e.type = SDL_USEREVENT; + e.user.code = 0; + e.user.data1 = NULL; + e.user.data2 = NULL; + SDL_PushEvent(& e); + + return interval; + } +#endif + +//////////////////////////////////////////////////////////////////////////////// bool App::init() { - if ((SDL_Init(SDL_INIT_VIDEO)) == -1) + if ((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) == -1) { std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl; return false; @@ -110,10 +141,15 @@ fractal = new Mandelbrot(); fractal->init(surf_fractal); - can_set_julia_K = true; + can_set_julia_k = true; + redraw = true; set_caption(); +#ifdef USE_SDL_WAITEVENT + SDL_TimerID timer_id = SDL_AddTimer(10, App::timer_callback, NULL); +#endif + return true; } @@ -127,22 +163,26 @@ void App::render() { - // Start with a clean slate. - SDL_FillRect(surf_display, &surf_display->clip_rect, - SDL_MapRGB(surf_display->format, 0, 0, 255)); + if (redraw) + { + // Start with a clean slate. + SDL_FillRect(surf_display, &surf_display->clip_rect, + SDL_MapRGB(surf_display->format, 0, 0, 255)); - fractal->calc_set(); - SDL_BlitSurface(surf_fractal, NULL, surf_display, NULL); + fractal->calc_set(); + SDL_BlitSurface(surf_fractal, NULL, surf_display, NULL); - // draw a selection box - if (setting_zoom) - { - SDL_BlitSurface(surf_selection, NULL, surf_display, NULL); + // draw a selection box + if (setting_zoom) + { + SDL_BlitSurface(surf_selection, NULL, surf_display, NULL); + } + + // Show it to the user. + SDL_Flip(surf_display); + + redraw = false; } - - // Show it to the user. - SDL_Flip(surf_display); - } //////////////////////////////////////////////////////////////////////////////// @@ -186,6 +226,8 @@ case SDLK_b: restore_view(); + fractal->set_calc_needed(); + redraw = true; set_caption(); break; @@ -194,11 +236,11 @@ break; case SDLK_UP: - fractal->max_iter++; + fractal->inc_max_iter(); set_caption(); break; case SDLK_DOWN: - fractal->max_iter--; + fractal->dec_max_iter(); set_caption(); break; @@ -209,8 +251,10 @@ delete fractal; fractal = new Mandelbrot(); fractal->init(surf_fractal); + fractal->set_calc_needed(); + redraw = true; set_caption(); - can_set_julia_K = true; + can_set_julia_k = true; } break; case SDLK_j: @@ -219,17 +263,18 @@ delete fractal; fractal = new Julia(); fractal->init(surf_fractal); + fractal->set_calc_needed(); + redraw = true; set_caption(); - can_set_julia_K = false; + can_set_julia_k = false; } break; case SDLK_k: - std::cerr << "can_set_julia_K " << can_set_julia_K << std::endl; - if (can_set_julia_K) + if (can_set_julia_k) { - if (setting_julia_K)setting_julia_K = false; - else setting_julia_K = true; + if (setting_julia_k) setting_julia_k = false; + else setting_julia_k = true; } break; @@ -240,6 +285,23 @@ //////////////////////////////////////////////////////////////////////////////// +void App::on_KeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) + { + switch (sym) + { + case SDLK_UP: + fractal->set_calc_needed(); + redraw = true; + break; + case SDLK_DOWN: + fractal->set_calc_needed(); + redraw = true; + break; + } + } + + +//////////////////////////////////////////////////////////////////////////////// void App::on_Exit() { @@ -251,18 +313,22 @@ void App::on_LButtonDown(int mx, int my) { - if (setting_julia_K) + if (setting_julia_k) { - double K_Re = fractal->Re_min + mx * (fractal->size)/(Options::width-1); - double K_Im = fractal->Im_max - my * (fractal->size)/(Options::height-1); + double K_Re = fractal->get_re_min() + mx * + (fractal->get_size())/(Options::width-1); + double K_Im = fractal->get_im_max() - my * + (fractal->get_size())/(Options::height-1); delete fractal; fractal = new Julia(); fractal->init(surf_fractal); - ((Julia *)fractal)->set_K(K_Re, K_Im); + ((Julia *)fractal)->set_k(K_Re, K_Im); + fractal->set_calc_needed(); + redraw = true; set_caption(); - setting_julia_K = false; + setting_julia_k = false; } else { @@ -292,24 +358,25 @@ selection_width = mouse_move_width(mx, my); // calculate new min/max re/im - double Re_scale = (fractal->size)/(Options::width-1); - double Im_scale = (fractal->size)/(Options::height-1); + double Re_scale = (fractal->get_size())/(Options::width-1); + double Im_scale = (fractal->get_size())/(Options::height-1); - double new_Re_min = fractal->Re_min + (selection_x) * Re_scale; - double new_Im_max = fractal->Im_max - (selection_y) * Im_scale; + double new_re_min = fractal->get_re_min() + (selection_x) * Re_scale; + double new_im_max = fractal->get_im_max() - (selection_y) * Im_scale; double new_size = selection_width * Re_scale; view * v = new view; - v->Re_min = fractal->Re_min; - v->Im_max = fractal->Im_max; - v->size = fractal->size; + v->re_min = fractal->get_re_min(); + v->im_max = fractal->get_im_max(); + v->size = fractal->get_size(); old_views.push_back(v); - fractal->Re_min = new_Re_min; - fractal->Im_max = new_Im_max; - fractal->size = new_size; - // std::cerr << "Set new bounds at (" << fractal->Re_min << "," << fractal->Im_max << ") size " << fractal->size << std::endl; + fractal->set_re_min(new_re_min); + fractal->set_im_max(new_im_max); + fractal->set_size(new_size); + fractal->set_calc_needed(); + redraw = true; set_caption(); @@ -345,11 +412,27 @@ rect.w = rect.h = w - 2; SDL_FillRect(surf_selection, &rect, clear); + + redraw = true; } } //////////////////////////////////////////////////////////////////////////////// +void App::on_Resize(int w, int h) + { + redraw = true; + } + + +//////////////////////////////////////////////////////////////////////////////// +void App::on_Expose() + { + redraw = true; + } + + +//////////////////////////////////////////////////////////////////////////////// int App::mouse_move_width(int mx, int my) { int move_width = (mx - selection_x); @@ -407,9 +490,9 @@ { view * v = old_views.back(); old_views.pop_back(); - fractal->Re_min = v->Re_min; - fractal->Im_max = v->Im_max; - fractal->size = v->size; + fractal->set_re_min(v->re_min); + fractal->set_im_max(v->im_max); + fractal->set_size(v->size); delete v; } } @@ -418,16 +501,63 @@ //////////////////////////////////////////////////////////////////////////////// void App::set_caption() { - char title[100]; - sprintf(title, "%s - " - "Real: %f to %f - " - "Imaginary %f to %f - " - "Max iterations: %d", - fractal->name(), - fractal->Re_min, fractal->Re_min + fractal->size, - fractal->Im_max, fractal->Im_max + fractal->size, - fractal->max_iter); - SDL_WM_SetCaption(title, NULL); + std::stringstream ss; + ss << fractal->get_name() << " / " + << "Real: " << fractal->get_re_min() + << " to "<< fractal->get_re_min() + fractal->get_size() + << " / Imaginary: " << fractal->get_im_max() - fractal->get_size() + << " to " << fractal->get_im_max() + << " / Max iterations: " << fractal->get_max_iter(); + SDL_WM_SetCaption(ss.str().c_str(), NULL); + } + + +//////////////////////////////////////////////////////////////////////////////// +// This file name generation is kuldgy. Need to learn how to do file i/o for +// real! + +int scandir_filter(const struct dirent * d) + { + if (memcmp(d->d_name, "mandelbrot-", 11) != 0) return 0; + if (memcmp(d->d_name+14, ".bmp", 4) != 0) return 0; + if (isdigit(d->d_name[11]) + && isdigit(d->d_name[12]) + && isdigit(d->d_name[13])) + return 1; + return 0; + } + + +bool App::save_image() + { + static int i = 0; + char name[20]; + + if (i == 0) + { + struct dirent ** file_list; + int num_files = scandir(".", &file_list, scandir_filter, alphasort); + if (num_files != 0) + sscanf(file_list[num_files-1]->d_name, "mandelbrot-%03d.bmp", &i); + } + i++; + sprintf(name, "mandelbrot-%03d.bmp", i); + + FILE * file; + if ((file = fopen(name, "wb")) == NULL) + { + std::cerr << "fopen() failed in save_image() on file " + << name << " for saving." << std::endl; + return false; + } + + if (SDL_SaveBMP(surf_display, name) != 0) + { + std::cerr << "Unable to save image file " << name << "" << std::endl; + return false; + } + + return true; } @@ -528,56 +658,3 @@ return true; } #endif - -//////////////////////////////////////////////////////////////////////////////// -#ifdef SAVE_AS_BMP - -// This file name generation is kuldgy. Need to learn how to do file i/o for -// real! - -int scandir_filter(const struct dirent * d) - { - if (memcmp(d->d_name, "mandelbrot-", 11) != 0) return 0; - if (memcmp(d->d_name+14, ".bmp", 4) != 0) return 0; - if (isdigit(d->d_name[11]) - && isdigit(d->d_name[12]) - && isdigit(d->d_name[13])) - return 1; - return 0; - } - - -bool App::save_image() - { - static int i = 0; - char name[20]; - - if (i == 0) - { - struct dirent ** file_list; - int num_files = scandir(".", &file_list, scandir_filter, alphasort); - if (num_files != 0) - sscanf(file_list[num_files-1]->d_name, "mandelbrot-%03d.bmp", &i); - } - i++; - sprintf(name, "mandelbrot-%03d.bmp", i); - - FILE * file; - if ((file = fopen(name, "wb")) == NULL) - { - std::cerr << "fopen() failed in save_image() on file " - << name << " for saving." << std::endl; - return false; - } - - if (SDL_SaveBMP(surf_display, name) != 0) - { - std::cerr << "Unable to save image file " << name << "" << std::endl; - return false; - } - - return true; - } -#endif - - diff -r 455406f5f021 -r 0684158d38a8 src/EventHandler.cpp --- a/src/EventHandler.cpp Tue Oct 19 03:37:31 2010 -0500 +++ b/src/EventHandler.cpp Fri Oct 22 02:21:52 2010 -0500 @@ -8,6 +8,7 @@ // EventHandler class implementation. // //////////////////////////////////////////////////////////////////////////////// +#include #include "EventHandler.h" @@ -27,6 +28,37 @@ //////////////////////////////////////////////////////////////////////////////// +// Note: SDL 1.3 adds an official SDL_WaitEventTimeout function. +#ifndef USE_SDL_WAIEVENT +int EventHandler::wait_event_timeout(SDL_Event * event, Uint32 timeout) + { + Uint32 end = SDL_GetTicks() + timeout; + int i; + + for (;;) + { + SDL_PumpEvents(); + i = SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS); + switch (i) + { + case -1: + return 0; + case 1: + return 1; + case 0: + if (SDL_GetTicks() >= end) + { + return 0; + } + SDL_Delay(10); + break; + } + } + } +#endif + + +//////////////////////////////////////////////////////////////////////////////// void EventHandler::on_event(SDL_Event * event) { switch(event->type) diff -r 455406f5f021 -r 0684158d38a8 src/Fractal.cpp --- a/src/Fractal.cpp Tue Oct 19 03:37:31 2010 -0500 +++ b/src/Fractal.cpp Fri Oct 22 02:21:52 2010 -0500 @@ -20,19 +20,104 @@ //////////////////////////////////////////////////////////////////////////////// Fractal::Fractal() : - the_name ("No fractal selected"), - Re_min (0), - Im_max (0), + name ("No fractal selected"), + re_min (0), + im_max (0), size (0), max_iter (0), - surface (NULL) + surface (NULL), + calc_needed (false) { } //////////////////////////////////////////////////////////////////////////////// -const char * Fractal::name() +void Fractal::set_re_min(const double r_min) { - return the_name.c_str(); + re_min = r_min; + } + + +//////////////////////////////////////////////////////////////////////////////// +void Fractal::set_im_max(const double i_max) + { + im_max = i_max; + } + + +//////////////////////////////////////////////////////////////////////////////// +void Fractal::set_size(const double s) + { + if (s > 0.0) + size = s; + else + size = 1.0; + } + + +//////////////////////////////////////////////////////////////////////////////// +void Fractal::set_max_iter(const unsigned int iter) + { + if (iter > 0) + max_iter = iter; + else + max_iter = 1; + } + + +//////////////////////////////////////////////////////////////////////////////// +void Fractal::inc_max_iter() + { + max_iter++; + } + + +//////////////////////////////////////////////////////////////////////////////// +void Fractal::dec_max_iter() + { + if (max_iter > 0) + max_iter--; + } + + +//////////////////////////////////////////////////////////////////////////////// +void Fractal::set_calc_needed() + { + calc_needed = true; + } + + +//////////////////////////////////////////////////////////////////////////////// +double Fractal::get_re_min() + { + return re_min; + } + + +//////////////////////////////////////////////////////////////////////////////// +double Fractal::get_im_max() + { + return im_max; + } + + +//////////////////////////////////////////////////////////////////////////////// +double Fractal::get_size() + { + return size; + } + + +//////////////////////////////////////////////////////////////////////////////// +unsigned int Fractal::get_max_iter() + { + return max_iter; + } + + +//////////////////////////////////////////////////////////////////////////////// +const char * Fractal::get_name() + { + return name.c_str(); } //////////////////////////////////////////////////////////////////////////////// diff -r 455406f5f021 -r 0684158d38a8 src/Julia.cpp --- a/src/Julia.cpp Tue Oct 19 03:37:31 2010 -0500 +++ b/src/Julia.cpp Fri Oct 22 02:21:52 2010 -0500 @@ -12,6 +12,7 @@ #include #include #include +#include #include "Julia.h" #include "Options.h" @@ -19,14 +20,15 @@ //////////////////////////////////////////////////////////////////////////////// Julia::Julia() : - K_Re (-0.565072), - K_Im ( 0.491657) + k_re (-0.565072), + k_im ( 0.491657) { - Re_min = -2.0; - Im_max = 2.0; + re_min = -2.0; + im_max = 2.0; size = 4.0; max_iter = 50; set_name(); + calc_needed = true; } @@ -38,59 +40,64 @@ // Code from http://warp.povusers.org/Julia/ - double Re_scale = (size)/(Options::width-1); - double Im_scale = (size)/(Options::height-1); + if (calc_needed) + { + double re_scale = (size)/(Options::width-1); + double im_scale = (size)/(Options::height-1); - int r, g, b; + int r, g, b; - Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0); - Uint32 color; - int halfway = max_iter/2 - 1; - float step = (float) 255 / halfway; + Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0); + Uint32 color; + int halfway = max_iter/2 - 1; + float step = (float) 255 / halfway; - unsigned x, y, n; + unsigned x, y, n; - for(y = 0; y < Options::height; ++y) - { - double c_im = Im_max - y * Im_scale; - for(x = 0; x < Options::width; ++x) + for(y = 0; y < Options::height; ++y) { - double c_re = Re_min + x * Re_scale; + double c_im = im_max - y * im_scale; + for(x = 0; x < Options::width; ++x) + { + double c_re = re_min + x * re_scale; - double Z_re = c_re, Z_im = c_im; - bool in_set = true; - for(n = 0; n < max_iter; ++n) - { - double Z_re2 = Z_re * Z_re, Z_im2 = Z_im * Z_im; - if(Z_re2 + Z_im2 > 4) + double z_re = c_re, z_im = c_im; + bool in_set = true; + for(n = 0; n < max_iter; ++n) { - in_set = false; - break; + double z_re2 = z_re * z_re, z_im2 = z_im * z_im; + if(z_re2 + z_im2 > 4) + { + in_set = false; + break; + } + z_im = 2*z_re * z_im + k_im; + z_re = z_re2 - z_im2 + k_re; } - Z_im = 2*Z_re * Z_im + K_Im; - Z_re = Z_re2 - Z_im2 + K_Re; - } - if(in_set) - { - draw_pixel(x, y, &black); - } - else - { - if (n <= halfway) + if(in_set) + { + draw_pixel(x, y, &black); + } + else { - r = n * step; - b = g = 0; + if (n <= halfway) + { + r = n * step; + b = g = 0; + } + else + { + r = 255; + b = g = (n - halfway) * step; + } + color = SDL_MapRGB(surface->format, r, g, b); + draw_pixel(x, y, &color); } - else - { - r = 255; - b = g = (n - halfway) * step; - } - color = SDL_MapRGB(surface->format, r, g, b); - draw_pixel(x, y, &color); } } } + + calc_needed = false; return true; } @@ -106,23 +113,24 @@ { surface = surf; } + calc_needed = true; return true; } //////////////////////////////////////////////////////////////////////////////// -void Julia::get_K(double & Re, double & Im) +void Julia::get_k(double & re, double & im) { - Re = K_Re; - Im = K_Im; + re = k_re; + im = k_im; } //////////////////////////////////////////////////////////////////////////////// -void Julia::set_K(double Re, double Im) +void Julia::set_k(double re, double im) { - K_Re = Re; - K_Im = Im; + k_re = re; + k_im = im; set_name(); } @@ -138,7 +146,7 @@ //////////////////////////////////////////////////////////////////////////////// void Julia::set_name() { - char tmp[100]; - sprintf(tmp, "Julia set K = (%f,%f)", K_Re, K_Im); - the_name.assign(tmp); + std::stringstream ss; + ss <<"Julia set K = (" << k_re << "," << k_im << ")"; + name = ss.str(); } diff -r 455406f5f021 -r 0684158d38a8 src/Mandelbrot.cpp --- a/src/Mandelbrot.cpp Tue Oct 19 03:37:31 2010 -0500 +++ b/src/Mandelbrot.cpp Fri Oct 22 02:21:52 2010 -0500 @@ -19,9 +19,9 @@ Mandelbrot::Mandelbrot() { - the_name.assign("Mandelbrot set"); - Re_min = -2.5; - Im_max = 2.5; + name.assign("Mandelbrot set"); + re_min = -2.5; + im_max = 2.5; size = 5.0; max_iter = 50; } @@ -38,6 +38,7 @@ { surface = surf; } + calc_needed = true; return true; } @@ -50,59 +51,64 @@ // Code from http://warp.povusers.org/Mandelbrot/ - double Re_scale = (size)/(Options::width-1); - double Im_scale = (size)/(Options::height-1); + if (calc_needed) + { + double re_scale = (size)/(Options::width-1); + double im_scale = (size)/(Options::height-1); - int r, g, b; + int r, g, b; - Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0); - Uint32 color; - int halfway = max_iter/2 - 1; - float step = (float) 255 / halfway; + Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0); + Uint32 color; + int halfway = max_iter/2 - 1; + float step = (float) 255 / halfway; - unsigned x, y, n; + unsigned x, y, n; - for(y = 0; y < Options::height; ++y) - { - double c_im = Im_max - y * Im_scale; - for(x = 0; x < Options::width; ++x) + for(y = 0; y < Options::height; ++y) { - double c_re = Re_min + x * Re_scale; + double c_im = im_max - y * im_scale; + for(x = 0; x < Options::width; ++x) + { + double c_re = re_min + x * re_scale; - double Z_re = c_re, Z_im = c_im; - bool in_set = true; - for(n=0; n < max_iter; ++n) - { - double Z_re2 = Z_re * Z_re, Z_im2 = Z_im * Z_im; - if(Z_re2 + Z_im2 > 4) + double z_re = c_re, z_im = c_im; + bool in_set = true; + for(n=0; n < max_iter; ++n) { - in_set = false; - break; + double z_re2 = z_re * z_re, z_im2 = z_im * z_im; + if(z_re2 + z_im2 > 4) + { + in_set = false; + break; + } + z_im = 2 * z_re * z_im + c_im; + z_re = z_re2 - z_im2 + c_re; } - Z_im = 2 * Z_re * Z_im + c_im; - Z_re = Z_re2 - Z_im2 + c_re; - } - if(in_set) - { - draw_pixel(x, y, &black); - } - else - { - if (n <= halfway) + if(in_set) + { + draw_pixel(x, y, &black); + } + else { - b = n * step; - r = g = 0; + if (n <= halfway) + { + b = n * step; + r = g = 0; + } + else + { + b = 255; + r = g = (n - halfway) * step; + } + color = SDL_MapRGB(surface->format, r, g, b); + draw_pixel(x, y, &color); } - else - { - b = 255; - r = g = (n - halfway) * step; - } - color = SDL_MapRGB(surface->format, r, g, b); - draw_pixel(x, y, &color); } } } + + calc_needed = false; return true; }