changeset 16:bdfe3327f4ce tip

Saving progress
author Eris Caffee <discordia@eldalin.com>
date Wed, 24 Nov 2010 21:34:11 -0600
parents 3cf740339643
children
files CMakeLists.txt include/App.h include/EventHandler.h src/App.cpp
diffstat 4 files changed, 191 insertions(+), 100 deletions(-) [+]
line diff
     1.1 --- a/CMakeLists.txt	Sat Nov 20 02:12:01 2010 -0600
     1.2 +++ b/CMakeLists.txt	Wed Nov 24 21:34:11 2010 -0600
     1.3 @@ -78,6 +78,18 @@
     1.4  
     1.5  
     1.6  ################################################################################
     1.7 +# Special options
     1.8 +
     1.9 +# Force SDL 1.3 only
    1.10 +if (WIN32)
    1.11 +  set (ENV{SDLDIR} "c:/gamedev/deps/sdl/SDL-build")
    1.12 +else ()
    1.13 +  set (ENV{SDLDIR} "/home/eris/gamedev/deps/sdl/SDL-build")
    1.14 +endif ()
    1.15 +add_definitions(-DSDL_NO_COMPAT)
    1.16 +
    1.17 +
    1.18 +################################################################################
    1.19  # Ensure that we are not building in our source directories.
    1.20  
    1.21  set (Build_Dir_OK "TRUE")
    1.22 @@ -108,7 +120,6 @@
    1.23  endif ()
    1.24  
    1.25  
    1.26 -
    1.27  ################################################################################
    1.28  # When using GCC turn on lots of warnings.
    1.29  
    1.30 @@ -177,10 +188,6 @@
    1.31  # CMake, then uncomment the if block below and put in the actual
    1.32  # locations of your SDL installation.
    1.33  
    1.34 -if (WIN32)
    1.35 -  set (ENV{SDLDIR} "c:/gamedev/deps/sdl/SDL-1.2.14")
    1.36 -endif ()
    1.37 -
    1.38  option(Option_SDL_Dev "Build an SDL application." ON)
    1.39  
    1.40  if (Option_SDL_Dev)
     2.1 --- a/include/App.h	Sat Nov 20 02:12:01 2010 -0600
     2.2 +++ b/include/App.h	Wed Nov 24 21:34:11 2010 -0600
     2.3 @@ -71,16 +71,18 @@
     2.4  
     2.5  
     2.6     // utility functions
     2.7 +   bool create_new_surface(SDL_Surface * &surface);
     2.8 +   bool init_sdl();
     2.9     int mouse_move_width(int mx, int my);
    2.10 -   bool create_new_surface(SDL_Surface * &surface);
    2.11 +   void set_caption();
    2.12 +   static Uint32 timer_callback(Uint32 interval, void *param);
    2.13 +
    2.14     void set_julia_k(int x, int y);
    2.15 +
    2.16     void restore_view();
    2.17     void save_view();
    2.18 -   void set_caption();
    2.19 +
    2.20     bool save_image();
    2.21 -
    2.22 -   static Uint32 timer_callback(Uint32 interval, void *param);
    2.23 -
    2.24     int get_next_file_num();
    2.25  
    2.26  #if !defined(WIN32)
    2.27 @@ -92,7 +94,7 @@
    2.28     bool running;
    2.29     bool redraw;
    2.30  
    2.31 -   SDL_Surface * surf_display;
    2.32 +   SDL_Window * window;
    2.33     SDL_Surface * surf_selection;
    2.34     SDL_Surface * surf_fractal;
    2.35  
     3.1 --- a/include/EventHandler.h	Sat Nov 20 02:12:01 2010 -0600
     3.2 +++ b/include/EventHandler.h	Wed Nov 24 21:34:11 2010 -0600
     3.3 @@ -31,6 +31,11 @@
     3.4  #ifndef EVENTHANDLER_H
     3.5  #define EVENTHANDLER_H
     3.6  
     3.7 +// Use SDL 1.3 only!
     3.8 +#ifndef SDL_NO_COMPAT
     3.9 +#define SDL_NO_COMPAT
    3.10 +#endif
    3.11 +
    3.12  #include "SDL.h"
    3.13  
    3.14  class EventHandler
    3.15 @@ -42,18 +47,27 @@
    3.16  
    3.17     virtual void on_Event(SDL_Event * event);
    3.18  
    3.19 -   // Active events
    3.20 +   // SDL_WindowEvent subtypes
    3.21 +   virtual void on_Show();
    3.22 +   virtual void on_Hide();
    3.23 +   virtual void on_Expose();
    3.24 +   virtual void on_Move(SDL_WindowEvent window);
    3.25 +   virtual void on_Resize(SDL_WindowEvent window);
    3.26 +   virtual void on_Minimize();
    3.27 +   virtual void on_Maximize();
    3.28 +   virtual void on_Restore();
    3.29     virtual void on_MouseFocus();
    3.30     virtual void on_MouseBlur();
    3.31 -   virtual void on_InputFocus();
    3.32 -   virtual void on_InputBlur();
    3.33 -   virtual void on_Minimize();
    3.34 -   virtual void on_Restore();
    3.35 -
    3.36 +   virtual void on_KeyboardFocus();
    3.37 +   virtual void on_KeyboardBlur();
    3.38 +   // Note: on_Close() will call on_Quit() by default.
    3.39 +   virtual void on_Close();
    3.40  
    3.41     // Keyboard events
    3.42     virtual void on_KeyDown(SDL_KeyboardEvent key);
    3.43     virtual void on_KeyUp(SDL_KeyboardEvent key);
    3.44 +   virtual void on_TextInput(SDL_TextInputEvent text);
    3.45 +   virtual void on_TextEdit(SDL_TextEditingEvent edit);
    3.46  
    3.47     // Mouse events
    3.48     virtual void on_MouseMove(SDL_MouseMotionEvent motion);
    3.49 @@ -63,22 +77,21 @@
    3.50     virtual void on_RButtonUp(SDL_MouseButtonEvent button);
    3.51     virtual void on_MButtonDown(SDL_MouseButtonEvent button);
    3.52     virtual void on_MButtonUp(SDL_MouseButtonEvent button);
    3.53 -   virtual void on_MouseWheelDown(SDL_MouseButtonEvent motion);
    3.54 -   virtual void on_MouseWheelUp(SDL_MouseButtonEvent motion);
    3.55 +   virtual void on_MouseWheel(SDL_MouseWheelEvent wheel);
    3.56  
    3.57     // Joystick events
    3.58     virtual void on_JoyAxis(SDL_JoyAxisEvent jaxis);
    3.59 +   virtual void on_JoyBall(SDL_JoyBallEvent jball);
    3.60 +   virtual void on_JoyHat(SDL_JoyHatEvent jhat);
    3.61     virtual void on_JoyButtonDown(SDL_JoyButtonEvent jbutton);
    3.62     virtual void on_JoyButtonUp(SDL_JoyButtonEvent jbutton);
    3.63 -   virtual void on_JoyHat(SDL_JoyHatEvent jhat);
    3.64 -   virtual void on_JoyBall(SDL_JoyBallEvent jball);
    3.65  
    3.66     // Misc events
    3.67 -   virtual void on_Resize(SDL_ResizeEvent resize);
    3.68 -   virtual void on_Expose();
    3.69     virtual void on_Quit();
    3.70     virtual void on_User(SDL_UserEvent user);
    3.71 -   virtual void on_SysWM(SDL_SysWMEvent syswm);
    3.72 +   virtual void on_SysWMEvent(SDL_SysWMEvent syswm);
    3.73 +
    3.74 +   // There's still a lot of new stuff not yet implemented here.
    3.75  
    3.76     };
    3.77  
     4.1 --- a/src/App.cpp	Sat Nov 20 02:12:01 2010 -0600
     4.2 +++ b/src/App.cpp	Wed Nov 24 21:34:11 2010 -0600
     4.3 @@ -59,7 +59,7 @@
     4.4  App::App() :
     4.5     running (true),
     4.6     redraw (false),
     4.7 -   surf_display (NULL),
     4.8 +   window (NULL),
     4.9     surf_selection (NULL),
    4.10     surf_fractal (NULL),
    4.11     selection_x (-1),
    4.12 @@ -115,25 +115,11 @@
    4.13  
    4.14  bool App::init()
    4.15     {
    4.16 -   if ((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) == -1)
    4.17 +   if (!init_sdl())
    4.18        {
    4.19 -      std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl;
    4.20        return false;
    4.21        }
    4.22  
    4.23 -   if ((surf_display = SDL_SetVideoMode(Options::width, 
    4.24 -					Options::height, 
    4.25 -					Options::bpp, 
    4.26 -					SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
    4.27 -      {
    4.28 -      std::cerr << "SDL_SetVideoMode() failed: " 
    4.29 -		<< SDL_GetError() << std::endl;
    4.30 -      return false;
    4.31 -      }
    4.32 -
    4.33 -   SDL_EnableKeyRepeat(500, 100);
    4.34 -
    4.35 -
    4.36     if (!create_new_surface(surf_selection)) { return false; }
    4.37     if (!create_new_surface(surf_fractal))   { return false; }
    4.38  
    4.39 @@ -157,11 +143,8 @@
    4.40  
    4.41     can_set_julia_k = true;
    4.42  
    4.43 -
    4.44     set_caption();
    4.45  
    4.46 -   SDL_AddTimer(10, App::timer_callback, NULL);
    4.47 -
    4.48     return true;
    4.49     }
    4.50  
    4.51 @@ -180,8 +163,8 @@
    4.52     if (redraw)
    4.53        {
    4.54        // Start with a clean slate.
    4.55 -      SDL_FillRect(surf_display, &surf_display->clip_rect, 
    4.56 -		   SDL_MapRGB(surf_display->format, 0, 0, 255));
    4.57 +      SDL_SetRenderDrawColor(0, 0, 0, SDL_ALPHA_OPAQUE);
    4.58 +      SDL_RenderClear();
    4.59  
    4.60        fractal->calc_set();
    4.61        SDL_BlitSurface(surf_fractal, NULL, surf_display, NULL);
    4.62 @@ -193,7 +176,7 @@
    4.63  	 }
    4.64  
    4.65        // Show it to the user.
    4.66 -      SDL_Flip(surf_display);
    4.67 +      SDL_RenderPresent();
    4.68  
    4.69        redraw = false;
    4.70        }
    4.71 @@ -210,10 +193,10 @@
    4.72        SDL_FreeSurface(surf_selection);
    4.73        surf_selection = NULL;
    4.74        }
    4.75 -   if (surf_display)
    4.76 +   if (window)
    4.77        {
    4.78 -      SDL_FreeSurface(surf_display);
    4.79 -      surf_display = NULL;
    4.80 +      SDL_DestroyWindow(window);
    4.81 +      window = NULL;
    4.82        }
    4.83  
    4.84     SDL_Quit();
    4.85 @@ -457,28 +440,6 @@
    4.86  
    4.87  
    4.88  ////////////////////////////////////////////////////////////////////////////////
    4.89 -int App::mouse_move_width(int mx, int my)
    4.90 -   {
    4.91 -   int move_width = (mx - selection_x);
    4.92 -   if (mx > selection_x)
    4.93 -      {
    4.94 -      move_width *= -1;
    4.95 -      }
    4.96 -   
    4.97 -   int tmp_i = (my - selection_y);
    4.98 -   if (my < selection_y)
    4.99 -      {
   4.100 -      tmp_i *= -1;
   4.101 -      }
   4.102 -   
   4.103 -   if (tmp_i > move_width)
   4.104 -      move_width = tmp_i;
   4.105 -
   4.106 -   return move_width;
   4.107 -   }
   4.108 -
   4.109 -
   4.110 -////////////////////////////////////////////////////////////////////////////////
   4.111  bool App::create_new_surface(SDL_Surface * &surface)
   4.112     {
   4.113     if (surface)
   4.114 @@ -507,6 +468,143 @@
   4.115  
   4.116  
   4.117  ////////////////////////////////////////////////////////////////////////////////
   4.118 +bool init_sdl()
   4.119 +   {
   4.120 +   if ((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) == -1)
   4.121 +      {
   4.122 +      std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl;
   4.123 +      return false;
   4.124 +      }
   4.125 +
   4.126 +   if ((window = SDL_CreateWindow("SDL 1.3 test", 
   4.127 +				  SDL_WINDOWPOS_UNDEFINED, 
   4.128 +				  SDL_WINDOWPOS_UNDEFINED,
   4.129 +				  Options::width, Options::height,
   4.130 +				  0)) == 0)
   4.131 +      {
   4.132 +      std::cerr << "SDL_CreateWindow() failed: " 
   4.133 +   		<< SDL_GetError() << std::endl;
   4.134 +      return false;
   4.135 +      }
   4.136 +
   4.137 +   // Go through the available renderers and find the one we need.
   4.138 +   // Do not use the x11 renderer.  At least for now it is not working
   4.139 +   // correctly..
   4.140 +   int max_drivers = SDL_GetNumRenderDrivers();
   4.141 +   if (max_drivers < 1)
   4.142 +      {
   4.143 +      std::cerr << "SDL_GetNumRenderDrivers() failed: "
   4.144 +		<< SDL_GetError() << std::endl;
   4.145 +      return false;
   4.146 +      }
   4.147 +
   4.148 +   int driver = -1;
   4.149 +   for(int index = 0; index < max_drivers ; index++) 
   4.150 +      { 
   4.151 +      SDL_RendererInfo info; 
   4.152 +      if(SDL_GetRenderDriverInfo(index, &info) != -1)
   4.153 +	 { 
   4.154 +	 if (strncmp(info.name, "software", 8) == 0) driver = index;
   4.155 +	 std::cerr << "Driver " << info.name << " index: "
   4.156 +		   << index << std::endl; 
   4.157 +	 } 
   4.158 +      else
   4.159 +	 {
   4.160 +	 std::cerr << "SDL_GetRenderDriverInfo() for index " << index 
   4.161 +		   << " failed: " 
   4.162 +		   << SDL_GetError() << std::endl;
   4.163 +	 return false;
   4.164 +	 }
   4.165 +      } 
   4.166 +
   4.167 +   if (driver == -1)
   4.168 +      {
   4.169 +      std::cerr << "SDL software renderer not available!" << std::endl;
   4.170 +      return false;
   4.171 +      }
   4.172 +
   4.173 +   if (SDL_CreateRenderer(window, -1, 
   4.174 +			  SDL_RENDERER_PRESENTFLIP2 | 
   4.175 +			  SDL_RENDERER_ACCELERATED) != 0)
   4.176 +      {
   4.177 +      std::cerr << "SDL_CreateRenderer() failed: " 
   4.178 +   		<< SDL_GetError() << std::endl;
   4.179 +      return false;
   4.180 +      }
   4.181 +
   4.182 +   SDL_RendererInfo info;
   4.183 +   if (SDL_GetRendererInfo(&info) == -1)
   4.184 +      {
   4.185 +      std::cerr << "SDL_GetRendererInfo() failed: "
   4.186 +		<< SDL_GetError() << std::endl;
   4.187 +      return false;
   4.188 +      }
   4.189 +   std::cerr << "Renderer: " << info.name << std::endl;
   4.190 +
   4.191 +
   4.192 +   if (SDL_AddTimer(10, App::timer_callback, NULL) == NULL)
   4.193 +      {
   4.194 +      std::cerr << "SDL_AddTimer() failed: "
   4.195 +		<< SDL_GetError() << std::endl;
   4.196 +      return false;
   4.197 +      }
   4.198 +
   4.199 +   return true;
   4.200 +   }
   4.201 +
   4.202 +
   4.203 +////////////////////////////////////////////////////////////////////////////////
   4.204 +int App::mouse_move_width(int mx, int my)
   4.205 +   {
   4.206 +   int move_width = (mx - selection_x);
   4.207 +   if (mx > selection_x)
   4.208 +      {
   4.209 +      move_width *= -1;
   4.210 +      }
   4.211 +   
   4.212 +   int tmp_i = (my - selection_y);
   4.213 +   if (my < selection_y)
   4.214 +      {
   4.215 +      tmp_i *= -1;
   4.216 +      }
   4.217 +   
   4.218 +   if (tmp_i > move_width)
   4.219 +      move_width = tmp_i;
   4.220 +
   4.221 +   return move_width;
   4.222 +   }
   4.223 +
   4.224 +
   4.225 +////////////////////////////////////////////////////////////////////////////////
   4.226 +void App::set_caption()
   4.227 +   {
   4.228 +   std::stringstream ss;
   4.229 +   ss << fractal->get_display_name() << " / "
   4.230 +      << "Real: " << fractal->get_re_min()
   4.231 +       << " to "<< fractal->get_re_min() + fractal->get_size()
   4.232 +      << " / Imaginary: " << fractal->get_im_max() - fractal->get_size()
   4.233 +      << " to " << fractal->get_im_max()
   4.234 +      << " / Max iterations: " << fractal->get_max_iter();
   4.235 +   SDL_SetWindowTitel(window, ss.str().c_str());
   4.236 +   }
   4.237 +
   4.238 +
   4.239 +////////////////////////////////////////////////////////////////////////////////
   4.240 +Uint32 App::timer_callback(Uint32 interval, void *param)
   4.241 +   {
   4.242 +   SDL_Event e;
   4.243 + 
   4.244 +   e.type = SDL_USEREVENT;
   4.245 +   e.user.code = 0;
   4.246 +   e.user.data1 = NULL;
   4.247 +   e.user.data2 = NULL;
   4.248 +   SDL_PushEvent(& e);
   4.249 + 
   4.250 +   return interval;
   4.251 +   }
   4.252 +
   4.253 +
   4.254 +////////////////////////////////////////////////////////////////////////////////
   4.255  void App::set_julia_k(int x, int y)
   4.256     {
   4.257     FractalFactory * fractal_factory = get_Fractal_map()[typeid(Julia).name()];
   4.258 @@ -580,20 +678,6 @@
   4.259  
   4.260  
   4.261  ////////////////////////////////////////////////////////////////////////////////
   4.262 -void App::set_caption()
   4.263 -   {
   4.264 -   std::stringstream ss;
   4.265 -   ss << fractal->get_display_name() << " / "
   4.266 -      << "Real: " << fractal->get_re_min()
   4.267 -       << " to "<< fractal->get_re_min() + fractal->get_size()
   4.268 -      << " / Imaginary: " << fractal->get_im_max() - fractal->get_size()
   4.269 -      << " to " << fractal->get_im_max()
   4.270 -      << " / Max iterations: " << fractal->get_max_iter();
   4.271 -   SDL_WM_SetCaption(ss.str().c_str(), NULL);
   4.272 -   }
   4.273 -
   4.274 -
   4.275 -////////////////////////////////////////////////////////////////////////////////
   4.276  bool App::save_image()
   4.277     {
   4.278     int i = App::get_next_file_num();
   4.279 @@ -612,21 +696,6 @@
   4.280     }
   4.281  
   4.282  
   4.283 -////////////////////////////////////////////////////////////////////////////////
   4.284 -Uint32 App::timer_callback(Uint32 interval, void *param)
   4.285 -   {
   4.286 -   SDL_Event e;
   4.287 - 
   4.288 -   e.type = SDL_USEREVENT;
   4.289 -   e.user.code = 0;
   4.290 -   e.user.data1 = NULL;
   4.291 -   e.user.data2 = NULL;
   4.292 -   SDL_PushEvent(& e);
   4.293 - 
   4.294 -   return interval;
   4.295 -   }
   4.296 -
   4.297 -
   4.298  #if defined(WIN32)
   4.299  ////////////////////////////////////////////////////////////////////////////////
   4.300  //Code for Windows