# HG changeset patch # User Eris Caffee # Date 1287825888 18000 # Node ID 0f4ad525f49accbd9a1bbe5af21998801ac93f1d # Parent 0684158d38a8f6c940dc0e8c0a85afa5994acf45 Lot's of cleanup. Applied AbstractFactory to class Fractal and derivatives. Decoupled most objects. Removed remaining standard C function calls. Improved old_views handling. diff -r 0684158d38a8 -r 0f4ad525f49a TODO --- a/TODO Fri Oct 22 02:21:52 2010 -0500 +++ b/TODO Sat Oct 23 04:24:48 2010 -0500 @@ -1,6 +1,5 @@ -- Make old_views store the fractal type (or clear the old_views list when changing type if lazy). - -- Make Fractal derived classes have unique type ID's that can can be used to identify the fractal type. Need this for the good old_views solution. +- Make View into a class of it's own. + Give it the ability to store a Fractals special options. - Find out how to save jpg or some other image format that allows comments I can use to store calculation data. @@ -20,13 +19,11 @@ - 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 + +- Is there a way to handle the FRACTAL_TYPE_NAME without using a preprocessor define? + +- Replace Options with a real class. Use Ini_File to save and load options. + Allow max_iter to be a configurable setting to override the Fractal + class default. \ No newline at end of file diff -r 0684158d38a8 -r 0f4ad525f49a include/App.h --- a/include/App.h Fri Oct 22 02:21:52 2010 -0500 +++ b/include/App.h Sat Oct 23 04:24:48 2010 -0500 @@ -14,7 +14,6 @@ #include -#include "png.h" #include "SDL.h" #include "EventHandler.h" @@ -30,6 +29,8 @@ int run(); + + private: bool init(); @@ -44,6 +45,7 @@ void on_Exit(); void on_LButtonDown(int mx, int my); void on_LButtonUp(int mx, int my); + void on_RButtonDown(int mx, int my); void on_MouseMove(int mx, int my, int relx, int rely, bool left, bool right, bool middle); @@ -52,9 +54,16 @@ int mouse_move_width(int mx, int my); + bool create_new_surface(SDL_Surface * &surface); + + void set_julia_k(int x, int y); + void restore_view(); + void save_view(); + void set_caption(); + bool save_image(); static Uint32 timer_callback(Uint32 interval, void *param); @@ -74,7 +83,9 @@ struct view { + std::string type; double re_min, im_max, size; + unsigned int max_iter; }; static std::vector old_views; diff -r 0684158d38a8 -r 0f4ad525f49a include/Fractal.h --- a/include/Fractal.h Fri Oct 22 02:21:52 2010 -0500 +++ b/include/Fractal.h Sat Oct 23 04:24:48 2010 -0500 @@ -13,8 +13,15 @@ #ifndef FRACTAL_H #define FRACTAL_H +#include #include +// Need to define this via the pre-processor because if it's a staticaly +// initialized const string we still have no guarantee that it will be +// initialized before it is needed by a FractalCreator class. +// Do the same in all derived classes. +#define FRACTAL_TYPE_NAME "Fractal" + class Fractal { public: @@ -22,35 +29,54 @@ virtual bool calc_set() = 0; virtual bool init(SDL_Surface * surf) = 0; - 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(); + + double get_re_min(); + double set_re_min(const double r_min); + + double get_im_max(); + double set_im_max(const double i_max); + + double get_size(); + double set_size(const double s); + + unsigned int get_max_iter(); + unsigned int set_max_iter(const unsigned int iter); + unsigned int inc_max_iter(); + unsigned int dec_max_iter(); + void set_calc_needed(); - double get_re_min(); - double get_im_max(); - double get_size(); - unsigned int get_max_iter(); - const char * get_name(); + virtual const char * get_display_name(); + virtual const char * get_type_name(); + + virtual bool get_option(std::string option, void * value) {}; + virtual bool set_option(std::string option, void * value) {}; + protected: virtual void draw_pixel(int x, int y, Uint32 * color); - - std::string name; + + std::string display_name; + std::string type_name; + double re_min; double im_max; double size; unsigned int max_iter; bool calc_needed; - + SDL_Surface * surface; - - + }; +class FractalCreator + { + public: + virtual Fractal * create() const = 0; + }; + +std::map & get_Fractal_map(); + #endif diff -r 0684158d38a8 -r 0f4ad525f49a include/Julia.h --- a/include/Julia.h Fri Oct 22 02:21:52 2010 -0500 +++ b/include/Julia.h Sat Oct 23 04:24:48 2010 -0500 @@ -18,6 +18,8 @@ #include "Fractal.h" +#define JULIA_TYPE_NAME "Julia" + class Julia : public Fractal { public: @@ -26,16 +28,33 @@ bool calc_set(); bool init(SDL_Surface * surf); - void get_k(double & re, double & im); - void set_k(double re, double im); + bool get_option(std::string option, void * value); + bool set_option(std::string option, void * value); + + const char * get_type_name(); private: void draw_pixel(int x, int y, Uint32 * color); - void set_name(); + void set_display_name(); double k_re; double k_im; }; +namespace + { + class JuliaCreator : public FractalCreator + { + public: + JuliaCreator() + { + get_Fractal_map()["Julia"]=this; + } + virtual Fractal * create() const + { + return new Julia; + } + } theCreator; + } #endif diff -r 0684158d38a8 -r 0f4ad525f49a include/Mandelbrot.h --- a/include/Mandelbrot.h Fri Oct 22 02:21:52 2010 -0500 +++ b/include/Mandelbrot.h Sat Oct 23 04:24:48 2010 -0500 @@ -18,6 +18,8 @@ #include "Fractal.h" +#define MANDELBROT_TYPE_NAME "Mandelbrot" + class Mandelbrot : public Fractal { public: @@ -26,9 +28,28 @@ bool calc_set(); bool init(SDL_Surface * surf); + const char * get_type_name(); + private: void draw_pixel(int x, int y, Uint32 * color); }; +namespace + { + class MandelbrotCreator : public FractalCreator + { + public: + MandelbrotCreator() + { + get_Fractal_map()["Mandelbrot"]=this; + } + + virtual Fractal * create() const + { + return new Mandelbrot; + } + } theCreator; + } + #endif diff -r 0684158d38a8 -r 0f4ad525f49a src/App.cpp --- a/src/App.cpp Fri Oct 22 02:21:52 2010 -0500 +++ b/src/App.cpp Sat Oct 23 04:24:48 2010 -0500 @@ -9,25 +9,19 @@ // //////////////////////////////////////////////////////////////////////////////// -#include -#include +#include #include #include +#include #include #include -#include "png.h" #include "SDL.h" #include "App.h" #include "Options.h" -#include "Mandelbrot.h" -#include "Julia.h" - - -#define USE_SDL_WAITEVENT std::vector App::old_views; @@ -48,17 +42,14 @@ { } + //////////////////////////////////////////////////////////////////////////////// App::~App() { - if (old_views.size() > 0) - { - view * v = old_views.back(); - old_views.pop_back(); - delete v; - } + old_views.clear(); } + //////////////////////////////////////////////////////////////////////////////// int App::run() @@ -70,19 +61,12 @@ while (running) { -#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(); @@ -95,24 +79,6 @@ return 0; } -//////////////////////////////////////////////////////////////////////////////// -// 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() @@ -139,26 +105,41 @@ if (!create_new_surface(surf_selection)) { return false; } if (!create_new_surface(surf_fractal)) { return false; } - fractal = new Mandelbrot(); + + std::cerr << "Available fractal types are" << std::endl; + std::map mymap = get_Fractal_map(); + std::map ::iterator it; + for ( it=mymap.begin() ; it != mymap.end(); it++ ) + std::cerr << " " << (*it).first << std::endl; + + FractalCreator * fractal_creator = get_Fractal_map()["Mandelbrot"]; + if (!fractal_creator) + { + std::cerr << "Mandelbrot set not available!" << std::endl; + return false; + } + fractal = fractal_creator->create(); fractal->init(surf_fractal); + redraw = 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; } + //////////////////////////////////////////////////////////////////////////////// void App::update() { } + //////////////////////////////////////////////////////////////////////////////// void App::render() @@ -185,6 +166,7 @@ } } + //////////////////////////////////////////////////////////////////////////////// void App::cleanup() @@ -204,6 +186,7 @@ SDL_Quit(); } + //////////////////////////////////////////////////////////////////////////////// void App::on_event(SDL_Event * event) @@ -211,6 +194,7 @@ EventHandler::on_event(event); } + //////////////////////////////////////////////////////////////////////////////// void App::on_KeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) @@ -226,7 +210,6 @@ case SDLK_b: restore_view(); - fractal->set_calc_needed(); redraw = true; set_caption(); break; @@ -248,8 +231,18 @@ case SDLK_m: if (fractal) { + save_view(); delete fractal; - fractal = new Mandelbrot(); + FractalCreator * fractal_creator = get_Fractal_map()["Mandelbrot"]; + if (!fractal_creator) + { + std::cerr << "Mandelbrot set not available!" << std::endl; + SDL_Event event; + event.type = SDL_QUIT; + SDL_PushEvent(&event); + break; + } + fractal = fractal_creator->create(); fractal->init(surf_fractal); fractal->set_calc_needed(); redraw = true; @@ -260,13 +253,22 @@ case SDLK_j: if (fractal) { - delete fractal; - fractal = new Julia(); - fractal->init(surf_fractal); - fractal->set_calc_needed(); - redraw = true; - set_caption(); - can_set_julia_k = false; + FractalCreator * fractal_creator = get_Fractal_map()["Julia"]; + if (!fractal_creator) + { + std::cerr << "Julia set not available!" << std::endl; + } + else + { + save_view(); + delete fractal; + fractal = fractal_creator->create(); + fractal->init(surf_fractal); + fractal->set_calc_needed(); + redraw = true; + set_caption(); + can_set_julia_k = false; + } } break; @@ -315,20 +317,7 @@ { if (setting_julia_k) { - 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); - fractal->set_calc_needed(); - redraw = true; - - set_caption(); - setting_julia_k = false; + set_julia_k(mx, my); } else { @@ -346,9 +335,9 @@ if (setting_zoom) { if ( (mx <= 0) || - (mx >= Options::width - 1) || + (mx >= surf_display->w - 1) || (my <= 0) || - (my >= Options::height - 1)) + (my >= surf_display->h - 1)) { // Selection cancelled setting_zoom = false; @@ -358,19 +347,15 @@ selection_width = mouse_move_width(mx, my); // calculate new min/max re/im - double Re_scale = (fractal->get_size())/(Options::width-1); - double Im_scale = (fractal->get_size())/(Options::height-1); + double Re_scale = (fractal->get_size())/(surf_fractal->w - 1); + double Im_scale = (fractal->get_size())/(surf_fractal->h - 1); 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->get_re_min(); - v->im_max = fractal->get_im_max(); - v->size = fractal->get_size(); - old_views.push_back(v); + + save_view(); fractal->set_re_min(new_re_min); fractal->set_im_max(new_im_max); @@ -386,6 +371,17 @@ //////////////////////////////////////////////////////////////////////////////// + +void App::on_RButtonDown(int mx, int my) + { + if (can_set_julia_k) + { + set_julia_k(mx, my); + } + } + + +//////////////////////////////////////////////////////////////////////////////// void App::on_MouseMove(int mx, int my, int relx, int rely, bool left, bool right, bool middle) @@ -465,8 +461,8 @@ SDL_Surface * tmp; if ((tmp = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_SRCALPHA, - Options::width, - Options::height, Options::bpp, 0, 0, 0, 0)) + Options::width, Options::height, + Options::bpp, 0, 0, 0, 0)) == NULL) { std::cerr << "SDL_CreateRGBSurface() failed in " @@ -483,6 +479,38 @@ //////////////////////////////////////////////////////////////////////////////// +void App::set_julia_k(int x, int y) + { + save_view(); + + double k[2]; + k[0]= fractal->get_re_min() + x * + (fractal->get_size())/(surf_fractal->w - 1); + k[1] = fractal->get_im_max() - y * + (fractal->get_size())/(surf_fractal->h - 1); + + FractalCreator * fractal_creator = get_Fractal_map()["Julia"]; + if (!fractal_creator) + { + std::cerr << "Julia set not available!" << std::endl; + return; + } + else + { + delete fractal; + fractal = fractal_creator->create(); + fractal->init(surf_fractal); + fractal->set_option("k", k); + fractal->set_calc_needed(); + redraw = true; + + set_caption(); + setting_julia_k = false; + } + } + + +//////////////////////////////////////////////////////////////////////////////// void App::restore_view() { @@ -490,19 +518,67 @@ { view * v = old_views.back(); old_views.pop_back(); + + if (v->type.compare(fractal->get_type_name()) != 0) + { + delete fractal; + FractalCreator * fractal_creator = get_Fractal_map()[v->type]; + if (!fractal_creator) + { + std::cerr << "Cannot restore old view: unknown Fractal type " + << v->type << std::endl; + SDL_Event event; + event.type = SDL_QUIT; + SDL_PushEvent(&event); + } + fractal = fractal_creator->create(); + fractal->init(surf_fractal); + } + fractal->set_re_min(v->re_min); fractal->set_im_max(v->im_max); fractal->set_size(v->size); + fractal->set_max_iter(v->max_iter); + fractal->set_calc_needed(); + + std::cerr << "Restored view:" << std::endl + << " type " << v->type << std::endl + << " re_min " << v->re_min << std::endl + << " im_max " << v->im_max << std::endl + << " size " << v->size << std::endl + << " max_iter " << v->max_iter << std::endl + << std::endl; delete v; } } //////////////////////////////////////////////////////////////////////////////// +void App::save_view() + { + view * v = new view; + v->type = fractal->get_type_name(); + v->re_min = fractal->get_re_min(); + v->im_max = fractal->get_im_max(); + v->size = fractal->get_size(); + v->max_iter = fractal->get_max_iter(); + old_views.push_back(v); + + std::cerr << "Saved view:" << std::endl + << " type " << v->type << std::endl + << " re_min " << v->re_min << std::endl + << " im_max " << v->im_max << std::endl + << " size " << v->size << std::endl + << " max_iter " << v->max_iter << std::endl + << std::endl; + } + + +//////////////////////////////////////////////////////////////////////////////// void App::set_caption() { std::stringstream ss; - ss << fractal->get_name() << " / " + ss << fractal->get_display_name() << " / " << "Real: " << fractal->get_re_min() << " to "<< fractal->get_re_min() + fractal->get_size() << " / Imaginary: " << fractal->get_im_max() - fractal->get_size() @@ -531,7 +607,6 @@ bool App::save_image() { static int i = 0; - char name[20]; if (i == 0) { @@ -541,120 +616,34 @@ 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::stringstream name; + name << "mandelbrot-" << std::setw(3) << std::setfill('0') << i << ".bmp"; + + if (SDL_SaveBMP(surf_display, name.str().c_str()) != 0) { - std::cerr << "fopen() failed in save_image() on file " - << name << " for saving." << std::endl; + std::cerr << "Unable to save image file " << name.str() << "" << std::endl; return false; } - if (SDL_SaveBMP(surf_display, name) != 0) - { - std::cerr << "Unable to save image file " << name << "" << std::endl; - return false; - } + std::cerr << "Saved image as " << name.str() << std::endl; return true; } //////////////////////////////////////////////////////////////////////////////// -#ifdef SAVE_AS_PNG -void png_user_error(png_structp png, png_const_charp s); -void png_user_warning(png_structp png, png_const_charp s); - -void png_user_warning(png_structp png, png_const_charp s) +Uint32 App::timer_callback(Uint32 interval, void *param) { - std::cerr << "libpng: warning " << (char *) s << std::endl; + SDL_Event e; + + e.type = SDL_USEREVENT; + e.user.code = 0; + e.user.data1 = NULL; + e.user.data2 = NULL; + SDL_PushEvent(& e); + + return interval; } -//////////////////////////////////////////////////////////////////////////////// -void png_user_error(png_structp png, png_const_charp s) - { - std::cerr << "libpng: error " << (char *) s << std::endl; - } - - -//////////////////////////////////////////////////////////////////////////////// - -bool App::save_image() - { - static int i = 0; - char name[20]; - - i++; - sprintf(name, "mandelbrot-%03d.png", i); - - png_save_surface(name, surf_display); - - FILE * file; - if ((file = fopen(name, "wb")) == NULL) - { - std::cerr << "fopen() failed in save_image() on file " - << name << " for saving." << std::endl; - return false; - } - - - - png_structp png; - png_infop png_info; - - if ((png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, - png_user_error, png_user_warning)) == NULL) - { - std::cerr << "png_create_write_struct() failed in save_image()" - << std::endl; - fclose(file); - return false; - } - - - if ((png_info = png_create_info_struct(png)) == NULL) - { - std::cerr << "png_create_info_struct() failed in save_image()" - << std::endl; - fclose(file); - return false; - } - - if (setjmp(png_jmpbuf(png))) - { - std::cerr << "png_jmpbuf() failed in save_image()" << std::endl; - png_destroy_write_struct(&png, &png_info); - fclose(file); - return false; - } - - png_init_io(png, file); - int colortype = PNG_COLOR_MASK_COLOR; - png_set_IHDR(png, png_info, - surf_display->w, surf_display->h, - 8, colortype, PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT); - - png_write_info(png, png_info); - png_set_packing(png); - - png_bytep * rows = (png_bytep *) malloc(sizeof(png_bytep)*surf_display->h); - for (int i = 0; i < surf_display->h; i++) - { - rows[i] = (png_bytep) surf_display->pixels + - i * surf_display->pitch; - } - - png_write_image(png, rows); - png_write_end(png, png_info); - - free(rows); - png_destroy_write_struct(&png, &png_info); - fclose(file); - - return true; - } -#endif diff -r 0684158d38a8 -r 0f4ad525f49a src/Fractal.cpp --- a/src/Fractal.cpp Fri Oct 22 02:21:52 2010 -0500 +++ b/src/Fractal.cpp Sat Oct 23 04:24:48 2010 -0500 @@ -20,7 +20,8 @@ //////////////////////////////////////////////////////////////////////////////// Fractal::Fractal() : - name ("No fractal selected"), + display_name ("No fractal selected"), + type_name (FRACTAL_TYPE_NAME), re_min (0), im_max (0), size (0), @@ -31,51 +32,91 @@ } //////////////////////////////////////////////////////////////////////////////// -void Fractal::set_re_min(const double r_min) +double Fractal::get_re_min() { - re_min = r_min; + return re_min; } //////////////////////////////////////////////////////////////////////////////// -void Fractal::set_im_max(const double i_max) +double Fractal::set_re_min(const double r_min) { - im_max = i_max; + double t = re_min; + re_min = r_min; + return t; } //////////////////////////////////////////////////////////////////////////////// -void Fractal::set_size(const double s) +double Fractal::get_im_max() { + return im_max; + } + + +//////////////////////////////////////////////////////////////////////////////// +double Fractal::set_im_max(const double i_max) + { + double t = im_max; + im_max = i_max; + return t; + } + + +//////////////////////////////////////////////////////////////////////////////// +double Fractal::get_size() + { + return size; + } + + +//////////////////////////////////////////////////////////////////////////////// +double Fractal::set_size(const double s) + { + double t; if (s > 0.0) size = s; else size = 1.0; + return t; } //////////////////////////////////////////////////////////////////////////////// -void Fractal::set_max_iter(const unsigned int iter) +unsigned int Fractal::get_max_iter() { + return max_iter; + } + + +//////////////////////////////////////////////////////////////////////////////// +unsigned int Fractal::set_max_iter(const unsigned int iter) + { + unsigned int t; if (iter > 0) max_iter = iter; else max_iter = 1; + return t; } //////////////////////////////////////////////////////////////////////////////// -void Fractal::inc_max_iter() +unsigned int Fractal::inc_max_iter() { + unsigned int t; max_iter++; + return t; } //////////////////////////////////////////////////////////////////////////////// -void Fractal::dec_max_iter() +unsigned int Fractal::dec_max_iter() { + unsigned int t; if (max_iter > 0) max_iter--; + return t; } @@ -87,40 +128,20 @@ //////////////////////////////////////////////////////////////////////////////// -double Fractal::get_re_min() +const char * Fractal::get_display_name() { - return re_min; + return display_name.c_str(); } //////////////////////////////////////////////////////////////////////////////// -double Fractal::get_im_max() +const char * Fractal::get_type_name() { - return im_max; + return FRACTAL_TYPE_NAME; } //////////////////////////////////////////////////////////////////////////////// -double Fractal::get_size() - { - return size; - } - - -//////////////////////////////////////////////////////////////////////////////// -unsigned int Fractal::get_max_iter() - { - return max_iter; - } - - -//////////////////////////////////////////////////////////////////////////////// -const char * Fractal::get_name() - { - return name.c_str(); - } - -//////////////////////////////////////////////////////////////////////////////// void Fractal::draw_pixel(int x, int y, Uint32 * color) { static char * data; @@ -135,3 +156,10 @@ if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); } +//////////////////////////////////////////////////////////////////////////////// + +std::map & get_Fractal_map() + { + static std::map the_Fractal_map; + return the_Fractal_map; + } diff -r 0684158d38a8 -r 0f4ad525f49a src/Julia.cpp --- a/src/Julia.cpp Fri Oct 22 02:21:52 2010 -0500 +++ b/src/Julia.cpp Sat Oct 23 04:24:48 2010 -0500 @@ -9,13 +9,11 @@ // //////////////////////////////////////////////////////////////////////////////// -#include -#include #include #include #include "Julia.h" -#include "Options.h" + //////////////////////////////////////////////////////////////////////////////// @@ -23,12 +21,13 @@ k_re (-0.565072), k_im ( 0.491657) { + type_name = JULIA_TYPE_NAME; re_min = -2.0; im_max = 2.0; size = 4.0; max_iter = 50; - set_name(); calc_needed = true; + set_display_name(); } @@ -42,8 +41,8 @@ if (calc_needed) { - double re_scale = (size)/(Options::width-1); - double im_scale = (size)/(Options::height-1); + double re_scale = (size)/(surface->w-1); + double im_scale = (size)/(surface->h-1); int r, g, b; @@ -54,10 +53,10 @@ unsigned x, y, n; - for(y = 0; y < Options::height; ++y) + for(y = 0; y < surface->h; ++y) { double c_im = im_max - y * im_scale; - for(x = 0; x < Options::width; ++x) + for(x = 0; x < surface->w; ++x) { double c_re = re_min + x * re_scale; @@ -119,19 +118,45 @@ //////////////////////////////////////////////////////////////////////////////// -void Julia::get_k(double & re, double & im) +bool Julia::get_option(std::string option, void * value) { - re = k_re; - im = k_im; + if (option.compare("k") == 0) + { + ((double *)value)[0] = k_re; + ((double *)value)[0] = k_im; + } + else + { + return false; + } + + return true; } //////////////////////////////////////////////////////////////////////////////// -void Julia::set_k(double re, double im) +bool Julia::set_option(std::string option, void * value) { - k_re = re; - k_im = im; - set_name(); + if (option.compare("k") == 0) + { + k_re = ((double *)value)[0]; + k_im = ((double *)value)[1]; + set_display_name(); + } + else + { + return false; + } + + + return true; + } + + +//////////////////////////////////////////////////////////////////////////////// +const char * Julia::get_type_name() + { + return JULIA_TYPE_NAME; } @@ -144,9 +169,9 @@ //////////////////////////////////////////////////////////////////////////////// -void Julia::set_name() +void Julia::set_display_name() { std::stringstream ss; - ss <<"Julia set K = (" << k_re << "," << k_im << ")"; - name = ss.str(); + ss << "Julia set K = (" << k_re << "," << k_im << ")"; + display_name = ss.str(); } diff -r 0684158d38a8 -r 0f4ad525f49a src/Mandelbrot.cpp --- a/src/Mandelbrot.cpp Fri Oct 22 02:21:52 2010 -0500 +++ b/src/Mandelbrot.cpp Sat Oct 23 04:24:48 2010 -0500 @@ -9,21 +9,22 @@ // //////////////////////////////////////////////////////////////////////////////// -#include #include #include "Mandelbrot.h" -#include "Options.h" + //////////////////////////////////////////////////////////////////////////////// Mandelbrot::Mandelbrot() { - name.assign("Mandelbrot set"); + display_name = "Mandelbrot set"; + type_name = MANDELBROT_TYPE_NAME; re_min = -2.5; im_max = 2.5; size = 5.0; max_iter = 50; + calc_needed = true; } @@ -53,8 +54,8 @@ if (calc_needed) { - double re_scale = (size)/(Options::width-1); - double im_scale = (size)/(Options::height-1); + double re_scale = (size)/(surface->w-1); + double im_scale = (size)/(surface->h-1); int r, g, b; @@ -65,10 +66,10 @@ unsigned x, y, n; - for(y = 0; y < Options::height; ++y) + for(y = 0; y < surface->h; ++y) { double c_im = im_max - y * im_scale; - for(x = 0; x < Options::width; ++x) + for(x = 0; x < surface->w; ++x) { double c_re = re_min + x * re_scale; @@ -114,8 +115,17 @@ //////////////////////////////////////////////////////////////////////////////// +const char * Mandelbrot::get_type_name() + { + return MANDELBROT_TYPE_NAME; + } + + +//////////////////////////////////////////////////////////////////////////////// void Mandelbrot::draw_pixel(int x, int y, Uint32 * color) { Fractal::draw_pixel(x, y, color); } + +