From 842aab44aa5cf4b754ec6e0ff3604c6210e5377c Mon Sep 17 00:00:00 2001 From: mjbudd77 Date: Mon, 31 May 2021 20:47:57 -0400 Subject: [PATCH] Added logic to also init SDL mouse cursor shape in addition to Qt just incase SDL is drawing mouse cursor. --- src/drivers/Qt/ConsoleViewerSDL.cpp | 103 ++++++++++++++++++++++++++++ src/drivers/Qt/ConsoleViewerSDL.h | 4 ++ 2 files changed, 107 insertions(+) diff --git a/src/drivers/Qt/ConsoleViewerSDL.cpp b/src/drivers/Qt/ConsoleViewerSDL.cpp index fab2d2c7..57f94ebb 100644 --- a/src/drivers/Qt/ConsoleViewerSDL.cpp +++ b/src/drivers/Qt/ConsoleViewerSDL.cpp @@ -65,6 +65,7 @@ ConsoleViewSDL_t::ConsoleViewSDL_t(QWidget *parent) sdlWindow = NULL; sdlRenderer = NULL; sdlTexture = NULL; + sdlCursor = NULL; vsyncEnabled = false; mouseButtonMask = 0; @@ -106,6 +107,10 @@ ConsoleViewSDL_t::~ConsoleViewSDL_t(void) { free( localBuf ); localBuf = NULL; } + if ( sdlCursor ) + { + SDL_FreeCursor(sdlCursor); sdlCursor = NULL; + } cleanup(); } @@ -303,6 +308,104 @@ void ConsoleViewSDL_t::reset(void) } } +void ConsoleViewSDL_t::setCursor(const QCursor &c) +{ + QImage pm; + SDL_Surface *s; + + pm = c.pixmap().toImage(); + + if (pm.format() != QImage::Format_ARGB32) + { + printf("Coverting Image to ARGB32\n"); + pm = pm.convertToFormat(QImage::Format_ARGB32); + } + + // QImage stores each pixel in ARGB format + // Mask appropriately for the endianness + #if SDL_BYTEORDER == SDL_BIG_ENDIAN + Uint32 amask = 0x000000ff; + Uint32 rmask = 0x0000ff00; + Uint32 gmask = 0x00ff0000; + Uint32 bmask = 0xff000000; + #else + Uint32 amask = 0xff000000; + Uint32 rmask = 0x00ff0000; + Uint32 gmask = 0x0000ff00; + Uint32 bmask = 0x000000ff; + #endif + + s = SDL_CreateRGBSurfaceFrom((void*)pm.constBits(), + pm.width(), pm.height(), pm.depth(), pm.bytesPerLine(), + rmask, gmask, bmask, amask); + + if ( s == NULL ) + { + printf("Error: Failed to create SDL Surface for Icon\n"); + return; + } + + if ( sdlCursor ) + { + SDL_FreeCursor(sdlCursor); sdlCursor = NULL; + } + + sdlCursor = SDL_CreateColorCursor( s, c.hotSpot().x(), c.hotSpot().y() ); + + if ( sdlCursor == NULL ) + { + printf("SDL Cursor Failed: %s\n", SDL_GetError() ); + } + else + { + printf("SDL Cursor Initialized at (%i,%i)\n", c.hotSpot().x(), c.hotSpot().y() ); + SDL_SetCursor(sdlCursor); + SDL_ShowCursor( SDL_ENABLE ); + } + + QWidget::setCursor(c); +} + +void ConsoleViewSDL_t::setCursor( Qt::CursorShape s ) +{ + SDL_SystemCursor sdlSysCursor; + + switch ( s ) + { + default: + case Qt::ArrowCursor: + sdlSysCursor = SDL_SYSTEM_CURSOR_ARROW; + break; + case Qt::BlankCursor: + sdlSysCursor = SDL_SYSTEM_CURSOR_ARROW; + break; + case Qt::CrossCursor: + sdlSysCursor = SDL_SYSTEM_CURSOR_CROSSHAIR; + break; + } + + if ( sdlCursor ) + { + SDL_FreeCursor(sdlCursor); sdlCursor = NULL; + } + + sdlCursor = SDL_CreateSystemCursor( sdlSysCursor ); + + if ( sdlCursor == NULL ) + { + printf("SDL Cursor Failed: %s\n", SDL_GetError() ); + } + else + { + printf("SDL System Cursor Initialized\n"); + SDL_SetCursor(sdlCursor); + } + + SDL_ShowCursor( (s == Qt::BlankCursor) ? SDL_DISABLE : SDL_ENABLE ); + + QWidget::setCursor(s); +} + void ConsoleViewSDL_t::showEvent(QShowEvent *event) { //printf("SDL Show: %i x %i \n", width(), height() ); diff --git a/src/drivers/Qt/ConsoleViewerSDL.h b/src/drivers/Qt/ConsoleViewerSDL.h index f4d979f1..dbe8eceb 100644 --- a/src/drivers/Qt/ConsoleViewerSDL.h +++ b/src/drivers/Qt/ConsoleViewerSDL.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include #include @@ -38,6 +39,8 @@ class ConsoleViewSDL_t : public QWidget void getAspectXY( double &x, double &y ); double getAspectRatio(void); + void setCursor(const QCursor &c); + void setCursor( Qt::CursorShape s ); protected: //void paintEvent(QPaintEvent *event); @@ -74,6 +77,7 @@ class ConsoleViewSDL_t : public QWidget SDL_Window *sdlWindow; SDL_Renderer *sdlRenderer; SDL_Texture *sdlTexture; + SDL_Cursor *sdlCursor; //SDL_Rect sdlViewport; private slots: