[Scummvm-git-logs] scummvm master -> 435ced7c5c55449b5aec90da7cc3f2fc1526c7bc

ccawley2011 noreply at scummvm.org
Thu Dec 22 12:26:22 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
435ced7c5c DS: Separate the Background class from Graphics::Surface


Commit: 435ced7c5c55449b5aec90da7cc3f2fc1526c7bc
    https://github.com/scummvm/scummvm/commit/435ced7c5c55449b5aec90da7cc3f2fc1526c7bc
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2022-12-22T12:25:47Z

Commit Message:
DS: Separate the Background class from Graphics::Surface

Changed paths:
    backends/platform/ds/background.cpp
    backends/platform/ds/background.h
    backends/platform/ds/ds-graphics.cpp
    backends/platform/ds/osystem_ds.h


diff --git a/backends/platform/ds/background.cpp b/backends/platform/ds/background.cpp
index 63c281a09ae..ac0f1c77bb0 100644
--- a/backends/platform/ds/background.cpp
+++ b/backends/platform/ds/background.cpp
@@ -24,15 +24,9 @@
 #include "backends/platform/ds/background.h"
 #include "backends/platform/ds/blitters.h"
 
-namespace DS {
+#include "graphics/surface.h"
 
-Background::Background() :
-	_bg(-1), _visible(true), _swScale(false),
-	_scaleX(1 << 8), _scaleY(1 << 8), _scrollX(0), _scrollY(0),
-	_realPitch(0), _realHeight(0),
-	_pfCLUT8(Graphics::PixelFormat::createFormatCLUT8()),
-	_pfABGR1555(Graphics::PixelFormat(2, 5, 5, 5, 1, 0, 5, 10, 15)) {
-}
+namespace DS {
 
 static BgSize getBgSize(uint16 width, uint16 height, bool isRGB, bool swScale, uint16 &realPitch, uint16 &realHeight) {
 	if (swScale) {
@@ -95,53 +89,17 @@ size_t Background::getRequiredVRAM(uint16 width, uint16 height, bool isRGB, bool
 	return realPitch * realHeight;
 }
 
-void Background::create(uint16 width, uint16 height, bool isRGB) {
-	const Graphics::PixelFormat f = isRGB ? _pfABGR1555 : _pfCLUT8;
-	Surface::create(width, height, f);
-	_bg = -1;
-	_swScale = false;
-	_scaleX = 1 << 8;
-	_scaleY = 1 << 8;
-	_scrollX = 0;
-	_scrollY = 0;
-}
-
-void Background::create(uint16 width, uint16 height, bool isRGB, int layer, bool isSub, int mapBase, bool swScale) {
-	const Graphics::PixelFormat f = isRGB ? _pfABGR1555 : _pfCLUT8;
-	Surface::create(width, height, f);
-
-	BgType type = (isRGB || swScale) ? BgType_Bmp16 : BgType_Bmp8;
-	BgSize size = getBgSize(width, height, isRGB, swScale, _realPitch, _realHeight);
-
-	if (isSub) {
-		_bg = bgInitSub(layer, type, size, mapBase, 0);
-	} else {
-		_bg = bgInit(layer, type, size, mapBase, 0);
-	}
-
-	_swScale = swScale;
-	_scaleX = 1 << 8;
-	_scaleY = 1 << 8;
-	_scrollX = 0;
-	_scrollY = 0;
-}
-
-void Background::init(Background *surface) {
-	Surface::init(surface->w, surface->h, surface->pitch, surface->pixels, surface->format);
-	_bg = -1;
-	_swScale = false;
-	_scaleX = 1 << 8;
-	_scaleY = 1 << 8;
-	_scrollX = 0;
-	_scrollY = 0;
-}
+Background::Background(Graphics::Surface *surface, int layer, bool isSub, int mapBase, bool swScale) :
+	_bg(-1), _visible(true), _swScale(swScale),
+	_scaleX(1 << 8), _scaleY(1 << 8), _scrollX(0), _scrollY(0),
+	_surface(surface), _pitch(0) {
 
-void Background::init(Background *surface, int layer, bool isSub, int mapBase, bool swScale) {
-	Surface::init(surface->w, surface->h, surface->pitch, surface->pixels, surface->format);
+	assert(_surface);
 
-	bool isRGB = (format != _pfCLUT8);
+	uint16 realPitch, realHeight;
+	bool isRGB = (_surface->format != Graphics::PixelFormat::createFormatCLUT8());
 	BgType type = (isRGB || swScale) ? BgType_Bmp16 : BgType_Bmp8;
-	BgSize size = getBgSize(w, h, isRGB, swScale, _realPitch, _realHeight);
+	BgSize size = getBgSize(_surface->w, _surface->h, isRGB, swScale, realPitch, realHeight);
 
 	if (isSub) {
 		_bg = bgInitSub(layer, type, size, mapBase, 0);
@@ -149,11 +107,7 @@ void Background::init(Background *surface, int layer, bool isSub, int mapBase, b
 		_bg = bgInit(layer, type, size, mapBase, 0);
 	}
 
-	_swScale = swScale;
-	_scaleX = 1 << 8;
-	_scaleY = 1 << 8;
-	_scrollX = 0;
-	_scrollY = 0;
+	_pitch = realPitch;
 }
 
 static void dmaBlit(uint16 *dst, const uint dstPitch, const uint16 *src, const uint srcPitch,
@@ -198,15 +152,15 @@ void Background::update() {
 
 	u16 *dst = bgGetGfxPtr(_bg);
 	if (_swScale) {
-		if (format == _pfCLUT8) {
+		if (_surface->format == Graphics::PixelFormat::createFormatCLUT8()) {
 			Rescale_320x256xPAL8_To_256x256x1555(
-				dst, (const u8 *)getPixels(), _realPitch / 2, pitch, BG_PALETTE, h);
+				dst, (const u8 *)_surface->getPixels(), _pitch / 2, _surface->pitch, BG_PALETTE, _surface->h);
 		} else {
 			Rescale_320x256x1555_To_256x256x1555(
-				dst, (const u16 *)getPixels(), _realPitch / 2, pitch / 2);
+				dst, (const u16 *)_surface->getPixels(), _pitch / 2, _surface->pitch / 2);
 		}
 	} else {
-		dmaBlit(dst, _realPitch, (const u16 *)getPixels(), pitch, w, h, format.bytesPerPixel);
+		dmaBlit(dst, _pitch, (const u16 *)_surface->getPixels(), _surface->pitch, _surface->w, _surface->h, _surface->format.bytesPerPixel);
 	}
 }
 
@@ -215,7 +169,7 @@ void Background::reset() {
 		return;
 
 	u16 *dst = bgGetGfxPtr(_bg);
-	dmaFillHalfWords(0, dst, _realPitch * h);
+	dmaFillHalfWords(0, dst, _pitch * _surface->h);
 }
 
 void Background::show() {
@@ -248,13 +202,13 @@ void Background::setScrollf(int32 x, int32 y) {
 	_scrollY = y;
 }
 
-Common::Point Background::realToScaled(int16 x, int16 y) {
-	x = CLIP<int16>(((x * _scaleX) + _scrollX) >> 8, 0, w  - 1);
-	y = CLIP<int16>(((y * _scaleY) + _scrollY) >> 8, 0, h - 1);
+Common::Point Background::realToScaled(int16 x, int16 y) const {
+	x = CLIP<int16>(((x * _scaleX) + _scrollX) >> 8, 0, _surface->w  - 1);
+	y = CLIP<int16>(((y * _scaleY) + _scrollY) >> 8, 0, _surface->h - 1);
 	return Common::Point(x, y);
 }
 
-Common::Point Background::scaledToReal(int16 x, int16 y) {
+Common::Point Background::scaledToReal(int16 x, int16 y) const {
 	x = ((x << 8) - _scrollX) / _scaleX;
 	y = ((y << 8) - _scrollY) / _scaleY;
 	return Common::Point(x, y);
diff --git a/backends/platform/ds/background.h b/backends/platform/ds/background.h
index 8a24bc94cf5..cc74203ae1b 100644
--- a/backends/platform/ds/background.h
+++ b/backends/platform/ds/background.h
@@ -22,20 +22,19 @@
 #ifndef DS_BACKGROUND_H
 #define DS_BACKGROUND_H
 
-#include "graphics/surface.h"
 #include "common/rect.h"
 
+namespace Graphics {
+struct Surface;
+}
+
 namespace DS {
 
-class Background : public Graphics::Surface {
+class Background {
 public:
-	Background();
+	Background(Graphics::Surface *surface, int layer, bool isSub, int mapBase, bool swScale);
 
-	size_t getRequiredVRAM(uint16 width, uint16 height, bool isRGB, bool swScale);
-	void create(uint16 width, uint16 height, bool isRGB);
-	void create(uint16 width, uint16 height, bool isRGB, int layer, bool isSub, int mapBase, bool swScale);
-	void init(Background *surface);
-	void init(Background *surface, int layer, bool isSub, int mapBase, bool swScale);
+	static size_t getRequiredVRAM(uint16 width, uint16 height, bool isRGB, bool swScale);
 
 	void update();
 	void reset();
@@ -49,26 +48,15 @@ public:
 	void setScrollf(int32 x, int32 y);
 	inline void setScroll(int x, int y) { setScrollf(x << 8, y << 8); }
 
-	Common::Point realToScaled(int16 x, int16 y);
-	Common::Point scaledToReal(int16 x, int16 y);
-
-	inline void clear() {
-		memset(getPixels(), 0, pitch * h);
-	}
-
-	inline void grab(byte *dst, int dstPitch) {
-		for (int y = 0; y < h; ++y) {
-			memcpy(dst, getBasePtr(0, y), w * format.bytesPerPixel);
-			dst += dstPitch;
-		}
-	}
+	Common::Point realToScaled(int16 x, int16 y) const;
+	Common::Point scaledToReal(int16 x, int16 y) const;
 
 protected:
 	int _bg;
 	bool _visible, _swScale;
-	uint16 _realPitch, _realHeight;
-	const Graphics::PixelFormat _pfCLUT8, _pfABGR1555;
+	uint16 _pitch;
 	int32 _scrollX, _scrollY, _scaleX, _scaleY;
+	Graphics::Surface *_surface;
 };
 
 } // End of namespace DS
diff --git a/backends/platform/ds/ds-graphics.cpp b/backends/platform/ds/ds-graphics.cpp
index 093d96f16c3..75ae848cef6 100644
--- a/backends/platform/ds/ds-graphics.cpp
+++ b/backends/platform/ds/ds-graphics.cpp
@@ -25,6 +25,8 @@
 
 #include "common/translation.h"
 
+#include "graphics/conversion.h"
+
 namespace DS {
 
 // From console.c in NDSLib
@@ -178,7 +180,12 @@ void OSystem_DS::initGraphics() {
 	oamInit(&oamMain, SpriteMapping_Bmp_1D_128, false);
 	_cursorSprite = oamAllocateGfx(&oamMain, SpriteSize_64x64, SpriteColorFormat_Bmp);
 
-	_overlay.create(256, 192, true, 2, false, 0, false);
+	_overlay.create(256, 192, _pfABGR1555);
+	_overlayScreen = new DS::Background(&_overlay, 2, false, 0, false);
+	_screen = nullptr;
+#ifndef DISABLE_TEXT_CONSOLE
+	_subScreen = nullptr;
+#endif
 
 	_keyboard = new DS::Keyboard(_eventManager->getEventDispatcher());
 #ifndef DISABLE_TEXT_CONSOLE
@@ -187,14 +194,18 @@ void OSystem_DS::initGraphics() {
 }
 
 void OSystem_DS::setMainScreen(int32 x, int32 y, int32 sx, int32 sy) {
-	_framebuffer.setScalef(sx, sy);
-	_framebuffer.setScrollf(x, y);
+	if (_screen) {
+		_screen->setScalef(sx, sy);
+		_screen->setScrollf(x, y);
+	}
 }
 
 void OSystem_DS::setSubScreen(int32 x, int32 y, int32 sx, int32 sy) {
 #ifdef DISABLE_TEXT_CONSOLE
-	_subScreen.setScalef(sx, sy);
-	_subScreen.setScrollf(x, y);
+	if (_subScreen) {
+		_subScreen->setScalef(sx, sy);
+		_subScreen->setScrollf(x, y);
+	}
 #endif
 }
 
@@ -212,8 +223,10 @@ void OSystem_DS::setFeatureState(Feature f, bool enable) {
 #ifdef DISABLE_TEXT_CONSOLE
 			if (_subScreenActive) {
 				_subScreenActive = false;
-				_subScreen.hide();
-				_subScreen.reset();
+				if (_subScreen) {
+					_subScreen->hide();
+					_subScreen->reset();
+				}
 				_keyboard->init(0, 34, 1, false);
 			}
 #endif
@@ -221,8 +234,10 @@ void OSystem_DS::setFeatureState(Feature f, bool enable) {
 		} else if (_keyboard->isVisible()) {
 			_keyboard->hide();
 #ifdef DISABLE_TEXT_CONSOLE
-			_subScreen.reset();
-			_subScreen.show();
+			if (_subScreen) {
+				_subScreen->reset();
+				_subScreen->show();
+			}
 			_subScreenActive = true;
 			_paletteDirty = true;
 #endif
@@ -319,25 +334,31 @@ Common::List<Graphics::PixelFormat> OSystem_DS::getSupportedFormats() const {
 
 void OSystem_DS::initSize(uint width, uint height, const Graphics::PixelFormat *format) {
 	Graphics::PixelFormat actualFormat = format ? *format : _pfCLUT8;
+	_framebuffer.create(width, height, actualFormat);
+
 	bool isRGB = (actualFormat != _pfCLUT8), swScale = ((_graphicsMode == GFX_SWSCALE) && (width == 320));
 
+	if (_screen)
+		_screen->reset();
+	delete _screen;
+	_screen = nullptr;
+
 	// For Lost in Time, the title screen is displayed in 640x400.
 	// In order to support this game, the screen mode is set, but
 	// all draw calls are ignored until the game switches to 320x200.
-	if (_framebuffer.getRequiredVRAM(width, height, isRGB, swScale) > 0x40000) {
-		_framebuffer.create(width, height, isRGB);
-	} else {
-		_framebuffer.reset();
-		_framebuffer.create(width, height, isRGB, 3, false, 8, swScale);
+	if (DS::Background::getRequiredVRAM(width, height, isRGB, swScale) <= 0x40000) {
+		_screen = new DS::Background(&_framebuffer, 3, false, 8, swScale);
 	}
 
 #ifdef DISABLE_TEXT_CONSOLE
-	if (_framebuffer.getRequiredVRAM(width, height, isRGB, false) > 0x20000) {
-		_subScreen.init(&_framebuffer);
-	} else {
-		if (_subScreenActive)
-			_subScreen.reset();
-		_subScreen.init(&_framebuffer, 3, true, 0, false);
+	if (_subScreen && _subScreenActive)
+		_subScreen->reset();
+
+	delete _subScreen;
+	_subScreen = nullptr;
+
+	if (DS::Background::getRequiredVRAM(width, height, isRGB, false) <= 0x20000) {
+		_subScreen = new DS::Background(&_framebuffer, 3, true, 0, false);
 	}
 #endif
 
@@ -406,24 +427,24 @@ void OSystem_DS::updateScreen() {
 	       SpriteColorFormat_Bmp, _cursorSprite, 0, false, !_cursorVisible, false, false, false);
 	oamUpdate(&oamMain);
 
-	if (_overlay.isVisible()) {
-		_overlay.update();
-	}
-
-	if (_framebuffer.isVisible()) {
-		if (_paletteDirty) {
-			dmaCopyHalfWords(3, _palette, BG_PALETTE, 256 * 2);
+	if (_paletteDirty) {
+		dmaCopyHalfWords(3, _palette, BG_PALETTE, 256 * 2);
 #ifdef DISABLE_TEXT_CONSOLE
-			if (_subScreenActive)
-				dmaCopyHalfWords(3, _palette, BG_PALETTE_SUB, 256 * 2);
+		if (_subScreenActive)
+			dmaCopyHalfWords(3, _palette, BG_PALETTE_SUB, 256 * 2);
 #endif
-			_paletteDirty = false;
-		}
+		_paletteDirty = false;
+	}
+
+	if (_overlayScreen && _overlayScreen->isVisible()) {
+		_overlayScreen->update();
+	}
 
-		_framebuffer.update();
+	if (_screen && _screen->isVisible()) {
+		_screen->update();
 #ifdef DISABLE_TEXT_CONSOLE
-		if (_subScreenActive)
-			_subScreen.update();
+		if (_subScreen && _subScreenActive)
+			_subScreen->update();
 #endif
 	}
 }
@@ -433,29 +454,36 @@ void OSystem_DS::setShakePos(int shakeXOffset, int shakeYOffset) {
 }
 
 void OSystem_DS::showOverlay(bool inGUI) {
+	assert(_overlayScreen);
 	_overlayInGUI = inGUI;
-	_overlay.reset();
-	_overlay.show();
+	_overlayScreen->reset();
+	_overlayScreen->show();
 }
 
 void OSystem_DS::hideOverlay() {
+	assert(_overlayScreen);
 	_overlayInGUI = false;
-	_overlay.hide();
+	_overlayScreen->hide();
 }
 
 bool OSystem_DS::isOverlayVisible() const {
-	return _overlay.isVisible();
+	assert(_overlayScreen);
+	return _overlayScreen->isVisible();
 }
 
 void OSystem_DS::clearOverlay() {
-	_overlay.clear();
+	memset(_overlay.getPixels(), 0, _overlay.pitch * _overlay.h);
 }
 
 void OSystem_DS::grabOverlay(Graphics::Surface &surface) {
-	assert(surface.w >= _overlay.w);
-	assert(surface.h >= _overlay.h);
+	assert(surface.w >= getOverlayWidth());
+	assert(surface.h >= getOverlayHeight());
 	assert(surface.format.bytesPerPixel == _overlay.format.bytesPerPixel);
-	_overlay.grab((byte *)surface.getPixels(), surface.pitch);
+
+	byte *src = (byte *)_overlay.getPixels();
+	byte *dst = (byte *)surface.getPixels();
+	Graphics::copyBlit(dst, src, surface.pitch, _overlay.pitch,
+		getOverlayWidth(), getOverlayHeight(), _overlay.format.bytesPerPixel);
 }
 
 void OSystem_DS::copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h) {
@@ -475,10 +503,10 @@ Graphics::PixelFormat OSystem_DS::getOverlayFormat() const {
 }
 
 Common::Point OSystem_DS::transformPoint(int16 x, int16 y) {
-	if (_overlayInGUI)
+	if (_overlayInGUI || !_screen)
 		return Common::Point(x, y);
 	else
-		return _framebuffer.realToScaled(x, y);
+		return _screen->realToScaled(x, y);
 }
 
 bool OSystem_DS::showMouse(bool visible) {
@@ -488,10 +516,10 @@ bool OSystem_DS::showMouse(bool visible) {
 }
 
 void OSystem_DS::warpMouse(int x, int y) {
-	if (_overlayInGUI)
+	if (_overlayInGUI || !_screen)
 		_cursorPos = Common::Point(x, y);
 	else
-		_cursorPos = _framebuffer.scaledToReal(x, y);
+		_cursorPos = _screen->scaledToReal(x, y);
 }
 
 void OSystem_DS::setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, u32 keycolor, bool dontScale, const Graphics::PixelFormat *format) {
diff --git a/backends/platform/ds/osystem_ds.h b/backends/platform/ds/osystem_ds.h
index def78b33278..5a189f684f4 100644
--- a/backends/platform/ds/osystem_ds.h
+++ b/backends/platform/ds/osystem_ds.h
@@ -39,9 +39,10 @@ enum {
 
 class OSystem_DS : public ModularMixerBackend, public PaletteManager {
 protected:
-	DS::Background _framebuffer, _overlay;
+	Graphics::Surface _framebuffer, _overlay;
+	DS::Background *_screen, *_overlayScreen;
 #ifdef DISABLE_TEXT_CONSOLE
-	DS::Background _subScreen;
+	DS::Background *_subScreen;
 #endif
 	bool _subScreenActive;
 	Graphics::Surface _cursor;




More information about the Scummvm-git-logs mailing list