Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct sdl_error : std::exception
- {
- const char* what() const throw()
- {
- return "SDL subsystem error.";
- }
- };
- class window
- {
- public:
- window( const char* title = NULL
- , const int window_pos_x = SDL_WINDOWPOS_CENTERED
- , const int window_pos_y = SDL_WINDOWPOS_CENTERED
- , const int window_width = 640
- , const int window_height = 480
- , const boost::uint32_t window_flags = SDL_WINDOW_SHOWN
- , const int renderer_index = -1
- , const boost::uint32_t renderer_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
- )
- : _error( false )
- {
- window::init();
- _window = window_ptr_t( SDL_CreateWindow( title
- , window_pos_x
- , window_pos_y
- , window_width
- , window_height
- , window_flags
- )
- , SDL_DestroyWindow
- );
- if( _window == NULL )
- {
- _error = true;
- return;
- }
- _renderer = renderer_ptr_t( SDL_CreateRenderer( _window.get()
- , renderer_index
- , renderer_flags
- )
- , SDL_DestroyRenderer
- );
- if( _renderer == NULL )
- {
- _error = true;
- return;
- }
- _surface = surface_ptr_t( SDL_CreateRGBSurface( 0 // obsolete
- , window_width
- , window_height
- , 32
- , 0, 0, 0, 0
- )
- , SDL_FreeSurface
- );
- if( _surface == NULL )
- {
- _error = true;
- return;
- }
- }
- void draw()
- {
- if( _error )
- {
- throw sdl_error();
- }
- texture_ptr_t texture = texture_ptr_t( SDL_CreateTextureFromSurface( _renderer.get()
- , _surface.get()
- )
- , SDL_DestroyTexture
- );
- if( texture == NULL )
- {
- throw sdl_error();
- }
- SDL_RenderCopy( _renderer.get()
- , texture.get()
- , NULL
- , NULL
- );
- boost::this_thread::sleep( boost::posix_time::milliseconds( 2000 ));
- }
- boost::gil::bgra8_view_t wrap_sdl_image( SDL_Surface* screen )
- {
- return interleaved_view( screen->w
- , screen->h
- , (bgra8_pixel_t*) screen->pixels
- , screen->pitch );
- }
- private:
- static void init()
- {
- static boost::once_flag flag = BOOST_ONCE_INIT;
- boost::call_once( flag, [] () { SDL_Init( SDL_INIT_EVERYTHING ); } );
- }
- static void quit()
- {
- static boost::once_flag flag = BOOST_ONCE_INIT;
- boost::call_once( flag, [] () { SDL_Quit(); } );
- }
- private:
- typedef SDL_Window window_t;
- typedef boost::shared_ptr< window_t > window_ptr_t;
- typedef SDL_Renderer renderer_t;
- typedef boost::shared_ptr< renderer_t > renderer_ptr_t;
- typedef SDL_Surface surface_t;
- typedef boost::shared_ptr< surface_t > surface_ptr_t;
- typedef SDL_Texture texture_t;
- typedef boost::shared_ptr< texture_t > texture_ptr_t;
- window_ptr_t _window;
- renderer_ptr_t _renderer;
- surface_ptr_t _surface;
- bool _error;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement