[Scummvm-git-logs] scummvm master -> b721f95ffeb32ed0f1e2b985dee1da60c224764e

scemino noreply at scummvm.org
Sun Apr 28 11:59:59 UTC 2024


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
b721f95ffe BACKENDS: Add imgui in OpenGL backend


Commit: b721f95ffeb32ed0f1e2b985dee1da60c224764e
    https://github.com/scummvm/scummvm/commit/b721f95ffeb32ed0f1e2b985dee1da60c224764e
Author: scemino (scemino74 at gmail.com)
Date: 2024-04-28T13:59:31+02:00

Commit Message:
BACKENDS: Add imgui in OpenGL backend

Changed paths:
  A engines/director/debugtools.cpp
  A engines/director/debugtools.h
    backends/graphics/graphics.h
    backends/graphics/opengl/opengl-graphics.cpp
    backends/graphics/openglsdl/openglsdl-graphics.cpp
    backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
    backends/graphics3d/openglsdl/openglsdl-graphics3d.h
    engines/director/director.cpp
    engines/director/module.mk
    engines/twp/twp.cpp


diff --git a/backends/graphics/graphics.h b/backends/graphics/graphics.h
index 2a060e67000..2d5de6c5beb 100644
--- a/backends/graphics/graphics.h
+++ b/backends/graphics/graphics.h
@@ -114,7 +114,10 @@ public:
 	virtual void saveScreenshot() {}
 	virtual bool lockMouse(bool lock) { return false; }
 
-	virtual void renderImGui(void(*render)()) {}
+	virtual void setImGuiRenderCallback(void(*render)()) { _imGuiRender = render; }
+
+protected:
+	void(*_imGuiRender)() = nullptr;
 };
 
 #endif
diff --git a/backends/graphics/opengl/opengl-graphics.cpp b/backends/graphics/opengl/opengl-graphics.cpp
index a122b1dc5d4..ed867af71b1 100644
--- a/backends/graphics/opengl/opengl-graphics.cpp
+++ b/backends/graphics/opengl/opengl-graphics.cpp
@@ -665,7 +665,9 @@ void OpenGLGraphicsManager::updateScreen() {
 	    && !_osdMessageSurface && !_osdIconSurface
 #endif
 	    ) {
+#if !defined(USE_IMGUI)
 		return;
+#endif
 	}
 
 	// Update changes to textures.
@@ -1759,9 +1761,9 @@ bool OpenGLGraphicsManager::saveScreenshot(const Common::Path &filename) const {
 
 	Common::Array<uint8> pixels;
 	pixels.resize(lineSize * height);
-#ifdef EMSCRIPTEN	
+#ifdef EMSCRIPTEN
 	// WebGL doesn't support GL_RGB, see https://registry.khronos.org/webgl/specs/latest/1.0/#5.14.12:
-	// "Only two combinations of format and type are accepted. The first is format RGBA and type UNSIGNED_BYTE. 
+	// "Only two combinations of format and type are accepted. The first is format RGBA and type UNSIGNED_BYTE.
 	// The second is an implementation-chosen format. " and the implementation-chosen formats are buggy:
 	// https://github.com/KhronosGroup/WebGL/issues/2747
 	GL_CALL(glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, &pixels.front()));
diff --git a/backends/graphics/openglsdl/openglsdl-graphics.cpp b/backends/graphics/openglsdl/openglsdl-graphics.cpp
index 163d476b682..7e0f04c84a9 100644
--- a/backends/graphics/openglsdl/openglsdl-graphics.cpp
+++ b/backends/graphics/openglsdl/openglsdl-graphics.cpp
@@ -34,6 +34,11 @@
 #include "common/translation.h"
 #endif
 
+#ifdef USE_IMGUI
+#include "backends/imgui/backends/imgui_impl_sdl2_scummvm.h"
+#include "backends/imgui/backends/imgui_impl_opengl3_scummvm.h"
+#endif
+
 OpenGLSdlGraphicsManager::OpenGLSdlGraphicsManager(SdlEventSource *eventSource, SdlWindow *window)
 	: SdlGraphicsManager(eventSource, window), _lastRequestedHeight(0),
 #if SDL_VERSION_ATLEAST(2, 0, 0)
