diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp index c4a18211c45..cab456ba339 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp @@ -134,7 +134,7 @@ Graphics::PixelBuffer SurfaceSdlGraphicsManager::setupScreen(int screenW, int sc } else #endif { - bpp = 24; + bpp = 16; sdlflags = SDL_HWSURFACE; } diff --git a/engines/grim/bitmap.cpp b/engines/grim/bitmap.cpp index db50a4ea3be..b88c3e283ca 100644 --- a/engines/grim/bitmap.cpp +++ b/engines/grim/bitmap.cpp @@ -199,7 +199,7 @@ bool BitmapData::loadGrimBm(const Common::String &fname, Common::SeekableReadStr return true; } -BitmapData::BitmapData(const char *data, int w, int h, int bpp, const char *fname) { +BitmapData::BitmapData(const Graphics::PixelBuffer &buf, int w, int h, const char *fname) { _fname = fname; _refCount = 1; Debug::debug(Debug::Bitmaps, "New bitmap loaded: %s\n", fname); @@ -211,13 +211,13 @@ BitmapData::BitmapData(const char *data, int w, int h, int bpp, const char *fnam _format = 1; _numTex = 0; _texIds = NULL; - _bpp = bpp; + _bpp = buf.getFormat().bytesPerPixel * 8; _hasTransparency = false; _colorFormat = BM_RGB565; - _pixelFormat = Graphics::createPixelFormat<565>(); + _pixelFormat = buf.getFormat(); _data = new char *[_numImages]; _data[0] = new char[_bpp / 8 * _width * _height]; - memcpy(_data[0], data, _bpp / 8 * _width * _height); + memcpy(_data[0], buf.getRawBuffer(), _bpp / 8 * _width * _height); g_driver->createBitmap(this); } @@ -248,24 +248,24 @@ BitmapData::~BitmapData() { } } } - + bool BitmapData::loadTGA(const Common::String &fname, Common::SeekableReadStream *data) { data->seek(0, SEEK_SET); if (data->readByte() != 0) // Verify that description-field is empty return false; data->seek(1, SEEK_CUR); - + int format = data->readByte(); if (format != 2) return false; - + data->seek(9, SEEK_CUR); _width = data->readUint16LE(); _height = data->readUint16LE();; _format = 1; _x = 0; _y = 0; - + int bpp = data->readByte(); if (bpp == 32) { _colorFormat = BM_RGBA; @@ -273,7 +273,7 @@ bool BitmapData::loadTGA(const Common::String &fname, Common::SeekableReadStream } else { return false; } - + uint8 desc = data->readByte(); uint8 flipped = !(desc & 32); @@ -289,16 +289,16 @@ bool BitmapData::loadTGA(const Common::String &fname, Common::SeekableReadStream writePtr -= (_width * bpp / 8); } } else { - data->read(_data[0], _width * _height * (bpp / 8)); + data->read(_data[0], _width * _height * (bpp / 8)); } - + uint8 x; for (int i = 0; i < _width * _height * (bpp / 8); i+=4) { x = _data[0][i]; _data[0][i] = _data[0][i + 2]; _data[0][i + 2] = x; } - + _numImages = 1; g_driver->createBitmap(this); return true; @@ -378,9 +378,9 @@ Bitmap::Bitmap(const Common::String &fname, Common::SeekableReadStream *data) : _currImage = 1; } -Bitmap::Bitmap(const char *data, int w, int h, int bpp, const char *fname) : +Bitmap::Bitmap(const Graphics::PixelBuffer &buf, int w, int h, const char *fname) : PoolObject() { - _data = new BitmapData(data, w, h, bpp, fname); + _data = new BitmapData(buf, w, h, fname); _x = _data->_x; _y = _data->_y; _currImage = 1; @@ -544,23 +544,27 @@ void BitmapData::convertToColorFormat(int num, int format) { } } -void BitmapData::convertToColorFormat(int num, const Graphics::PixelFormat &format) { +void BitmapData::convertToColorFormat(const Graphics::PixelFormat &format) { if (_pixelFormat == format) { return; } - Graphics::PixelBuffer src(_pixelFormat, (byte *)_data[num]); - Graphics::PixelBuffer dst(format, _width * _height, DisposeAfterUse::NO); + for (int num = 0; num < _numImages; ++num) { + Graphics::PixelBuffer src(_pixelFormat, (byte *)_data[num]); + Graphics::PixelBuffer dst(format, _width * _height, DisposeAfterUse::NO); - for (int i = 0; i < _width * _height; ++i) { - if (src.getValueAt(i) == 0xf81f) { //transparency - dst.setPixelAt(i, 0xf81f); - } else { - dst.setPixelAt(i, src); + for (int i = 0; i < _width * _height; ++i) { + if (src.getValueAt(i) == 0xf81f) { //transparency + dst.setPixelAt(i, 0xf81f); + } else { + dst.setPixelAt(i, src); + } } + delete _data[num]; + _data[num] = (char *)dst.getRawBuffer(); } - delete _data[num]; - _data[num] = (char *)dst.getRawBuffer(); + + _pixelFormat = format; } #define GET_BIT do { bit = bitstr_value & 1; \ diff --git a/engines/grim/bitmap.h b/engines/grim/bitmap.h index dc34de739a6..80af0c91dfc 100644 --- a/engines/grim/bitmap.h +++ b/engines/grim/bitmap.h @@ -27,6 +27,10 @@ #include "engines/grim/pool.h" +namespace Graphics { +class PixelBuffer; +} + namespace Grim { /** @@ -39,7 +43,7 @@ namespace Grim { class BitmapData { public: BitmapData(const Common::String &fname, Common::SeekableReadStream *data); - BitmapData(const char *data, int w, int h, int bpp, const char *fname); + BitmapData(const Graphics::PixelBuffer &buf, int w, int h, const char *fname); BitmapData(); ~BitmapData(); @@ -67,7 +71,7 @@ public: */ void convertToColorFormat(int num, int format); - void convertToColorFormat(int num, const Graphics::PixelFormat &format); + void convertToColorFormat(const Graphics::PixelFormat &format); Common::String _fname; int _numImages; @@ -98,7 +102,7 @@ public: * @param len the length of the data */ Bitmap(const Common::String &filename, Common::SeekableReadStream *data); - Bitmap(const char *data, int width, int height, int bpp, const char *filename); + Bitmap(const Graphics::PixelBuffer &buf, int width, int height, const char *filename); Bitmap(); const Common::String &getFilename() const { return _data->_fname; } @@ -127,6 +131,7 @@ public: char *getData() const { return getData(_currImage); } void *getTexIds() const { return _data->_texIds; } int getNumTex() const { return _data->_numTex; } + const Graphics::PixelFormat &getPixelFormat() const { return _data->_pixelFormat; } void saveState(SaveGame *state) const; void restoreState(SaveGame *state); diff --git a/engines/grim/gfx_base.h b/engines/grim/gfx_base.h index 6e416bc16bc..7a641fe7f9e 100644 --- a/engines/grim/gfx_base.h +++ b/engines/grim/gfx_base.h @@ -25,6 +25,8 @@ #include "math/vector3d.h" +#include "graphics/pixelformat.h" + namespace Graphics { struct Surface; } @@ -233,6 +235,7 @@ protected: bool _renderBitmaps; bool _renderZBitmaps; bool _shadowModeActive; + Graphics::PixelFormat _pixelFormat; }; // Factory-like functions: diff --git a/engines/grim/gfx_opengl.cpp b/engines/grim/gfx_opengl.cpp index 87cd5ddea9e..3e261b22c72 100644 --- a/engines/grim/gfx_opengl.cpp +++ b/engines/grim/gfx_opengl.cpp @@ -92,7 +92,7 @@ GfxOpenGL::~GfxOpenGL() { } byte *GfxOpenGL::setupScreen(int screenW, int screenH, bool fullscreen) { - g_system->setupScreen(screenW, screenH, fullscreen, true); + _pixelFormat = g_system->setupScreen(screenW, screenH, fullscreen, true).getFormat(); _screenWidth = screenW; _screenHeight = screenH; @@ -1211,40 +1211,32 @@ void GfxOpenGL::drawEmergString(int x, int y, const char *text, const Color &fgC } Bitmap *GfxOpenGL::getScreenshot(int w, int h) { - uint16 *buffer = new uint16[w * h]; - uint32 *src = (uint32 *)_storedDisplay; + Graphics::PixelBuffer buffer = Graphics::PixelBuffer::createBuffer<565>(w * h, DisposeAfterUse::YES); + Graphics::PixelBuffer src(Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24), _screenWidth * _screenHeight, DisposeAfterUse::YES); + glReadPixels(0, 0, _screenWidth, _screenHeight, GL_RGBA, GL_UNSIGNED_BYTE, src.getRawBuffer()); int step = 0; for (int y = 0; y <= 479; y++) { for (int x = 0; x <= 639; x++) { - uint32 pixel = *(src + y * 640 + x); - uint8 r = (pixel & 0xFF0000); - uint8 g = (pixel & 0x00FF00); - uint8 b = (pixel & 0x0000FF); + uint8 r, g, b; + src.getRGBAt(y * 640 + x, r, g, b); uint32 color = (r + g + b) / 3; - src[step++] = ((color << 24) | (color << 16) | (color << 8) | color); + src.setPixelAt(step++, color, color, color); } } - float step_x = _screenWidth * 1.0f / w; - float step_y = _screenHeight * 1.0f / h; + float step_x = (float)_screenWidth / w; + float step_y = (float)_screenHeight / h; step = 0; - for (float y = 0; y < 479; y += step_y) { + for (float y = 479; y >= 0; y -= step_y) { for (float x = 0; x < 639; x += step_x) { - uint32 pixel = *(src + (int)y * _screenWidth + (int)x); - uint8 r = (pixel & 0xFF0000) >> 16; - uint8 g = (pixel & 0x00FF00) >> 8; - uint8 b = (pixel & 0x0000FF); - int pos = step / w; - int wpos = step - pos * w; - // source is upside down, flip appropriately while storing - buffer[h * w - (pos * w + w - wpos)] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); - step++; + uint8 r, g, b; + src.getRGBAt((int)y * _screenWidth + (int)x, r, g, b); + buffer.setPixelAt(step++, r, g, b); } } - Bitmap *screenshot = new Bitmap((char *)buffer, w, h, 16, "screenshot"); - delete[] buffer; + Bitmap *screenshot = new Bitmap(buffer, w, h, "screenshot"); return screenshot; } diff --git a/engines/grim/gfx_tinygl.cpp b/engines/grim/gfx_tinygl.cpp index 1281a74e524..072439024dc 100644 --- a/engines/grim/gfx_tinygl.cpp +++ b/engines/grim/gfx_tinygl.cpp @@ -152,7 +152,6 @@ GfxTinyGL::GfxTinyGL() { } GfxTinyGL::~GfxTinyGL() { - delete[] _storedDisplay; if (_zb) { TinyGL::glClose(); ZB_close(_zb); @@ -172,12 +171,12 @@ byte *GfxTinyGL::setupScreen(int screenW, int screenH, bool fullscreen) { g_system->setWindowCaption("ResidualVM: Software 3D Renderer"); _pixelFormat = buf.getFormat(); - _zb = TinyGL::ZB_open(screenW, screenH, _pixelFormat, buffer); + _zb = TinyGL::ZB_open(screenW, screenH, buf); TinyGL::glInit(_zb); _screenSize = 640 * 480 * _pixelFormat.bytesPerPixel; - _storedDisplay = new byte[_screenSize]; - memset(_storedDisplay, 0, _screenSize); + _storedDisplay.create(_pixelFormat, 640 * 480, DisposeAfterUse::YES); + _storedDisplay.clear(640 * 480); _currentShadowArray = NULL; @@ -214,9 +213,9 @@ void GfxTinyGL::positionCamera(Math::Vector3d pos, Math::Vector3d interest) { } void GfxTinyGL::clearScreen() { - memset(_zb->pbuf, 0, _screenSize); - memset(_zb->zbuf, 0, _screenSize); - memset(_zb->zbuf2, 0, _screenSize); + _zb->pbuf.clear(_screenSize); + memset(_zb->zbuf, 0, 640 * 480 * 2); + memset(_zb->zbuf2, 0, 640 * 480 * 4); } void GfxTinyGL::flipBuffer() { @@ -630,13 +629,10 @@ void GfxTinyGL::turnOffLight(int lightId) { } void GfxTinyGL::createBitmap(BitmapData *bitmap) { - // We want an RGB565-bitmap in TinyGL. -// if (bitmap->_colorFormat != BM_RGB565) { if (bitmap->_format == 1) { - bitmap->convertToColorFormat(0, _pixelFormat); -// bitmap->convertToColorFormat(0, BM_RGB565); + bitmap->convertToColorFormat(_pixelFormat); } else { // The zbuffer is still 16 bpp, 565 -// bitmap->convertToColorFormat(0, Graphics::createPixelFormat<565>()); + bitmap->convertToColorFormat(Graphics::createPixelFormat<565>()); } if (bitmap->_format != 1) { for (int pic = 0; pic < bitmap->_numImages; pic++) { @@ -653,7 +649,7 @@ void GfxTinyGL::createBitmap(BitmapData *bitmap) { } } -void TinyGLBlit(TinyGL::ZBuffer *zb, byte *dst, byte *src, int x, int y, int width, int height, bool trans) { +void TinyGLBlit(const Graphics::PixelFormat &format, byte *dst, byte *src, int x, int y, int width, int height, bool trans) { int srcX, srcY; int l, r; @@ -679,13 +675,13 @@ void TinyGLBlit(TinyGL::ZBuffer *zb, byte *dst, byte *src, int x, int y, int wid if (y + height > 480) height -= (y + height) - 480; - dst += (x + (y * 640)) * zb->pixelbytes; - src += (srcX + (srcY * width)) * zb->pixelbytes; + dst += (x + (y * 640)) * format.bytesPerPixel; + src += (srcX + (srcY * width)) * format.bytesPerPixel; - int copyWidth = width * zb->pixelbytes; + int copyWidth = width * format.bytesPerPixel; - Graphics::PixelBuffer srcBuf(zb->cmode, src); - Graphics::PixelBuffer dstBuf(zb->cmode, dst); + Graphics::PixelBuffer srcBuf(format, src); + Graphics::PixelBuffer dstBuf(format, dst); if (!trans) { for (l = 0; l < height; l++) { @@ -714,10 +710,10 @@ void GfxTinyGL::drawBitmap(const Bitmap *bitmap) { assert(bitmap->getActiveImage() > 0); if (bitmap->getFormat() == 1) - TinyGLBlit(_zb, (byte *)_zb->pbuf, (byte *)bitmap->getData(bitmap->getActiveImage() - 1), + TinyGLBlit(bitmap->getPixelFormat(), (byte *)_zb->pbuf.getRawBuffer(), (byte *)bitmap->getData(bitmap->getActiveImage() - 1), bitmap->getX(), bitmap->getY(), bitmap->getWidth(), bitmap->getHeight(), true); else - TinyGLBlit(_zb, (byte *)_zb->zbuf, (byte *)bitmap->getData(bitmap->getActiveImage() - 1), + TinyGLBlit(bitmap->getPixelFormat(), (byte *)_zb->zbuf, (byte *)bitmap->getData(bitmap->getActiveImage() - 1), bitmap->getX(), bitmap->getY(), bitmap->getWidth(), bitmap->getHeight(), false); } @@ -772,11 +768,8 @@ void GfxTinyGL::createTextObject(TextObject *text) { startOffset += charWidth; } - byte *bu = new byte[width * height * _zb->pixelbytes]; - Graphics::PixelBuffer buf(_zb->cmode, bu); + Graphics::PixelBuffer buf(_pixelFormat, width * height, DisposeAfterUse::NO); - uint16 *texData = new uint16[width * height]; - uint16 *texDataPtr = texData; uint8 *bitmapData = _textBitmap; uint8 r = fgColor->getRed(); uint8 g = fgColor->getGreen(); @@ -814,7 +807,7 @@ void GfxTinyGL::drawTextObject(TextObject *text) { if (userData) { int numLines = text->getNumLines(); for (int i = 0; i < numLines; ++i) { - TinyGLBlit(_zb, (byte *)_zb->pbuf, userData[i].data, userData[i].x, userData[i].y, userData[i].width, userData[i].height, true); + TinyGLBlit(_pixelFormat, (byte *)_zb->pbuf.getRawBuffer(), userData[i].data, userData[i].x, userData[i].y, userData[i].width, userData[i].height, true); } } @@ -901,19 +894,17 @@ void GfxTinyGL::destroyMaterial(Texture *material) { void GfxTinyGL::prepareMovieFrame(Graphics::Surface* frame) { _smushWidth = frame->w; _smushHeight = frame->h; - _smushBitmap = (byte *)frame->pixels; Graphics::PixelBuffer srcBuf(frame->format, (byte *)frame->pixels); - Graphics::PixelBuffer dstBuf(_pixelFormat, frame->w * frame->h, DisposeAfterUse::NO); - dstBuf.copyBuffer(0, frame->w * frame->h, srcBuf); - _smushBitmap = dstBuf.getRawBuffer(); + _smushBitmap.create(_pixelFormat, frame->w * frame->h, DisposeAfterUse::YES); + _smushBitmap.copyBuffer(0, frame->w * frame->h, srcBuf); } void GfxTinyGL::drawMovieFrame(int offsetX, int offsetY) { if (_smushWidth == 640 && _smushHeight == 480) { - memcpy(_zb->pbuf, _smushBitmap, _screenSize); + _zb->pbuf.copyBuffer(0, 640 * 480, _smushBitmap); } else { - TinyGLBlit(_zb, (byte *)_zb->pbuf, _smushBitmap, offsetX, offsetY, _smushWidth, _smushHeight, false); + TinyGLBlit(_pixelFormat, (byte *)_zb->pbuf.getRawBuffer(), _smushBitmap.getRawBuffer(), offsetX, offsetY, _smushWidth, _smushHeight, false); } } @@ -925,7 +916,6 @@ void GfxTinyGL::loadEmergFont() { void GfxTinyGL::drawEmergString(int x, int y, const char *text, const Color &fgColor) { uint32 color = _pixelFormat.RGBToColor(fgColor.getRed(), fgColor.getGreen(), fgColor.getBlue()); - Graphics::PixelBuffer buf(_pixelFormat, _zb->pbuf); for (int l = 0; l < (int)strlen(text); l++) { int c = text[l]; @@ -939,7 +929,7 @@ void GfxTinyGL::drawEmergString(int x, int y, const char *text, const Color &fgC int pixel = line & 0x80; line <<= 1; if (pixel) { - buf.setPixelAt(((py + y) * 640) + (px + x), color); + _zb->pbuf.setPixelAt(((py + y) * 640) + (px + x), color); } } } @@ -950,19 +940,15 @@ void GfxTinyGL::drawEmergString(int x, int y, const char *text, const Color &fgC } Bitmap *GfxTinyGL::getScreenshot(int w, int h) { - uint16 *buffer = new uint16[w * h]; - uint16 *src = (uint16 *)_storedDisplay; - assert(buffer); + Graphics::PixelBuffer buffer = Graphics::PixelBuffer::createBuffer<565>(w * h, DisposeAfterUse::YES); int step = 0; for (int y = 0; y <= 479; y++) { for (int x = 0; x <= 639; x++) { - uint16 pixel = *(src + y * 640 + x); - uint8 r = (pixel & 0xF800) >> 8; - uint8 g = (pixel & 0x07E0) >> 3; - uint8 b = (pixel & 0x001F) << 3; + uint8 r, g, b; + _zb->pbuf.getRGBAt(y * 640 + x, r, g, b); uint32 color = (r + g + b) / 3; - src[step++] = ((color & 0xF8) << 8) | ((color & 0xFC) << 3) | (color >> 3); + _zb->pbuf.setPixelAt(step++, color, color,color); } } @@ -971,98 +957,92 @@ Bitmap *GfxTinyGL::getScreenshot(int w, int h) { step = 0; for (float y = 0; y < 479; y += step_y) { for (float x = 0; x < 639; x += step_x) { - uint16 pixel = *(src + (int)y * 640 + (int)x); - buffer[step++] = pixel; + uint8 r, g, b; + _zb->pbuf.getRGBAt((int)y * 640 + (int)x, r, g, b); + buffer.setPixelAt(step++, r, g, b); } } - Bitmap *screenshot = new Bitmap((char *)buffer, w, h, 16, "screenshot"); - delete[] buffer; + Bitmap *screenshot = new Bitmap(buffer, w, h, "screenshot"); return screenshot; } void GfxTinyGL::storeDisplay() { - memcpy(_storedDisplay, _zb->pbuf, _screenSize); + _storedDisplay.copyBuffer(0, 640 * 408, _zb->pbuf); } void GfxTinyGL::copyStoredToDisplay() { - memcpy(_zb->pbuf, _storedDisplay, _screenSize); + _zb->pbuf.copyBuffer(0, 640 * 480, _storedDisplay); } void GfxTinyGL::dimScreen() { - Graphics::PixelBuffer buf(_pixelFormat, (byte *)_storedDisplay); for (int l = 0; l < 640 * 480; l++) { uint8 r, g, b; - buf.getRGBAt(l, r, g, b); - buf.setPixelAt(l, r / 10, g / 10, b / 10); + _storedDisplay.getRGBAt(l, r, g, b); + uint32 color = (r + g + b) / 10; + _storedDisplay.setPixelAt(l, color, color, color); } } void GfxTinyGL::dimRegion(int x, int y, int w, int h, float level) { - uint32 *data = (uint32 *)_zb->pbuf; for (int ly = y; ly < y + h; ly++) { for (int lx = x; lx < x + w; lx++) { - uint16 pixel = data[ly * 640 + lx]; - uint8 r = (pixel & 0xF800) >> 8; - uint8 g = (pixel & 0x07E0) >> 3; - uint8 b = (pixel & 0x001F) << 3; - uint16 color = (uint16)(((r + g + b) / 3) * level); - data[ly * 640 + lx] = ((color & 0xF8) << 8) | ((color & 0xFC) << 3) | (color >> 3); + uint8 r, g, b; + _zb->pbuf.getRGBAt(ly * 640 + lx, r, g, b); + uint32 color = ((r + g + b) / 3) * level; + _zb->pbuf.setPixelAt(ly * 640 + lx, color, color, color); } } } void GfxTinyGL::irisAroundRegion(int x1, int y1, int x2, int y2) { - uint16 *data = (uint16 *)_zb->pbuf; for (int ly = 0; ly < _screenHeight; ly++) { for (int lx = 0; lx < _screenWidth; lx++) { // Don't do anything with the data in the region we draw Around if(lx > x1 && lx < x2 && ly > y1 && ly < y2) continue; // But set everything around it to black. - data[ly * 640 + lx] = (uint16)0.0f; + _zb->pbuf.setPixelAt(ly * 640 + lx, 0); } } } void GfxTinyGL::drawRectangle(PrimitiveObject *primitive) { - uint16 *dst = (uint16 *)_zb->pbuf; int x1 = primitive->getP1().x; int y1 = primitive->getP1().y; int x2 = primitive->getP2().x; int y2 = primitive->getP2().y; const Color &color = *primitive->getColor(); - uint16 c = ((color.getRed() & 0xF8) << 8) | ((color.getGreen() & 0xFC) << 3) | (color.getBlue() >> 3); + uint32 c = _pixelFormat.RGBToColor(color.getRed(), color.getGreen(), color.getBlue()); if (primitive->isFilled()) { for (; y1 <= y2; y1++) if (y1 >= 0 && y1 < 480) for (int x = x1; x <= x2; x++) if (x >= 0 && x < 640) - WRITE_UINT16(dst + 640 * y1 + x, c); + _zb->pbuf.setPixelAt(640 * y1 + x, c); } else { if (y1 >= 0 && y1 < 480) for (int x = x1; x <= x2; x++) if (x >= 0 && x < 640) - WRITE_UINT16(dst + 640 * y1 + x, c); + _zb->pbuf.setPixelAt(640 * y1 + x, c); if (y2 >= 0 && y2 < 480) for (int x = x1; x <= x2; x++) if (x >= 0 && x < 640) - WRITE_UINT16(dst + 640 * y2 + x, c); + _zb->pbuf.setPixelAt(640 * y2 + x, c); if (x1 >= 0 && x1 < 640) for (int y = y1; y <= y2; y++) if (y >= 0 && y < 480) - WRITE_UINT16(dst + 640 * y + x1, c); + _zb->pbuf.setPixelAt(640 * y + x1, c); if (x2 >= 0 && x2 < 640) for (int y = y1; y <= y2; y++) if (y >= 0 && y < 480) - WRITE_UINT16(dst + 640 * y + x2, c); + _zb->pbuf.setPixelAt(640 * y + x2, c); } } void GfxTinyGL::drawLine(PrimitiveObject *primitive) { - Graphics::PixelBuffer buf(_pixelFormat, _zb->pbuf); int x1 = primitive->getP1().x; int y1 = primitive->getP1().y; int x2 = primitive->getP2().x; @@ -1073,7 +1053,7 @@ void GfxTinyGL::drawLine(PrimitiveObject *primitive) { if (x2 == x1) { for (int y = y1; y <= y2; y++) { if (x1 >= 0 && x1 < 640 && y >= 0 && y < 480) - buf.setPixelAt(640 * y + x1, color.getRed(), color.getGreen(), color.getBlue()); + _zb->pbuf.setPixelAt(640 * y + x1, color.getRed(), color.getGreen(), color.getBlue()); } } else { float m = (y2 - y1) / (x2 - x1); @@ -1081,13 +1061,12 @@ void GfxTinyGL::drawLine(PrimitiveObject *primitive) { for (int x = x1; x <= x2; x++) { int y = (int)(m * x) + b; if (x >= 0 && x < 640 && y >= 0 && y < 480) - buf.setPixelAt(640 * y + x, color.getRed(), color.getGreen(), color.getBlue()); + _zb->pbuf.setPixelAt(640 * y + x, color.getRed(), color.getGreen(), color.getBlue()); } } } void GfxTinyGL::drawPolygon(PrimitiveObject *primitive) { - uint16 *dst = (uint16 *)_zb->pbuf; int x1 = primitive->getP1().x; int y1 = primitive->getP1().y; int x2 = primitive->getP2().x; @@ -1100,21 +1079,21 @@ void GfxTinyGL::drawPolygon(PrimitiveObject *primitive) { int b; const Color &color = *primitive->getColor(); - uint16 c = ((color.getRed() & 0xF8) << 8) | ((color.getGreen() & 0xFC) << 3) | (color.getBlue() >> 3); + uint32 c = _pixelFormat.RGBToColor(color.getRed(), color.getGreen(), color.getBlue()); m = (y2 - y1) / (x2 - x1); b = (int)(-m * x1 + y1); for (int x = x1; x <= x2; x++) { int y = (int)(m * x) + b; if (x >= 0 && x < 640 && y >= 0 && y < 480) - WRITE_UINT16(dst + 640 * y + x, c); + _zb->pbuf.setPixelAt(640 * y + x, c); } m = (y4 - y3) / (x4 - x3); b = (int)(-m * x3 + y3); for (int x = x3; x <= x4; x++) { int y = (int)(m * x) + b; if (x >= 0 && x < 640 && y >= 0 && y < 480) - WRITE_UINT16(dst + 640 * y + x, c); + _zb->pbuf.setPixelAt(640 * y + x, c); } } diff --git a/engines/grim/gfx_tinygl.h b/engines/grim/gfx_tinygl.h index 9c8d66c48a5..1a287e0b649 100644 --- a/engines/grim/gfx_tinygl.h +++ b/engines/grim/gfx_tinygl.h @@ -115,13 +115,11 @@ public: protected: private: - Graphics::PixelFormat _pixelFormat; TinyGL::ZBuffer *_zb; - byte *_screen; - byte *_smushBitmap; + Graphics::PixelBuffer _smushBitmap; int _smushWidth; int _smushHeight; - byte *_storedDisplay; + Graphics::PixelBuffer _storedDisplay; }; } // end of namespace Grim diff --git a/engines/grim/lua_v1.cpp b/engines/grim/lua_v1.cpp index f19694f3344..35d21f17de6 100644 --- a/engines/grim/lua_v1.cpp +++ b/engines/grim/lua_v1.cpp @@ -25,6 +25,9 @@ #include "common/endian.h" #include "common/system.h" +#include "graphics/pixelbuffer.h" +#include "graphics/colormasks.h" + #include "math/matrix3.h" #include "engines/grim/debug.h" @@ -892,7 +895,8 @@ void Lua_V1::GetSaveGameImage() { for (int l = 0; l < dataSize / 2; l++) { data[l] = savedState->readLEUint16(); } - screenshot = new Bitmap((char *)data, width, height, 16, "screenshot"); + Graphics::PixelBuffer buf(Graphics::createPixelFormat<565>(), (byte *)data); + screenshot = new Bitmap(buf, width, height, "screenshot"); delete[] data; if (screenshot) { lua_pushusertag(screenshot->getId(), MKTAG('V','B','U','F')); diff --git a/engines/grim/lua_v1_graphics.cpp b/engines/grim/lua_v1_graphics.cpp index f7fb38c5d8a..28dc0f6477e 100644 --- a/engines/grim/lua_v1_graphics.cpp +++ b/engines/grim/lua_v1_graphics.cpp @@ -482,7 +482,6 @@ void Lua_V1::ScreenShot() { GrimEngine::EngineMode mode = g_grim->getMode(); g_grim->setMode(GrimEngine::NormalMode); g_grim->updateDisplayScene(); - g_driver->storeDisplay(); Bitmap *screenshot = g_driver->getScreenshot(width, height); g_grim->setMode(mode); if (screenshot) { diff --git a/graphics/pixelbuffer.cpp b/graphics/pixelbuffer.cpp index 34c88ce106f..8dac7de92ed 100644 --- a/graphics/pixelbuffer.cpp +++ b/graphics/pixelbuffer.cpp @@ -30,7 +30,8 @@ PixelBuffer::PixelBuffer() } -PixelBuffer::PixelBuffer(const PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose) { +PixelBuffer::PixelBuffer(const PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose) + : _buffer(NULL) { create(format, buffersize, dispose); } @@ -51,30 +52,56 @@ PixelBuffer::~PixelBuffer() { } void PixelBuffer::create(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose) { + if (_dispose == DisposeAfterUse::YES) + free(); + _format = format; _dispose = dispose; _buffer = new byte[buffersize * format.bytesPerPixel]; } +void PixelBuffer::create(int buffersize, DisposeAfterUse::Flag dispose) { + if (_dispose == DisposeAfterUse::YES) + free(); + + _dispose = dispose; + _buffer = new byte[buffersize * _format.bytesPerPixel]; +} + +void PixelBuffer::set(const Graphics::PixelFormat &format, byte *buffer) { + if (_dispose == DisposeAfterUse::YES) + free(); + + _format = format; + _buffer = buffer; +} + void PixelBuffer::free() { delete[] _buffer; _buffer = NULL; } +void PixelBuffer::clear(int length) { + memset(_buffer, 0, length); +} + void PixelBuffer::setPixelAt(int pixel, uint32 value) { +#if defined(SCUMM_BIG_ENDIAN) + byte *buffer = _buffer + pixel * _format.bytesPerPixel; + for (int i = 0; i < _format.bytesPerPixel; ++i) { + buffer[i] = value >> ((_format.bytesPerPixel - i - 1) * 8) & 0xFF; + } +#elif defined(SCUMM_LITTLE_ENDIAN) byte *buffer = _buffer + pixel * _format.bytesPerPixel; for (int i = 0; i < _format.bytesPerPixel; ++i) { buffer[i] = value >> (i * 8) & 0xFF; } -} - -void PixelBuffer::copyBuffer(int from, int length, const PixelBuffer &buf) { - copyBuffer(from, from, length, buf); +#endif } void PixelBuffer::copyBuffer(int thisFrom, int otherFrom, int length, const PixelBuffer &buf) { if (buf._format == _format) { - memcpy(_buffer + thisFrom * _format.bytesPerPixel , buf._buffer + otherFrom * _format.bytesPerPixel, length * _format.bytesPerPixel); + memcpy(_buffer + thisFrom * _format.bytesPerPixel, buf._buffer + otherFrom * _format.bytesPerPixel, length * _format.bytesPerPixel); } else { uint8 r, g, b; for (int i = 0; i < length; ++i) { @@ -85,21 +112,21 @@ void PixelBuffer::copyBuffer(int thisFrom, int otherFrom, int length, const Pixe } uint32 PixelBuffer::getValueAt(int i) const { - //TODO: Check this +#if defined(SCUMM_BIG_ENDIAN) + byte *buffer = _buffer + i * _format.bytesPerPixel; + uint32 p = buffer[0] << ((_format.bytesPerPixel - 1) * 8); + for (int l = 1; l < _format.bytesPerPixel; ++l) { + p = p | (buffer[l] << (8 * (_format.bytesPerPixel - l - 1))); + } + return p; +#elif defined(SCUMM_LITTLE_ENDIAN) byte *buffer = _buffer + i * _format.bytesPerPixel; uint32 p = buffer[0]; for (int l = 1; l < _format.bytesPerPixel; ++l) { p = p | (buffer[l] << (8 * l)); } return p; -} - -byte *PixelBuffer::getRawBuffer() const { - return _buffer; -} - -PixelFormat PixelBuffer::getFormat() const { - return _format; +#endif } PixelBuffer &PixelBuffer::operator=(const PixelBuffer &buf) { diff --git a/graphics/pixelbuffer.h b/graphics/pixelbuffer.h index 735bd6eb7ff..2f7d7afb510 100644 --- a/graphics/pixelbuffer.h +++ b/graphics/pixelbuffer.h @@ -25,45 +25,193 @@ #include "common/types.h" +#include "graphics/colormasks.h" #include "graphics/pixelformat.h" namespace Graphics { class PixelBuffer { public: + /** + * Create a PixelBuffer. + * Convenience syntax for PixelBuffer(createPixelFormat(), buffersize, dispose). + */ + template + inline static PixelBuffer createBuffer(int buffersize, DisposeAfterUse::Flag dispose) { + return PixelBuffer(createPixelFormat(), buffersize, dispose); + } + + /** + * Create a PixelBuffer. + * Convenience syntax for PixelBuffer(createPixelFormat(), buffer). + */ + template + inline static PixelBuffer createBuffer(byte *buffer) { + return PixelBuffer(createPixelFormat(), buffer); + } + + /** + * Construct an empty PixelBuffer. + */ PixelBuffer(); + /** + * Construct a PixelBuffer, allocating the internal buffer. + * + * @param format The format of the pixels in this buffer. + * @param buffersize The number of pixels the buffer will store. + * @param dispose If YES the internal buffer will be deleted when this object is destroyed, + */ PixelBuffer(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose); + /** + * Construct a PixelBuffer, using an already allocated buffer. + * + * @param format The format of the pixels in this buffer. + * @param buffer The raw buffer containing the pixels. + */ PixelBuffer(const Graphics::PixelFormat &format, byte *buffer); + /** + * Copy constructor. + * The internal buffer will NOT be duplicated, it will be shared between the instances. + */ PixelBuffer(const PixelBuffer &buf); + /** + * Destroy the object. + */ ~PixelBuffer(); + /** + * Initialize the buffer. + * + * @param format The format of the pixels. + * @param buffersize The number of pixels the buffer will store. + * @param dispose If YES the internal buffer will be deleted when this object is destroyed, + */ void create(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose); + /** + * Initialize the buffer, using the already set pixel format. + * @note If the pixel format was not set before the results are undefined. + * + * @param buffersize The number of pixels the buffer will store. + * @param dispose If YES the internal buffer will be deleted when this object is destroyed, + */ + void create(int buffersize, DisposeAfterUse::Flag dispose); + + /** + * Initialize the buffer. + * + * @param format The format of the pixels in this buffer. + * @param buffer The raw buffer containing the pixels. + */ + void set(const Graphics::PixelFormat &format, byte *buffer); + + /** + * Delete the internal pixel buffer. + */ void free(); + /** + * Reset the value of the pixels. + * + * @param length The length of the buffer, in pixels. + */ + void clear(int length); + + /** + * Set the value of the pixel at index 'pixel' to 'value', + */ void setPixelAt(int pixel, uint32 value); + /** + * Set the value of a pixel. The pixel will be converted from a pixel in another PixelBuffer, + * at the same index. + * + * @param pixel The index of the pixel to set. + * @param buf The buffer storing the source pixel. + */ inline void setPixelAt(int pixel, const PixelBuffer &buf) { setPixelAt(pixel, buf, pixel); } + /** + * Set the value of a pixel. The pixel will be converted from a pixel in another PixelBuffer. + * + * @param thisPix The index of the pixel to set. + * @param buf The buffer storing the source pixel. + * @param otherPix The index of the source pixel in 'buf'. + */ inline void setPixelAt(int thisPix, const PixelBuffer &buf, int otherPix) { uint8 a, r, g, b; buf.getARGBAt(otherPix, a, r, g, b); setPixelAt(thisPix, a, r, g, b); } + /** + * Set a pixel, from RGB values. + */ inline void setPixelAt(int pixel, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.RGBToColor(r, g, b)); } + /** + * Set a pixel, from ARGB values. + */ inline void setPixelAt(int pixel, uint8 a, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.ARGBToColor(a, r, g, b)); } - void copyBuffer(int from, int length, const PixelBuffer &buf); + /** + * Copy some pixels from a buffer. The pixels will be converted, storing the same ARGB value. + * + * @param from The starting index. + * @param length The number of pixels to copy. + * @param buf The source buffer. + */ + inline void copyBuffer(int from, int length, const PixelBuffer &buf) { copyBuffer(from, from, length, buf); } + /** + * Copy some pixels from a buffer. The pixels will be converted, storing the same ARGB value. + * + * @param thisFrom The starting index. + * @param otherFrom The starting index in the source buffer. + * @param length The number of pixels to copy. + * @param buf The source buffer. + */ void copyBuffer(int thisFrom, int otherFrom, int length, const PixelBuffer &buf); + /** + * Shift the internal buffer of some pixels, losing some pixels at the start of the buffer. + * The pixels lost will NOT be deleted. + */ inline void shiftBy(int amount) { _buffer += amount * _format.bytesPerPixel; } + /** + * Return the encoded value of the pixel at the given index. + */ uint32 getValueAt(int i) const; + /** + * Return the RGB value of the pixel at the given index. + */ inline void getRGBAt(int i, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToRGB(getValueAt(i), r, g, b); } + /** + * Return the ARGB value of the pixel at the given index. + */ inline void getARGBAt(int i, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToARGB(getValueAt(i), a, r, g, b); } - byte *getRawBuffer() const; - PixelFormat getFormat() const; + /** + * Return the internal buffer. + */ + inline byte *getRawBuffer() const { return _buffer; } + /** + * Return the pixel format used. + */ + inline PixelFormat getFormat() const { return _format; } + /** + * Copy a PixelBuffer object. + * The internal buffer will NOT be duplicated, it will be shared between the instances. + */ PixelBuffer &operator=(const PixelBuffer &buf); + /** + * Set the internal buffer to an already allocated array. + * + * @param buffer The pointer to the array. + */ PixelBuffer &operator=(byte *buffer); + + /** + * Check if the interal buffer is allocated. + * + * @returns true if allocated. + */ inline operator bool() const { return (_buffer); } private: diff --git a/graphics/tinygl/zbuffer.cpp b/graphics/tinygl/zbuffer.cpp index e0654198631..96d3e9e5ce6 100644 --- a/graphics/tinygl/zbuffer.cpp +++ b/graphics/tinygl/zbuffer.cpp @@ -13,7 +13,7 @@ namespace TinyGL { uint8 PSZB; -ZBuffer *ZB_open(int xsize, int ysize, const Graphics::PixelFormat &mode, void *frame_buffer) { +ZBuffer *ZB_open(int xsize, int ysize, const Graphics::PixelBuffer &frame_buffer) { ZBuffer *zb; int size; @@ -23,9 +23,9 @@ ZBuffer *ZB_open(int xsize, int ysize, const Graphics::PixelFormat &mode, void * zb->xsize = xsize; zb->ysize = ysize; - zb->cmode = mode; - PSZB = zb->pixelbytes = mode.bytesPerPixel; - zb->pixelbits = mode.bytesPerPixel * 8; + zb->cmode = frame_buffer.getFormat(); + PSZB = zb->pixelbytes = zb->cmode.bytesPerPixel; + zb->pixelbits = zb->cmode.bytesPerPixel * 8; zb->linesize = (xsize * zb->pixelbytes + 3) & ~3; size = zb->xsize * zb->ysize * sizeof(unsigned short); @@ -42,20 +42,19 @@ ZBuffer *ZB_open(int xsize, int ysize, const Graphics::PixelFormat &mode, void * goto error; } if (!frame_buffer) { - zb->pbuf = (byte *)gl_malloc(zb->ysize * zb->linesize); - if (!zb->pbuf) { + byte *pbuf = (byte *)gl_malloc(zb->ysize * zb->linesize); + if (!pbuf) { gl_free(zb->zbuf); gl_free(zb->zbuf2); goto error; } + zb->pbuf.set(zb->cmode, pbuf); zb->frame_buffer_allocated = 1; } else { zb->frame_buffer_allocated = 0; - zb->pbuf = (byte *)frame_buffer; + zb->pbuf = frame_buffer; } - zb->buffer = Graphics::PixelBuffer(mode, zb->pbuf); - zb->current_texture = NULL; zb->shadow_mask_buf = NULL; @@ -67,7 +66,7 @@ error: void ZB_close(ZBuffer *zb) { if (zb->frame_buffer_allocated) - gl_free(zb->pbuf); + zb->pbuf.free(); gl_free(zb->zbuf); gl_free(zb->zbuf2); @@ -95,10 +94,11 @@ void ZB_resize(ZBuffer *zb, void *frame_buffer, int xsize, int ysize) { zb->zbuf2 = (unsigned int *)gl_malloc(size); if (zb->frame_buffer_allocated) - gl_free(zb->pbuf); + zb->pbuf.free(); if (!frame_buffer) { - zb->pbuf = (byte *)gl_malloc(zb->ysize * zb->linesize); + byte *pbuf = (byte *)gl_malloc(zb->ysize * zb->linesize); + zb->pbuf.set(zb->cmode, pbuf); zb->frame_buffer_allocated = 1; } else { zb->pbuf = (byte *)frame_buffer; @@ -108,10 +108,10 @@ void ZB_resize(ZBuffer *zb, void *frame_buffer, int xsize, int ysize) { static void ZB_copyBuffer(ZBuffer *zb, void *buf, int linesize) { unsigned char *p1; - char *q; + byte *q; int y, n; - q = (char *)zb->pbuf; + q = zb->pbuf.getRawBuffer(); p1 = (unsigned char *)buf; n = zb->xsize * zb->pixelbytes; for (y = 0; y < zb->ysize; y++) { @@ -172,7 +172,7 @@ void memset_l(void *adr, int val, int count) { void ZB_clear(ZBuffer *zb, int clear_z, int z, int clear_color, int r, int g, int b) { uint32 color; int y; - char *pp; + byte *pp; if (clear_z) { memset_s(zb->zbuf, z, zb->xsize * zb->ysize); @@ -181,7 +181,7 @@ void ZB_clear(ZBuffer *zb, int clear_z, int z, int clear_color, int r, int g, in memset_l(zb->zbuf2, z, zb->xsize * zb->ysize); } if (clear_color) { - pp = (char *)zb->pbuf; + pp = zb->pbuf.getRawBuffer(); for (y = 0; y < zb->ysize; y++) { color = zb->cmode.RGBToColor(r, g, b); memset_s(pp, color, zb->xsize); diff --git a/graphics/tinygl/zbuffer.h b/graphics/tinygl/zbuffer.h index d2984f98852..d19f2606b11 100644 --- a/graphics/tinygl/zbuffer.h +++ b/graphics/tinygl/zbuffer.h @@ -46,8 +46,7 @@ typedef struct { int shadow_color_r; int shadow_color_g; int shadow_color_b; - byte *pbuf; - Graphics::PixelBuffer buffer; + Graphics::PixelBuffer pbuf; int frame_buffer_allocated; unsigned char *dctable; @@ -65,7 +64,7 @@ typedef struct { // zbuffer.c -ZBuffer *ZB_open(int xsize, int ysize, const Graphics::PixelFormat &mode, void *frame_buffer); +ZBuffer *ZB_open(int xsize, int ysize, const Graphics::PixelBuffer &buffer); void ZB_close(ZBuffer *zb); void ZB_resize(ZBuffer *zb, void *frame_buffer, int xsize, int ysize); void ZB_clear(ZBuffer *zb, int clear_z, int z, int clear_color, int r, int g, int b); diff --git a/graphics/tinygl/zline.cpp b/graphics/tinygl/zline.cpp index 731d2ffa10e..7f027aec97f 100644 --- a/graphics/tinygl/zline.cpp +++ b/graphics/tinygl/zline.cpp @@ -13,7 +13,7 @@ void ZB_plot(ZBuffer * zb, ZBufferPoint * p) { pz = zb->zbuf + (p->y * zb->xsize + p->x); pz_2 = zb->zbuf2 + (p->y * zb->xsize + p->x); - pp = (PIXEL *)((char *) zb->pbuf + zb->linesize * p->y + p->x * PSZB); + pp = (PIXEL *)((char *) zb->pbuf.getRawBuffer() + zb->linesize * p->y + p->x * PSZB); zz = p->z >> ZB_POINT_Z_FRAC_BITS; if ((ZCMP(zz, *pz)) && (ZCMP((unsigned int)p->z, *pz_2))) { *pp = RGB_TO_PIXEL(p->r, p->g, p->b); diff --git a/graphics/tinygl/zline.h b/graphics/tinygl/zline.h index 240d1145669..217a4870177 100644 --- a/graphics/tinygl/zline.h +++ b/graphics/tinygl/zline.h @@ -22,7 +22,7 @@ p2 = tmp; } sx = zb->xsize; - pp = (PIXEL *)((char *) zb->pbuf + zb->linesize * p1->y + p1->x * PSZB); + pp = (PIXEL *)((char *) zb->pbuf.getRawBuffer() + zb->linesize * p1->y + p1->x * PSZB); #ifdef INTERP_Z pz = zb->zbuf + (p1->y * sx + p1->x); pz_2 = zb->zbuf2 + (p1->y * sx + p1->x); @@ -116,4 +116,4 @@ #undef PUTPIXEL #undef ZZ #undef RGB -#undef RGBPIXEL +#undef RGBPIXEL diff --git a/graphics/tinygl/ztriangle.cpp b/graphics/tinygl/ztriangle.cpp index 2b195466f40..eb2f03ef5da 100644 --- a/graphics/tinygl/ztriangle.cpp +++ b/graphics/tinygl/ztriangle.cpp @@ -131,7 +131,6 @@ void ZB_fillTriangleMappingPerspective(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoi float fdx1, fdx2, fdy1, fdy2, fz0, d1, d2; unsigned short *pz1; unsigned int *pz2; - PIXEL *pp1; int part, update_left, update_right; int nb_lines, dx1, dy1, tmp, dx2, dy2; @@ -230,7 +229,7 @@ void ZB_fillTriangleMappingPerspective(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoi // screen coordinates - pp1 = (PIXEL *)((char *)zb->pbuf + zb->linesize * p0->y); + byte *pp1 = zb->pbuf.getRawBuffer() + zb->linesize * p0->y; pz1 = zb->zbuf + p0->y * zb->xsize; pz2 = zb->zbuf2 + p0->y * zb->xsize; @@ -343,8 +342,8 @@ void ZB_fillTriangleMappingPerspective(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoi fz = (float)z1; zinv = (float)(1.0 / fz); - Graphics::PixelBuffer &buf = zb->buffer; - buf = (byte *)pp1 + x1 * PSZB; + Graphics::PixelBuffer buf = zb->pbuf; + buf = pp1 + x1 * PSZB; pz = pz1 + x1; pz_2 = pz2 + x1; @@ -479,7 +478,7 @@ void ZB_fillTriangleMappingPerspective(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoi x2 += dx2dy2; // screen coordinates - pp1 = (PIXEL *)((char *)pp1 + zb->linesize); + pp1 += zb->linesize; pz1 += zb->xsize; pz2 += zb->xsize; } diff --git a/graphics/tinygl/ztriangle.h b/graphics/tinygl/ztriangle.h index 842cef06cfa..9b6a736b220 100644 --- a/graphics/tinygl/ztriangle.h +++ b/graphics/tinygl/ztriangle.h @@ -92,13 +92,13 @@ dbdy = (int)(fdx1 * d2 - fdx2 * d1); #endif - + #ifdef INTERP_ST d1 = (float)(p1->s - p0->s); d2 = (float)(p2->s - p0->s); dsdx = (int)(fdy2 * d1 - fdy1 * d2); dsdy = (int)(fdx1 * d2 - fdx2 * d1); - + d1 = (float)(p1->t - p0->t); d2 = (float)(p2->t - p0->t); dtdx = (int)(fdy2 * d1 - fdy1 * d2); @@ -132,7 +132,7 @@ // screen coordinates - pp1 = (PIXEL *)((char *)zb->pbuf + zb->linesize * p0->y); + pp1 = (PIXEL *)((char *)zb->pbuf.getRawBuffer() + zb->linesize * p0->y); pz1 = zb->zbuf + p0->y * zb->xsize; pz2 = zb->zbuf2 + p0->y * zb->xsize; @@ -166,7 +166,7 @@ } else { update_left = 1; update_right = 0; - l1 = p1; + l1 = p1; l2 = p2; } nb_lines = p2->y - p1->y + 1; @@ -177,7 +177,7 @@ if (update_left) { dy1 = l2->y - l1->y; dx1 = l2->x - l1->x; - if (dy1 > 0) + if (dy1 > 0) tmp = (dx1 << 16) / dy1; else tmp = 0; @@ -189,7 +189,7 @@ #ifdef INTERP_Z z1 = l1->z; - dzdl_min = (dzdy + dzdx * dxdy_min); + dzdl_min = (dzdy + dzdx * dxdy_min); dzdl_max = dzdl_min + dzdx; #endif #ifdef INTERP_RGB @@ -230,7 +230,7 @@ if (update_right) { dx2 = (pr2->x - pr1->x); dy2 = (pr2->y - pr1->y); - if (dy2>0) + if (dy2>0) dx2dy2 = ( dx2 << 16) / dy2; else dx2dy2 = 0; @@ -306,7 +306,7 @@ #else DRAW_LINE(); #endif - + // left edge error += derror; if (error > 0) { @@ -346,8 +346,8 @@ sz1 += dszdl_min; tz1 += dtzdl_min; #endif - } - + } + // right edge x2 += dx2dy2; @@ -365,5 +365,5 @@ #undef INTERP_STZ #undef DRAW_INIT -#undef DRAW_LINE +#undef DRAW_LINE #undef PUT_PIXEL diff --git a/graphics/tinygl/ztriangle_shadow.cpp b/graphics/tinygl/ztriangle_shadow.cpp index 8d38abd4851..170a35e7e3d 100644 --- a/graphics/tinygl/ztriangle_shadow.cpp +++ b/graphics/tinygl/ztriangle_shadow.cpp @@ -95,7 +95,7 @@ void ZB_fillTriangleFlatShadowMask(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint * if (update_left) { dy1 = l2->y - l1->y; dx1 = l2->x - l1->x; - if (dy1 > 0) + if (dy1 > 0) tmp = (dx1 << 16) / dy1; else tmp = 0; @@ -111,7 +111,7 @@ void ZB_fillTriangleFlatShadowMask(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint * if (update_right) { dx2 = (pr2->x - pr1->x); dy2 = (pr2->y - pr1->y); - if (dy2 > 0) + if (dy2 > 0) dx2dy2 = ( dx2 << 16) / dy2; else dx2dy2 = 0; @@ -141,7 +141,7 @@ void ZB_fillTriangleFlatShadowMask(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint * n -= 1; } } - + // left edge error += derror; if (error > 0) { @@ -149,8 +149,8 @@ void ZB_fillTriangleFlatShadowMask(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint * x1 += dxdy_max; } else { x1 += dxdy_min; - } - + } + // right edge x2 += dx2dy2; @@ -167,7 +167,7 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, unsigned char *pm1; unsigned short *pz1; unsigned int *pz2; - PIXEL *pp1; + byte *pp1; int part, update_left, update_right; int nb_lines, dx1, dy1, tmp, dx2, dy2; @@ -221,7 +221,7 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, // screen coordinates - pp1 = (PIXEL *)((char *)zb->pbuf + zb->linesize * p0->y); + pp1 = zb->pbuf.getRawBuffer() + zb->linesize * p0->y; pm1 = zb->shadow_mask_buf + p0->y * zb->xsize; pz1 = zb->zbuf + p0->y * zb->xsize; pz2 = zb->zbuf2 + p0->y * zb->xsize; @@ -256,7 +256,7 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, } else { update_left = 1; update_right = 0; - l1 = p1; + l1 = p1; l2 = p2; } nb_lines = p2->y - p1->y + 1; @@ -267,7 +267,7 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, if (update_left) { dy1 = l2->y - l1->y; dx1 = l2->x - l1->x; - if (dy1 > 0) + if (dy1 > 0) tmp = (dx1 << 16) / dy1; else tmp = 0; @@ -278,7 +278,7 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, dxdy_max = dxdy_min + 1; z1 = l1->z; - dzdl_min = (dzdy + dzdx * dxdy_min); + dzdl_min = (dzdy + dzdx * dxdy_min); dzdl_max = dzdl_min + dzdx; } @@ -287,7 +287,7 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, if (update_right) { dx2 = (pr2->x - pr1->x); dy2 = (pr2->y - pr1->y); - if (dy2>0) + if (dy2>0) dx2dy2 = ( dx2 << 16) / dy2; else dx2dy2 = 0; @@ -300,7 +300,6 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, nb_lines--; // generic draw line { - register PIXEL *pp; register unsigned char *pm; register int n; register unsigned short *pz; @@ -308,7 +307,10 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, register unsigned int z, zz; n = (x2 >> 16) - x1; - pp = (PIXEL *)((char *)pp1 + x1 * PSZB); + + Graphics::PixelBuffer buf = zb->pbuf; + buf = pp1 + x1 * PSZB; + pm = pm1 + x1; pz = pz1 + x1; pz_2 = pz2 + x1; @@ -317,7 +319,7 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, for (int a = 0; a < 4; a++) { zz = z >> ZB_POINT_Z_FRAC_BITS; if ((ZCMP(zz, pz[a])) && (ZCMP(z, pz_2[a])) && pm[0]) { - pp[a] = color; + buf.setPixelAt(a, color); pz_2[a] = z; } z += dzdx; @@ -325,23 +327,23 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, pz += 4; pz_2 += 4; pm += 4; - pp = (PIXEL *)((char *)pp + 4 * PSZB); + buf.shiftBy(4); n -= 4; } while (n >= 0) { zz = z >> ZB_POINT_Z_FRAC_BITS; if ((ZCMP(zz, pz[0])) && (ZCMP(z, pz_2[0])) && pm[0]) { - pp[0] = color; + buf.setPixelAt(0, color); pz_2[0] = z; } pz += 1; pz_2 += 1; pm += 1; - pp = (PIXEL *)((char *)pp + PSZB); + buf.shiftBy(1); n -= 1; } } - + // left edge error += derror; if (error > 0) { @@ -351,13 +353,13 @@ void ZB_fillTriangleFlatShadow(ZBuffer *zb, ZBufferPoint *p0, ZBufferPoint *p1, } else { x1 += dxdy_min; z1 += dzdl_min; - } - + } + // right edge x2 += dx2dy2; // screen coordinates - pp1 = (PIXEL *)((char *)pp1 + zb->linesize); + pp1 += zb->linesize; pz1 += zb->xsize; pz2 += zb->xsize; pm1 += zb->xsize;