c++ - Creating two sdl window using vector emplace creates only one window -


wrapping sdl window in class , creating vector. emplacing 2 window objects creates 1 window, though 2 expected.

#include <sdl.h> #include <vector> class window {     sdl_window*     m_window;     sdl_renderer*   m_renderer;     int m_windowid; public:     window( int w, int h, uint32 mode = sdl_window_shown )     {          m_window = sdl_createwindow( "", sdl_windowpos_undefined, sdl_windowpos_undefined, w, h, mode );         m_renderer = sdl_createrenderer( m_window , -1, sdl_renderer_presentvsync );         m_windowid = sdl_getwindowid(m_window);     }     ~window()     {         sdl_destroyrenderer(m_renderer);         sdl_destroywindow( m_window);     }     void render()     {         sdl_setrenderdrawcolor(m_renderer, 0xff, 0xff, 0xff, 0xff);         sdl_renderclear(m_renderer);         sdl_renderpresent(m_renderer);     }     void settitle(const std::string& title)     {         sdl_setwindowtitle(m_window, title.c_str());     } }; int main( int argc, char* argv[] ) {     std::vector<window> window;     window.emplace_back(/*width*/600,/*height*/480);     window.emplace_back(/*width*/600,/*height*/480);     window[0].settitle(/*title*/"window-1");     window[1].settitle(/*title*/"window-2");     sdl_event event;     bool quit = false;     while( !quit )     {         while( sdl_pollevent( &event ) )         {             if( event.type == sdl_quit)             {                 quit = true;             }         }         for(auto& w: window)         {             w.render();         }     }     return 0; } 

sdl_createwindow being called twice yet 1 window being created, why?

update: setting title of window reveals last created window remains.

you're violating rule of three/five.

after push second window, vector reallocated, , first window "copied". new copy preserved, destructor of old 1 called. new copy points destroyed window. (also, window destroyed second time @ end of program, causes undefined behaviour)

this wouldn't have happened if used std::unique_ptr appropriate deleter.

#include <sdl2/sdl.h> #include <vector> #include <memory>  struct sdlwindowdestroyer {     void operator()(sdl_window* w) const     {         sdl_destroywindow(w);     } };  struct sdlrendererdestroyer {     void operator()(sdl_renderer* r) const     {         sdl_destroyrenderer(r);     } };  class window {     std::unique_ptr<sdl_window, sdlwindowdestroyer>     m_window;     std::unique_ptr<sdl_renderer, sdlrendererdestroyer>   m_renderer;     int m_windowid; public:     window( int w, int h, const char* name, uint32 mode = sdl_window_shown ) :         m_window(sdl_createwindow( name, sdl_windowpos_undefined, sdl_windowpos_undefined, w, h, mode )),         m_renderer(sdl_createrenderer( m_window.get() , -1, sdl_renderer_presentvsync )),         m_windowid(sdl_getwindowid(m_window.get()))     {      }      void render()     {         sdl_setrenderdrawcolor(m_renderer.get(), 0xff, 0xff, 0xff, 0xff);         sdl_renderclear(m_renderer.get());         sdl_renderpresent(m_renderer.get());     } }; 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -