From 1ebc6fa13e9801eb9901e79ca238fc1fdb5776d8 Mon Sep 17 00:00:00 2001 From: Bret Curtis Date: Tue, 9 Aug 2022 21:49:28 +0200 Subject: [PATCH] go from softwareDecompress to decompress --- components/sdlutil/sdlcursormanager.cpp | 151 ++++++++---------------- 1 file changed, 46 insertions(+), 105 deletions(-) diff --git a/components/sdlutil/sdlcursormanager.cpp b/components/sdlutil/sdlcursormanager.cpp index dc8a6dbeda..6a67119ad8 100644 --- a/components/sdlutil/sdlcursormanager.cpp +++ b/components/sdlutil/sdlcursormanager.cpp @@ -24,110 +24,6 @@ USE_GRAPHICSWINDOW() #endif -namespace CursorDecompression -{ - class MyGraphicsContext { - public: - MyGraphicsContext(int w, int h) - { - osg::ref_ptr traits = new osg::GraphicsContext::Traits; - traits->x = 0; - traits->y = 0; - traits->width = w; - traits->height = h; - traits->red = 8; - traits->green = 8; - traits->blue = 8; - traits->alpha = 8; - traits->windowDecoration = false; - traits->doubleBuffer = false; - traits->sharedContext = nullptr; - traits->pbuffer = true; - - osg::GraphicsContext::ScreenIdentifier si; - si.readDISPLAY(); - if (si.displayNum<0) si.displayNum = 0; - traits->displayNum = si.displayNum; - traits->screenNum = si.screenNum; - traits->hostName = si.hostName; - - _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); - - if (!_gc) - { - Log(Debug::Warning) << "Failed to create pbuffer, failing back to normal graphics window."; - - traits->pbuffer = false; - _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); - if (!_gc) - throw std::runtime_error("Failed to create graphics context for image decompression"); - } - - if (_gc.valid()) - { - _gc->realize(); - _gc->makeCurrent(); - } - } - - osg::ref_ptr getContext() - { - return _gc; - } - - bool valid() const { return _gc.valid() && _gc->isRealized(); } - - private: - osg::ref_ptr _gc; - }; - - SDLUtil::SurfaceUniquePtr softwareDecompress (osg::ref_ptr source, float rotDegrees) - { - int width = source->s(); - int height = source->t(); - bool useAlpha = source->isImageTranslucent(); - - osg::ref_ptr decompressedImage = new osg::Image; - decompressedImage->setFileName(source->getFileName()); - decompressedImage->allocateImage(width, height, 1, useAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE); - for (int s=0; ssetColor(source->getColor(s,t,0), s,t,0); - - Uint32 redMask = 0x000000ff; - Uint32 greenMask = 0x0000ff00; - Uint32 blueMask = 0x00ff0000; - Uint32 alphaMask = useAlpha ? 0xff000000 : 0; - - SDL_Surface *cursorSurface = SDL_CreateRGBSurfaceFrom(decompressedImage->data(), - width, - height, - decompressedImage->getPixelSizeInBits(), - decompressedImage->getRowSizeInBytes(), - redMask, - greenMask, - blueMask, - alphaMask); - - SDL_Surface *targetSurface = SDL_CreateRGBSurface(0, width, height, 32, redMask, greenMask, blueMask, alphaMask); - SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(targetSurface); - - SDL_RenderClear(renderer); - - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); - SDL_Texture *cursorTexture = SDL_CreateTextureFromSurface(renderer, cursorSurface); - - SDL_RenderCopyEx(renderer, cursorTexture, nullptr, nullptr, -rotDegrees, nullptr, SDL_FLIP_VERTICAL); - - SDL_DestroyTexture(cursorTexture); - SDL_FreeSurface(cursorSurface); - SDL_DestroyRenderer(renderer); - - return SDLUtil::SurfaceUniquePtr(targetSurface, SDL_FreeSurface); - } - -} - namespace SDLUtil { @@ -190,13 +86,58 @@ namespace SDLUtil #endif } + SDLUtil::SurfaceUniquePtr decompress(osg::ref_ptr source, float rotDegrees) + { + int width = source->s(); + int height = source->t(); + bool useAlpha = source->isImageTranslucent(); + + osg::ref_ptr decompressedImage = new osg::Image; + decompressedImage->setFileName(source->getFileName()); + decompressedImage->allocateImage(width, height, 1, useAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE); + for (int s=0; ssetColor(source->getColor(s,t,0), s,t,0); + + Uint32 redMask = 0x000000ff; + Uint32 greenMask = 0x0000ff00; + Uint32 blueMask = 0x00ff0000; + Uint32 alphaMask = useAlpha ? 0xff000000 : 0; + + SDL_Surface *cursorSurface = SDL_CreateRGBSurfaceFrom(decompressedImage->data(), + width, + height, + decompressedImage->getPixelSizeInBits(), + decompressedImage->getRowSizeInBytes(), + redMask, + greenMask, + blueMask, + alphaMask); + + SDL_Surface *targetSurface = SDL_CreateRGBSurface(0, width, height, 32, redMask, greenMask, blueMask, alphaMask); + SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(targetSurface); + + SDL_RenderClear(renderer); + + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); + SDL_Texture *cursorTexture = SDL_CreateTextureFromSurface(renderer, cursorSurface); + + SDL_RenderCopyEx(renderer, cursorTexture, nullptr, nullptr, -rotDegrees, nullptr, SDL_FLIP_VERTICAL); + + SDL_DestroyTexture(cursorTexture); + SDL_FreeSurface(cursorSurface); + SDL_DestroyRenderer(renderer); + + return SDLUtil::SurfaceUniquePtr(targetSurface, SDL_FreeSurface); + } + void SDLCursorManager::_createCursorFromResource(const std::string& name, int rotDegrees, osg::Image* image, Uint8 hotspot_x, Uint8 hotspot_y) { if (mCursorMap.find(name) != mCursorMap.end()) return; try { - auto surface = CursorDecompression::softwareDecompress(image, static_cast(rotDegrees)); + auto surface = decompress(image, static_cast(rotDegrees)); //set the cursor and store it for later SDL_Cursor* curs = SDL_CreateColorCursor(surface.get(), hotspot_x, hotspot_y);