Mercurial > fracter
changeset 1:455406f5f021
Wow. Forgot to actually add any files to the repository when I made my first commit.
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Tue, 19 Oct 2010 03:37:31 -0500 |
parents | 0495feee881f |
children | 0684158d38a8 |
files | .hgignore CMakeLists.txt TODO include/App.h include/EventHandler.h include/Fractal.h include/Julia.h include/Mandelbrot.h include/Options.h src/App.cpp src/EventHandler.cpp src/Fractal.cpp src/Julia.cpp src/Mandelbrot.cpp src/Options.cpp src/main.cpp |
diffstat | 16 files changed, 1550 insertions(+), 1 deletions(-) [+] |
line diff
1.1 --- a/.hgignore Sun Oct 17 23:08:48 2010 -0500 1.2 +++ b/.hgignore Tue Oct 19 03:37:31 2010 -0500 1.3 @@ -2,4 +2,4 @@ 1.4 1.5 build*/* 1.6 *~ 1.7 - 1.8 +saved-images/*
2.1 --- a/CMakeLists.txt Sun Oct 17 23:08:48 2010 -0500 2.2 +++ b/CMakeLists.txt Tue Oct 19 03:37:31 2010 -0500 2.3 @@ -141,6 +141,7 @@ 2.4 # Although we listed the library directories above, we also need to list the 2.5 # individual libraries we will be linking against. 2.6 target_link_libraries (${App_Name} 2.7 + png 2.8 ) 2.9 2.10
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/TODO Tue Oct 19 03:37:31 2010 -0500 3.3 @@ -0,0 +1,11 @@ 3.4 +- Make old_views store the fractal type (or clear the old_views list when changing type if lazy). 3.5 + 3.6 +- 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. 3.7 + 3.8 +- Find out how to save jpg or some other image format that allows comments I can use to store calculation data. 3.9 + 3.10 +- Read saved image and continue calculation. 3.11 + 3.12 +- Unzoom feature that enlarges the area while keeping the image cenetered on the same spot. 3.13 + 3.14 +- Performance improvment ideas:
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/include/App.h Tue Oct 19 03:37:31 2010 -0500 4.3 @@ -0,0 +1,81 @@ 4.4 +//////////////////////////////////////////////////////////////////////////////// 4.5 +// 4.6 +// (C) 2010 Sarah Eris Horsley Caffee 4.7 +// 4.8 +// mandelbrot - A simple Mandelbrot Set viewer. 4.9 +// 4.10 +// App.h 4.11 +// Application class definition 4.12 +// 4.13 +//////////////////////////////////////////////////////////////////////////////// 4.14 + 4.15 +#ifndef APP_H 4.16 +#define APP_H 4.17 + 4.18 +#include <vector> 4.19 + 4.20 +#include "png.h" 4.21 +#include "SDL.h" 4.22 + 4.23 +#include "EventHandler.h" 4.24 +#include "Mandelbrot.h" 4.25 + 4.26 + 4.27 +class App : public EventHandler 4.28 + { 4.29 + public: 4.30 + 4.31 + App(); 4.32 + ~App(); 4.33 + 4.34 + int run(); 4.35 + 4.36 + private: 4.37 + 4.38 + bool init(); 4.39 + void update(); 4.40 + void render(); 4.41 + void cleanup(); 4.42 + 4.43 + // Overrides of EventHandler: 4.44 + void on_event(SDL_Event * event); 4.45 + void on_KeyDown(SDLKey sym, SDLMod mod, Uint16 unicode); 4.46 + void on_Exit(); 4.47 + void on_LButtonDown(int mx, int my); 4.48 + void on_LButtonUp(int mx, int my); 4.49 + void on_MouseMove(int mx, int my, 4.50 + int relx, int rely, 4.51 + bool left, bool right, bool middle); 4.52 + 4.53 + int mouse_move_width(int mx, int my); 4.54 + bool create_new_surface(SDL_Surface * &surface); 4.55 + void restore_view(); 4.56 + void set_caption(); 4.57 + // void png_user_warning(png_structp png, png_const_charp s); 4.58 + // void png_user_error(png_structp png, png_const_charp s); 4.59 + bool save_image(); 4.60 + 4.61 + 4.62 + 4.63 + bool running; 4.64 + 4.65 + SDL_Surface * surf_display; 4.66 + SDL_Surface * surf_selection; 4.67 + SDL_Surface * surf_fractal; 4.68 + 4.69 + int selection_x, selection_y, selection_width; 4.70 + bool setting_zoom; 4.71 + bool can_set_julia_K; 4.72 + bool setting_julia_K; 4.73 + 4.74 + struct view 4.75 + { 4.76 + double Re_min, Im_max, size; 4.77 + }; 4.78 + static std::vector<view *> old_views; 4.79 + 4.80 + 4.81 + Fractal * fractal; 4.82 + }; 4.83 + 4.84 +#endif
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/include/EventHandler.h Tue Oct 19 03:37:31 2010 -0500 5.3 @@ -0,0 +1,56 @@ 5.4 +//////////////////////////////////////////////////////////////////////////////// 5.5 +// 5.6 +// (C) 2010 Sarah Eris Horsley Caffee 5.7 +// 5.8 +// mandelbrot - A simple Mandelbrot Set viewer. 5.9 +// 5.10 +// EventHandler.h 5.11 +// EventHandler class definition. This is a virtual class that is meant to be 5.12 +// an interface implemented by an application class. It does, though, provide 5.13 +// default implementations of it's methods so that unused events can be quietly 5.14 +// ignored. 5.15 +// 5.16 +//////////////////////////////////////////////////////////////////////////////// 5.17 + 5.18 +#ifndef EVENTHANDLER_H 5.19 +#define EVENTHANDLER_H 5.20 + 5.21 +#include "SDL.h" 5.22 + 5.23 +class EventHandler 5.24 + { 5.25 + public: 5.26 + 5.27 + EventHandler(); 5.28 + virtual ~EventHandler(); 5.29 + 5.30 + virtual void on_event(SDL_Event * event); 5.31 + 5.32 + virtual void on_InputFocus(); 5.33 + virtual void on_InputBlur(); 5.34 + virtual void on_KeyDown(SDLKey sym, SDLMod mod, Uint16 unicode); 5.35 + virtual void on_KeyUp(SDLKey sym, SDLMod mod, Uint16 unicode); 5.36 + virtual void on_MouseFocus(); 5.37 + virtual void on_MouseBlur(); 5.38 + virtual void on_MouseMove(int mx, int my, int relx, int rely, bool left, bool right, bool middle); 5.39 + virtual void on_MouseWheel(bool up, bool down); 5.40 + virtual void on_LButtonDown(int mx, int my); 5.41 + virtual void on_LButtonUp(int mx, int my); 5.42 + virtual void on_RButtonDown(int mx, int my); 5.43 + virtual void on_RButtonUp(int mx, int my); 5.44 + virtual void on_MButtonDown(int mx, int my); 5.45 + virtual void on_MButtonUp(int mx, int my); 5.46 + virtual void on_JoyAxis(Uint8 ehich, Uint8 axis, Sint16 value); 5.47 + virtual void on_JoyButtonDown(Uint8 which, Uint8 button); 5.48 + virtual void on_JoyButtonUp(Uint8 which, Uint8 button); 5.49 + virtual void on_JoyHat(Uint8 which, Uint8 hat, Uint8 value); 5.50 + virtual void on_JoyBall(Uint8 which, Uint8 ball, Sint16 xrel, Sint16 yrel); 5.51 + virtual void on_Minimize(); 5.52 + virtual void on_Restore(); 5.53 + virtual void on_Resize(int w, int h); 5.54 + virtual void on_Expose(); 5.55 + virtual void on_Exit(); 5.56 + virtual void on_User(Uint8 type, int code, void * data1, void * data2); 5.57 + }; 5.58 + 5.59 +#endif
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/include/Fractal.h Tue Oct 19 03:37:31 2010 -0500 6.3 @@ -0,0 +1,41 @@ 6.4 +//////////////////////////////////////////////////////////////////////////////// 6.5 +// 6.6 +// (C) 2010 Sarah Eris Horsley Caffee 6.7 +// 6.8 +// mandelbrot - A simple Mandelbrot Set viewer. 6.9 +// 6.10 +// Fractal.h 6.11 +// Fractal calculator virtual class definition. Actual fractals should 6.12 +// implement this. 6.13 +// 6.14 +//////////////////////////////////////////////////////////////////////////////// 6.15 + 6.16 +#ifndef FRACTAL_H 6.17 +#define FRACTAL_H 6.18 + 6.19 +#include <string> 6.20 + 6.21 +class Fractal 6.22 + { 6.23 + public: 6.24 + Fractal(); 6.25 + 6.26 + virtual bool calc_set() = 0; 6.27 + virtual bool init(SDL_Surface * surf) = 0; 6.28 + const char * name(); 6.29 + 6.30 + double Re_min; 6.31 + double Im_max; 6.32 + double size; 6.33 + unsigned int max_iter; 6.34 + 6.35 + protected: 6.36 + 6.37 + virtual void draw_pixel(int x, int y, Uint32 * color); 6.38 + 6.39 + std::string the_name; 6.40 + SDL_Surface * surface; 6.41 + 6.42 + }; 6.43 + 6.44 +#endif
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/include/Julia.h Tue Oct 19 03:37:31 2010 -0500 7.3 @@ -0,0 +1,41 @@ 7.4 +//////////////////////////////////////////////////////////////////////////////// 7.5 +// 7.6 +// (C) 2010 Sarah Eris Horsley Caffee 7.7 +// 7.8 +// mandelbrot - A simple Mandelbrot Set viewer. 7.9 +// 7.10 +// Julia.h 7.11 +// Julia set calculator class definition 7.12 +// 7.13 +//////////////////////////////////////////////////////////////////////////////// 7.14 + 7.15 +#ifndef JULIA_H 7.16 +#define JULIA_H 7.17 + 7.18 +#include <string> 7.19 + 7.20 +#include "SDL.h" 7.21 + 7.22 +#include "Fractal.h" 7.23 + 7.24 +class Julia : public Fractal 7.25 + { 7.26 + public: 7.27 + Julia(); 7.28 + 7.29 + bool calc_set(); 7.30 + bool init(SDL_Surface * surf); 7.31 + 7.32 + void get_K(double & Re, double & Im); 7.33 + void set_K(double Re, double Im); 7.34 + 7.35 + private: 7.36 + 7.37 + void draw_pixel(int x, int y, Uint32 * color); 7.38 + void set_name(); 7.39 + 7.40 + double K_Re; 7.41 + double K_Im; 7.42 + }; 7.43 + 7.44 +#endif
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/include/Mandelbrot.h Tue Oct 19 03:37:31 2010 -0500 8.3 @@ -0,0 +1,34 @@ 8.4 +//////////////////////////////////////////////////////////////////////////////// 8.5 +// 8.6 +// (C) 2010 Sarah Eris Horsley Caffee 8.7 +// 8.8 +// mandelbrot - A simple Mandelbrot Set viewer. 8.9 +// 8.10 +// Mandelbrot.h 8.11 +// Mandelbrot set calculator class definition 8.12 +// 8.13 +//////////////////////////////////////////////////////////////////////////////// 8.14 + 8.15 +#ifndef MANDELBROT_H 8.16 +#define MANDELBROT_H 8.17 + 8.18 +#include <string> 8.19 + 8.20 +#include "SDL.h" 8.21 + 8.22 +#include "Fractal.h" 8.23 + 8.24 +class Mandelbrot : public Fractal 8.25 + { 8.26 + public: 8.27 + Mandelbrot(); 8.28 + 8.29 + bool calc_set(); 8.30 + bool init(SDL_Surface * surf); 8.31 + 8.32 + private: 8.33 + 8.34 + void draw_pixel(int x, int y, Uint32 * color); 8.35 + }; 8.36 + 8.37 +#endif
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/include/Options.h Tue Oct 19 03:37:31 2010 -0500 9.3 @@ -0,0 +1,26 @@ 9.4 +//////////////////////////////////////////////////////////////////////////////// 9.5 +// 9.6 +// (C) 2010 Sarah Eris Horsley Caffee 9.7 +// 9.8 +// mandelbrot - A simple Mandelbrot Set viewer. 9.9 +// 9.10 +// options.h 9.11 +// global variables and definitions 9.12 +// 9.13 +//////////////////////////////////////////////////////////////////////////////// 9.14 + 9.15 +#ifndef OPTIONS_H 9.16 +#define OPTIONS_H 9.17 + 9.18 +#include <string> 9.19 + 9.20 +class Options 9.21 + { 9.22 + public: 9.23 + 9.24 + static int width; 9.25 + static int height; 9.26 + static int bpp; 9.27 + }; 9.28 + 9.29 +#endif
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/src/App.cpp Tue Oct 19 03:37:31 2010 -0500 10.3 @@ -0,0 +1,583 @@ 10.4 +//////////////////////////////////////////////////////////////////////////////// 10.5 +// 10.6 +// (C) 2010 Sarah Eris Horsley Caffee 10.7 +// 10.8 +// mandelbrot - A simple Mandelbrot Set viewer. 10.9 +// 10.10 +// App.cpp 10.11 +// Application class implementation 10.12 +// 10.13 +//////////////////////////////////////////////////////////////////////////////// 10.14 + 10.15 +#include <iostream> 10.16 +#include <cstdio> 10.17 +#include <cstdlib> 10.18 +#include <cstring> 10.19 +#include <vector> 10.20 + 10.21 +#include <dirent.h> 10.22 + 10.23 +#include "png.h" 10.24 +#include "SDL.h" 10.25 + 10.26 +#include "App.h" 10.27 +#include "Options.h" 10.28 + 10.29 +#include "Mandelbrot.h" 10.30 +#include "Julia.h" 10.31 + 10.32 + 10.33 +#define SAVE_AS_BMP 10.34 + 10.35 +std::vector<App::view *> App::old_views; 10.36 + 10.37 +//////////////////////////////////////////////////////////////////////////////// 10.38 + 10.39 +App::App() : 10.40 + running (true), 10.41 + surf_display (NULL), 10.42 + surf_selection (NULL), 10.43 + surf_fractal (NULL), 10.44 + selection_x (-1), 10.45 + selection_y (-1), 10.46 + selection_width (-1), 10.47 + setting_zoom (false), 10.48 + can_set_julia_K (false), 10.49 + setting_julia_K (false) 10.50 + { 10.51 + } 10.52 + 10.53 +//////////////////////////////////////////////////////////////////////////////// 10.54 +App::~App() 10.55 + { 10.56 + if (old_views.size() > 0) 10.57 + { 10.58 + view * v = old_views.back(); 10.59 + old_views.pop_back(); 10.60 + delete v; 10.61 + } 10.62 + } 10.63 + 10.64 +//////////////////////////////////////////////////////////////////////////////// 10.65 + 10.66 +int App::run() 10.67 + { 10.68 + if (init() == false) 10.69 + return 1; 10.70 + 10.71 + SDL_Event event; 10.72 + 10.73 + while (running) 10.74 + { 10.75 + while (SDL_PollEvent(&event)) 10.76 + { 10.77 + on_event(&event); 10.78 + } 10.79 + update(); 10.80 + render(); 10.81 + } 10.82 + 10.83 + cleanup(); 10.84 + 10.85 + return 0; 10.86 + } 10.87 + 10.88 +//////////////////////////////////////////////////////////////////////////////// 10.89 + 10.90 +bool App::init() 10.91 + { 10.92 + if ((SDL_Init(SDL_INIT_VIDEO)) == -1) 10.93 + { 10.94 + std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl; 10.95 + return false; 10.96 + } 10.97 + 10.98 + if ((surf_display = SDL_SetVideoMode(Options::width, 10.99 + Options::height, 10.100 + Options::bpp, 10.101 + SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) 10.102 + { 10.103 + std::cerr << "SDL_SetVideoMode() failed: " 10.104 + << SDL_GetError() << std::endl; 10.105 + return false; 10.106 + } 10.107 + 10.108 + SDL_EnableKeyRepeat(500, 100); 10.109 + 10.110 + 10.111 + if (!create_new_surface(surf_selection)) { return false; } 10.112 + if (!create_new_surface(surf_fractal)) { return false; } 10.113 + 10.114 + fractal = new Mandelbrot(); 10.115 + fractal->init(surf_fractal); 10.116 + can_set_julia_K = true; 10.117 + 10.118 + set_caption(); 10.119 + 10.120 + return true; 10.121 + } 10.122 + 10.123 +//////////////////////////////////////////////////////////////////////////////// 10.124 + 10.125 +void App::update() 10.126 + { 10.127 + } 10.128 + 10.129 +//////////////////////////////////////////////////////////////////////////////// 10.130 + 10.131 +void App::render() 10.132 + { 10.133 + // Start with a clean slate. 10.134 + SDL_FillRect(surf_display, &surf_display->clip_rect, 10.135 + SDL_MapRGB(surf_display->format, 0, 0, 255)); 10.136 + 10.137 + fractal->calc_set(); 10.138 + SDL_BlitSurface(surf_fractal, NULL, surf_display, NULL); 10.139 + 10.140 + // draw a selection box 10.141 + if (setting_zoom) 10.142 + { 10.143 + SDL_BlitSurface(surf_selection, NULL, surf_display, NULL); 10.144 + } 10.145 + 10.146 + // Show it to the user. 10.147 + SDL_Flip(surf_display); 10.148 + 10.149 + } 10.150 + 10.151 +//////////////////////////////////////////////////////////////////////////////// 10.152 + 10.153 +void App::cleanup() 10.154 + { 10.155 + 10.156 + if (surf_selection) 10.157 + { 10.158 + SDL_FreeSurface(surf_selection); 10.159 + surf_selection = NULL; 10.160 + } 10.161 + if (surf_display) 10.162 + { 10.163 + SDL_FreeSurface(surf_display); 10.164 + surf_display = NULL; 10.165 + } 10.166 + 10.167 + SDL_Quit(); 10.168 + } 10.169 + 10.170 +//////////////////////////////////////////////////////////////////////////////// 10.171 + 10.172 +void App::on_event(SDL_Event * event) 10.173 + { 10.174 + EventHandler::on_event(event); 10.175 + } 10.176 + 10.177 +//////////////////////////////////////////////////////////////////////////////// 10.178 + 10.179 +void App::on_KeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) 10.180 + { 10.181 + switch (sym) 10.182 + { 10.183 + case SDLK_ESCAPE: 10.184 + case SDLK_q: 10.185 + SDL_Event event; 10.186 + event.type = SDL_QUIT; 10.187 + SDL_PushEvent(&event); 10.188 + break; 10.189 + 10.190 + case SDLK_b: 10.191 + restore_view(); 10.192 + set_caption(); 10.193 + break; 10.194 + 10.195 + case SDLK_s: 10.196 + save_image(); 10.197 + break; 10.198 + 10.199 + case SDLK_UP: 10.200 + fractal->max_iter++; 10.201 + set_caption(); 10.202 + break; 10.203 + case SDLK_DOWN: 10.204 + fractal->max_iter--; 10.205 + set_caption(); 10.206 + break; 10.207 + 10.208 + 10.209 + case SDLK_m: 10.210 + if (fractal) 10.211 + { 10.212 + delete fractal; 10.213 + fractal = new Mandelbrot(); 10.214 + fractal->init(surf_fractal); 10.215 + set_caption(); 10.216 + can_set_julia_K = true; 10.217 + } 10.218 + break; 10.219 + case SDLK_j: 10.220 + if (fractal) 10.221 + { 10.222 + delete fractal; 10.223 + fractal = new Julia(); 10.224 + fractal->init(surf_fractal); 10.225 + set_caption(); 10.226 + can_set_julia_K = false; 10.227 + } 10.228 + break; 10.229 + 10.230 + case SDLK_k: 10.231 + std::cerr << "can_set_julia_K " << can_set_julia_K << std::endl; 10.232 + if (can_set_julia_K) 10.233 + { 10.234 + if (setting_julia_K)setting_julia_K = false; 10.235 + else setting_julia_K = true; 10.236 + } 10.237 + break; 10.238 + 10.239 + default: 10.240 + break; 10.241 + } 10.242 + } 10.243 + 10.244 + 10.245 +//////////////////////////////////////////////////////////////////////////////// 10.246 + 10.247 +void App::on_Exit() 10.248 + { 10.249 + running = false; 10.250 + } 10.251 + 10.252 + 10.253 +//////////////////////////////////////////////////////////////////////////////// 10.254 + 10.255 +void App::on_LButtonDown(int mx, int my) 10.256 + { 10.257 + if (setting_julia_K) 10.258 + { 10.259 + double K_Re = fractal->Re_min + mx * (fractal->size)/(Options::width-1); 10.260 + double K_Im = fractal->Im_max - my * (fractal->size)/(Options::height-1); 10.261 + 10.262 + delete fractal; 10.263 + fractal = new Julia(); 10.264 + fractal->init(surf_fractal); 10.265 + ((Julia *)fractal)->set_K(K_Re, K_Im); 10.266 + 10.267 + set_caption(); 10.268 + setting_julia_K = false; 10.269 + } 10.270 + else 10.271 + { 10.272 + selection_x = mx; 10.273 + selection_y = my; 10.274 + setting_zoom = true; 10.275 + } 10.276 + } 10.277 + 10.278 + 10.279 +//////////////////////////////////////////////////////////////////////////////// 10.280 + 10.281 +void App::on_LButtonUp(int mx, int my) 10.282 + { 10.283 + if (setting_zoom) 10.284 + { 10.285 + if ( (mx <= 0) || 10.286 + (mx >= Options::width - 1) || 10.287 + (my <= 0) || 10.288 + (my >= Options::height - 1)) 10.289 + { 10.290 + // Selection cancelled 10.291 + setting_zoom = false; 10.292 + return; 10.293 + } 10.294 + 10.295 + selection_width = mouse_move_width(mx, my); 10.296 + 10.297 + // calculate new min/max re/im 10.298 + double Re_scale = (fractal->size)/(Options::width-1); 10.299 + double Im_scale = (fractal->size)/(Options::height-1); 10.300 + 10.301 + double new_Re_min = fractal->Re_min + (selection_x) * Re_scale; 10.302 + double new_Im_max = fractal->Im_max - (selection_y) * Im_scale; 10.303 + double new_size = selection_width * Re_scale; 10.304 + 10.305 + 10.306 + view * v = new view; 10.307 + v->Re_min = fractal->Re_min; 10.308 + v->Im_max = fractal->Im_max; 10.309 + v->size = fractal->size; 10.310 + old_views.push_back(v); 10.311 + 10.312 + fractal->Re_min = new_Re_min; 10.313 + fractal->Im_max = new_Im_max; 10.314 + fractal->size = new_size; 10.315 + // std::cerr << "Set new bounds at (" << fractal->Re_min << "," << fractal->Im_max << ") size " << fractal->size << std::endl; 10.316 + 10.317 + set_caption(); 10.318 + 10.319 + setting_zoom = false; 10.320 + } 10.321 + } 10.322 + 10.323 + 10.324 +//////////////////////////////////////////////////////////////////////////////// 10.325 +void App::on_MouseMove(int mx, int my, 10.326 + int relx, int rely, 10.327 + bool left, bool right, bool middle) 10.328 + { 10.329 + if (setting_zoom) 10.330 + { 10.331 + Uint32 clear = SDL_MapRGBA(surf_selection->format, 10.332 + 0, 0, 0, SDL_ALPHA_TRANSPARENT); 10.333 + Uint32 white = SDL_MapRGBA(surf_selection->format, 10.334 + 255, 255, 255, SDL_ALPHA_OPAQUE); 10.335 + 10.336 + SDL_FillRect(surf_selection, &surf_selection->clip_rect, clear); 10.337 + 10.338 + SDL_Rect rect; 10.339 + rect.x = selection_x; 10.340 + rect.y = selection_y; 10.341 + int w = mouse_move_width(mx, my); 10.342 + rect.w = rect.h = w; 10.343 + 10.344 + SDL_FillRect(surf_selection, &rect, white); 10.345 + 10.346 + rect.x = selection_x + 1; 10.347 + rect.y = selection_y + 1; 10.348 + rect.w = rect.h = w - 2; 10.349 + 10.350 + SDL_FillRect(surf_selection, &rect, clear); 10.351 + } 10.352 + } 10.353 + 10.354 + 10.355 +//////////////////////////////////////////////////////////////////////////////// 10.356 +int App::mouse_move_width(int mx, int my) 10.357 + { 10.358 + int move_width = (mx - selection_x); 10.359 + if (mx > selection_x) 10.360 + { 10.361 + move_width *= -1; 10.362 + } 10.363 + 10.364 + int tmp_i = (my - selection_y); 10.365 + if (my < selection_y) 10.366 + { 10.367 + tmp_i *= -1; 10.368 + } 10.369 + 10.370 + if (tmp_i > move_width) 10.371 + move_width = tmp_i; 10.372 + 10.373 + return move_width; 10.374 + } 10.375 + 10.376 + 10.377 +//////////////////////////////////////////////////////////////////////////////// 10.378 +bool App::create_new_surface(SDL_Surface * &surface) 10.379 + { 10.380 + if (surface) 10.381 + { 10.382 + SDL_FreeSurface(surface); 10.383 + surface = NULL; 10.384 + } 10.385 + 10.386 + SDL_Surface * tmp; 10.387 + if ((tmp = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_SRCALPHA, 10.388 + Options::width, 10.389 + Options::height, Options::bpp, 0, 0, 0, 0)) 10.390 + == NULL) 10.391 + { 10.392 + std::cerr << "SDL_CreateRGBSurface() failed in " 10.393 + << "App::create_new_surface(): " 10.394 + << SDL_GetError() << std::endl; 10.395 + return false; 10.396 + } 10.397 + 10.398 + surface = SDL_DisplayFormatAlpha(tmp); 10.399 + SDL_FreeSurface(tmp); 10.400 + 10.401 + return true; 10.402 + } 10.403 + 10.404 + 10.405 +//////////////////////////////////////////////////////////////////////////////// 10.406 + 10.407 +void App::restore_view() 10.408 + { 10.409 + if (old_views.size() > 0) 10.410 + { 10.411 + view * v = old_views.back(); 10.412 + old_views.pop_back(); 10.413 + fractal->Re_min = v->Re_min; 10.414 + fractal->Im_max = v->Im_max; 10.415 + fractal->size = v->size; 10.416 + delete v; 10.417 + } 10.418 + } 10.419 + 10.420 + 10.421 +//////////////////////////////////////////////////////////////////////////////// 10.422 +void App::set_caption() 10.423 + { 10.424 + char title[100]; 10.425 + sprintf(title, "%s - " 10.426 + "Real: %f to %f - " 10.427 + "Imaginary %f to %f - " 10.428 + "Max iterations: %d", 10.429 + fractal->name(), 10.430 + fractal->Re_min, fractal->Re_min + fractal->size, 10.431 + fractal->Im_max, fractal->Im_max + fractal->size, 10.432 + fractal->max_iter); 10.433 + SDL_WM_SetCaption(title, NULL); 10.434 + } 10.435 + 10.436 + 10.437 +//////////////////////////////////////////////////////////////////////////////// 10.438 +#ifdef SAVE_AS_PNG 10.439 +void png_user_error(png_structp png, png_const_charp s); 10.440 +void png_user_warning(png_structp png, png_const_charp s); 10.441 + 10.442 +void png_user_warning(png_structp png, png_const_charp s) 10.443 + { 10.444 + std::cerr << "libpng: warning " << (char *) s << std::endl; 10.445 + } 10.446 + 10.447 + 10.448 +//////////////////////////////////////////////////////////////////////////////// 10.449 +void png_user_error(png_structp png, png_const_charp s) 10.450 + { 10.451 + std::cerr << "libpng: error " << (char *) s << std::endl; 10.452 + } 10.453 + 10.454 + 10.455 +//////////////////////////////////////////////////////////////////////////////// 10.456 + 10.457 +bool App::save_image() 10.458 + { 10.459 + static int i = 0; 10.460 + char name[20]; 10.461 + 10.462 + i++; 10.463 + sprintf(name, "mandelbrot-%03d.png", i); 10.464 + 10.465 + png_save_surface(name, surf_display); 10.466 + 10.467 + FILE * file; 10.468 + if ((file = fopen(name, "wb")) == NULL) 10.469 + { 10.470 + std::cerr << "fopen() failed in save_image() on file " 10.471 + << name << " for saving." << std::endl; 10.472 + return false; 10.473 + } 10.474 + 10.475 + 10.476 + 10.477 + png_structp png; 10.478 + png_infop png_info; 10.479 + 10.480 + if ((png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, 10.481 + png_user_error, png_user_warning)) == NULL) 10.482 + { 10.483 + std::cerr << "png_create_write_struct() failed in save_image()" 10.484 + << std::endl; 10.485 + fclose(file); 10.486 + return false; 10.487 + } 10.488 + 10.489 + 10.490 + if ((png_info = png_create_info_struct(png)) == NULL) 10.491 + { 10.492 + std::cerr << "png_create_info_struct() failed in save_image()" 10.493 + << std::endl; 10.494 + fclose(file); 10.495 + return false; 10.496 + } 10.497 + 10.498 + if (setjmp(png_jmpbuf(png))) 10.499 + { 10.500 + std::cerr << "png_jmpbuf() failed in save_image()" << std::endl; 10.501 + png_destroy_write_struct(&png, &png_info); 10.502 + fclose(file); 10.503 + return false; 10.504 + } 10.505 + 10.506 + png_init_io(png, file); 10.507 + int colortype = PNG_COLOR_MASK_COLOR; 10.508 + png_set_IHDR(png, png_info, 10.509 + surf_display->w, surf_display->h, 10.510 + 8, colortype, PNG_INTERLACE_NONE, 10.511 + PNG_COMPRESSION_TYPE_DEFAULT, 10.512 + PNG_FILTER_TYPE_DEFAULT); 10.513 + 10.514 + png_write_info(png, png_info); 10.515 + png_set_packing(png); 10.516 + 10.517 + png_bytep * rows = (png_bytep *) malloc(sizeof(png_bytep)*surf_display->h); 10.518 + for (int i = 0; i < surf_display->h; i++) 10.519 + { 10.520 + rows[i] = (png_bytep) surf_display->pixels + 10.521 + i * surf_display->pitch; 10.522 + } 10.523 + 10.524 + png_write_image(png, rows); 10.525 + png_write_end(png, png_info); 10.526 + 10.527 + free(rows); 10.528 + png_destroy_write_struct(&png, &png_info); 10.529 + fclose(file); 10.530 + 10.531 + return true; 10.532 + } 10.533 +#endif 10.534 + 10.535 +//////////////////////////////////////////////////////////////////////////////// 10.536 +#ifdef SAVE_AS_BMP 10.537 + 10.538 +// This file name generation is kuldgy. Need to learn how to do file i/o for 10.539 +// real! 10.540 + 10.541 +int scandir_filter(const struct dirent * d) 10.542 + { 10.543 + if (memcmp(d->d_name, "mandelbrot-", 11) != 0) return 0; 10.544 + if (memcmp(d->d_name+14, ".bmp", 4) != 0) return 0; 10.545 + if (isdigit(d->d_name[11]) 10.546 + && isdigit(d->d_name[12]) 10.547 + && isdigit(d->d_name[13])) 10.548 + return 1; 10.549 + return 0; 10.550 + } 10.551 + 10.552 + 10.553 +bool App::save_image() 10.554 + { 10.555 + static int i = 0; 10.556 + char name[20]; 10.557 + 10.558 + if (i == 0) 10.559 + { 10.560 + struct dirent ** file_list; 10.561 + int num_files = scandir(".", &file_list, scandir_filter, alphasort); 10.562 + if (num_files != 0) 10.563 + sscanf(file_list[num_files-1]->d_name, "mandelbrot-%03d.bmp", &i); 10.564 + } 10.565 + i++; 10.566 + sprintf(name, "mandelbrot-%03d.bmp", i); 10.567 + 10.568 + FILE * file; 10.569 + if ((file = fopen(name, "wb")) == NULL) 10.570 + { 10.571 + std::cerr << "fopen() failed in save_image() on file " 10.572 + << name << " for saving." << std::endl; 10.573 + return false; 10.574 + } 10.575 + 10.576 + if (SDL_SaveBMP(surf_display, name) != 0) 10.577 + { 10.578 + std::cerr << "Unable to save image file " << name << "" << std::endl; 10.579 + return false; 10.580 + } 10.581 + 10.582 + return true; 10.583 + } 10.584 +#endif 10.585 + 10.586 +
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/src/EventHandler.cpp Tue Oct 19 03:37:31 2010 -0500 11.3 @@ -0,0 +1,309 @@ 11.4 +//////////////////////////////////////////////////////////////////////////////// 11.5 +// 11.6 +// (C) 2010 Sarah Eris Horsley Caffee 11.7 +// 11.8 +// mandelbrot - A simple Mandelbrot Set viewer. 11.9 +// 11.10 +// EventHandler.cpp 11.11 +// EventHandler class implementation. 11.12 +// 11.13 +//////////////////////////////////////////////////////////////////////////////// 11.14 + 11.15 +#include "EventHandler.h" 11.16 + 11.17 + 11.18 +//////////////////////////////////////////////////////////////////////////////// 11.19 +EventHandler::EventHandler() 11.20 + { 11.21 + } 11.22 + 11.23 + 11.24 + 11.25 +//////////////////////////////////////////////////////////////////////////////// 11.26 +EventHandler::~EventHandler() 11.27 + { 11.28 + } 11.29 + 11.30 + 11.31 + 11.32 +//////////////////////////////////////////////////////////////////////////////// 11.33 +void EventHandler::on_event(SDL_Event * event) 11.34 + { 11.35 + switch(event->type) 11.36 + { 11.37 + case SDL_ACTIVEEVENT: 11.38 + { 11.39 + switch(event->active.state) 11.40 + { 11.41 + case SDL_APPMOUSEFOCUS: 11.42 + { 11.43 + if (event->active.gain) on_MouseFocus(); 11.44 + else on_MouseBlur(); 11.45 + break; 11.46 + } 11.47 + case SDL_APPINPUTFOCUS: 11.48 + { 11.49 + if (event->active.gain) on_InputFocus(); 11.50 + else on_InputBlur(); 11.51 + break; 11.52 + } 11.53 + case SDL_APPACTIVE: 11.54 + { 11.55 + if (event->active.gain) on_Restore(); 11.56 + else on_Minimize(); 11.57 + break; 11.58 + } 11.59 + } 11.60 + break; 11.61 + } 11.62 + case SDL_KEYDOWN: 11.63 + { 11.64 + on_KeyDown(event->key.keysym.sym, event->key.keysym.mod, 11.65 + event->key.keysym.unicode); 11.66 + break; 11.67 + } 11.68 + case SDL_KEYUP: 11.69 + { 11.70 + on_KeyUp(event->key.keysym.sym, event->key.keysym.mod, 11.71 + event->key.keysym.unicode); 11.72 + break; 11.73 + } 11.74 + case SDL_MOUSEMOTION: 11.75 + { 11.76 + on_MouseMove(event->motion.x, 11.77 + event->motion.y, 11.78 + event->motion.xrel, 11.79 + event->motion.yrel, 11.80 + (event->motion.state & SDL_BUTTON(SDL_BUTTON_LEFT)) !=0, 11.81 + (event->motion.state & SDL_BUTTON(SDL_BUTTON_RIGHT)) !=0, 11.82 + (event->motion.state & SDL_BUTTON(SDL_BUTTON_MIDDLE))!=0); 11.83 + break; 11.84 + } 11.85 + case SDL_MOUSEBUTTONDOWN: 11.86 + { 11.87 + switch (event->button.button) 11.88 + { 11.89 + case SDL_BUTTON_LEFT: 11.90 + { 11.91 + on_LButtonDown(event->button.x, event->button.y); 11.92 + break; 11.93 + } 11.94 + case SDL_BUTTON_RIGHT: 11.95 + { 11.96 + on_RButtonDown(event->button.x, event->button.y); 11.97 + break; 11.98 + } 11.99 + case SDL_BUTTON_MIDDLE: 11.100 + { 11.101 + on_MButtonDown(event->button.x, event->button.y); 11.102 + break; 11.103 + } 11.104 + } 11.105 + break; 11.106 + } 11.107 + case SDL_MOUSEBUTTONUP: 11.108 + { 11.109 + switch (event->button.button) 11.110 + { 11.111 + case SDL_BUTTON_LEFT: 11.112 + { 11.113 + on_LButtonUp(event->button.x, event->button.y); 11.114 + break; 11.115 + } 11.116 + case SDL_BUTTON_RIGHT: 11.117 + { 11.118 + on_RButtonUp(event->button.x, event->button.y); 11.119 + break; 11.120 + } 11.121 + case SDL_BUTTON_MIDDLE: 11.122 + { 11.123 + on_MButtonUp(event->button.x, event->button.y); 11.124 + break; 11.125 + } 11.126 + } 11.127 + break; 11.128 + } 11.129 + case SDL_JOYAXISMOTION: 11.130 + { 11.131 + on_JoyAxis(event->jaxis.which, event->jaxis.axis, event->jaxis.value); 11.132 + break; 11.133 + } 11.134 + case SDL_JOYBALLMOTION: 11.135 + { 11.136 + on_JoyBall(event->jball.which, event->jball.ball, 11.137 + event->jball.xrel, event->jball.yrel); 11.138 + break; 11.139 + } 11.140 + case SDL_JOYHATMOTION: 11.141 + { 11.142 + on_JoyHat(event->jhat.which, event->jhat.hat, event->jhat.value); 11.143 + break; 11.144 + } 11.145 + case SDL_JOYBUTTONDOWN: 11.146 + { 11.147 + on_JoyButtonDown(event->jbutton.which, event->jbutton.button); 11.148 + break; 11.149 + } 11.150 + case SDL_JOYBUTTONUP: 11.151 + { 11.152 + on_JoyButtonUp(event->jbutton.which, event->jbutton.button); 11.153 + break; 11.154 + } 11.155 + case SDL_QUIT: 11.156 + { 11.157 + on_Exit(); 11.158 + break; 11.159 + } 11.160 + case SDL_SYSWMEVENT: 11.161 + { 11.162 + // Ignore 11.163 + break; 11.164 + } 11.165 + case SDL_VIDEORESIZE: 11.166 + { 11.167 + on_Resize(event->resize.w, event->resize.h); 11.168 + break; 11.169 + } 11.170 + case SDL_VIDEOEXPOSE: 11.171 + { 11.172 + on_Expose(); 11.173 + break; 11.174 + } 11.175 + default: 11.176 + { 11.177 + on_User(event->user.type, event->user.code, 11.178 + event->user.data1, event->user.data2); 11.179 + break; 11.180 + } 11.181 + } 11.182 + } 11.183 + 11.184 + 11.185 + 11.186 +//////////////////////////////////////////////////////////////////////////////// 11.187 +void EventHandler::on_InputFocus() 11.188 + { 11.189 + } 11.190 + 11.191 + 11.192 +void EventHandler::on_InputBlur() 11.193 + { 11.194 + } 11.195 + 11.196 + 11.197 +void EventHandler::on_KeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) 11.198 + { 11.199 + } 11.200 + 11.201 + 11.202 +void EventHandler::on_KeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) 11.203 + { 11.204 + } 11.205 + 11.206 + 11.207 +void EventHandler::on_MouseFocus() 11.208 + { 11.209 + } 11.210 + 11.211 + 11.212 +void EventHandler::on_MouseBlur() 11.213 + { 11.214 + } 11.215 + 11.216 +void EventHandler::on_MouseMove(int mx, int my, 11.217 + int relx, int rely, 11.218 + bool left, bool right, bool middle) 11.219 + { 11.220 + } 11.221 + 11.222 + 11.223 +void EventHandler::on_MouseWheel(bool up, bool down) 11.224 + { 11.225 + } 11.226 + 11.227 + 11.228 +void EventHandler::on_LButtonDown(int mx, int my) 11.229 + { 11.230 + } 11.231 + 11.232 + 11.233 +void EventHandler::on_LButtonUp(int mx, int my) 11.234 + { 11.235 + } 11.236 + 11.237 + 11.238 +void EventHandler::on_RButtonDown(int mx, int my) 11.239 + { 11.240 + } 11.241 + 11.242 + 11.243 +void EventHandler::on_RButtonUp(int mx, int my) 11.244 + { 11.245 + } 11.246 + 11.247 + 11.248 +void EventHandler::on_MButtonDown(int mx, int my) 11.249 + { 11.250 + } 11.251 + 11.252 + 11.253 +void EventHandler::on_MButtonUp(int mx, int my) 11.254 + { 11.255 + } 11.256 + 11.257 + 11.258 +void EventHandler::on_JoyAxis(Uint8 ehich, Uint8 axis, Sint16 value) 11.259 + { 11.260 + } 11.261 + 11.262 + 11.263 +void EventHandler::on_JoyButtonDown(Uint8 which, Uint8 button) 11.264 + { 11.265 + } 11.266 + 11.267 + 11.268 +void EventHandler::on_JoyButtonUp(Uint8 which, Uint8 button) 11.269 + { 11.270 + } 11.271 + 11.272 + 11.273 +void EventHandler::on_JoyHat(Uint8 which, Uint8 hat, Uint8 value) 11.274 + { 11.275 + } 11.276 + 11.277 + 11.278 +void EventHandler::on_JoyBall(Uint8 which, Uint8 ball, Sint16 xrel, Sint16 yrel) 11.279 + { 11.280 + } 11.281 + 11.282 + 11.283 +void EventHandler::on_Minimize() 11.284 + { 11.285 + } 11.286 + 11.287 + 11.288 +void EventHandler::on_Restore() 11.289 + { 11.290 + } 11.291 + 11.292 + 11.293 +void EventHandler::on_Resize(int w, int h) 11.294 + { 11.295 + } 11.296 + 11.297 + 11.298 +void EventHandler::on_Expose() 11.299 + { 11.300 + } 11.301 + 11.302 + 11.303 +void EventHandler::on_Exit() 11.304 + { 11.305 + } 11.306 + 11.307 + 11.308 +void EventHandler::on_User(Uint8 type, int code, void * data1, void * data2) 11.309 + { 11.310 + } 11.311 + 11.312 +
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/src/Fractal.cpp Tue Oct 19 03:37:31 2010 -0500 12.3 @@ -0,0 +1,52 @@ 12.4 +//////////////////////////////////////////////////////////////////////////////// 12.5 +// 12.6 +// (C) 2010 Sarah Eris Horsley Caffee 12.7 +// 12.8 +// mandelbrot - A simple Mandelbrot Set viewer. 12.9 +// 12.10 +// Fractal.cpp 12.11 +// Fractal calculator virtual class implementation. Actual fractals should 12.12 +// implement this. 12.13 +// 12.14 +//////////////////////////////////////////////////////////////////////////////// 12.15 + 12.16 +#include <string> 12.17 + 12.18 +#include "SDL.h" 12.19 + 12.20 +#include "Fractal.h" 12.21 + 12.22 + 12.23 +//////////////////////////////////////////////////////////////////////////////// 12.24 + 12.25 +Fractal::Fractal() : 12.26 + the_name ("No fractal selected"), 12.27 + Re_min (0), 12.28 + Im_max (0), 12.29 + size (0), 12.30 + max_iter (0), 12.31 + surface (NULL) 12.32 + { 12.33 + } 12.34 + 12.35 +//////////////////////////////////////////////////////////////////////////////// 12.36 +const char * Fractal::name() 12.37 + { 12.38 + return the_name.c_str(); 12.39 + } 12.40 + 12.41 +//////////////////////////////////////////////////////////////////////////////// 12.42 +void Fractal::draw_pixel(int x, int y, Uint32 * color) 12.43 + { 12.44 + static char * data; 12.45 + 12.46 + if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); 12.47 + 12.48 + data = (char *) surface->pixels 12.49 + + y * surface->pitch 12.50 + + x * surface->format->BytesPerPixel; 12.51 + memcpy(data, color, surface->format->BytesPerPixel); 12.52 + 12.53 + if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); 12.54 + } 12.55 +
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/src/Julia.cpp Tue Oct 19 03:37:31 2010 -0500 13.3 @@ -0,0 +1,144 @@ 13.4 +//////////////////////////////////////////////////////////////////////////////// 13.5 +// 13.6 +// (C) 2010 Sarah Eris Horsley Caffee 13.7 +// 13.8 +// mandelbrot - A simple Mandelbrot Set viewer. 13.9 +// 13.10 +// Julia.cpp 13.11 +// Julia set calculator class implementation 13.12 +// 13.13 +//////////////////////////////////////////////////////////////////////////////// 13.14 + 13.15 +#include <cstdlib> 13.16 +#include <iostream> 13.17 +#include <string> 13.18 + 13.19 +#include "Julia.h" 13.20 +#include "Options.h" 13.21 + 13.22 +//////////////////////////////////////////////////////////////////////////////// 13.23 + 13.24 +Julia::Julia() : 13.25 + K_Re (-0.565072), 13.26 + K_Im ( 0.491657) 13.27 + { 13.28 + Re_min = -2.0; 13.29 + Im_max = 2.0; 13.30 + size = 4.0; 13.31 + max_iter = 50; 13.32 + set_name(); 13.33 + } 13.34 + 13.35 + 13.36 +//////////////////////////////////////////////////////////////////////////////// 13.37 + 13.38 +bool Julia::calc_set() 13.39 + { 13.40 + if (!surface) return false; 13.41 + 13.42 + // Code from http://warp.povusers.org/Julia/ 13.43 + 13.44 + double Re_scale = (size)/(Options::width-1); 13.45 + double Im_scale = (size)/(Options::height-1); 13.46 + 13.47 + int r, g, b; 13.48 + 13.49 + Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0); 13.50 + Uint32 color; 13.51 + int halfway = max_iter/2 - 1; 13.52 + float step = (float) 255 / halfway; 13.53 + 13.54 + unsigned x, y, n; 13.55 + 13.56 + for(y = 0; y < Options::height; ++y) 13.57 + { 13.58 + double c_im = Im_max - y * Im_scale; 13.59 + for(x = 0; x < Options::width; ++x) 13.60 + { 13.61 + double c_re = Re_min + x * Re_scale; 13.62 + 13.63 + double Z_re = c_re, Z_im = c_im; 13.64 + bool in_set = true; 13.65 + for(n = 0; n < max_iter; ++n) 13.66 + { 13.67 + double Z_re2 = Z_re * Z_re, Z_im2 = Z_im * Z_im; 13.68 + if(Z_re2 + Z_im2 > 4) 13.69 + { 13.70 + in_set = false; 13.71 + break; 13.72 + } 13.73 + Z_im = 2*Z_re * Z_im + K_Im; 13.74 + Z_re = Z_re2 - Z_im2 + K_Re; 13.75 + } 13.76 + if(in_set) 13.77 + { 13.78 + draw_pixel(x, y, &black); 13.79 + } 13.80 + else 13.81 + { 13.82 + if (n <= halfway) 13.83 + { 13.84 + r = n * step; 13.85 + b = g = 0; 13.86 + } 13.87 + else 13.88 + { 13.89 + r = 255; 13.90 + b = g = (n - halfway) * step; 13.91 + } 13.92 + color = SDL_MapRGB(surface->format, r, g, b); 13.93 + draw_pixel(x, y, &color); 13.94 + } 13.95 + } 13.96 + } 13.97 + return true; 13.98 + } 13.99 + 13.100 + 13.101 +//////////////////////////////////////////////////////////////////////////////// 13.102 +bool Julia::init(SDL_Surface * surf) 13.103 + { 13.104 + if (!surf) 13.105 + { 13.106 + return false; 13.107 + } 13.108 + else 13.109 + { 13.110 + surface = surf; 13.111 + } 13.112 + return true; 13.113 + } 13.114 + 13.115 + 13.116 +//////////////////////////////////////////////////////////////////////////////// 13.117 +void Julia::get_K(double & Re, double & Im) 13.118 + { 13.119 + Re = K_Re; 13.120 + Im = K_Im; 13.121 + } 13.122 + 13.123 + 13.124 +//////////////////////////////////////////////////////////////////////////////// 13.125 +void Julia::set_K(double Re, double Im) 13.126 + { 13.127 + K_Re = Re; 13.128 + K_Im = Im; 13.129 + set_name(); 13.130 + } 13.131 + 13.132 + 13.133 +//////////////////////////////////////////////////////////////////////////////// 13.134 + 13.135 +void Julia::draw_pixel(int x, int y, Uint32 * color) 13.136 + { 13.137 + Fractal::draw_pixel(x, y, color); 13.138 + } 13.139 + 13.140 + 13.141 +//////////////////////////////////////////////////////////////////////////////// 13.142 +void Julia::set_name() 13.143 + { 13.144 + char tmp[100]; 13.145 + sprintf(tmp, "Julia set K = (%f,%f)", K_Re, K_Im); 13.146 + the_name.assign(tmp); 13.147 + }
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/src/Mandelbrot.cpp Tue Oct 19 03:37:31 2010 -0500 14.3 @@ -0,0 +1,115 @@ 14.4 +//////////////////////////////////////////////////////////////////////////////// 14.5 +// 14.6 +// (C) 2010 Sarah Eris Horsley Caffee 14.7 +// 14.8 +// mandelbrot - A simple Mandelbrot Set viewer. 14.9 +// 14.10 +// Mandelbrot.cpp 14.11 +// Mandelbrot set calculator class implementation 14.12 +// 14.13 +//////////////////////////////////////////////////////////////////////////////// 14.14 + 14.15 +#include <iostream> 14.16 +#include <string> 14.17 + 14.18 +#include "Mandelbrot.h" 14.19 +#include "Options.h" 14.20 + 14.21 +//////////////////////////////////////////////////////////////////////////////// 14.22 + 14.23 +Mandelbrot::Mandelbrot() 14.24 + { 14.25 + the_name.assign("Mandelbrot set"); 14.26 + Re_min = -2.5; 14.27 + Im_max = 2.5; 14.28 + size = 5.0; 14.29 + max_iter = 50; 14.30 + } 14.31 + 14.32 + 14.33 +//////////////////////////////////////////////////////////////////////////////// 14.34 +bool Mandelbrot::init(SDL_Surface * surf) 14.35 + { 14.36 + if (!surf) 14.37 + { 14.38 + return false; 14.39 + } 14.40 + else 14.41 + { 14.42 + surface = surf; 14.43 + } 14.44 + return true; 14.45 + } 14.46 + 14.47 + 14.48 +//////////////////////////////////////////////////////////////////////////////// 14.49 + 14.50 +bool Mandelbrot::calc_set() 14.51 + { 14.52 + if (!surface) return false; 14.53 + 14.54 + // Code from http://warp.povusers.org/Mandelbrot/ 14.55 + 14.56 + double Re_scale = (size)/(Options::width-1); 14.57 + double Im_scale = (size)/(Options::height-1); 14.58 + 14.59 + int r, g, b; 14.60 + 14.61 + Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0); 14.62 + Uint32 color; 14.63 + int halfway = max_iter/2 - 1; 14.64 + float step = (float) 255 / halfway; 14.65 + 14.66 + unsigned x, y, n; 14.67 + 14.68 + for(y = 0; y < Options::height; ++y) 14.69 + { 14.70 + double c_im = Im_max - y * Im_scale; 14.71 + for(x = 0; x < Options::width; ++x) 14.72 + { 14.73 + double c_re = Re_min + x * Re_scale; 14.74 + 14.75 + double Z_re = c_re, Z_im = c_im; 14.76 + bool in_set = true; 14.77 + for(n=0; n < max_iter; ++n) 14.78 + { 14.79 + double Z_re2 = Z_re * Z_re, Z_im2 = Z_im * Z_im; 14.80 + if(Z_re2 + Z_im2 > 4) 14.81 + { 14.82 + in_set = false; 14.83 + break; 14.84 + } 14.85 + Z_im = 2 * Z_re * Z_im + c_im; 14.86 + Z_re = Z_re2 - Z_im2 + c_re; 14.87 + } 14.88 + if(in_set) 14.89 + { 14.90 + draw_pixel(x, y, &black); 14.91 + } 14.92 + else 14.93 + { 14.94 + if (n <= halfway) 14.95 + { 14.96 + b = n * step; 14.97 + r = g = 0; 14.98 + } 14.99 + else 14.100 + { 14.101 + b = 255; 14.102 + r = g = (n - halfway) * step; 14.103 + } 14.104 + color = SDL_MapRGB(surface->format, r, g, b); 14.105 + draw_pixel(x, y, &color); 14.106 + } 14.107 + } 14.108 + } 14.109 + return true; 14.110 + } 14.111 + 14.112 + 14.113 +//////////////////////////////////////////////////////////////////////////////// 14.114 + 14.115 +void Mandelbrot::draw_pixel(int x, int y, Uint32 * color) 14.116 + { 14.117 + Fractal::draw_pixel(x, y, color); 14.118 + }
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/src/Options.cpp Tue Oct 19 03:37:31 2010 -0500 15.3 @@ -0,0 +1,16 @@ 15.4 +//////////////////////////////////////////////////////////////////////////////// 15.5 +// 15.6 +// (C) 2010 Sarah Eris Horsley Caffee 15.7 +// 15.8 +// mandelbrot - A simple Mandelbrot Set viewer. 15.9 +// 15.10 +// options.cpp 15.11 +// global variables and definitions 15.12 +// 15.13 +//////////////////////////////////////////////////////////////////////////////// 15.14 + 15.15 +#include "Options.h" 15.16 + 15.17 +int Options::width = 900; 15.18 +int Options::height = 900; 15.19 +int Options::bpp = 32;
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/src/main.cpp Tue Oct 19 03:37:31 2010 -0500 16.3 @@ -0,0 +1,39 @@ 16.4 +//////////////////////////////////////////////////////////////////////////////// 16.5 +// 16.6 +// (C) 2010 Sarah Eris Horsley Caffee 16.7 +// 16.8 +// mandelbrot 16.9 +// 16.10 +// A simple Mandelbrot Set viewer. Written in honor of Benoit Mandelbrot, 16.11 +// who died two days ago on Oct 14, 2010. 16.12 +// 16.13 +// main.cpp 16.14 +// 16.15 +//////////////////////////////////////////////////////////////////////////////// 16.16 + 16.17 +#include <exception> 16.18 +#include <iostream> 16.19 + 16.20 +#include "App.h" 16.21 + 16.22 +//////////////////////////////////////////////////////////////////////////////// 16.23 + 16.24 +int main (int argc, char* argv[]) 16.25 + { 16.26 + int retval = 0; 16.27 + 16.28 + try 16.29 + { 16.30 + App the_app; 16.31 + retval = the_app.run(); 16.32 + } 16.33 + catch (const std::exception & error) 16.34 + { 16.35 + std::string e = "An unanticipated error has occurred. Please contacct the developers and report the following information:\nCaught exception: "; 16.36 + e += error.what(); 16.37 + std::cerr << e << std::endl; 16.38 + retval = 1; 16.39 + } 16.40 + 16.41 + return retval;; 16.42 + }