Mercurial > fracter
changeset 4:df02a7de7fe2
I think I've got the Abstract Factory code working pretty robustly now, though I still need to make the factory singletons thread-safe
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Tue, 26 Oct 2010 02:03:47 -0500 |
parents | 0f4ad525f49a |
children | d691ce98f406 |
files | CMakeLists.txt TODO include/App.h include/Fractal.h include/Julia.h include/Mandelbrot.h src/App.cpp src/Fractal.cpp src/Julia.cpp src/Mandelbrot.cpp |
diffstat | 10 files changed, 191 insertions(+), 94 deletions(-) [+] |
line diff
1.1 --- a/CMakeLists.txt Sat Oct 23 04:24:48 2010 -0500 1.2 +++ b/CMakeLists.txt Tue Oct 26 02:03:47 2010 -0500 1.3 @@ -159,11 +159,12 @@ 1.4 # CMake, then uncomment the if block below and put in the actual 1.5 # locations of your SDL installation. 1.6 1.7 -# if (WIN32) 1.8 -# set (ENV{SDLDIR} "c:\gamedev\deps\sdl\SDL-build") 1.9 +if (WIN32) 1.10 + set (ENV{SDLDIR} "c:/gamedev/deps/sdl/SDL-1.2.14") 1.11 + set (ENV{SDLIMAGEDIR} "c:/gamedev/deps/sdl/SDL_image-1.2.10") 1.12 # else () 1.13 # set (ENV{SDLDIR} "/home/eris/gamedev/deps/sdl/SDL-build") 1.14 -# endif () 1.15 +endif () 1.16 1.17 option(Option_SDL_Dev "Build an SDL application." OFF) 1.18
2.1 --- a/TODO Sat Oct 23 04:24:48 2010 -0500 2.2 +++ b/TODO Tue Oct 26 02:03:47 2010 -0500 2.3 @@ -1,29 +1,39 @@ 2.4 +Important 2.5 + 2.6 - Make View into a class of it's own. 2.7 Give it the ability to store a Fractals special options. 2.8 2.9 +- Windows file handling 2.10 +- Better file handling in general 2.11 + 2.12 +- Is there a way to handle the FRACTAL_TYPE_NAME without using a preprocessor define? 2.13 + 2.14 +- Replace Options with a real class. Use Ini_File to save and load options. 2.15 + Allow max_iter to be a configurable setting to override the Fractal 2.16 + class default. 2.17 + 2.18 + 2.19 +--------------------------------------------------------------------------------- 2.20 +Somewhat important 2.21 + 2.22 +- Threading 2.23 + 2.24 + 2.25 +--------------------------------------------------------------------------------- 2.26 +Least important 2.27 + 2.28 +- Performance improvment ideas: 2.29 + - Create a partial rendering mechanism. 2.30 + 2.31 +- real GUI interface 2.32 + - gtk? 2.33 +- More color options 2.34 +- More factal types 2.35 + 2.36 - Find out how to save jpg or some other image format that allows comments I can use to store calculation data. 2.37 - 2.38 - Read saved image and continue calculation. 2.39 2.40 - Zoom feature that zooms in while staying centered on the same spot. 2.41 - Unzoom feature that enlarges the area while keeping the image cenetered on the same spot. 2.42 - Pan feature to let you move side to side while staying at the same zoom level. 2.43 2.44 -- Windows file handling 2.45 -- Better file handling in general 2.46 - 2.47 -- real GUI interface 2.48 - - gtk? 2.49 - 2.50 -- More color options 2.51 - 2.52 -- More factal types 2.53 - 2.54 -- Performance improvment ideas: 2.55 - - Create a partial rendering mechanism. 2.56 - 2.57 -- Is there a way to handle the FRACTAL_TYPE_NAME without using a preprocessor define? 2.58 - 2.59 -- Replace Options with a real class. Use Ini_File to save and load options. 2.60 - Allow max_iter to be a configurable setting to override the Fractal 2.61 - class default. 2.62 \ No newline at end of file
3.1 --- a/include/App.h Sat Oct 23 04:24:48 2010 -0500 3.2 +++ b/include/App.h Tue Oct 26 02:03:47 2010 -0500 3.3 @@ -17,8 +17,7 @@ 3.4 #include "SDL.h" 3.5 3.6 #include "EventHandler.h" 3.7 -#include "Mandelbrot.h" 3.8 - 3.9 +#include "Fractal.h" 3.10 3.11 class App : public EventHandler 3.12 { 3.13 @@ -68,6 +67,11 @@ 3.14 3.15 static Uint32 timer_callback(Uint32 interval, void *param); 3.16 3.17 + int get_next_file_num(); 3.18 + 3.19 +#if !defined(WIN32) 3.20 + static int scandir_filter(const struct dirent * d); 3.21 +#endif 3.22 3.23 bool running; 3.24 bool redraw;
4.1 --- a/include/Fractal.h Sat Oct 23 04:24:48 2010 -0500 4.2 +++ b/include/Fractal.h Tue Oct 26 02:03:47 2010 -0500 4.3 @@ -20,7 +20,6 @@ 4.4 // initialized const string we still have no guarantee that it will be 4.5 // initialized before it is needed by a FractalCreator class. 4.6 // Do the same in all derived classes. 4.7 -#define FRACTAL_TYPE_NAME "Fractal" 4.8 4.9 class Fractal 4.10 { 4.11 @@ -30,26 +29,27 @@ 4.12 virtual bool calc_set() = 0; 4.13 virtual bool init(SDL_Surface * surf) = 0; 4.14 4.15 - double get_re_min(); 4.16 + double get_re_min() const; 4.17 double set_re_min(const double r_min); 4.18 4.19 - double get_im_max(); 4.20 + double get_im_max() const; 4.21 double set_im_max(const double i_max); 4.22 4.23 - double get_size(); 4.24 + double get_size() const; 4.25 double set_size(const double s); 4.26 4.27 - unsigned int get_max_iter(); 4.28 + unsigned int get_max_iter() const; 4.29 unsigned int set_max_iter(const unsigned int iter); 4.30 unsigned int inc_max_iter(); 4.31 unsigned int dec_max_iter(); 4.32 4.33 void set_calc_needed(); 4.34 4.35 - virtual const char * get_display_name(); 4.36 - virtual const char * get_type_name(); 4.37 + virtual std::string const & get_display_name() const; 4.38 + virtual std::string const & get_fractal_name() const; 4.39 4.40 - virtual bool get_option(std::string option, void * value) {}; 4.41 + 4.42 + virtual bool get_option(std::string option, void * value) const {}; 4.43 virtual bool set_option(std::string option, void * value) {}; 4.44 4.45 4.46 @@ -58,8 +58,12 @@ 4.47 4.48 virtual void draw_pixel(int x, int y, Uint32 * color); 4.49 4.50 + // fractal_name is the basic name of ths fractal, such as "Mandelbrot set" 4.51 + // or "Julia set" 4.52 + // display_name is probably the same, but might have a bit of extra info, 4.53 + // such as "Julia set K=(1,2) 4.54 std::string display_name; 4.55 - std::string type_name; 4.56 + std::string fractal_name; 4.57 4.58 double re_min; 4.59 double im_max;
5.1 --- a/include/Julia.h Sat Oct 23 04:24:48 2010 -0500 5.2 +++ b/include/Julia.h Tue Oct 26 02:03:47 2010 -0500 5.3 @@ -12,14 +12,14 @@ 5.4 #ifndef JULIA_H 5.5 #define JULIA_H 5.6 5.7 +#include <iostream> 5.8 #include <string> 5.9 +#include <typeinfo> 5.10 5.11 #include "SDL.h" 5.12 5.13 #include "Fractal.h" 5.14 5.15 -#define JULIA_TYPE_NAME "Julia" 5.16 - 5.17 class Julia : public Fractal 5.18 { 5.19 public: 5.20 @@ -28,10 +28,10 @@ 5.21 bool calc_set(); 5.22 bool init(SDL_Surface * surf); 5.23 5.24 - bool get_option(std::string option, void * value); 5.25 + bool get_option(std::string option, void * value) const; 5.26 bool set_option(std::string option, void * value); 5.27 5.28 - const char * get_type_name(); 5.29 + std::string const & get_fractal_name() const; 5.30 5.31 private: 5.32 5.33 @@ -40,6 +40,7 @@ 5.34 5.35 double k_re; 5.36 double k_im; 5.37 + 5.38 }; 5.39 5.40 namespace 5.41 @@ -49,12 +50,14 @@ 5.42 public: 5.43 JuliaCreator() 5.44 { 5.45 - get_Fractal_map()["Julia"]=this; 5.46 + std::cerr << "Julia is called >" << typeid(Julia).name() << "<" << std::endl; 5.47 + get_Fractal_map()[typeid(Julia).name()]=this; 5.48 } 5.49 + 5.50 virtual Fractal * create() const 5.51 { 5.52 return new Julia; 5.53 } 5.54 - } theCreator; 5.55 + } the_Julia_creator; 5.56 } 5.57 #endif
6.1 --- a/include/Mandelbrot.h Sat Oct 23 04:24:48 2010 -0500 6.2 +++ b/include/Mandelbrot.h Tue Oct 26 02:03:47 2010 -0500 6.3 @@ -13,13 +13,12 @@ 6.4 #define MANDELBROT_H 6.5 6.6 #include <string> 6.7 +#include <typeinfo> 6.8 6.9 #include "SDL.h" 6.10 6.11 #include "Fractal.h" 6.12 6.13 -#define MANDELBROT_TYPE_NAME "Mandelbrot" 6.14 - 6.15 class Mandelbrot : public Fractal 6.16 { 6.17 public: 6.18 @@ -28,7 +27,7 @@ 6.19 bool calc_set(); 6.20 bool init(SDL_Surface * surf); 6.21 6.22 - const char * get_type_name(); 6.23 + std::string const & get_fractal_name() const; 6.24 6.25 private: 6.26 6.27 @@ -42,14 +41,14 @@ 6.28 public: 6.29 MandelbrotCreator() 6.30 { 6.31 - get_Fractal_map()["Mandelbrot"]=this; 6.32 + get_Fractal_map()[typeid(Mandelbrot).name()]=this; 6.33 } 6.34 6.35 virtual Fractal * create() const 6.36 { 6.37 return new Mandelbrot; 6.38 } 6.39 - } theCreator; 6.40 + } the_Mandelbrot_creator; 6.41 } 6.42 6.43 #endif
7.1 --- a/src/App.cpp Sat Oct 23 04:24:48 2010 -0500 7.2 +++ b/src/App.cpp Tue Oct 26 02:03:47 2010 -0500 7.3 @@ -15,12 +15,28 @@ 7.4 #include <typeinfo> 7.5 #include <vector> 7.6 7.7 +#if defined(WIN32) 7.8 +#include <cstdlib> 7.9 +#include <windows.h> 7.10 +#include <Shlwapi.h> 7.11 + 7.12 +#else 7.13 +#include <cctype> 7.14 +#include <cstring> 7.15 +#include <cstdio> 7.16 + 7.17 #include <dirent.h> 7.18 +#endif 7.19 + 7.20 7.21 #include "SDL.h" 7.22 7.23 #include "App.h" 7.24 + 7.25 #include "Options.h" 7.26 +#include "Mandelbrot.h" 7.27 +#include "Julia.h" 7.28 + 7.29 7.30 7.31 std::vector<App::view *> App::old_views; 7.32 @@ -112,7 +128,8 @@ 7.33 for ( it=mymap.begin() ; it != mymap.end(); it++ ) 7.34 std::cerr << " " << (*it).first << std::endl; 7.35 7.36 - FractalCreator * fractal_creator = get_Fractal_map()["Mandelbrot"]; 7.37 + FractalCreator * fractal_creator = 7.38 + get_Fractal_map()[typeid(Mandelbrot).name()]; 7.39 if (!fractal_creator) 7.40 { 7.41 std::cerr << "Mandelbrot set not available!" << std::endl; 7.42 @@ -233,7 +250,8 @@ 7.43 { 7.44 save_view(); 7.45 delete fractal; 7.46 - FractalCreator * fractal_creator = get_Fractal_map()["Mandelbrot"]; 7.47 + FractalCreator * fractal_creator = 7.48 + get_Fractal_map()[typeid(Mandelbrot).name()]; 7.49 if (!fractal_creator) 7.50 { 7.51 std::cerr << "Mandelbrot set not available!" << std::endl; 7.52 @@ -253,7 +271,8 @@ 7.53 case SDLK_j: 7.54 if (fractal) 7.55 { 7.56 - FractalCreator * fractal_creator = get_Fractal_map()["Julia"]; 7.57 + FractalCreator * fractal_creator = 7.58 + get_Fractal_map()[typeid(Julia).name()]; 7.59 if (!fractal_creator) 7.60 { 7.61 std::cerr << "Julia set not available!" << std::endl; 7.62 @@ -489,7 +508,7 @@ 7.63 k[1] = fractal->get_im_max() - y * 7.64 (fractal->get_size())/(surf_fractal->h - 1); 7.65 7.66 - FractalCreator * fractal_creator = get_Fractal_map()["Julia"]; 7.67 + FractalCreator * fractal_creator = get_Fractal_map()[typeid(Julia).name()]; 7.68 if (!fractal_creator) 7.69 { 7.70 std::cerr << "Julia set not available!" << std::endl; 7.71 @@ -519,7 +538,7 @@ 7.72 view * v = old_views.back(); 7.73 old_views.pop_back(); 7.74 7.75 - if (v->type.compare(fractal->get_type_name()) != 0) 7.76 + if (v->type.compare(fractal->get_fractal_name()) != 0) 7.77 { 7.78 delete fractal; 7.79 FractalCreator * fractal_creator = get_Fractal_map()[v->type]; 7.80 @@ -530,6 +549,7 @@ 7.81 SDL_Event event; 7.82 event.type = SDL_QUIT; 7.83 SDL_PushEvent(&event); 7.84 + return; 7.85 } 7.86 fractal = fractal_creator->create(); 7.87 fractal->init(surf_fractal); 7.88 @@ -557,7 +577,7 @@ 7.89 void App::save_view() 7.90 { 7.91 view * v = new view; 7.92 - v->type = fractal->get_type_name(); 7.93 + v->type = typeid(*fractal).name(); 7.94 v->re_min = fractal->get_re_min(); 7.95 v->im_max = fractal->get_im_max(); 7.96 v->size = fractal->get_size(); 7.97 @@ -589,34 +609,9 @@ 7.98 7.99 7.100 //////////////////////////////////////////////////////////////////////////////// 7.101 -// This file name generation is kuldgy. Need to learn how to do file i/o for 7.102 -// real! 7.103 - 7.104 -int scandir_filter(const struct dirent * d) 7.105 - { 7.106 - if (memcmp(d->d_name, "mandelbrot-", 11) != 0) return 0; 7.107 - if (memcmp(d->d_name+14, ".bmp", 4) != 0) return 0; 7.108 - if (isdigit(d->d_name[11]) 7.109 - && isdigit(d->d_name[12]) 7.110 - && isdigit(d->d_name[13])) 7.111 - return 1; 7.112 - return 0; 7.113 - } 7.114 - 7.115 - 7.116 bool App::save_image() 7.117 { 7.118 - static int i = 0; 7.119 - 7.120 - if (i == 0) 7.121 - { 7.122 - struct dirent ** file_list; 7.123 - int num_files = scandir(".", &file_list, scandir_filter, alphasort); 7.124 - if (num_files != 0) 7.125 - sscanf(file_list[num_files-1]->d_name, "mandelbrot-%03d.bmp", &i); 7.126 - } 7.127 - i++; 7.128 - 7.129 + int i = App::get_next_file_num(); 7.130 std::stringstream name; 7.131 name << "mandelbrot-" << std::setw(3) << std::setfill('0') << i << ".bmp"; 7.132 7.133 @@ -647,3 +642,84 @@ 7.134 } 7.135 7.136 7.137 +#if defined(WIN32) 7.138 +//////////////////////////////////////////////////////////////////////////////// 7.139 +//Code for Windows 7.140 +int App::get_next_file_num() 7.141 + { 7.142 + static int i = 0; 7.143 + 7.144 + if (i == 0) 7.145 + { 7.146 + WIN32_FIND_DATA ffd; 7.147 + HANDLE hFind = INVALID_HANDLE_VALUE; 7.148 + 7.149 + char * file_spec = "mandelbrot-???.bmp"; 7.150 + hFind = FindFirstFile(file_spec, &ffd); 7.151 + if (hFind != INVALID_HANDLE_VALUE) 7.152 + { 7.153 + std::string s; 7.154 + std::stringstream ss; 7.155 + do 7.156 + { 7.157 + std::cerr << ffd.cFileName << std::endl; 7.158 + ss << ffd.cFileName; 7.159 + if (ss.str().size() == 18) 7.160 + { 7.161 + s = ss.str().substr(0, 11); 7.162 + if ( StrCmpI(s.c_str(), "mandelbrot-") == 0 ) 7.163 + { 7.164 + s = ss.str().substr(14, 4); 7.165 + if (StrCmpI(s.c_str(), ".bmp") == 0) 7.166 + { 7.167 + s = ss.str().substr(11, 3); 7.168 + int j = atoi(s.c_str()); 7.169 + if (j > i) i = j; 7.170 + } 7.171 + } 7.172 + } 7.173 + } 7.174 + while (FindNextFile(hFind, &ffd) != 0); 7.175 + } 7.176 + } 7.177 + 7.178 + i++; 7.179 + return i; 7.180 + } 7.181 + 7.182 + 7.183 + 7.184 +#else 7.185 +//////////////////////////////////////////////////////////////////////////////// 7.186 +// Code for Linux 7.187 + 7.188 +int App::scandir_filter(const struct dirent * d) 7.189 + { 7.190 + if (memcmp(d->d_name, "mandelbrot-", 11) != 0) return 0; 7.191 + if (memcmp(d->d_name+14, ".bmp", 4) != 0) return 0; 7.192 + if (isdigit(d->d_name[11]) 7.193 + && isdigit(d->d_name[12]) 7.194 + && isdigit(d->d_name[13])) 7.195 + return 1; 7.196 + return 0; 7.197 + } 7.198 + 7.199 + 7.200 +int App::get_next_file_num() 7.201 + { 7.202 + static int i = 0; 7.203 + 7.204 + if (i == 0) 7.205 + { 7.206 + struct dirent ** file_list; 7.207 + int num_files = scandir(".", &file_list, scandir_filter, alphasort); 7.208 + if (num_files != 0) 7.209 + sscanf(file_list[num_files-1]->d_name, "mandelbrot-%03d.bmp", &i); 7.210 + } 7.211 + i++; 7.212 + 7.213 + return i; 7.214 + } 7.215 + 7.216 +#endif 7.217 +
8.1 --- a/src/Fractal.cpp Sat Oct 23 04:24:48 2010 -0500 8.2 +++ b/src/Fractal.cpp Tue Oct 26 02:03:47 2010 -0500 8.3 @@ -20,8 +20,8 @@ 8.4 //////////////////////////////////////////////////////////////////////////////// 8.5 8.6 Fractal::Fractal() : 8.7 - display_name ("No fractal selected"), 8.8 - type_name (FRACTAL_TYPE_NAME), 8.9 + display_name ("No fractal"), 8.10 + fractal_name ("No fractal"), 8.11 re_min (0), 8.12 im_max (0), 8.13 size (0), 8.14 @@ -32,7 +32,7 @@ 8.15 } 8.16 8.17 //////////////////////////////////////////////////////////////////////////////// 8.18 -double Fractal::get_re_min() 8.19 +double Fractal::get_re_min() const 8.20 { 8.21 return re_min; 8.22 } 8.23 @@ -48,7 +48,7 @@ 8.24 8.25 8.26 //////////////////////////////////////////////////////////////////////////////// 8.27 -double Fractal::get_im_max() 8.28 +double Fractal::get_im_max() const 8.29 { 8.30 return im_max; 8.31 } 8.32 @@ -64,7 +64,7 @@ 8.33 8.34 8.35 //////////////////////////////////////////////////////////////////////////////// 8.36 -double Fractal::get_size() 8.37 +double Fractal::get_size() const 8.38 { 8.39 return size; 8.40 } 8.41 @@ -83,7 +83,7 @@ 8.42 8.43 8.44 //////////////////////////////////////////////////////////////////////////////// 8.45 -unsigned int Fractal::get_max_iter() 8.46 +unsigned int Fractal::get_max_iter() const 8.47 { 8.48 return max_iter; 8.49 } 8.50 @@ -128,16 +128,16 @@ 8.51 8.52 8.53 //////////////////////////////////////////////////////////////////////////////// 8.54 -const char * Fractal::get_display_name() 8.55 +std::string const & Fractal::get_display_name() const 8.56 { 8.57 - return display_name.c_str(); 8.58 + return display_name; 8.59 } 8.60 8.61 8.62 //////////////////////////////////////////////////////////////////////////////// 8.63 -const char * Fractal::get_type_name() 8.64 +std::string const & Fractal::get_fractal_name() const 8.65 { 8.66 - return FRACTAL_TYPE_NAME; 8.67 + return fractal_name; 8.68 } 8.69 8.70
9.1 --- a/src/Julia.cpp Sat Oct 23 04:24:48 2010 -0500 9.2 +++ b/src/Julia.cpp Tue Oct 26 02:03:47 2010 -0500 9.3 @@ -21,13 +21,13 @@ 9.4 k_re (-0.565072), 9.5 k_im ( 0.491657) 9.6 { 9.7 - type_name = JULIA_TYPE_NAME; 9.8 re_min = -2.0; 9.9 im_max = 2.0; 9.10 size = 4.0; 9.11 max_iter = 50; 9.12 calc_needed = true; 9.13 set_display_name(); 9.14 + fractal_name = "Julia set"; 9.15 } 9.16 9.17 9.18 @@ -118,7 +118,7 @@ 9.19 9.20 9.21 //////////////////////////////////////////////////////////////////////////////// 9.22 -bool Julia::get_option(std::string option, void * value) 9.23 +bool Julia::get_option(std::string option, void * value) const 9.24 { 9.25 if (option.compare("k") == 0) 9.26 { 9.27 @@ -154,9 +154,9 @@ 9.28 9.29 9.30 //////////////////////////////////////////////////////////////////////////////// 9.31 -const char * Julia::get_type_name() 9.32 +std::string const & Julia::get_fractal_name() const 9.33 { 9.34 - return JULIA_TYPE_NAME; 9.35 + return fractal_name; 9.36 } 9.37 9.38
10.1 --- a/src/Mandelbrot.cpp Sat Oct 23 04:24:48 2010 -0500 10.2 +++ b/src/Mandelbrot.cpp Tue Oct 26 02:03:47 2010 -0500 10.3 @@ -18,13 +18,13 @@ 10.4 10.5 Mandelbrot::Mandelbrot() 10.6 { 10.7 - display_name = "Mandelbrot set"; 10.8 - type_name = MANDELBROT_TYPE_NAME; 10.9 re_min = -2.5; 10.10 im_max = 2.5; 10.11 size = 5.0; 10.12 max_iter = 50; 10.13 calc_needed = true; 10.14 + display_name = "Mandelbrot set"; 10.15 + fractal_name = "Mandelbrot set"; 10.16 } 10.17 10.18 10.19 @@ -115,9 +115,9 @@ 10.20 10.21 10.22 //////////////////////////////////////////////////////////////////////////////// 10.23 -const char * Mandelbrot::get_type_name() 10.24 +std::string const & Mandelbrot::get_fractal_name() const 10.25 { 10.26 - return MANDELBROT_TYPE_NAME; 10.27 + return fractal_name; 10.28 } 10.29 10.30