Advertisement
Guest User

Untitled

a guest
Mar 6th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. struct sdl_error : std::exception
  2. {
  3.     const char* what() const throw()
  4.     {
  5.         return "SDL subsystem error.";
  6.     }
  7. };
  8.  
  9. class window
  10. {
  11. public:
  12.  
  13.     window( const char*           title = NULL
  14.           , const int             window_pos_x = SDL_WINDOWPOS_CENTERED
  15.           , const int             window_pos_y = SDL_WINDOWPOS_CENTERED
  16.           , const int             window_width = 640
  17.           , const int             window_height = 480
  18.           , const boost::uint32_t window_flags = SDL_WINDOW_SHOWN
  19.           , const int             renderer_index = -1
  20.           , const boost::uint32_t renderer_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
  21.           )
  22.     : _error( false )
  23.     {
  24.         window::init();
  25.  
  26.         _window = window_ptr_t( SDL_CreateWindow( title
  27.                                                 , window_pos_x
  28.                                                 , window_pos_y
  29.                                                 , window_width
  30.                                                 , window_height
  31.                                                 , window_flags
  32.                                                 )
  33.  
  34.                               , SDL_DestroyWindow
  35.                               );
  36.  
  37.         if( _window == NULL )
  38.         {
  39.             _error = true;
  40.             return;
  41.         }
  42.  
  43.         _renderer = renderer_ptr_t( SDL_CreateRenderer( _window.get()
  44.                                                       , renderer_index
  45.                                                       , renderer_flags
  46.                                                       )
  47.                                   , SDL_DestroyRenderer
  48.                                   );
  49.  
  50.  
  51.         if( _renderer == NULL )
  52.         {
  53.             _error = true;
  54.             return;
  55.         }
  56.  
  57.         _surface = surface_ptr_t( SDL_CreateRGBSurface( 0 // obsolete
  58.                                                       , window_width
  59.                                                       , window_height
  60.                                                       , 32
  61.                                                       , 0, 0, 0, 0
  62.                                                       )
  63.                                 , SDL_FreeSurface
  64.                                 );
  65.  
  66.         if( _surface == NULL )
  67.         {
  68.             _error = true;
  69.             return;
  70.         }
  71.     }
  72.  
  73.     void draw()
  74.     {
  75.         if( _error )
  76.         {
  77.             throw sdl_error();
  78.         }
  79.  
  80.         texture_ptr_t texture = texture_ptr_t( SDL_CreateTextureFromSurface( _renderer.get()
  81.                                                                            , _surface.get()
  82.                                                                            )
  83.                                              , SDL_DestroyTexture
  84.                                              );
  85.  
  86.         if( texture == NULL )
  87.         {
  88.             throw sdl_error();
  89.         }
  90.        
  91.         SDL_RenderCopy( _renderer.get()
  92.                       , texture.get()
  93.                       , NULL
  94.                       , NULL
  95.                       );
  96.  
  97.         boost::this_thread::sleep( boost::posix_time::milliseconds( 2000 ));
  98.     }
  99.  
  100.     boost::gil::bgra8_view_t wrap_sdl_image( SDL_Surface* screen )
  101.     {
  102.        return interleaved_view( screen->w
  103.                               , screen->h
  104.                               , (bgra8_pixel_t*) screen->pixels
  105.                               , screen->pitch   );
  106.     }
  107.  
  108. private:
  109.  
  110.     static void init()
  111.     {
  112.         static boost::once_flag flag = BOOST_ONCE_INIT;
  113.         boost::call_once( flag, [] () { SDL_Init( SDL_INIT_EVERYTHING ); } );
  114.     }
  115.  
  116.     static void quit()
  117.     {
  118.         static boost::once_flag flag = BOOST_ONCE_INIT;
  119.         boost::call_once( flag, [] () { SDL_Quit(); } );
  120.     }
  121.  
  122. private:
  123.     typedef SDL_Window window_t;
  124.     typedef boost::shared_ptr< window_t > window_ptr_t;
  125.  
  126.     typedef SDL_Renderer renderer_t;
  127.     typedef boost::shared_ptr< renderer_t > renderer_ptr_t;
  128.  
  129.     typedef SDL_Surface surface_t;
  130.     typedef boost::shared_ptr< surface_t > surface_ptr_t;
  131.  
  132.     typedef SDL_Texture texture_t;
  133.     typedef boost::shared_ptr< texture_t > texture_ptr_t;
  134.  
  135.     window_ptr_t   _window;
  136.     renderer_ptr_t _renderer;
  137.     surface_ptr_t  _surface;
  138.  
  139.     bool _error;
  140. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement