mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-16 03:45:24 +00:00
t/gui: Add imgui code
This commit is contained in:
parent
71b3de9c46
commit
74bdb0cb38
|
@ -15,6 +15,8 @@ include_directories(
|
|||
|
||||
set(SOURCE_FILES
|
||||
gui_common.h
|
||||
gui_imgui.c
|
||||
gui_imgui.h
|
||||
gui_main.c
|
||||
gui_ogl.c
|
||||
gui_prober.c
|
||||
|
@ -22,6 +24,22 @@ set(SOURCE_FILES
|
|||
gui_sdl2.c
|
||||
../../../external/glad/gl.h
|
||||
../../../external/glad/gl.c
|
||||
../../../external/imgui/cimgui.cpp
|
||||
../../../external/imgui/cimgui.h
|
||||
../../../external/imgui/imconfig.h
|
||||
../../../external/imgui/imgui.cpp
|
||||
../../../external/imgui/imgui.h
|
||||
../../../external/imgui/imgui_demo.cpp
|
||||
../../../external/imgui/imgui_draw.cpp
|
||||
../../../external/imgui/imgui_impl_opengl3.cpp
|
||||
../../../external/imgui/imgui_impl_opengl3.h
|
||||
../../../external/imgui/imgui_impl_sdl.cpp
|
||||
../../../external/imgui/imgui_impl_sdl.h
|
||||
../../../external/imgui/imgui_internal.h
|
||||
../../../external/imgui/imgui_widgets.cpp
|
||||
../../../external/imgui/imstb_rectpack.h
|
||||
../../../external/imgui/imstb_textedit.h
|
||||
../../../external/imgui/imstb_truetype.h
|
||||
)
|
||||
|
||||
add_executable(gui
|
||||
|
|
|
@ -94,6 +94,14 @@ struct gui_ogl_texture
|
|||
int
|
||||
gui_sdl2_init(struct program *p);
|
||||
|
||||
/*!
|
||||
* Loop until user request quit and show Imgui interface.
|
||||
*
|
||||
* @ingroup gui
|
||||
*/
|
||||
void
|
||||
gui_imgui_loop(struct program *p);
|
||||
|
||||
/*!
|
||||
* Loop until quit signal has been received.
|
||||
*
|
||||
|
|
125
src/xrt/targets/gui/gui_imgui.c
Normal file
125
src/xrt/targets/gui/gui_imgui.c
Normal file
|
@ -0,0 +1,125 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Main entrypoint for the Monado GUI program.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
* @ingroup gui
|
||||
*/
|
||||
|
||||
#include "util/u_var.h"
|
||||
#include "util/u_time.h"
|
||||
#include "util/u_misc.h"
|
||||
#include "util/u_sink.h"
|
||||
#include "util/u_format.h"
|
||||
|
||||
#include "xrt/xrt_prober.h"
|
||||
|
||||
#include "gui_common.h"
|
||||
#include "gui_imgui.h"
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* Structs and defines.
|
||||
*
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Internal gui state.
|
||||
*
|
||||
* @ingroup gui
|
||||
*/
|
||||
struct gui_imgui
|
||||
{
|
||||
bool show_demo_window;
|
||||
struct xrt_colour_rgb_f32 clear;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* 'Exported' functions.
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
gui_imgui_loop(struct program *p)
|
||||
{
|
||||
// Need to call this before any other Imgui call.
|
||||
igCreateContext(NULL);
|
||||
|
||||
// Local state
|
||||
ImGuiIO *io = igGetIO();
|
||||
|
||||
// Setup Platform/Renderer bindings
|
||||
igImGui_ImplSDL2_InitForOpenGL(p->win, p->ctx);
|
||||
igImGui_ImplOpenGL3_Init(NULL);
|
||||
|
||||
// Setup Dear ImGui style
|
||||
igStyleColorsDark(NULL);
|
||||
|
||||
// Main loop
|
||||
struct gui_imgui gui = {0};
|
||||
gui.clear.r = 0.45f;
|
||||
gui.clear.g = 0.55f;
|
||||
gui.clear.b = 0.60f;
|
||||
u_var_add_root(&gui, "GUI Control", false);
|
||||
u_var_add_rgb_f32(&gui, &gui.clear, "Clear Colour");
|
||||
u_var_add_bool(&gui, &gui.show_demo_window, "Demo Window");
|
||||
u_var_add_bool(&gui, &p->stopped, "Exit");
|
||||
|
||||
while (!p->stopped) {
|
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
igImGui_ImplSDL2_ProcessEvent(&event);
|
||||
|
||||
if (event.type == SDL_QUIT) {
|
||||
p->stopped = true;
|
||||
}
|
||||
|
||||
if (event.type == SDL_WINDOWEVENT &&
|
||||
event.window.event == SDL_WINDOWEVENT_CLOSE &&
|
||||
event.window.windowID == SDL_GetWindowID(p->win)) {
|
||||
p->stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
igImGui_ImplOpenGL3_NewFrame();
|
||||
igImGui_ImplSDL2_NewFrame(p->win);
|
||||
|
||||
// Start new frame.
|
||||
igNewFrame();
|
||||
|
||||
// Render the scene into it.
|
||||
gui_scene_manager_render(p);
|
||||
|
||||
// Handle this here.
|
||||
if (gui.show_demo_window) {
|
||||
igShowDemoWindow(&gui.show_demo_window);
|
||||
}
|
||||
|
||||
// Build the DrawData (EndFrame).
|
||||
igRender();
|
||||
|
||||
// Clear the background.
|
||||
glViewport(0, 0, (int)io->DisplaySize.x,
|
||||
(int)io->DisplaySize.y);
|
||||
glClearColor(gui.clear.r, gui.clear.g, gui.clear.b, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
igImGui_ImplOpenGL3_RenderDrawData(igGetDrawData());
|
||||
|
||||
SDL_GL_SwapWindow(p->win);
|
||||
|
||||
gui_prober_update(p);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
u_var_remove_root(&gui);
|
||||
igImGui_ImplOpenGL3_Shutdown();
|
||||
igImGui_ImplSDL2_Shutdown();
|
||||
igDestroyContext(NULL);
|
||||
}
|
14
src/xrt/targets/gui/gui_imgui.h
Normal file
14
src/xrt/targets/gui/gui_imgui.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Imgui integration for gui.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
* @ingroup gui
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
|
||||
#include "imgui/cimgui.h"
|
||||
#include "glad/gl.h"
|
Loading…
Reference in a new issue