# Movies on Multiwindow

**URL:** <https://discourse.libcinder.org/t/movies-on-multiwindow/806>\
**Category:** Using Cinder\
**Created:** [July 26, 2017, 10:16pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806 "2017-07-26T22:16:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![t\_k](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@t\_k](https://discourse.libcinder.org/u/t_k)\
**Post date:** [July 26, 2017, 10:16pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806/1 "2017-07-26T22:16:26Z")

</div>

I’d like to simply play back different movies on different windows but it doesn’t draw well and play them back strangely. I’m using Cinder 0.9.1:

```
void testApp::setup() {
  /* ..load, create and play MovieGl.. */

  win1 = app::getWindow();
  win1->getSignalDraw().connect( [&]{ drawWin1(); } );

  win2 = createWindow(Window::Format().size(ivec2(1280,720)));
  win2->getSignalDraw().connect( [&]{ drawWin2(); } );
}

void testApp::update() {
  tex1 = mov1->getTexture();
  tex2 = mov2->getTexture();
}

void testApp::drawWin1() {
  gl::clear();
  if (tex1) gl::draw(tex1);
}

void testApp::drawWin2() {
  gl::clear();
  if (tex2) gl::draw(tex2);
}

```

Any help appreciated.  
Thanks

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 27, 2017, 2:12pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806/2 "2017-07-27T14:12:51Z")

</div>

Hi,

each window has its own OpenGL context (see: [State Machine](https://learnopengl.com/#!Getting-started/OpenGL)). A context can be seen as a separate “copy” of OpenGL, each with its own textures, shaders, etc. While textures can be shared between contexts (so all contexts can use the texture), this is not done automatically and requires careful setup.

In your application, the textures for `mov1` and `mov2` are both created on the main context (created for the default application window, `win1`). The 2nd window, `win2` has its own context and will probably not display `mov2`'s texture properly.

To fix this, try creating `mov2` with the correct context enabled (see: `Window::getRenderer()` and `RendererGl::makeCurrentContext(true)`, but there are more ways that lead to Rome), then carefully use the correct context when updating the movie / getting the texture. You may want to encapsulate a window and movie player into a custom class, so you can more easily control the pair of them.

-Paul

Edit: another approach could be to create a shared context based on the main context, then use it when creating the 2nd window. That way, both windows will be able to access the movie textures.

---

<div class="post-metadata">

**Author:** ![t\_k](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@t\_k](https://discourse.libcinder.org/u/t_k)\
**Post date:** [July 27, 2017, 6:00pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806/3 "2017-07-27T18:00:39Z")

</div>

Thank you for your feedback, I’ve changed setup and update as below. Now second window works correctly but it doesn’t draw movie’s texture well on first window. I might not understand well on context on window?

```
void testApp::setup() {
    win1 = app::getWindow();
    win1->getRenderer()->makeCurrentContext(true);
    mov1 = qtime::MovieGl::create(loadResource("mov1.mov")->getFilePath());
    mov1->play();
    win1->getSignalDraw().connect( [&]{ drawWin1(); } );

    win2 = createWindow( Window::Format().size( ivec2(1280, 720) ) );
    win2->getRenderer()->makeCurrentContext(true);
    mov2 = qtime::MovieGl::create(loadResource("mov2.mov")->getFilePath());
    mov2->play();
    win2->getSignalDraw().connect( [&]{ drawWin2(); } );
}

void testApp::update() {
    win1->getRenderer()->makeCurrentContext(true);
    if(mov1) tex1 = mov1->getTexture();

    win2->getRenderer()->makeCurrentContext(true);
    if(mov2) tex2 = mov2->getTexture();
}

```

Thanks again!

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 27, 2017, 6:30pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806/4 "2017-07-27T18:30:46Z")

</div>

Hmm, code follows my recommendations perfectly. I thought it would work, but maybe something deep inside the bowels of Cinder prevents your code from working.

Maybe try the other approach: create a 2nd window that already uses a shared context (as in: shared with the main context). That way you don’t have to worry about which context is active.

-Paul

---

<div class="post-metadata">

**Author:** ![t\_k](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@t\_k](https://discourse.libcinder.org/u/t_k)\
**Post date:** [July 27, 2017, 9:24pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806/5 "2017-07-27T21:24:23Z")

</div>

Thank you, Paul. I now changed it to your 2nd recommendation as below. But unfortunately it doesn’t work. Am I misunderstanding what you wrote maybe?

```
void testApp::createMvCtx(gl::ContextRef context, qtime::MovieGlRef &m1, qtime::MovieGlRef &m2, gl::TextureRef &t1, gl::TextureRef &t2) {
    ThreadSetup thSetup;
    context->makeCurrent();
    
    m1 = qtime::MovieGl::create( loadResource("mov1.mov")->getFilePath() );
    m1->play();
    
    m2 = qtime::MovieGl::create( loadResource("mov2.mov")->getFilePath() );
    m2->play();
}

void testApp::setup() {
    gl::ContextRef ctx = gl::Context::create(gl::Context::getCurrent());
    mTh = shared_ptr<thread>(new thread(bind(&testApp::createMvCtx, this, ctx, ref(mov1), ref(mov2), ref(tex1), ref(tex2))));
    
    win1 = app::getWindow();
    win1->getSignalDraw().connect( [&]{ drawWin1(); } );
    
    win2 = createWindow( Window::Format().size( ivec2(1280, 720) ) );
    win2->getSignalDraw().connect( [&]{ drawWin2(); } );
}

void testApp::update() {
    if(mov1) tex1 = mov1->getTexture();
    if(mov2) tex2 = mov2->getTexture();
}

```

Thanks again!!

---

<div class="post-metadata">

**Author:** ![t\_k](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@t\_k](https://discourse.libcinder.org/u/t_k)\
**Post date:** [July 27, 2017, 9:50pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806/6 "2017-07-27T21:50:09Z")

</div>

It worked when texture is gotten in each signal draw, not in main update. So is this the way following yours? Thanks.

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 27, 2017, 10:41pm UTC](https://discourse.libcinder.org/t/movies-on-multiwindow/806/7 "2017-07-27T22:41:04Z")

</div>

Hi,

I’m sorry I’m not in a position to give a code example at the moment. Your 2nd attempt isn’t exactly what I had in mind. I meant for you to create a second, shared context (like what you do in `setup()`), but use it to create the second window. Either that or assign the shared context to the second window after creating it. That way, both windows will share their textures with each other. And since the movie textures will be created on the main context and it is shared with the second window’s context, it does not matter in which window you draw the texture.

Does that help?