@@ -191,6 +196,15 @@ OpenGLSdlGraphicsManager::OpenGLSdlGraphicsManager(SdlEventSource *eventSource,
 
 OpenGLSdlGraphicsManager::~OpenGLSdlGraphicsManager() {
 #if SDL_VERSION_ATLEAST(2, 0, 0)
+
+#ifdef USE_IMGUI
+	if (_glContext) {
+		ImGui_ImplOpenGL3_Shutdown();
+		ImGui_ImplSDL2_Shutdown();
+		ImGui::DestroyContext();
+	}
+#endif
+
 	notifyContextDestroy();
 	SDL_GL_DeleteContext(_glContext);
 #else
@@ -315,7 +329,7 @@ void OpenGLSdlGraphicsManager::notifyResize(const int width, const int height) {
 		currentWidth = width;
 		currentHeight = height;
 	}
-	
+
 	debug(3, "req: %d x %d  cur: %d x %d, scale: %f", width, height, currentWidth, currentHeight, dpiScale);
 
 	if (ConfMan.getBool("force_resize", Common::ConfigManager::kApplicationDomain)) {
@@ -442,7 +456,21 @@ void OpenGLSdlGraphicsManager::refreshScreen() {
 		SdlGraphicsManager::saveScreenshot();
 		_queuedScreenshot = false;
 	}
-#endif 
+#endif
+
+#ifdef USE_IMGUI
+	if(_imGuiRender) {
+		ImGui_ImplOpenGL3_NewFrame();
+		ImGui_ImplSDL2_NewFrame(_window->getSDLWindow());
+
+		ImGui::NewFrame();
+		_imGuiRender();
+		ImGui::Render();
+
+		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+	}
+#endif
+
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 	SDL_GL_SwapWindow(_window->getSDLWindow());
 #else
@@ -521,6 +549,12 @@ bool OpenGLSdlGraphicsManager::setupMode(uint width, uint height) {
 	if (_glContext) {
 		notifyContextDestroy();
 
+#ifdef USE_IMGUI
+		ImGui_ImplOpenGL3_Shutdown();
+		ImGui_ImplSDL2_Shutdown();
+		ImGui::DestroyContext();
+#endif
+
 		SDL_GL_DeleteContext(_glContext);
 		_glContext = nullptr;
 	}
@@ -569,6 +603,17 @@ bool OpenGLSdlGraphicsManager::setupMode(uint width, uint height) {
 		return false;
 	}
 
+#ifdef USE_IMGUI
+	// Setup Dear ImGui
+	IMGUI_CHECKVERSION();
+	ImGui::CreateContext();
+	ImGui_ImplSDL2_InitForOpenGL(_window->getSDLWindow(), _glContext);
+	ImGui_ImplOpenGL3_Init("#version 110");
+	ImGui::StyleColorsDark();
+	ImGuiIO &io = ImGui::GetIO();
+	io.IniFilename = nullptr;
+#endif
+
 	if (SDL_GL_SetSwapInterval(_vsync ? 1 : 0)) {
 		warning("Unable to %s VSync: %s", _vsync ? "enable" : "disable", SDL_GetError());
 	}
diff --git a/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp b/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
index dba1795f40a..0361fe810a6 100644
--- a/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
+++ b/backends/graphics3d/openglsdl/openglsdl-graphics3d.cpp
@@ -658,19 +658,6 @@ OpenGL::FrameBuffer *OpenGLSdlGraphics3dManager::createFramebuffer(uint width, u
 	}
 }
 
-#if defined(USE_IMGUI) && SDL_VERSION_ATLEAST(2, 0, 0)
-void OpenGLSdlGraphics3dManager::renderImGui(void(*render)()) {
-	ImGui_ImplOpenGL3_NewFrame();
-	ImGui_ImplSDL2_NewFrame(_window->getSDLWindow());
-
-	ImGui::NewFrame();
-	render();
-	ImGui::Render();
-
-	ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
-}
-#endif
-
 void OpenGLSdlGraphics3dManager::updateScreen() {
 
 	GLint prevStateViewport[4];
@@ -700,7 +687,20 @@ void OpenGLSdlGraphics3dManager::updateScreen() {
 		SdlGraphicsManager::saveScreenshot();
 		_queuedScreenshot = false;
 	}
-#endif 
+#endif
+
+#ifdef USE_IMGUI
+	if (_imGuiRender) {
+		ImGui_ImplOpenGL3_NewFrame();
+		ImGui_ImplSDL2_NewFrame(_window->getSDLWindow());
+
+		ImGui::NewFrame();
+		_imGuiRender();
+		ImGui::Render();
+
+		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+	}
+#endif
 
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 	SDL_GL_SwapWindow(_window->getSDLWindow());
@@ -862,9 +862,9 @@ bool OpenGLSdlGraphics3dManager::saveScreenshot(const Common::Path &filename) co
 
 	Common::Array<uint8> pixels;
 	pixels.resize(lineSize * height);
-#ifdef EMSCRIPTEN	
+#ifdef EMSCRIPTEN
 	// WebGL doesn't support GL_RGB, see https://registry.khronos.org/webgl/specs/latest/1.0/#5.14.12:
-	// "Only two combinations of format and type are accepted. The first is format RGBA and type UNSIGNED_BYTE. 
+	// "Only two combinations of format and type are accepted. The first is format RGBA and type UNSIGNED_BYTE.
 	// The second is an implementation-chosen format. " and the implementation-chosen formats are buggy:
 	// https://github.com/KhronosGroup/WebGL/issues/2747
 	glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, &pixels.front());
diff --git a/backends/graphics3d/openglsdl/openglsdl-graphics3d.h b/backends/graphics3d/openglsdl/openglsdl-graphics3d.h
index f4909c99460..8ccaee2c86b 100644
--- a/backends/graphics3d/openglsdl/openglsdl-graphics3d.h
+++ b/backends/graphics3d/openglsdl/openglsdl-graphics3d.h
@@ -115,10 +115,6 @@ public:
 
 	void showSystemMouseCursor(bool visible) override;
 
-#if defined(USE_IMGUI) && SDL_VERSION_ATLEAST(2, 0, 0)
-	void renderImGui(void(*render)()) override;
-#endif
-
 protected:
 #if SDL_VERSION_ATLEAST(2, 0, 0)
 	int _glContextProfileMask, _glContextMajor, _glContextMinor;
@@ -157,12 +153,12 @@ protected:
 	void handleResizeImpl(const int width, const int height) override;
 
 #ifdef EMSCRIPTEN
-	/** 
+	/**
 	 * See https://registry.khronos.org/webgl/specs/latest/1.0/#2 :
 	 * " By default, after compositing the contents of the drawing buffer shall be cleared to their default values [...]
 	 *   Techniques like synchronous drawing buffer access (e.g., calling readPixels or toDataURL in the same function
 	 *   that renders to the drawing buffer) can be used to get the contents of the drawing buffer "
-	 * 
+	 *
 	 * This means we need to take the screenshot at the correct time, which we do by queueing taking the screenshot
 	 * for the next frame instead of taking it right away.
 	 */
diff --git a/engines/director/debugtools.cpp b/engines/director/debugtools.cpp
new file mode 100644
index 00000000000..dd2df33a0ef
--- /dev/null
+++ b/engines/director/debugtools.cpp
@@ -0,0 +1,54 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "backends/imgui/imgui.h"
+
+#include "director/director.h"
+#include "director/lingo/lingo.h"
+#include "director/debugtools.h"
+
+namespace Director {
+
+void onImGuiInit() {
+}
+
+void onImGuiRender() {
+	Director::Lingo* lingo = g_director->getLingo();
+
+	ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);
+	ImGui::SetNextWindowSize(ImVec2(120, 120), ImGuiCond_FirstUseEver);
+	ImGui::Begin("Vars");
+	ImGui::Text("%s", lingo->formatAllVars().c_str());
+	ImGui::Separator();
+	ImGuiIO &io = ImGui::GetIO();
+	ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
+	ImGui::End();
+
+	ImGui::SetNextWindowPos(ImVec2(20, 160), ImGuiCond_FirstUseEver);
+	ImGui::SetNextWindowSize(ImVec2(120, 120), ImGuiCond_FirstUseEver);
+	ImGui::Begin("CallStack");
+	ImGui::Text("%s", lingo->formatCallStack(lingo->_state->pc).c_str());
+	ImGui::End();
+}
+
+void onImGuiCleanup() {
+}
+} // namespace Twp
diff --git a/engines/director/debugtools.h b/engines/director/debugtools.h
new file mode 100644
index 00000000000..1ef7eb0c8d4
--- /dev/null
+++ b/engines/director/debugtools.h
@@ -0,0 +1,31 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef DIRECTOR_DEBUGTOOLS_H
+#define DIRECTOR_DEBUGTOOLS_H
+
+namespace Director {
+void onImGuiInit();
+void onImGuiRender();
+void onImGuiCleanup();
+} // namespace Director
+
+#endif
diff --git a/engines/director/director.cpp b/engines/director/director.cpp
index ff4ab027145..940f38086a8 100644
--- a/engines/director/director.cpp
+++ b/engines/director/director.cpp
@@ -19,6 +19,9 @@
  *
  */
 
+#include "backends/modular-backend.h"
+#include "backends/graphics/graphics.h"
+
 #include "common/config-manager.h"
 #include "common/debug-channels.h"
 #include "common/tokenizer.h"
@@ -28,6 +31,7 @@
 
 #include "director/director.h"
 #include "director/debugger.h"
+#include "director/debugtools.h"
 #include "director/archive.h"
 #include "director/cast.h"
 #include "director/movie.h"
@@ -285,6 +289,14 @@ Common::Error DirectorEngine::run() {
 		g_system->updateScreen();
 	}
 
+#ifdef USE_IMGUI
+	onImGuiInit();
+	ModularGraphicsBackend *gfxBackend = dynamic_cast<ModularGraphicsBackend *>(_system);
+	if (gfxBackend) {
+		gfxBackend->getGraphicsManager()->setImGuiRenderCallback(onImGuiRender);
+	}
+#endif
+
 	bool loop = true;
 
 	while (loop) {
@@ -311,6 +323,10 @@ Common::Error DirectorEngine::run() {
 		g_director->delayMillis(10);
 	}
 
+#ifdef USE_IMGUI
+	onImGuiCleanup();
+#endif
+
 	return Common::kNoError;
 }
 
diff --git a/engines/director/module.mk b/engines/director/module.mk
index f5bbb5050e7..0019d1e0500 100644
--- a/engines/director/module.mk
+++ b/engines/director/module.mk
@@ -149,6 +149,12 @@ MODULE_OBJS = \
 	lingo/xlibs/yasix.o \
 	lingo/xtras/scrnutil.o
 
+ifdef USE_IMGUI
+MODULE_OBJS += \
+	debugtools.o \
+
+endif
+
 # HACK: Skip this when including the file for detection objects.
 ifeq "$(USE_RULES)" "1"
 director-grammar:
diff --git a/engines/twp/twp.cpp b/engines/twp/twp.cpp
index 79f353988d5..1cfbb698fc1 100644
--- a/engines/twp/twp.cpp
+++ b/engines/twp/twp.cpp
@@ -98,10 +98,6 @@ TwpEngine::TwpEngine(OSystem *syst, const TwpGameDescription *gameDesc)
 }
 
 TwpEngine::~TwpEngine() {
-#ifdef USE_IMGUI
-	onImGuiCleanup();
-#endif
-
 	_mixer->stopAll();
 }
 
@@ -778,14 +774,6 @@ void TwpEngine::draw(RenderTexture *outTexture) {
 
 	// imgui render
 	_gfx.use(nullptr);
-
-#ifdef USE_IMGUI
-	ModularGraphicsBackend *sdl_g_system = dynamic_cast<ModularGraphicsBackend *>(_system);
-	if (sdl_g_system) {
-		sdl_g_system->getGraphicsManager()->renderImGui(onImGuiRender);
-	}
-#endif
-
 	_system->updateScreen();
 }
 
@@ -884,6 +872,10 @@ Common::Error TwpEngine::run() {
 
 #ifdef USE_IMGUI
 	onImGuiInit();
+	ModularGraphicsBackend *gfxBackend = dynamic_cast<ModularGraphicsBackend *>(_system);
+	if (gfxBackend) {
+		gfxBackend->getGraphicsManager()->setImGuiRenderCallback(onImGuiRender);
+	}
 #endif
 
 	// Simple event handling loop
@@ -1110,6 +1102,10 @@ Common::Error TwpEngine::run() {
 		}
 	}
 
+#ifdef USE_IMGUI
+	onImGuiCleanup();
+#endif
+
 	return Common::kNoError;
 }
 




More information about the Scummvm-git-logs mailing list