1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#include "sdlcursormanager.hpp"
#include <cassert>
#include <stdexcept>
#include <cstdlib>
#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 <components/debug/debuglog.hpp>
#include "imagetosurface.hpp"
#if defined(OSG_LIBRARY_STATIC) && !defined(ANDROID)
// Sets the default windowing system interface according to the OS.
// Necessary for OpenSceneGraph to do some things, like decompression.
USE_GRAPHICSWINDOW()
#endif
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:
MyGraphicsContext(int w, int h)
{
osg::ref_ptr<osg::GraphicsContext::Traits> 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<osg::GraphicsContext> getContext()
{
return _gc;
}
bool valid() const { return _gc.valid() && _gc->isRealized(); }
private:
osg::ref_ptr<osg::GraphicsContext> _gc;
};
SDLUtil::SurfaceUniquePtr hardwareDecompress (osg::ref_ptr<osg::Image> source, float rotDegrees)
{
int width = source->s();
int height = source->t();
MyGraphicsContext context(width, height);
osg::ref_ptr<osg::State> state = context.getContext()->getState();
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(source);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
texture->setBorderColor(osg::Vec4f(0.f, 0.f, 0.f, 0.f));
osg::ref_ptr<osg::TexMat> texmat = new osg::TexMat;
osg::Matrix texRot (osg::Matrix::identity());
float theta ( osg::DegreesToRadians(-rotDegrees) );
float cosTheta = std::cos(theta);
float sinTheta = std::sin(theta);
texRot(0,0) = cosTheta;
texRot(1,0) = -sinTheta;
texRot(0,1) = sinTheta;
texRot(1,1) = cosTheta;
// Offset center of rotation to center of texture
texRot(3,0) = 0.5f + ( (-0.5f * cosTheta) - (-0.5f * sinTheta) );
texRot(3,1) = 0.5f + ( (-0.5f * sinTheta) + (-0.5f * cosTheta) );
texmat->setMatrix(texRot);
state->applyTextureAttribute(0, texmat);
osg::ref_ptr<osg::RefMatrix> identity (new osg::RefMatrix(osg::Matrix::identity()));
state->applyModelViewMatrix(identity);
state->applyProjectionMatrix(identity);
state->applyMode(GL_TEXTURE_2D, true);
state->applyTextureAttribute(0, texture);
osg::ref_ptr<osg::Image> resultImage = new osg::Image;
resultImage->allocateImage(width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
osg::RenderInfo renderInfo;
renderInfo.setState(state);
glViewport(0, 0, width, height);
osg::ref_ptr<osg::Geometry> geom;
geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,-1,0), osg::Vec3(2,0,0), osg::Vec3(0,2,0));
geom->drawImplementation(renderInfo);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, resultImage->data());
geom->releaseGLObjects();
source->releaseGLObjects();
texture->releaseGLObjects();
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, nullptr, nullptr, -rotDegrees, nullptr, SDL_FLIP_VERTICAL);
SDL_DestroyTexture(cursorTexture);
SDL_FreeSurface(cursorSurface);
SDL_DestroyRenderer(renderer);
return SDLUtil::SurfaceUniquePtr(targetSurface, SDL_FreeSurface);
}
}
namespace SDLUtil
{
SDLCursorManager::SDLCursorManager() :
mEnabled(false),
mInitialized(false)
{
}
SDLCursorManager::~SDLCursorManager()
{
CursorMap::const_iterator curs_iter = mCursorMap.begin();
while(curs_iter != mCursorMap.end())
{
SDL_FreeCursor(curs_iter->second);
++curs_iter;
}
mCursorMap.clear();
}
void SDLCursorManager::setEnabled(bool enabled)
{
if(mInitialized && enabled == mEnabled)
return;
mInitialized = true;
mEnabled = enabled;
//turn on hardware cursors
if(enabled)
{
_setGUICursor(mCurrentCursor);
}
//turn off hardware cursors
else
{
SDL_ShowCursor(SDL_FALSE);
}
}
void SDLCursorManager::cursorChanged(const std::string& name)
{
mCurrentCursor = name;
_setGUICursor(name);
}
void SDLCursorManager::_setGUICursor(const std::string &name)
{
auto it = mCursorMap.find(name);
if (it != mCursorMap.end())
SDL_SetCursor(it->second);
}
void SDLCursorManager::createCursor(const std::string& name, int rotDegrees, osg::Image* image, Uint8 hotspot_x, Uint8 hotspot_y)
{
#ifndef ANDROID
_createCursorFromResource(name, rotDegrees, image, hotspot_x, hotspot_y);
#endif
}
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;
static bool forceSoftwareDecompression = (getenv("OPENMW_DECOMPRESS_TEXTURES") != nullptr);
SurfaceUniquePtr (*decompressionFunction)(osg::ref_ptr<osg::Image>, float);
if (forceSoftwareDecompression || CursorDecompression::DXTCSupported) {
decompressionFunction = CursorDecompression::softwareDecompress;
} else {
decompressionFunction = CursorDecompression::hardwareDecompress;
}
try {
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) {
Log(Debug::Warning) << e.what();
Log(Debug::Warning) << "Using default cursor.";
return;
}
}
}
|