Skip to content

Commit

Permalink
Merge pull request #1740 from nikolaykasyanov/software-cursor-decompr…
Browse files Browse the repository at this point in the history
…ession

Decompress cursors using SDL software renderer on Mac or if OSG >= 3.5.8 or if OPENMW_DECOMPRESS_TEXTURES is set
  • Loading branch information
psi29a authored and Bret Curtis committed Jun 20, 2018
1 parent 98063c5 commit ef85b13
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
Bug #4412: openmw-iniimporter ignores data paths from config
Bug #4413: Moving with 0 strength uses all of your fatigue
Bug #4420: Camera flickering when I open up and close menus while sneaking
Bug #4424: [macOS] Cursor is either empty or garbage when compiled against macOS 10.13 SDK
Bug #4435: Item health is considered a signed integer
Bug #4441: Adding items to currently disabled weapon-wielding creatures crashes the game
Feature #1786: Round up encumbrance value in the encumbrance bar
Expand Down
5 changes: 2 additions & 3 deletions apps/openmw/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,8 @@ void OMW::Engine::setWindowIcon()
else
{
osg::ref_ptr<osg::Image> image = result.getImage();
SDL_Surface* surface = SDLUtil::imageToSurface(image, true);
SDL_SetWindowIcon(mWindow, surface);
SDL_FreeSurface(surface);
auto surface = SDLUtil::imageToSurface(image, true);
SDL_SetWindowIcon(mWindow, surface.get());
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/sdlutil/imagetosurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace SDLUtil
{

SDL_Surface* imageToSurface(osg::Image *image, bool flip)
SurfaceUniquePtr imageToSurface(osg::Image *image, bool flip)
{
int width = image->s();
int height = image->t();
Expand All @@ -22,7 +22,7 @@ SDL_Surface* imageToSurface(osg::Image *image, bool flip)
static_cast<Uint8>(clr.g() * 255), static_cast<Uint8>(clr.b() * 255), static_cast<Uint8>(clr.a() * 255));
}

return surface;
return SurfaceUniquePtr(surface, SDL_FreeSurface);
}

}
6 changes: 4 additions & 2 deletions components/sdlutil/imagetosurface.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H
#define OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H

#include <memory>

struct SDL_Surface;

namespace osg
Expand All @@ -10,10 +12,10 @@ namespace osg

namespace SDLUtil
{
typedef std::unique_ptr<SDL_Surface, void (*)(SDL_Surface *)> SurfaceUniquePtr;

/// Convert an osg::Image to an SDL_Surface.
/// @note The returned surface must be freed using SDL_FreeSurface.
SDL_Surface* imageToSurface(osg::Image* image, bool flip=false);
SurfaceUniquePtr imageToSurface(osg::Image* image, bool flip=false);

}

Expand Down
100 changes: 72 additions & 28 deletions components/sdlutil/sdlcursormanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#include <SDL_mouse.h>
#include <SDL_endian.h>
#include <SDL_render.h>
#include <SDL_hints.h>

#include <osg/GraphicsContext>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osg/TexMat>
#include <osg/Version>
#include <osgViewer/GraphicsWindow>

#include "imagetosurface.hpp"
Expand All @@ -22,8 +25,14 @@
USE_GRAPHICSWINDOW()
#endif

namespace
namespace CursorDecompression
{
// macOS builds use the OSG fork that includes DXTC commit
#if OSG_VERSION_GREATER_OR_EQUAL(3, 5, 8) || defined(__APPLE__)
static const bool DXTCSupported = true;
#else
static const bool DXTCSupported = false;
#endif

class MyGraphicsContext {
public:
Expand Down Expand Up @@ -80,10 +89,8 @@ namespace
osg::ref_ptr<osg::GraphicsContext> _gc;
};

osg::ref_ptr<osg::Image> decompress (osg::ref_ptr<osg::Image> source, float rotDegrees)
SDLUtil::SurfaceUniquePtr hardwareDecompress (osg::ref_ptr<osg::Image> source, float rotDegrees)
{
// TODO: use software decompression once S3TC patent expires

int width = source->s();
int height = source->t();

Expand Down Expand Up @@ -132,17 +139,6 @@ namespace

osg::ref_ptr<osg::Geometry> geom;

#if defined(__APPLE__)
// Extra flip needed on OS X systems due to a driver bug
const char* envval = getenv("OPENMW_CURSOR_WORKAROUND");
bool workaround = !envval || envval == std::string("1");
std::string vendorString = (const char*)glGetString(GL_VENDOR);
if (!envval)
workaround = vendorString.find("Intel") != std::string::npos || vendorString.find("ATI") != std::string::npos || vendorString.find("AMD") != std::string::npos;
if (workaround)
geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,1,0), osg::Vec3(2,0,0), osg::Vec3(0,-2,0));
else
#endif
geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,-1,0), osg::Vec3(2,0,0), osg::Vec3(0,2,0));

geom->drawImplementation(renderInfo);
Expand All @@ -153,7 +149,52 @@ namespace
source->releaseGLObjects();
texture->releaseGLObjects();

return resultImage;
return SDLUtil::imageToSurface(resultImage, true);
}

SDLUtil::SurfaceUniquePtr softwareDecompress (osg::ref_ptr<osg::Image> source, float rotDegrees)
{
int width = source->s();
int height = source->t();
bool useAlpha = source->isImageTranslucent();

osg::ref_ptr<osg::Image> 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; s<width; ++s)
for (int t=0; t<height; ++t)
decompressedImage->setColor(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, NULL, NULL, -rotDegrees, NULL, SDL_FLIP_VERTICAL);

SDL_DestroyTexture(cursorTexture);
SDL_FreeSurface(cursorSurface);
SDL_DestroyRenderer(renderer);

return SDLUtil::SurfaceUniquePtr(targetSurface, SDL_FreeSurface);
}

}
Expand Down Expand Up @@ -222,27 +263,30 @@ namespace SDLUtil

void SDLCursorManager::_createCursorFromResource(const std::string& name, int rotDegrees, osg::Image* image, Uint8 hotspot_x, Uint8 hotspot_y)
{
osg::ref_ptr<osg::Image> decompressed;

if (mCursorMap.find(name) != mCursorMap.end())
return;

static bool forceSoftwareDecompression = (getenv("OPENMW_DECOMPRESS_TEXTURES") != 0);

SurfaceUniquePtr (*decompressionFunction)(osg::ref_ptr<osg::Image>, float);
if (forceSoftwareDecompression || CursorDecompression::DXTCSupported) {
decompressionFunction = CursorDecompression::softwareDecompress;
} else {
decompressionFunction = CursorDecompression::hardwareDecompress;
}

try {
decompressed = decompress(image, static_cast<float>(rotDegrees));
auto surface = decompressionFunction(image, static_cast<float>(rotDegrees));

//set the cursor and store it for later
SDL_Cursor* curs = SDL_CreateColorCursor(surface.get(), hotspot_x, hotspot_y);

mCursorMap.insert(CursorMap::value_type(std::string(name), curs));
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
std::cerr <<"Using default cursor."<<std::endl;
return;
}

SDL_Surface* surf = SDLUtil::imageToSurface(decompressed, true);

//set the cursor and store it for later
SDL_Cursor* curs = SDL_CreateColorCursor(surf, hotspot_x, hotspot_y);
mCursorMap.insert(CursorMap::value_type(std::string(name), curs));

//clean up
SDL_FreeSurface(surf);
}

}

0 comments on commit ef85b13

Please sign in to comment.