JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
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_sdl.h>
24 #include <imgui_impl_opengl3.h>
25 #include <imgui.h>
26 #include <jevois/GPU/OpenGL.H>
27 
28 // ##############################################################################################################
29 jevois::ImGuiBackendSDL::ImGuiBackendSDL() :
30  jevois::ImGuiBackend(), itsSDLctx(0), itsSDLwin(nullptr)
31 { }
32 
33 // ##############################################################################################################
34 jevois::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 // ##############################################################################################################
46 void jevois::ImGuiBackendSDL::init(unsigned short JEVOIS_UNUSED_PARAM(w), unsigned short JEVOIS_UNUSED_PARAM(h),
47  bool JEVOIS_UNUSED_PARAM(fullscreen))
48 { }
49 
50 // ##############################################################################################################
51 void jevois::ImGuiBackendSDL::init(unsigned short w, unsigned short h, bool fullscreen, float scale,
52  bool JEVOIS_UNUSED_PARAM(conslock))
53 {
54  if (itsSDLwin) { LERROR("Already initialized -- IGNORED"); return; }
55 
56  // Setup SDL:
57  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER))
58  LFATAL("SDL initialization error: " << SDL_GetError());
59 
60  // Decide GL+GLSL versions
61  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
62  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
63  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
64 
65  // Create window with graphics context
66  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
67  //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
68  //SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
69 
70  SDL_WindowFlags window_flags;
71  if (fullscreen) window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL|SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
72  else window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
73  itsSDLwin = SDL_CreateWindow("JeVois-Pro", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, window_flags);
74  itsSDLctx = SDL_GL_CreateContext(itsSDLwin);
75  SDL_GL_MakeCurrent(itsSDLwin, itsSDLctx);
76  SDL_GL_SetSwapInterval(1); // Enable vsync
77 
78  // Setup Dear ImGui context
79  IMGUI_CHECKVERSION();
80  ImGui::CreateContext();
81  ImGuiIO & io = ImGui::GetIO(); (void)io;
82  io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
83  io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
84 
85  // Setup Dear ImGui style
86  ImGui::StyleColorsDark();
87  //ImGui::StyleColorsClassic();
88 
89  io.FontGlobalScale = scale;
90  ImGui::GetStyle().ScaleAllSizes(scale);
91 
92  // Setup Platform/Renderer backends
93  ImGui_ImplSDL2_InitForOpenGL(itsSDLwin, itsSDLctx);
94  ImGui_ImplOpenGL3_Init("#version 300 es");
95 
96  // Load Fonts
97  // - 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.
98  // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
99  // - 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).
100  // - 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.
101  // - Read 'docs/FONTS.md' for more instructions and details.
102  // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
103  //io.Fonts->AddFontDefault();
104  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
105  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
106  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
107  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
108  //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
109  //IM_ASSERT(font != NULL);
110 
111  // Show OpenGL-ES version:
112  LINFO(glGetString(GL_VERSION) <<' '<< glGetString(GL_VENDOR) << " (" << glGetString(GL_RENDERER) <<')');
113  //LINFO("OpenGL extensions: " << glGetString(GL_EXTENSIONS));
114 }
115 
116 // ##############################################################################################################
117 bool jevois::ImGuiBackendSDL::pollEvents(bool & shouldclose)
118 {
119  SDL_Event event;
120  bool ret = false;
121  while (SDL_PollEvent(&event))
122  {
123  ret = true;
124  ImGui_ImplSDL2_ProcessEvent(&event);
125  if (event.type == SDL_QUIT) shouldclose = true;
126  if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
127  event.window.windowID == SDL_GetWindowID(itsSDLwin)) shouldclose = true;
128 
129  // handle resizing here: https://retifrav.github.io/blog/2019/05/26/sdl-imgui/
130 
131  }
132  return ret;
133 }
134 
135 // ##############################################################################################################
136 void jevois::ImGuiBackendSDL::newFrame()
137 {
138  glClear(GL_COLOR_BUFFER_BIT);
139  ImGui_ImplOpenGL3_NewFrame();
140  ImGui_ImplSDL2_NewFrame(itsSDLwin);
141  ImGui::NewFrame();
142 }
143 
144 // ##############################################################################################################
145 void jevois::ImGuiBackendSDL::render()
146 {
147  ImGui::Render();
148  ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
149  SDL_GL_SwapWindow(itsSDLwin);
150 }
151 
152 // ##############################################################################################################
153 void jevois::ImGuiBackendSDL::getWindowSize(unsigned short & w, unsigned short & h) const
154 {
155  if (itsSDLwin)
156  {
157  int ww = 0, hh = 0;
158  SDL_GetWindowSize(itsSDLwin, &ww, &hh);
159  w = ww; h = hh;
160  }
161  else
162  {
163  w = 0; h = 0;
164  }
165 }
166 
167 #endif // JEVOIS_HOST_PRO
LERROR
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition: Log.H:211
OpenGL.H
jevois
Definition: Concepts.dox:1
Log.H
ImGuiBackendSDL.H
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
h
int h
Definition: GUIhelper.C:2373
LINFO
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:194