Skip to content

Commit

Permalink
close all windows if main window closed
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaclarty committed Mar 6, 2016
1 parent 079e4dd commit 0b03959
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
25 changes: 25 additions & 0 deletions examples/multi_win.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local window1 = am.window{title="Window 1"}
local window2 = am.window{title="Window 2"}
window1.scene = am.rotate(0) ^ am.scale(4) ^ am.text("Window 1", vec4(1, 0, 0.5, 1))
window2.scene = am.rotate(0) ^ am.scale(4) ^ am.text("Window 2", vec4(0, 1, 1, 1))

window1.scene:action(function(node)
node.angle = am.frame_time * math.pi * 2
if window1:key_pressed"1" then
window1:close()
end
if window1:mouse_pressed"left" then
window1.clear_color = math.randvec4()
end
end)
local t = 0
window2.scene:action(function(node)
t = t + am.delta_time
node.angle = t * math.pi * 2
if window2:key_pressed"2" then
window2:close()
end
if window2:mouse_pressed"left" then
window2.clear_color = math.randvec4()
end
end)
12 changes: 7 additions & 5 deletions src/am_backend_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,14 @@ void am_set_native_window_lock_pointer(am_native_window *window, bool enabled) {
}

void am_destroy_native_window(am_native_window* window) {
for (unsigned int i = 0; i < windows.size(); i++) {
if (windows[i].window == (SDL_Window*)window) {
SDL_Window *sdl_win = (SDL_Window*)window;
for (int i = windows.size() - 1; i >= 0; i--) {
// if main window closed, close all windows.
if (windows[i].window == sdl_win || main_window == sdl_win) {
if (windows[i].window != main_window) {
SDL_DestroyWindow((SDL_Window*)window);
}
windows.erase(windows.begin() + i);
break;
}
}
}
Expand Down Expand Up @@ -525,17 +526,18 @@ int main( int argc, char *argv[] )
SDL_CloseAudioDevice(capture_device);
}
}
// destroy lua state before window, so gl context not
// destroy lua state before main window, so gl context not
// destroyed before textures and vbos deleted.
if (eng != NULL) am_destroy_engine(eng);
for (unsigned int i = 0; i < windows.size(); i++) {
// XXX why not destroy main window here?
// destroy main window last, because it owns the gl context.
if (windows[i].window != main_window) {
SDL_DestroyWindow(windows[i].window);
}
}
if (main_window != NULL) {
SDL_DestroyWindow(main_window);
main_window = NULL;
}
if (sdl_initialized) {
SDL_Quit();
Expand Down

0 comments on commit 0b03959

Please sign in to comment.