diff --git a/MainWindow.cpp b/MainWindow.cpp index 927f84b4fd2d4271bfed90cd532123279ff2b376..6b670263fea3c992b2f427c6ab8ab6397c72586b 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -4,22 +4,9 @@ std::shared_ptr<MainWindow> MainWindow::self = nullptr; Uint32 MainWindow::wakeup_on_mpv_render_update = 0; Uint32 MainWindow::wakeup_on_mpv_events = 0; - int __one[] = {1}; - mpv_opengl_init_params mpv_gl_init_params = { - .get_proc_address = [] (void *, const char *name) -> void * { return SDL_GL_GetProcAddress(name); }, - }; - - mpv_render_param params[] = { - {MPV_RENDER_PARAM_API_TYPE, (void *) MPV_RENDER_API_TYPE_OPENGL}, - {MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &mpv_gl_init_params}, - {MPV_RENDER_PARAM_ADVANCED_CONTROL, &__one}, /* Async commands only */ - {(mpv_render_param_type)0} - }; - - MainWindow::MainWindow(void) : show_demo_window(true), show_another_window(false), clear_color(0.45f, 0.55f, 0.60f, 1.00f), - mpv_gl(nullptr), mpv(mpv_create()) + mpv_gl(nullptr), mpv(mpv_create()), done(false) { if (!mpv) throw 1; /* TODO */ @@ -60,6 +47,18 @@ MainWindow::MainWindow(void) : throw 1; } + int __one[] = {1}; + mpv_opengl_init_params mpv_gl_init_params = { + .get_proc_address = [] (void *, const char *name) -> void * { return SDL_GL_GetProcAddress(name); }, + }; + + mpv_render_param params[] = { + {MPV_RENDER_PARAM_API_TYPE, (void *) MPV_RENDER_API_TYPE_OPENGL}, + {MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &mpv_gl_init_params}, + {MPV_RENDER_PARAM_ADVANCED_CONTROL, &__one}, /* Async commands only */ + {(mpv_render_param_type)0} + }; + if (mpv_render_context_create(&mpv_gl, mpv, params) < 0) /* Resolv already created context */ throw 1; @@ -84,6 +83,7 @@ MainWindow::MainWindow(void) : io->ConfigFlags |= ImGuiConfigFlags_DockingEnable; io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + ImGui::StyleColorsDark(); ImGui_ImplSDL2_InitForOpenGL(window, gl_context); ImGui_ImplOpenGL3_Init(glsl_version); @@ -122,139 +122,154 @@ MainWindow::DeInit(void) void MainWindow::Main(void) { - bool done = false; while (!done) { - // Poll and handle events (inputs, window resize, etc.) - // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. - // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. - // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. - // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. - SDL_Event event; - while (SDL_PollEvent(&event)) + HandleEvents(); + HandleLayers(); + HandleRender(); + } + + DeInit(); +} + +void +MainWindow::HandleRender(void) +{ + // Rendering + ImGui::Render(); + + /* Always redraw */ + int w, h; + int __one__one = 1; + SDL_GetWindowSize(window, &w, &h); + mpv_opengl_fbo fbo = { + .fbo = 0, + .w = w, + .h = h, + }; + mpv_render_param params[] = { + {MPV_RENDER_PARAM_OPENGL_FBO, &fbo}, + {MPV_RENDER_PARAM_FLIP_Y, &__one__one}, + {(mpv_render_param_type)0} + }; + // mpv_render_context_render(mpv_gl, params); + + SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow(); + SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext(); + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + SDL_GL_MakeCurrent(backup_current_window, backup_current_context); + mpv_render_context_render(mpv_gl, params); + + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + SDL_GL_SwapWindow(window); +} + +void +MainWindow::HandleEvents(void) +{ + // Poll and handle events (inputs, window resize, etc.) + // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. + // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. + // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. + // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. + SDL_Event event; + while (SDL_PollEvent(&event)) + { + ImGui_ImplSDL2_ProcessEvent(&event); + if (event.type == SDL_QUIT) + done = true; + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) + done = true; + if (event.type == SDL_KEYDOWN) { - ImGui_ImplSDL2_ProcessEvent(&event); - if (event.type == SDL_QUIT) - done = true; - if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) - done = true; - if (event.type == SDL_KEYDOWN) + if (event.key.keysym.sym == SDLK_SPACE) { - if (event.key.keysym.sym == SDLK_SPACE) - { - const char *cmd_pause[] = {"cycle", "pause", NULL}; - mpv_command_async(mpv, 0, cmd_pause); - } - if (event.key.keysym.sym == SDLK_s) - { - // Also requires MPV_RENDER_PARAM_ADVANCED_CONTROL if you want - // screenshots to be rendered on GPU (like --vo=gpu would do). - const char *cmd_scr[] = {"screenshot-to-file", - "screenshot.png", - "window", - NULL}; - printf("attempting to save screenshot to %s\n", cmd_scr[1]); - mpv_command_async(mpv, 0, cmd_scr); - } + const char *cmd_pause[] = {"cycle", "pause", NULL}; + mpv_command_async(mpv, 0, cmd_pause); } - if (event.type == wakeup_on_mpv_render_update) + if (event.key.keysym.sym == SDLK_s) { - // Need to be called to update - mpv_render_context_update(mpv_gl); + // Also requires MPV_RENDER_PARAM_ADVANCED_CONTROL if you want + // screenshots to be rendered on GPU (like --vo=gpu would do). + const char *cmd_scr[] = {"screenshot-to-file", + "screenshot.png", + "window", + NULL}; + printf("attempting to save screenshot to %s\n", cmd_scr[1]); + mpv_command_async(mpv, 0, cmd_scr); } - // Happens when at least 1 new event is in the mpv event queue. - if (event.type == wakeup_on_mpv_events) - { - // Handle all remaining mpv events. - int count = 10; - while (count) { - mpv_event *mp_event = (mpv_event*) mpv_wait_event(mpv, 0); - if (mp_event->event_id == MPV_EVENT_NONE) - break; - if (mp_event->event_id == MPV_EVENT_LOG_MESSAGE) { - mpv_event_log_message *msg = (mpv_event_log_message *) mp_event->data; - if (strstr(msg->text, "DR image")) - printf("log: %s", msg->text); - continue; - } - count --; - printf("event: %s\n", mpv_event_name(mp_event->event_id)); + } + if (event.type == wakeup_on_mpv_render_update) + { + // Need to be called to update + mpv_render_context_update(mpv_gl); + } + // Happens when at least 1 new event is in the mpv event queue. + if (event.type == wakeup_on_mpv_events) + { + // Handle all remaining mpv events. + int count = 10; + while (count) { + mpv_event *mp_event = (mpv_event*) mpv_wait_event(mpv, 0); + if (mp_event->event_id == MPV_EVENT_NONE) + break; + if (mp_event->event_id == MPV_EVENT_LOG_MESSAGE) { + mpv_event_log_message *msg = (mpv_event_log_message *) mp_event->data; + if (strstr(msg->text, "DR image")) + printf("log: %s", msg->text); + continue; } + count --; + printf("event: %s\n", mpv_event_name(mp_event->event_id)); } } + } +} - // Start the Dear ImGui frame - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplSDL2_NewFrame(window); - ImGui::NewFrame(); - - // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). - if (show_demo_window) - ImGui::ShowDemoWindow(&show_demo_window); - - // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. - { - static float f = 0.0f; - static int counter = 0; +void +MainWindow::HandleLayers(void) +{ + // Start the Dear ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplSDL2_NewFrame(window); + ImGui::NewFrame(); - ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. + // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). + if (show_demo_window) + ImGui::ShowDemoWindow(&show_demo_window); - ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) - ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state - ImGui::Checkbox("Another Window", &show_another_window); + // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. + { + static float f = 0.0f; + static int counter = 0; - ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f - ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color + ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. - if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) - counter++; - ImGui::SameLine(); - ImGui::Text("counter = %d", counter); + ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) + ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state + ImGui::Checkbox("Another Window", &show_another_window); - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); - ImGui::End(); - } + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f + ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color - // 3. Show another simple window. - if (show_another_window) - { - ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) - ImGui::Text("Hello from another window!"); - if (ImGui::Button("Close Me")) - show_another_window = false; - ImGui::End(); - } + if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) + counter++; + ImGui::SameLine(); + ImGui::Text("counter = %d", counter); - // Rendering - ImGui::Render(); - - /* Always redraw */ - int w, h; - int __one__one = 1; - SDL_GetWindowSize(window, &w, &h); - mpv_opengl_fbo fbo = { - .fbo = 0, - .w = w, - .h = h, - }; - mpv_render_param params[] = { - {MPV_RENDER_PARAM_OPENGL_FBO, &fbo}, - {MPV_RENDER_PARAM_FLIP_Y, &__one__one}, - {(mpv_render_param_type)0} - }; - // mpv_render_context_render(mpv_gl, params); - - SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow(); - SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext(); - ImGui::UpdatePlatformWindows(); - ImGui::RenderPlatformWindowsDefault(); - SDL_GL_MakeCurrent(backup_current_window, backup_current_context); - mpv_render_context_render(mpv_gl, params); - - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - - SDL_GL_SwapWindow(window); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + ImGui::End(); } - DeInit(); + // 3. Show another simple window. + if (show_another_window) + { + ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) + ImGui::Text("Hello from another window!"); + if (ImGui::Button("Close Me")) + show_another_window = false; + ImGui::End(); + } } diff --git a/MainWindow.h b/MainWindow.h index 0a85899d1a4adb8c53fc793e5a7cf1267b3d6986..a8f63e1af6c33a890206eb00be34128912dca5e7 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -24,12 +24,18 @@ private: ImGuiIO *io; ImGuiStyle *style; + bool done; + static Uint32 wakeup_on_mpv_render_update, wakeup_on_mpv_events; /* Need to be static */ static std::shared_ptr<MainWindow> self; MainWindow(void); void DeInit(void); + void HandleEvents(void); + void HandleLayers(void); + void HandleRender(void); + public: void Main(void); diff --git a/imgui.ini b/imgui.ini index e8c0f802ec00b6ec51bb15074820b372c5f701ee..c988e6103c15e587a7b70c449cc307113d194173 100644 --- a/imgui.ini +++ b/imgui.ini @@ -4,13 +4,15 @@ Size=400,400 Collapsed=0 [Window][Hello, world!] -Pos=225,155 -Size=642,389 +ViewportPos=165,1 +ViewportId=0xEBE6C6E6 +Size=630,377 Collapsed=0 [Window][Dear ImGui Demo] -Pos=1019,130 -Size=744,491 +ViewportPos=1160,4 +ViewportId=0xE927CF2F +Size=738,485 Collapsed=0 [Window][Another Window]