JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
ImGuiBackendSDL.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#ifdef JEVOIS_HOST_PRO
19
21#include <jevois/Debug/Log.H>
22
23#include <imgui_impl_sdl2.h>
24#include <imgui_impl_opengl3.h>
25#include <imgui.h>
26#include <jevois/GPU/OpenGL.H>
27
28// ##############################################################################################################
29jevois::ImGuiBackendSDL::ImGuiBackendSDL() :
30 jevois::ImGuiBackend(), itsSDLctx(0), itsSDLwin(nullptr)
31{ }
32
33// ##############################################################################################################
34jevois::ImGuiBackendSDL::~ImGuiBackendSDL()
35{
36 ImGui_ImplOpenGL3_Shutdown();
37 ImGui_ImplSDL2_Shutdown();
38 ImGui::DestroyContext();
39
40 if (itsSDLwin) SDL_DestroyWindow(itsSDLwin);
41 if (itsSDLctx) SDL_GL_DeleteContext(itsSDLctx);
42 SDL_Quit();
43}
44
45// ##############################################################################################################
46void jevois::ImGuiBackendSDL::init(unsigned short, unsigned short, bool)
47{ }
48
49// ##############################################################################################################
50void jevois::ImGuiBackendSDL::init(unsigned short w, unsigned short h, bool fullscreen, float scale, bool)
51{
52 if (itsSDLwin) { LERROR("Already initialized -- IGNORED"); return; }
53
54 // Setup SDL:
55 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) // This is slow on noble: | SDL_INIT_GAMECONTROLLER))
56 LFATAL("SDL initialization error: " << SDL_GetError());
57
58 // Decide GL+GLSL versions
59 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
60 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
61 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
62
63 // Create window with graphics context
64 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
65 //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
66 //SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
67
68 SDL_WindowFlags window_flags;
69 if (fullscreen) window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL|SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
70 else window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
71 itsSDLwin = SDL_CreateWindow("JeVois-Pro", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, window_flags);
72 itsSDLctx = SDL_GL_CreateContext(itsSDLwin);
73 SDL_GL_MakeCurrent(itsSDLwin, itsSDLctx);
74 SDL_GL_SetSwapInterval(1); // Enable vsync
75
76 // Setup Dear ImGui context
77 IMGUI_CHECKVERSION();
78 ImGui::CreateContext();
79 ImGuiIO & io = ImGui::GetIO();
80 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
81 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
82
83 // Setup Dear ImGui style
84 ImGui::StyleColorsDark();
85 io.FontGlobalScale = scale;
86 ImGui::GetStyle().ScaleAllSizes(scale);
87
88 // Setup Platform/Renderer backends
89 ImGui_ImplSDL2_InitForOpenGL(itsSDLwin, itsSDLctx);
90 ImGui_ImplOpenGL3_Init("#version 300 es");
91
92 // Load Fonts
93 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
94 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
95 // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
96 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
97 // - Read 'docs/FONTS.md' for more instructions and details.
98 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
99 //io.Fonts->AddFontDefault();
100 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
101 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
102 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
103 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
104 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
105 //IM_ASSERT(font != NULL);
106
107 // Show OpenGL-ES version:
108 LINFO(glGetString(GL_VERSION) <<' '<< glGetString(GL_VENDOR) << " (" << glGetString(GL_RENDERER) <<')');
109 //LINFO("OpenGL extensions: " << glGetString(GL_EXTENSIONS));
110
111 // Weird ID conflict with column separators in GUIhelper::drawParameters() reported by ImGui, disable the warning:
112 io.ConfigDebugHighlightIdConflicts = false;
113}
114
115// ##############################################################################################################
116bool jevois::ImGuiBackendSDL::pollEvents(bool & shouldclose)
117{
118 SDL_Event event;
119 bool ret = false;
120 while (SDL_PollEvent(&event))
121 {
122 ret = true;
123 ImGui_ImplSDL2_ProcessEvent(&event);
124 if (event.type == SDL_QUIT) shouldclose = true;
125 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
126 event.window.windowID == SDL_GetWindowID(itsSDLwin)) shouldclose = true;
127
128 // handle resizing here: https://retifrav.github.io/blog/2019/05/26/sdl-imgui/
129
130 }
131 return ret;
132}
133
134// ##############################################################################################################
135void jevois::ImGuiBackendSDL::newFrame()
136{
137 glClear(GL_COLOR_BUFFER_BIT);
138 ImGui_ImplOpenGL3_NewFrame();
139 ImGui_ImplSDL2_NewFrame();
140 ImGui::NewFrame();
141}
142
143// ##############################################################################################################
144void jevois::ImGuiBackendSDL::render()
145{
146 ImGui::Render();
147 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
148 SDL_GL_SwapWindow(itsSDLwin);
149}
150
151// ##############################################################################################################
152void jevois::ImGuiBackendSDL::getWindowSize(unsigned short & w, unsigned short & h) const
153{
154 if (itsSDLwin)
155 {
156 int ww = 0, hh = 0;
157 SDL_GetWindowSize(itsSDLwin, &ww, &hh);
158 w = ww; h = hh;
159 }
160 else
161 {
162 w = 0; h = 0;
163 }
164}
165
166#endif // JEVOIS_HOST_PRO
int h
Definition GUIhelper.C:2520
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition Log.H:211
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition Log.H:194
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2