From bbdb87a83151e1fdf6770523f0a90fe825e88965 Mon Sep 17 00:00:00 2001 From: Alejandro Marzini Date: Fri, 23 Jul 2010 06:57:23 +0000 Subject: OPENGL: Add basic game screen drawing. Changed Overlay PixelFormat to RGBA5551. svn-id: r51193 --- backends/graphics/opengl/gltexture.cpp | 19 ++++++-- backends/graphics/opengl/gltexture.h | 3 +- backends/graphics/opengl/opengl-graphics.cpp | 54 +++++++++++++++++----- backends/graphics/openglsdl/openglsdl-graphics.cpp | 4 +- 4 files changed, 62 insertions(+), 18 deletions(-) (limited to 'backends') diff --git a/backends/graphics/opengl/gltexture.cpp b/backends/graphics/opengl/gltexture.cpp index c5dc2f2507..98291edfac 100644 --- a/backends/graphics/opengl/gltexture.cpp +++ b/backends/graphics/opengl/gltexture.cpp @@ -104,6 +104,10 @@ void GLTexture::refresh() { _refresh = true; } +void GLTexture::refreshBuffer() { + updateBuffer(_surface.pixels, _surface.pitch, 0, 0, _surface.w, _surface.h); +} + void GLTexture::allocBuffer(GLuint w, GLuint h) { _realWidth = w; _realHeight = h; @@ -133,7 +137,7 @@ void GLTexture::allocBuffer(GLuint w, GLuint h) { if (_surface.w != _textureWidth || _surface.h != _textureHeight) _surface.create(_textureWidth, _textureHeight, _bytesPerPixel); else if (_refresh) - updateBuffer(_surface.pixels, _surface.pitch, 0, 0, _surface.w, _surface.h); + refreshBuffer(); _refresh = false; } @@ -147,7 +151,7 @@ void GLTexture::updateBuffer(const void *buf, int pitch, GLuint x, GLuint y, GLu if (buf != _surface.pixels) memcpy(_surface.getBasePtr(x, y), buf, h * pitch); } else { - const byte* src = static_cast(buf); + const byte *src = static_cast(buf); do { glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, 1, _glFormat, _glType, src); CHECK_GL_ERROR(); @@ -159,8 +163,15 @@ void GLTexture::updateBuffer(const void *buf, int pitch, GLuint x, GLuint y, GLu } } -void GLTexture::fillBuffer(byte x) { - memset(_surface.pixels, x, _surface.h * _surface.pitch); +void GLTexture::fillBuffer(uint32 x) { + if (_bytesPerPixel == 1) + memset(_surface.pixels, x, _surface.w * _surface.h); + else { + for (int i = 0; i < _surface.w * _surface.h; i++) { + memcpy(_surface.pixels, &x, _bytesPerPixel); + } + } + glBindTexture(GL_TEXTURE_2D, _textureName); CHECK_GL_ERROR(); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _surface.w, _surface.h, _glFormat, _glType, _surface.pixels); CHECK_GL_ERROR(); diff --git a/backends/graphics/opengl/gltexture.h b/backends/graphics/opengl/gltexture.h index 4a3ba5da8e..abdb7eceaa 100644 --- a/backends/graphics/opengl/gltexture.h +++ b/backends/graphics/opengl/gltexture.h @@ -59,9 +59,10 @@ public: virtual ~GLTexture(); virtual void refresh(); + virtual void refreshBuffer(); virtual void allocBuffer(GLuint width, GLuint height); - virtual void fillBuffer(byte x); + virtual void fillBuffer(uint32 x); virtual void updateBuffer(const void *buf, int pitch, GLuint x, GLuint y, GLuint w, GLuint h); diff --git a/backends/graphics/opengl/opengl-graphics.cpp b/backends/graphics/opengl/opengl-graphics.cpp index 8cc1e1ac29..5bb496d1ba 100644 --- a/backends/graphics/opengl/opengl-graphics.cpp +++ b/backends/graphics/opengl/opengl-graphics.cpp @@ -168,8 +168,7 @@ void OpenGLGraphicsManager::initSize(uint width, uint height, const Graphics::Pi // Avoid redundant format changes Graphics::PixelFormat newFormat; if (!format) - newFormat = Graphics::PixelFormat(3, 8, 8, 8, 0, 16, 8, 0, 0); - //newFormat = Graphics::PixelFormat::createFormatCLUT8(); + newFormat = Graphics::PixelFormat::createFormatCLUT8(); else newFormat = *format; @@ -327,7 +326,30 @@ void OpenGLGraphicsManager::grabPalette(byte *colors, uint start, uint num) { } void OpenGLGraphicsManager::copyRectToScreen(const byte *buf, int pitch, int x, int y, int w, int h) { - _gameTexture->updateBuffer(buf, pitch, x, y, w, h); + if (_screenFormat == Graphics::PixelFormat::createFormatCLUT8()) { + // Create a temporary RGBA888 surface + byte *surface = new byte[w * h * 3]; + + // Convert the paletted buffer to RGBA888 + const byte *src = buf; + byte *dst = surface; + for (int i = 0; i < h; i++) { + for (int j = 0; j < w; j++) { + dst[0] = _gamePalette[src[j] * 4]; + dst[1] = _gamePalette[src[j] * 4 + 1]; + dst[2] = _gamePalette[src[j] * 4 + 2]; + dst += 3; + } + src += pitch; + } + + // Update the texture + _gameTexture->updateBuffer(surface, w * 3, x, y, w, h); + + // Free the temp surface + delete[] surface; + } else + _gameTexture->updateBuffer(buf, pitch, x, y, w, h); } Graphics::Surface *OpenGLGraphicsManager::lockScreen() { @@ -335,7 +357,7 @@ Graphics::Surface *OpenGLGraphicsManager::lockScreen() { } void OpenGLGraphicsManager::unlockScreen() { - _gameTexture->refresh(); + _gameTexture->refreshBuffer(); } void OpenGLGraphicsManager::fillScreen(uint32 col) { @@ -545,17 +567,18 @@ void OpenGLGraphicsManager::getGLPixelFormat(Graphics::PixelFormat pixelFormat, bpp = 2; glFormat = GL_RGB; gltype = GL_UNSIGNED_SHORT_5_6_5; - } else if (pixelFormat == Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0)) { // RGB555 + } else if (pixelFormat == Graphics::PixelFormat(2, 5, 5, 5, 1, 11, 6, 1, 0)) { // RGB5551 bpp = 2; - glFormat = GL_RGB; + glFormat = GL_RGBA; gltype = GL_UNSIGNED_SHORT_5_5_5_1; } else if (pixelFormat == Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0)) { // RGBA4444 bpp = 2; glFormat = GL_RGBA; gltype = GL_UNSIGNED_SHORT_4_4_4_4; } else if (pixelFormat == Graphics::PixelFormat::createFormatCLUT8()) { // CLUT8 - bpp = 1; - glFormat = GL_COLOR_INDEX; + // If uses a palette, create as RGBA888, then convert + bpp = 3; + glFormat = GL_RGB; gltype = GL_UNSIGNED_BYTE; } else { error("Not supported format"); @@ -621,14 +644,23 @@ void OpenGLGraphicsManager::loadTextures() { byte bpp; GLenum format; GLenum type; +#ifdef USE_RGB_COLOR getGLPixelFormat(_screenFormat, bpp, format, type); +#else + getGLPixelFormat(Graphics::PixelFormat::createFormatCLUT8(), bpp, format, type); +#endif _gameTexture = new GLTexture(bpp, format, type); } - _overlayFormat = Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0); + _overlayFormat = Graphics::PixelFormat(2, 5, 5, 5, 1, 11, 6, 1, 0); - if (!_overlayTexture) - _overlayTexture = new GLTexture(2, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4); + if (!_overlayTexture) { + byte bpp; + GLenum format; + GLenum type; + getGLPixelFormat(_overlayFormat, bpp, format, type); + _overlayTexture = new GLTexture(bpp, format, type); + } if (!_cursorTexture) _cursorTexture = new GLTexture(4, GL_RGBA, GL_UNSIGNED_BYTE); diff --git a/backends/graphics/openglsdl/openglsdl-graphics.cpp b/backends/graphics/openglsdl/openglsdl-graphics.cpp index 4b77cf6f17..aa0a9aa717 100644 --- a/backends/graphics/openglsdl/openglsdl-graphics.cpp +++ b/backends/graphics/openglsdl/openglsdl-graphics.cpp @@ -127,8 +127,8 @@ bool OpenGLSdlGraphicsManager::loadGFXMode() { _videoMode.overlayWidth = _videoMode.screenWidth * _videoMode.scaleFactor; _videoMode.overlayHeight = _videoMode.screenHeight * _videoMode.scaleFactor; if (!_screenResized) { - _videoMode.hardwareWidth = _videoMode.screenWidth * _videoMode.scaleFactor; - _videoMode.hardwareHeight = _videoMode.screenHeight * _videoMode.scaleFactor; + _videoMode.hardwareWidth = _videoMode.overlayWidth; + _videoMode.hardwareHeight = _videoMode.overlayHeight; } _screenResized = false; -- cgit v1.2.3