OPENGL: Refactor texture instantiation.

This commit is contained in:
Johannes Schickel 2014-02-11 11:07:38 +01:00
parent 0063568484
commit 1f4638fe82
2 changed files with 47 additions and 38 deletions

View file

@ -265,21 +265,15 @@ OSystem::TransactionError OpenGLGraphicsManager::endGFXTransaction() {
delete _gameScreen; delete _gameScreen;
_gameScreen = nullptr; _gameScreen = nullptr;
GLenum glIntFormat, glFormat, glType;
#ifdef USE_RGB_COLOR #ifdef USE_RGB_COLOR
if (_currentState.gameFormat.bytesPerPixel == 1) { _gameScreen = createTexture(_currentState.gameFormat);
#else
_gameScreen = createTexture(Graphics::PixelFormat::createFormatCLUT8());
#endif #endif
const bool supported = getGLPixelFormat(_defaultFormat, glIntFormat, glFormat, glType); assert(_gameScreen);
assert(supported); if (_gameScreen->hasPalette()) {
_gameScreen = new TextureCLUT8(glIntFormat, glFormat, glType, _defaultFormat);
_gameScreen->setPalette(0, 255, _gamePalette); _gameScreen->setPalette(0, 255, _gamePalette);
#ifdef USE_RGB_COLOR
} else {
const bool supported = getGLPixelFormat(_currentState.gameFormat, glIntFormat, glFormat, glType);
assert(supported);
_gameScreen = new Texture(glIntFormat, glFormat, glType, _currentState.gameFormat);
} }
#endif
_gameScreen->allocate(_currentState.gameWidth, _currentState.gameHeight); _gameScreen->allocate(_currentState.gameWidth, _currentState.gameHeight);
_gameScreen->enableLinearFiltering(_currentState.graphicsMode == GFX_LINEAR); _gameScreen->enableLinearFiltering(_currentState.graphicsMode == GFX_LINEAR);
@ -566,27 +560,19 @@ void OpenGLGraphicsManager::setMouseCursor(const void *buf, uint w, uint h, int
GLenum glIntFormat, glFormat, glType; GLenum glIntFormat, glFormat, glType;
if (inputFormat.bytesPerPixel == 1) { Graphics::PixelFormat textureFormat;
// In case this is not supported this is a serious programming if (inputFormat.bytesPerPixel == 1 || (inputFormat.aBits() && getGLPixelFormat(inputFormat, glIntFormat, glFormat, glType))) {
// error and the assert a bit below will trigger! // There is two cases when we can use the cursor format directly.
const bool supported = getGLPixelFormat(_defaultFormatAlpha, glIntFormat, glFormat, glType); // The first is when it's CLUT8, here color key handling can
assert(supported); // always be applied because we use the alpha channel of
_cursor = new TextureCLUT8(glIntFormat, glFormat, glType, _defaultFormatAlpha); // _defaultFormatAlpha for that.
// The other is when the input format has alpha bits and
// furthermore is directly supported.
textureFormat = inputFormat;
} else { } else {
// Try to use the format specified as input directly. We can only textureFormat = _defaultFormatAlpha;
// do so when it actually has alpha bits.
if (inputFormat.aBits() != 0 && getGLPixelFormat(inputFormat, glIntFormat, glFormat, glType)) {
_cursor = new Texture(glIntFormat, glFormat, glType, inputFormat);
}
// Otherwise fall back to the default alpha format.
if (!_cursor) {
const bool supported = getGLPixelFormat(_defaultFormatAlpha, glIntFormat, glFormat, glType);
assert(supported);
_cursor = new Texture(glIntFormat, glFormat, glType, _defaultFormatAlpha);
}
} }
_cursor = createTexture(textureFormat);
assert(_cursor); assert(_cursor);
_cursor->enableLinearFiltering(_currentState.graphicsMode == GFX_LINEAR); _cursor->enableLinearFiltering(_currentState.graphicsMode == GFX_LINEAR);
} }
@ -778,10 +764,8 @@ void OpenGLGraphicsManager::setActualScreenSize(uint width, uint height) {
delete _overlay; delete _overlay;
_overlay = nullptr; _overlay = nullptr;
GLenum glIntFormat, glFormat, glType; _overlay = createTexture(_defaultFormatAlpha);
const bool supported = getGLPixelFormat(_defaultFormatAlpha, glIntFormat, glFormat, glType); assert(_overlay);
assert(supported);
_overlay = new Texture(glIntFormat, glFormat, glType, _defaultFormatAlpha);
// We always filter the overlay with GL_LINEAR. This assures it's // We always filter the overlay with GL_LINEAR. This assures it's
// readable in case it needs to be scaled and does not affect it // readable in case it needs to be scaled and does not affect it
// otherwise. // otherwise.
@ -795,10 +779,8 @@ void OpenGLGraphicsManager::setActualScreenSize(uint width, uint height) {
delete _osd; delete _osd;
_osd = nullptr; _osd = nullptr;
GLenum glIntFormat, glFormat, glType; _osd = createTexture(_defaultFormatAlpha);
const bool supported = getGLPixelFormat(_defaultFormatAlpha, glIntFormat, glFormat, glType); assert(_osd);
assert(supported);
_osd = new Texture(glIntFormat, glFormat, glType, _defaultFormatAlpha);
// We always filter the osd with GL_LINEAR. This assures it's // We always filter the osd with GL_LINEAR. This assures it's
// readable in case it needs to be scaled and does not affect it // readable in case it needs to be scaled and does not affect it
// otherwise. // otherwise.
@ -929,6 +911,25 @@ void OpenGLGraphicsManager::adjustMousePosition(int16 &x, int16 &y) {
} }
} }
Texture *OpenGLGraphicsManager::createTexture(const Graphics::PixelFormat &format) {
GLenum glIntFormat, glFormat, glType;
if (format.bytesPerPixel == 1) {
const bool supported = getGLPixelFormat(_defaultFormat, glIntFormat, glFormat, glType);
if (!supported) {
return nullptr;
} else {
return new TextureCLUT8(glIntFormat, glFormat, glType, _defaultFormat);
}
} else {
const bool supported = getGLPixelFormat(format, glIntFormat, glFormat, glType);
if (!supported) {
return nullptr;
} else {
return new Texture(glIntFormat, glFormat, glType, format);
}
}
}
bool OpenGLGraphicsManager::getGLPixelFormat(const Graphics::PixelFormat &pixelFormat, GLenum &glIntFormat, GLenum &glFormat, GLenum &glType) const { bool OpenGLGraphicsManager::getGLPixelFormat(const Graphics::PixelFormat &pixelFormat, GLenum &glIntFormat, GLenum &glFormat, GLenum &glType) const {
if (pixelFormat == Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0)) { // RGBA8888 if (pixelFormat == Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0)) { // RGBA8888
glIntFormat = GL_RGBA; glIntFormat = GL_RGBA;

View file

@ -171,6 +171,14 @@ protected:
virtual void setInternalMousePosition(int x, int y) = 0; virtual void setInternalMousePosition(int x, int y) = 0;
private: private:
/**
* Create a texture with the specified pixel format.
*
* @param format The pixel format the Texture object should accept as input.
* @return A pointer to the texture or nullptr on failure.
*/
Texture *createTexture(const Graphics::PixelFormat &format);
// //
// Transaction support // Transaction support
// //