# HG changeset patch # User Eris Caffee # Date 1288076627 18000 # Node ID df02a7de7fe2e9e423b7ccdfb525d0c3eddfe26d # Parent 0f4ad525f49accbd9a1bbe5af21998801ac93f1d I think I've got the Abstract Factory code working pretty robustly now, though I still need to make the factory singletons thread-safe diff -r 0f4ad525f49a -r df02a7de7fe2 CMakeLists.txt --- a/CMakeLists.txt Sat Oct 23 04:24:48 2010 -0500 +++ b/CMakeLists.txt Tue Oct 26 02:03:47 2010 -0500 @@ -159,11 +159,12 @@ # CMake, then uncomment the if block below and put in the actual # locations of your SDL installation. -# if (WIN32) -# set (ENV{SDLDIR} "c:\gamedev\deps\sdl\SDL-build") +if (WIN32) + set (ENV{SDLDIR} "c:/gamedev/deps/sdl/SDL-1.2.14") + set (ENV{SDLIMAGEDIR} "c:/gamedev/deps/sdl/SDL_image-1.2.10") # else () # set (ENV{SDLDIR} "/home/eris/gamedev/deps/sdl/SDL-build") -# endif () +endif () option(Option_SDL_Dev "Build an SDL application." OFF) diff -r 0f4ad525f49a -r df02a7de7fe2 TODO --- a/TODO Sat Oct 23 04:24:48 2010 -0500 +++ b/TODO Tue Oct 26 02:03:47 2010 -0500 @@ -1,29 +1,39 @@ +Important + - Make View into a class of it's own. Give it the ability to store a Fractals special options. +- Windows file handling +- Better file handling in general + +- 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. + + +--------------------------------------------------------------------------------- +Somewhat important + +- Threading + + +--------------------------------------------------------------------------------- +Least important + +- Performance improvment ideas: + - Create a partial rendering mechanism. + +- real GUI interface + - gtk? +- More color options +- More factal types + - Find out how to save jpg or some other image format that allows comments I can use to store calculation data. - - 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 - -- Performance improvment ideas: - - Create a partial rendering mechanism. - -- 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 0f4ad525f49a -r df02a7de7fe2 include/App.h --- a/include/App.h Sat Oct 23 04:24:48 2010 -0500 +++ b/include/App.h Tue Oct 26 02:03:47 2010 -0500 @@ -17,8 +17,7 @@ #include "SDL.h" #include "EventHandler.h" -#include "Mandelbrot.h" - +#include "Fractal.h" class App : public EventHandler { @@ -68,6 +67,11 @@ static Uint32 timer_callback(Uint32 interval, void *param); + int get_next_file_num(); + +#if !defined(WIN32) + static int scandir_filter(const struct dirent * d); +#endif bool running; bool redraw; diff -r 0f4ad525f49a -r df02a7de7fe2 include/Fractal.h --- a/include/Fractal.h Sat Oct 23 04:24:48 2010 -0500 +++ b/include/Fractal.h Tue Oct 26 02:03:47 2010 -0500 @@ -20,7 +20,6 @@ // 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 { @@ -30,26 +29,27 @@ virtual bool calc_set() = 0; virtual bool init(SDL_Surface * surf) = 0; - double get_re_min(); + double get_re_min() const; double set_re_min(const double r_min); - double get_im_max(); + double get_im_max() const; double set_im_max(const double i_max); - double get_size(); + double get_size() const; double set_size(const double s); - unsigned int get_max_iter(); + unsigned int get_max_iter() const; unsigned int set_max_iter(const unsigned int iter); unsigned int inc_max_iter(); unsigned int dec_max_iter(); void set_calc_needed(); - virtual const char * get_display_name(); - virtual const char * get_type_name(); + virtual std::string const & get_display_name() const; + virtual std::string const & get_fractal_name() const; - virtual bool get_option(std::string option, void * value) {}; + + virtual bool get_option(std::string option, void * value) const {}; virtual bool set_option(std::string option, void * value) {}; @@ -58,8 +58,12 @@ virtual void draw_pixel(int x, int y, Uint32 * color); + // fractal_name is the basic name of ths fractal, such as "Mandelbrot set" + // or "Julia set" + // display_name is probably the same, but might have a bit of extra info, + // such as "Julia set K=(1,2) std::string display_name; - std::string type_name; + std::string fractal_name; double re_min; double im_max; diff -r 0f4ad525f49a -r df02a7de7fe2 include/Julia.h --- a/include/Julia.h Sat Oct 23 04:24:48 2010 -0500 +++ b/include/Julia.h Tue Oct 26 02:03:47 2010 -0500 @@ -12,14 +12,14 @@ #ifndef JULIA_H #define JULIA_H +#include #include +#include #include "SDL.h" #include "Fractal.h" -#define JULIA_TYPE_NAME "Julia" - class Julia : public Fractal { public: @@ -28,10 +28,10 @@ bool calc_set(); bool init(SDL_Surface * surf); - bool get_option(std::string option, void * value); + bool get_option(std::string option, void * value) const; bool set_option(std::string option, void * value); - const char * get_type_name(); + std::string const & get_fractal_name() const; private: @@ -40,6 +40,7 @@ double k_re; double k_im; + }; namespace @@ -49,12 +50,14 @@ public: JuliaCreator() { - get_Fractal_map()["Julia"]=this; + std::cerr << "Julia is called >" << typeid(Julia).name() << "<" << std::endl; + get_Fractal_map()[typeid(Julia).name()]=this; } + virtual Fractal * create() const { return new Julia; } - } theCreator; + } the_Julia_creator; } #endif diff -r 0f4ad525f49a -r df02a7de7fe2 include/Mandelbrot.h --- a/include/Mandelbrot.h Sat Oct 23 04:24:48 2010 -0500 +++ b/include/Mandelbrot.h Tue Oct 26 02:03:47 2010 -0500 @@ -13,13 +13,12 @@ #define MANDELBROT_H #include +#include #include "SDL.h" #include "Fractal.h" -#define MANDELBROT_TYPE_NAME "Mandelbrot" - class Mandelbrot : public Fractal { public: @@ -28,7 +27,7 @@ bool calc_set(); bool init(SDL_Surface * surf); - const char * get_type_name(); + std::string const & get_fractal_name() const; private: @@ -42,14 +41,14 @@ public: MandelbrotCreator() { - get_Fractal_map()["Mandelbrot"]=this; + get_Fractal_map()[typeid(Mandelbrot).name()]=this; } virtual Fractal * create() const { return new Mandelbrot; } - } theCreator; + } the_Mandelbrot_creator; } #endif diff -r 0f4ad525f49a -r df02a7de7fe2 src/App.cpp --- a/src/App.cpp Sat Oct 23 04:24:48 2010 -0500 +++ b/src/App.cpp Tue Oct 26 02:03:47 2010 -0500 @@ -15,12 +15,28 @@ #include #include +#if defined(WIN32) +#include +#include +#include + +#else +#include +#include +#include + #include +#endif + #include "SDL.h" #include "App.h" + #include "Options.h" +#include "Mandelbrot.h" +#include "Julia.h" + std::vector App::old_views; @@ -112,7 +128,8 @@ for ( it=mymap.begin() ; it != mymap.end(); it++ ) std::cerr << " " << (*it).first << std::endl; - FractalCreator * fractal_creator = get_Fractal_map()["Mandelbrot"]; + FractalCreator * fractal_creator = + get_Fractal_map()[typeid(Mandelbrot).name()]; if (!fractal_creator) { std::cerr << "Mandelbrot set not available!" << std::endl; @@ -233,7 +250,8 @@ { save_view(); delete fractal; - FractalCreator * fractal_creator = get_Fractal_map()["Mandelbrot"]; + FractalCreator * fractal_creator = + get_Fractal_map()[typeid(Mandelbrot).name()]; if (!fractal_creator) { std::cerr << "Mandelbrot set not available!" << std::endl; @@ -253,7 +271,8 @@ case SDLK_j: if (fractal) { - FractalCreator * fractal_creator = get_Fractal_map()["Julia"]; + FractalCreator * fractal_creator = + get_Fractal_map()[typeid(Julia).name()]; if (!fractal_creator) { std::cerr << "Julia set not available!" << std::endl; @@ -489,7 +508,7 @@ k[1] = fractal->get_im_max() - y * (fractal->get_size())/(surf_fractal->h - 1); - FractalCreator * fractal_creator = get_Fractal_map()["Julia"]; + FractalCreator * fractal_creator = get_Fractal_map()[typeid(Julia).name()]; if (!fractal_creator) { std::cerr << "Julia set not available!" << std::endl; @@ -519,7 +538,7 @@ view * v = old_views.back(); old_views.pop_back(); - if (v->type.compare(fractal->get_type_name()) != 0) + if (v->type.compare(fractal->get_fractal_name()) != 0) { delete fractal; FractalCreator * fractal_creator = get_Fractal_map()[v->type]; @@ -530,6 +549,7 @@ SDL_Event event; event.type = SDL_QUIT; SDL_PushEvent(&event); + return; } fractal = fractal_creator->create(); fractal->init(surf_fractal); @@ -557,7 +577,7 @@ void App::save_view() { view * v = new view; - v->type = fractal->get_type_name(); + v->type = typeid(*fractal).name(); v->re_min = fractal->get_re_min(); v->im_max = fractal->get_im_max(); v->size = fractal->get_size(); @@ -589,34 +609,9 @@ //////////////////////////////////////////////////////////////////////////////// -// 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; - - 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++; - + int i = App::get_next_file_num(); std::stringstream name; name << "mandelbrot-" << std::setw(3) << std::setfill('0') << i << ".bmp"; @@ -647,3 +642,84 @@ } +#if defined(WIN32) +//////////////////////////////////////////////////////////////////////////////// +//Code for Windows +int App::get_next_file_num() + { + static int i = 0; + + if (i == 0) + { + WIN32_FIND_DATA ffd; + HANDLE hFind = INVALID_HANDLE_VALUE; + + char * file_spec = "mandelbrot-???.bmp"; + hFind = FindFirstFile(file_spec, &ffd); + if (hFind != INVALID_HANDLE_VALUE) + { + std::string s; + std::stringstream ss; + do + { + std::cerr << ffd.cFileName << std::endl; + ss << ffd.cFileName; + if (ss.str().size() == 18) + { + s = ss.str().substr(0, 11); + if ( StrCmpI(s.c_str(), "mandelbrot-") == 0 ) + { + s = ss.str().substr(14, 4); + if (StrCmpI(s.c_str(), ".bmp") == 0) + { + s = ss.str().substr(11, 3); + int j = atoi(s.c_str()); + if (j > i) i = j; + } + } + } + } + while (FindNextFile(hFind, &ffd) != 0); + } + } + + i++; + return i; + } + + + +#else +//////////////////////////////////////////////////////////////////////////////// +// Code for Linux + +int App::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; + } + + +int App::get_next_file_num() + { + static int i = 0; + + 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++; + + return i; + } + +#endif + diff -r 0f4ad525f49a -r df02a7de7fe2 src/Fractal.cpp --- a/src/Fractal.cpp Sat Oct 23 04:24:48 2010 -0500 +++ b/src/Fractal.cpp Tue Oct 26 02:03:47 2010 -0500 @@ -20,8 +20,8 @@ //////////////////////////////////////////////////////////////////////////////// Fractal::Fractal() : - display_name ("No fractal selected"), - type_name (FRACTAL_TYPE_NAME), + display_name ("No fractal"), + fractal_name ("No fractal"), re_min (0), im_max (0), size (0), @@ -32,7 +32,7 @@ } //////////////////////////////////////////////////////////////////////////////// -double Fractal::get_re_min() +double Fractal::get_re_min() const { return re_min; } @@ -48,7 +48,7 @@ //////////////////////////////////////////////////////////////////////////////// -double Fractal::get_im_max() +double Fractal::get_im_max() const { return im_max; } @@ -64,7 +64,7 @@ //////////////////////////////////////////////////////////////////////////////// -double Fractal::get_size() +double Fractal::get_size() const { return size; } @@ -83,7 +83,7 @@ //////////////////////////////////////////////////////////////////////////////// -unsigned int Fractal::get_max_iter() +unsigned int Fractal::get_max_iter() const { return max_iter; } @@ -128,16 +128,16 @@ //////////////////////////////////////////////////////////////////////////////// -const char * Fractal::get_display_name() +std::string const & Fractal::get_display_name() const { - return display_name.c_str(); + return display_name; } //////////////////////////////////////////////////////////////////////////////// -const char * Fractal::get_type_name() +std::string const & Fractal::get_fractal_name() const { - return FRACTAL_TYPE_NAME; + return fractal_name; } diff -r 0f4ad525f49a -r df02a7de7fe2 src/Julia.cpp --- a/src/Julia.cpp Sat Oct 23 04:24:48 2010 -0500 +++ b/src/Julia.cpp Tue Oct 26 02:03:47 2010 -0500 @@ -21,13 +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; calc_needed = true; set_display_name(); + fractal_name = "Julia set"; } @@ -118,7 +118,7 @@ //////////////////////////////////////////////////////////////////////////////// -bool Julia::get_option(std::string option, void * value) +bool Julia::get_option(std::string option, void * value) const { if (option.compare("k") == 0) { @@ -154,9 +154,9 @@ //////////////////////////////////////////////////////////////////////////////// -const char * Julia::get_type_name() +std::string const & Julia::get_fractal_name() const { - return JULIA_TYPE_NAME; + return fractal_name; } diff -r 0f4ad525f49a -r df02a7de7fe2 src/Mandelbrot.cpp --- a/src/Mandelbrot.cpp Sat Oct 23 04:24:48 2010 -0500 +++ b/src/Mandelbrot.cpp Tue Oct 26 02:03:47 2010 -0500 @@ -18,13 +18,13 @@ Mandelbrot::Mandelbrot() { - 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; + display_name = "Mandelbrot set"; + fractal_name = "Mandelbrot set"; } @@ -115,9 +115,9 @@ //////////////////////////////////////////////////////////////////////////////// -const char * Mandelbrot::get_type_name() +std::string const & Mandelbrot::get_fractal_name() const { - return MANDELBROT_TYPE_NAME; + return fractal_name; }