From 639d633f12b58c3a00aacfdf693e8f44cfdd5866 Mon Sep 17 00:00:00 2001 From: Le Philousophe Date: Sat, 25 Dec 2021 23:25:49 +0100 Subject: [PATCH] ANDROID: Add touch controls to 2D backend This is not usable for now as it's always disabled --- .../graphics/android/android-graphics.cpp | 70 ++++++++++++++++++- backends/graphics/android/android-graphics.h | 11 ++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/backends/graphics/android/android-graphics.cpp b/backends/graphics/android/android-graphics.cpp index c35dcbefc1c..27d1e7bb3fb 100644 --- a/backends/graphics/android/android-graphics.cpp +++ b/backends/graphics/android/android-graphics.cpp @@ -36,19 +36,48 @@ // for the Android port #define FORBIDDEN_SYMBOL_EXCEPTION_printf +#include "graphics/conversion.h" + +#include "backends/graphics/opengl/pipelines/pipeline.h" + #include "backends/graphics/android/android-graphics.h" #include "backends/platform/android/android.h" #include "backends/platform/android/jni-android.h" +static void loadBuiltinTexture(JNI::BitmapResources resource, OpenGL::Surface *surf) { + const Graphics::Surface *src = JNI::getBitmapResource(resource); + if (!src) { + error("Failed to fetch touch arrows bitmap"); + } + + surf->allocate(src->w, src->h); + Graphics::Surface *dst = surf->getSurface(); + + Graphics::crossBlit( + (byte *)dst->getPixels(), (const byte *)src->getPixels(), + dst->pitch, src->pitch, + src->w, src->h, + src->format, dst->format); + + delete src; +} + // // AndroidGraphicsManager // -AndroidGraphicsManager::AndroidGraphicsManager() { +AndroidGraphicsManager::AndroidGraphicsManager() : + _touchcontrols(nullptr) { ENTER(); // Initialize our OpenGL ES context. initSurface(); + _touchcontrols = createSurface(_defaultFormatAlpha); + loadBuiltinTexture(JNI::BitmapResources::TOUCH_ARROWS_BITMAP, _touchcontrols); + _touchcontrols->updateGLTexture(); + + // In 2D we always fallback to standard 2D mode + JNI::setTouch3DMode(false); } AndroidGraphicsManager::~AndroidGraphicsManager() { @@ -78,6 +107,13 @@ void AndroidGraphicsManager::initSurface() { #endif } + if (_touchcontrols) { + _touchcontrols->recreate(); + _touchcontrols->updateGLTexture(); + } + dynamic_cast(g_system)->getTouchControls().init( + this, JNI::egl_surface_width, JNI::egl_surface_height); + handleResize(JNI::egl_surface_width, JNI::egl_surface_height); } @@ -87,6 +123,13 @@ void AndroidGraphicsManager::deinitSurface() { LOGD("deinitializing 2D surface"); + // Deregister us from touch control + dynamic_cast(g_system)->getTouchControls().init( + nullptr, 0, 0); + if (_touchcontrols) { + _touchcontrols->destroy(); + } + notifyContextDestroy(); JNI::deinitSurface(); @@ -107,6 +150,17 @@ void AndroidGraphicsManager::displayMessageOnOSD(const Common::U32String &msg) { JNI::displayMessageOnOSD(msg); } +bool AndroidGraphicsManager::showMouse(bool visible) { + bool last = OpenGL::OpenGLGraphicsManager::showMouse(visible); + + if (visible && last != visible) { + // We just displayed a mouse cursor, disable the 3D mode if user enabled it + JNI::setTouch3DMode(false); + } + + return last; +} + float AndroidGraphicsManager::getHiDPIScreenFactor() const { // TODO: Use JNI to get DisplayMetrics.density, which according to the documentation // seems to be what we want. @@ -130,9 +184,23 @@ bool AndroidGraphicsManager::loadVideoMode(uint requestedWidth, uint requestedHe void AndroidGraphicsManager::refreshScreen() { //ENTER(); + // Last minute draw of touch controls + dynamic_cast(g_system)->getTouchControls().draw(); + JNI::swapBuffers(); } +void AndroidGraphicsManager::touchControlDraw(int16 x, int16 y, int16 w, int16 h, const Common::Rect &clip) { + _backBuffer.enableBlend(OpenGL::Framebuffer::kBlendModeTraditionalTransparency); + OpenGL::g_context.getActivePipeline()->drawTexture(_touchcontrols->getGLTexture(), + x, y, w, h, clip); +} + +void AndroidGraphicsManager::touchControlNotifyChanged() { + // Make sure we redraw the screen + _forceRedraw = true; +} + void *AndroidGraphicsManager::getProcAddress(const char *name) const { ENTER("%s", name); diff --git a/backends/graphics/android/android-graphics.h b/backends/graphics/android/android-graphics.h index b0e71d9590d..76e493acc50 100644 --- a/backends/graphics/android/android-graphics.h +++ b/backends/graphics/android/android-graphics.h @@ -25,6 +25,8 @@ #include "common/scummsys.h" #include "backends/graphics/opengl/opengl-graphics.h" +#include "backends/platform/android/touchcontrols.h" + class AndroidCommonGraphics { public: virtual ~AndroidCommonGraphics() {} @@ -61,7 +63,8 @@ public: virtual bool setState(const State &state) = 0; }; -class AndroidGraphicsManager : public OpenGL::OpenGLGraphicsManager, public AndroidCommonGraphics { +class AndroidGraphicsManager : + public OpenGL::OpenGLGraphicsManager, public AndroidCommonGraphics, public TouchControlsDrawer { public: AndroidGraphicsManager(); virtual ~AndroidGraphicsManager(); @@ -81,8 +84,12 @@ public: float getHiDPIScreenFactor() const override; + void touchControlNotifyChanged() override; + void touchControlDraw(int16 x, int16 y, int16 w, int16 h, const Common::Rect &clip) override; + protected: void setSystemMousePosition(const int x, const int y) override {} + bool showMouse(bool visible) override; bool loadVideoMode(uint requestedWidth, uint requestedHeight, const Graphics::PixelFormat &format) override; @@ -90,6 +97,8 @@ protected: void *getProcAddress(const char *name) const override; +private: + OpenGL::Surface *_touchcontrols; }; #endif