[Scummvm-git-logs] scummvm master -> 5f9fa6f967fdc9cf1d24db37f1ca7f5679b5f279

Scorpeg noreply at scummvm.org
Mon Nov 2 13:25:49 UTC 2026


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

Summary:
5f9fa6f967 GRAPHICS: Rework HotspotRenderer - caching, correctness, engine styling


Commit: 5f9fa6f967fdc9cf1d24db37f1ca7f5679b5f279
    https://github.com/scummvm/scummvm/commit/5f9fa6f967fdc9cf1d24db37f1ca7f5679b5f279
Author: Scorp (scorp at mrs.mn)
Date: 2026-11-02T16:25:40+03:00

Commit Message:
GRAPHICS: Rework HotspotRenderer - caching, correctness, engine styling

Changed paths:
    engines/engine.cpp
    engines/engine.h
    graphics/hotspot_renderer.cpp
    graphics/hotspot_renderer.h
    gui/editgamedialog.cpp


diff --git a/engines/engine.cpp b/engines/engine.cpp
index 8c55932db99..aa045c0dbae 100644
--- a/engines/engine.cpp
+++ b/engines/engine.cpp
@@ -216,6 +216,9 @@ Engine::Engine(OSystem *syst)
 }
 
 Engine::~Engine() {
+	if (_showHotspots)
+		showHotspots(false);
+	delete _hotspotRenderer;
 	_mixer->stopAll();
 
 	delete _debugger;
@@ -725,6 +728,7 @@ void Engine::resumeEngine() {
 	_pauseLevel--;
 
 	if (_pauseLevel == 0) {
+		_hotspotForceRedraw = true;
 		if (_pauseScreenChangeID != g_system->getScreenChangeID()) {
 			// Inject a screen change event in the event loop for the engine
 			Common::Event ev;
@@ -744,16 +748,18 @@ void Engine::pauseEngineIntern(bool pause) {
 }
 
 void Engine::showHotspots(bool show) {
-	bool wasShowing = _showHotspots;
 	_showHotspots = show;
 
 	if (show) {
 		_hotspotForceRedraw = true;
-		if (!wasShowing)
-			_hotspotPrevCursorVisible = CursorMan.showMouse(false);
 	} else {
-		if (wasShowing)
+		if (_hotspotRenderer)
+			_hotspotRenderer->releaseResources();
+		_hotspotScratch.resize(0);
+		if (_hotspotCursorHidden) {
 			CursorMan.showMouse(_hotspotPrevCursorVisible);
+			_hotspotCursorHidden = false;
+		}
 		if (g_system->isOverlayVisible())
 			g_system->hideOverlay();
 	}
@@ -766,48 +772,43 @@ bool Engine::hotspotDirty() const {
 	return true;
 }
 
+Graphics::HotspotRenderer *Engine::createHotspotRenderer() {
+	return new Graphics::HotspotRenderer();
+}
+
 void Engine::drawHotspots() {
 	if (!_showHotspots)
 		return;
-
-	if (!hotspotDirty() && !_hotspotForceRedraw)
-		return;
+	if (!_hotspotRenderer) {
+		_hotspotRenderer = createHotspotRenderer();
+		_hotspotForceRedraw = true;
+	}
+	if (hotspotDirty() || _hotspotForceRedraw) {
+		_hotspotScratch.resize(0);
+		getHotspotPositions(_hotspotScratch);
+		_hotspotRenderer->exchangeHotspots(_hotspotScratch);
+	}
+	if (_hotspotForceRedraw) {
+		if (!ConfMan.getBool("enable_hotspots")) {
+			showHotspots(false);
+			return;
+		}
+		_hotspotShowText = !ConfMan.hasKey("show_hotspot_text") || ConfMan.getBool("show_hotspot_text");
+		const int marker = ConfMan.hasKey("hotspot_marker") ? ConfMan.getInt("hotspot_marker") : Graphics::kMarkerDefault;
+		_hotspotMarker = marker == Graphics::kMarkerDefault ? Graphics::kMarkerCrosshair :
+			CLIP(marker, (int)Graphics::kMarkerCrosshair, (int)Graphics::kMarkerPoint);
+	}
+	if (_hotspotRenderer->empty()) {
+		if (_hotspotCursorHidden) {
+			CursorMan.showMouse(_hotspotPrevCursorVisible);
+			_hotspotCursorHidden = false;
+		}
+	} else if (!_hotspotCursorHidden && hideCursorForHotspots()) {
+		_hotspotPrevCursorVisible = CursorMan.showMouse(false);
+		_hotspotCursorHidden = true;
+	}
+	_hotspotRenderer->renderOverlay(*_system, (Graphics::MarkerShape)_hotspotMarker, _hotspotShowText, _hotspotForceRedraw);
 	_hotspotForceRedraw = false;
-
-	Common::Array<Graphics::HotspotInfo> hotspots;
-	getHotspotPositions(hotspots);
-
-	if (hotspots.empty())
-		return;
-
-	int16 gameWidth = g_system->getWidth();
-	int16 gameHeight = g_system->getHeight();
-	int16 overlayWidth = g_system->getOverlayWidth();
-	int16 overlayHeight = g_system->getOverlayHeight();
-	Graphics::PixelFormat overlayFormat = g_system->getOverlayFormat();
-
-	if (!g_system->isOverlayVisible())
-		g_system->showOverlay(false);
-
-	g_system->clearOverlay();
-
-	Graphics::Surface overlayBuffer;
-	overlayBuffer.create(overlayWidth, overlayHeight, overlayFormat);
-	g_system->grabOverlay(overlayBuffer);
-
-	bool showText = ConfMan.getBool("show_hotspot_text");
-	if (!ConfMan.hasKey("show_hotspot_text"))
-		showText = true;
-
-	int markerType = ConfMan.getInt("hotspot_marker");
-
-	Graphics::HotspotRenderer renderer;
-	renderer.render(&overlayBuffer, hotspots, gameWidth, gameHeight,
-		overlayWidth, overlayHeight, overlayFormat,
-		(Graphics::MarkerShape)markerType, showText);
-
-	g_system->copyRectToOverlay(overlayBuffer.getPixels(), overlayBuffer.pitch, 0, 0, overlayWidth, overlayHeight);
-	overlayBuffer.free();
 }
 
 void Engine::openMainMenuDialog() {
diff --git a/engines/engine.h b/engines/engine.h
index a7f8f313016..b21d92e00ab 100644
--- a/engines/engine.h
+++ b/engines/engine.h
@@ -56,6 +56,7 @@ class Dialog;
 }
 namespace Graphics {
 struct HotspotInfo;
+class HotspotRenderer;
 }
 
 /**
@@ -205,6 +206,11 @@ protected:
 	 * overlay), and this remembers the state to restore afterwards.
 	 */
 	bool _hotspotPrevCursorVisible;
+	bool _hotspotCursorHidden = false;
+	Graphics::HotspotRenderer *_hotspotRenderer = nullptr;
+	Common::Array<Graphics::HotspotInfo> _hotspotScratch;
+	bool _hotspotShowText = true;
+	int _hotspotMarker = 0;
 
 private:
 	/**
@@ -550,6 +556,8 @@ protected:
 	 * circles, automatically converting from game coordinates to overlay coordinates.
 	 */
 	virtual void drawHotspots();
+	virtual Graphics::HotspotRenderer *createHotspotRenderer();
+	virtual bool hideCursorForHotspots() const { return true; }
 
 	/**
 	 * Returns whether hotspot markers need to be re-rendered this frame.
diff --git a/graphics/hotspot_renderer.cpp b/graphics/hotspot_renderer.cpp
index 14cbc6a67b0..9fce6063bbd 100644
--- a/graphics/hotspot_renderer.cpp
+++ b/graphics/hotspot_renderer.cpp
@@ -21,6 +21,7 @@
 
 #include "graphics/hotspot_renderer.h"
 #include "common/util.h"
+#include "common/system.h"
 #include "graphics/font.h"
 #include "graphics/fontman.h"
 #include "graphics/pixelformat.h"
@@ -29,11 +30,189 @@
 namespace Graphics {
 
 HotspotRenderer::HotspotRenderer() :
-		_sizeScale(1.0f), _markerSize(kBaseMarkerSize),
-		_pointRadius(kBasePointRadius), _lineThickness(kBaseLineThickness) {
+		_sizeScale(1.0f), _markerSize(_style.markerSize),
+		_pointRadius(_style.pointRadius), _lineThickness(_style.lineThickness),
+		_markerBlendColor(0) {
 }
 
 HotspotRenderer::~HotspotRenderer() {
+	releaseResources();
+}
+
+void HotspotRenderer::releaseResources() {
+	_overlay.free();
+	clearLabels();
+	_positions.clear();
+	_labelLayouts.clear();
+	_previousBounds = Common::Rect();
+	_drawBounds = Common::Rect();
+	_dirty = true;
+}
+
+void HotspotRenderer::invalidate() {
+	_dirty = true;
+}
+
+void HotspotRenderer::clearLabels() {
+	_labels.clear();
+	_labelBytes = 0;
+	_labelClock = 0;
+}
+
+static Common::Rect unionRects(Common::Rect first, const Common::Rect &second) {
+	if (first.isEmpty())
+		return second;
+	if (!second.isEmpty())
+		first.extend(second);
+	return first;
+}
+
+void HotspotRenderer::addDrawRect(Common::Rect rect) {
+	_drawBounds = unionRects(_drawBounds, rect);
+}
+
+void HotspotRenderer::packLabelMask(const Surface &scaled, LabelMask &mask) {
+	mask.width = scaled.w;
+	mask.height = scaled.h;
+	mask.stride = (scaled.w + 7) / 8;
+	mask.bits.resize(mask.stride * mask.height);
+	for (int y = 0; y < mask.height; ++y) {
+		byte *row = &mask.bits[y * mask.stride];
+		for (int x = 0; x < mask.width; ++x)
+			if (scaled.getPixel(x, y) != 0)
+				row[x >> 3] |= (1 << (x & 7));
+	}
+}
+
+const HotspotRenderer::LabelMask &HotspotRenderer::getLabelMask(const Common::U32String &text, const Font *font,
+		int width, int height, int baseWidth, int baseHeight, const PixelFormat &format, LabelMask &scratch) {
+	for (uint i = 0; i < _labels.size(); ++i) {
+		CachedLabel &label = _labels[i];
+		if (label.mask.width == width && label.mask.height == height &&
+				label.font == font && label.format == format && label.text == text) {
+			label.lastUse = ++_labelClock;
+			return label.mask;
+		}
+	}
+	Surface original;
+	original.create(baseWidth, baseHeight, format);
+	font->drawString(&original, text, 0, 0, baseWidth, format.RGBToColor(255, 255, 255), kTextAlignLeft);
+	Surface *scaled = original.scale(width, height, false);
+	original.free();
+	LabelMask built;
+	packLabelMask(*scaled, built);
+	scaled->free();
+	delete scaled;
+
+	const uint bytes = (uint)built.bits.size();
+	const uint budget = 4 * 1024 * 1024;
+	if (bytes > budget) {
+		scratch = Common::move(built);
+		return scratch;
+	}
+	while (!_labels.empty() && (_labelBytes + bytes > budget || _labels.size() >= 512)) {
+		uint oldest = 0;
+		for (uint i = 1; i < _labels.size(); ++i)
+			if (_labels[i].lastUse < _labels[oldest].lastUse)
+				oldest = i;
+		_labelBytes -= (uint)_labels[oldest].mask.bits.size();
+		_labels.erase(_labels.begin() + oldest);
+	}
+	CachedLabel label;
+	label.text = text;
+	label.mask = Common::move(built);
+	label.font = font;
+	label.format = format;
+	label.lastUse = ++_labelClock;
+	_labelBytes += bytes;
+	_labels.push_back(Common::move(label));
+	return _labels.back().mask;
+}
+
+void HotspotRenderer::setStyle(const HotspotStyle &style) {
+	HotspotStyle next = style;
+	next.markerSize = CLIP(next.markerSize, 2, 256);
+	next.pointRadius = CLIP(next.pointRadius, 1, 256);
+	next.lineThickness = CLIP(next.lineThickness, 1, 256);
+	next.borderThickness = CLIP(next.borderThickness, 1, 256);
+	next.labelGap = CLIP(next.labelGap, 0, 256);
+	next.paddingX = CLIP(next.paddingX, 0, 256);
+	next.paddingY = CLIP(next.paddingY, 0, 256);
+	next.markerColor &= 0xffffff;
+	next.textColor &= 0xffffff;
+	next.backgroundColor &= 0xffffff;
+	next.borderColor &= 0xffffff;
+	_style = next;
+	invalidate();
+}
+
+void HotspotRenderer::setHotspots(Common::Array<HotspotInfo> hotspots) {
+	exchangeHotspots(hotspots);
+}
+
+void HotspotRenderer::exchangeHotspots(Common::Array<HotspotInfo> &hotspots) {
+	bool changed = hotspots.size() != _hotspots.size();
+	for (uint i = 0; !changed && i < hotspots.size(); ++i) {
+		changed = hotspots[i].position != _hotspots[i].position ||
+			hotspots[i].name != _hotspots[i].name || hotspots[i].type != _hotspots[i].type;
+	}
+	if (changed) {
+		_hotspots.swap(hotspots);
+		_dirty = true;
+	}
+}
+
+void HotspotRenderer::renderOverlay(OSystem &system, MarkerShape markerShape, bool showText, bool forceRedraw) {
+	if (_hotspots.empty()) {
+		if (system.isOverlayVisible())
+			system.hideOverlay();
+		_overlay.free();
+		_previousBounds = Common::Rect();
+		_drawBounds = Common::Rect();
+		_dirty = true;
+		return;
+	}
+	const int width = system.getOverlayWidth();
+	const int height = system.getOverlayHeight();
+	const int gameWidth = system.getWidth();
+	const int gameHeight = system.getHeight();
+	const PixelFormat format = system.getOverlayFormat();
+	const int screenChangeID = system.getScreenChangeID();
+	const Font *font = showText ? (_style.font ? _style.font : FontMan.getFontByUsage(FontManager::kGUIFont)) : nullptr;
+	if (width <= 0 || height <= 0 || gameWidth <= 0 || gameHeight <= 0 || format.bytesPerPixel < 2)
+		return;
+	const bool visible = system.isOverlayVisible();
+	const bool resized = _overlay.w != width || _overlay.h != height || _overlay.format != format;
+	if (!_dirty && !forceRedraw && !resized && visible && format.aBits() &&
+		_gameSize.x == gameWidth && _gameSize.y == gameHeight && _screenChangeID == screenChangeID &&
+		_shape == markerShape && _font == font)
+		return;
+	if (!visible)
+		system.showOverlay(false);
+	if (resized)
+		_overlay.create(width, height, format);
+	if (format.aBits()) {
+		if (!resized)
+			_overlay.fillRect(_previousBounds, 0);
+	} else {
+		system.clearOverlay();
+		system.grabOverlay(_overlay);
+	}
+	renderFrame(&_overlay, _hotspots, gameWidth, gameHeight, width, height, format, markerShape, font);
+	Common::Rect upload = _drawBounds;
+	if (format.aBits())
+		upload = unionRects(upload, _previousBounds);
+	if (resized || !visible || forceRedraw || screenChangeID != _screenChangeID)
+		upload = Common::Rect(width, height);
+	if (!upload.isEmpty())
+		system.copyRectToOverlay(_overlay.getBasePtr(upload.left, upload.top), _overlay.pitch,
+			upload.left, upload.top, upload.width(), upload.height());
+	_previousBounds = _drawBounds;
+	_gameSize = Common::Point(gameWidth, gameHeight);
+	_screenChangeID = screenChangeID;
+	_shape = markerShape;
+	_font = font;
+	_dirty = false;
 }
 
 void HotspotRenderer::render(Surface *surface,
@@ -43,51 +222,71 @@ void HotspotRenderer::render(Surface *surface,
 		const PixelFormat &format,
 		MarkerShape markerShape,
 		bool showText) {
+	const Font *font = showText ? (_style.font ? _style.font : FontMan.getFontByUsage(FontManager::kGUIFont)) : nullptr;
+	renderFrame(surface, hotspots, gameWidth, gameHeight, overlayWidth, overlayHeight, format, markerShape, font);
+}
+
+void HotspotRenderer::renderFrame(Surface *surface, const Common::Array<HotspotInfo> &hotspots,
+		int gameWidth, int gameHeight, int overlayWidth, int overlayHeight,
+		const PixelFormat &format, MarkerShape markerShape, const Font *font) {
 	if (!surface || hotspots.empty())
 		return;
 
-	if (format.bytesPerPixel < 2)
+	if (format.bytesPerPixel < 2 || gameWidth <= 0 || gameHeight <= 0 || overlayWidth <= 0 || overlayHeight <= 0)
 		return;
 
 	float scaleX = (float)overlayWidth / (float)gameWidth;
 	float scaleY = (float)overlayHeight / (float)gameHeight;
 
-	_sizeScale = scaleX;
-	_markerSize = MAX(2, (int)(kBaseMarkerSize * _sizeScale));
-	_pointRadius = MAX(1, (int)(kBasePointRadius * _sizeScale));
-	_lineThickness = MAX(1, (int)(kBaseLineThickness * _sizeScale));
-
-	const Font *font = nullptr;
-	if (showText) {
-		font = FontMan.getFontByUsage(FontManager::kGUIFont);
+	_sizeScale = _style.scaleWithGame ? MIN(scaleX, scaleY) : 1.0f;
+	_markerSize = MAX(2, (int)(_style.markerSize * _sizeScale));
+	_pointRadius = MAX(1, (int)(_style.pointRadius * _sizeScale));
+	_lineThickness = MAX(1, (int)(_style.lineThickness * _sizeScale));
+	_markerBlendColor = format.aBits() ? format.ARGBToColor(_style.markerOpacity,
+		_style.markerColor >> 16, _style.markerColor >> 8, _style.markerColor) : 0;
+
+	_drawBounds = Common::Rect();
+	prepareRender(hotspots, gameWidth, gameHeight, overlayWidth, overlayHeight);
+
+	_positions.resize(hotspots.size());
+	_labelLayouts.resize(hotspots.size());
+	for (uint i = 0; i < hotspots.size(); ++i) {
+		_positions[i] = placeHotspot(hotspots[i], Common::Point(
+			(int)(hotspots[i].position.x * scaleX), (int)(hotspots[i].position.y * scaleY)));
+		_labelLayouts[i] = font && !hotspots[i].name.empty() ?
+			layoutHotspotLabel(hotspots[i], _positions[i], overlayWidth, overlayHeight, font) : LabelLayout();
 	}
-
-	for (uint i = 0; i < hotspots.size(); i++) {
-		int gameX = hotspots[i].position.x;
-		int gameY = hotspots[i].position.y;
-
-		int overlayX = (int)(gameX * scaleX);
-		int overlayY = (int)(gameY * scaleY);
-
-		drawMarker(surface, overlayX, overlayY, format, markerShape);
-
-		if (font && !hotspots[i].name.empty()) {
-			drawHotspotLabel(surface, overlayX, overlayY, hotspots[i].name,
-				overlayWidth, overlayHeight, format, font);
-		}
+	if (font) {
+		for (uint i = 0; i < hotspots.size(); ++i)
+			if (!hotspots[i].name.empty())
+				drawHotspotLabelBackground(surface, _labelLayouts[i], hotspots[i],
+					overlayWidth, overlayHeight, format, font);
+	}
+	for (uint i = 0; i < hotspots.size(); ++i)
+		drawMarker(surface, hotspots[i], _positions[i].x, _positions[i].y, format, markerShape);
+	if (font) {
+		for (uint i = 0; i < hotspots.size(); ++i)
+			if (!hotspots[i].name.empty())
+				drawHotspotLabel(surface, _labelLayouts[i], hotspots[i],
+					overlayWidth, overlayHeight, format, font);
 	}
+	_drawBounds.clip(Common::Rect(overlayWidth, overlayHeight));
 }
 
-void HotspotRenderer::drawMarker(Surface *surface, int x, int y, const PixelFormat &format, MarkerShape markerShape) {
+void HotspotRenderer::drawMarker(Surface *surface, const HotspotInfo &hotspot, int x, int y, const PixelFormat &format, MarkerShape markerShape) {
 	int width = surface->w;
 	int height = surface->h;
+	const int extent = markerShape == kMarkerPoint ? _pointRadius : _markerSize / 2 + _lineThickness;
+	Common::Rect bounds(x - extent, y - extent, x + extent + 1, y + extent + 1);
+	bounds.clip(Common::Rect(width, height));
+	addDrawRect(bounds);
 
 	switch (markerShape) {
 	case kMarkerCrosshair:
-		drawCrosshairMarker(surface, x, y, width, height, format);
+		drawCrosshairMarker(surface, x, y, format);
 		break;
 	case kMarkerSquare:
-		drawSquareMarker(surface, x, y, width, height, format);
+		drawSquareMarker(surface, x, y, format);
 		break;
 	case kMarkerPoint:
 		drawPointMarker(surface, x, y, width, height, format);
@@ -95,27 +294,42 @@ void HotspotRenderer::drawMarker(Surface *surface, int x, int y, const PixelForm
 	}
 }
 
-void HotspotRenderer::drawHotspotLabel(Surface *surface, int overlayX, int overlayY,
-		const Common::U32String &label, int overlayWidth, int overlayHeight,
+void HotspotRenderer::drawHotspotLabelBackground(Surface *surface, const LabelLayout &layout,
+		const HotspotInfo &hotspot, int overlayWidth, int overlayHeight,
 		const PixelFormat &format, const Font *font) {
-	int baseTextWidth = font->getStringWidth(label);
-	int baseTextHeight = font->getFontHeight();
+	drawLabel(surface, layout, hotspot, overlayWidth, overlayHeight, format, font, true);
+}
+
+void HotspotRenderer::drawHotspotLabel(Surface *surface, const LabelLayout &layout,
+		const HotspotInfo &hotspot, int overlayWidth, int overlayHeight,
+		const PixelFormat &format, const Font *font) {
+	drawLabel(surface, layout, hotspot, overlayWidth, overlayHeight, format, font, false);
+}
+
+HotspotRenderer::LabelLayout HotspotRenderer::layoutHotspotLabel(const HotspotInfo &hotspot, Common::Point position,
+		int overlayWidth, int overlayHeight, const Font *font) const {
+	LabelLayout layout;
+	const Common::U32String &label = hotspot.name;
+	layout.baseTextWidth = font->getStringWidth(label);
+	const int baseTextWidth = layout.baseTextWidth;
+	layout.baseTextHeight = font->getFontHeight();
+	const int baseTextHeight = layout.baseTextHeight;
 	if (baseTextWidth <= 0 || baseTextHeight <= 0)
-		return;
+		return layout;
 
-	int gap = MAX(1, (int)(8 * _sizeScale));
-	int padX = MAX(1, (int)(4 * _sizeScale));
-	int padY = MAX(1, (int)(2 * _sizeScale));
+	int gap = MAX(0, (int)(_style.labelGap * _sizeScale));
+	int padX = MAX(0, (int)(_style.paddingX * _sizeScale));
+	int padY = MAX(0, (int)(_style.paddingY * _sizeScale));
 
 	Common::Rect overlayBounds(0, 0, (int16)overlayWidth, (int16)overlayHeight);
 
 	Common::Rect textRect((int16)MAX(1, (int)(baseTextWidth * _sizeScale)),
 		(int16)MAX(1, (int)(baseTextHeight * _sizeScale)));
-	textRect.moveTo(overlayX + _markerSize / 2 + gap, overlayY - textRect.height() / 2);
+	textRect.moveTo(position.x + _markerSize / 2 + gap, position.y - textRect.height() / 2);
 
 	// swap to left side if the text would go off the right edge of the overlay
 	if (textRect.right + padX > overlayBounds.right)
-		textRect.moveTo(overlayX - _markerSize / 2 - gap - textRect.width(), textRect.top);
+		textRect.moveTo(position.x - _markerSize / 2 - gap - textRect.width(), textRect.top);
 
 	if (textRect.bottom + padY > overlayBounds.bottom)
 		textRect.moveTo(textRect.left, overlayBounds.bottom - textRect.height() - padY);
@@ -125,51 +339,97 @@ void HotspotRenderer::drawHotspotLabel(Surface *surface, int overlayX, int overl
 	Common::Rect boxRect((int16)(textRect.left - padX), (int16)(textRect.top - padY),
 		(int16)(textRect.right + padX), (int16)(textRect.bottom + padY));
 
-	drawLabelBox(surface, boxRect, overlayWidth, overlayHeight, format);
-
-	Surface textSurface;
-	textSurface.create(baseTextWidth, baseTextHeight, format);
-	textSurface.fillRect(Common::Rect(0, 0, baseTextWidth, baseTextHeight), 0);
-
-	uint32 textColor = format.RGBToColor(255, 255, 255);
-	font->drawString(&textSurface, label, 0, 0, baseTextWidth, textColor, kTextAlignLeft);
+	layout.textRect = textRect;
+	layout.boxRect = boxRect;
+	return layout;
+}
 
-	Surface *scaledText = textSurface.scale(textRect.width(), textRect.height(), false);
-	textSurface.free();
-	if (!scaledText)
+void HotspotRenderer::drawLabel(Surface *surface, const LabelLayout &layout,
+		const HotspotInfo &hotspot, int overlayWidth, int overlayHeight,
+		const PixelFormat &format, const Font *font, bool background) {
+	if (layout.textRect.isEmpty())
 		return;
-
-	for (int sy = 0; sy < textRect.height(); sy++) {
-		int py = textRect.top + sy;
-		for (int sx = 0; sx < textRect.width(); sx++) {
-			int px = textRect.left + sx;
-			if (!overlayBounds.contains(px, py))
-				continue;
-			uint32 pixel = scaledText->getPixel(sx, sy);
-			if (pixel == 0)
+	const Common::U32String &label = hotspot.name;
+	const int baseTextWidth = layout.baseTextWidth;
+	const int baseTextHeight = layout.baseTextHeight;
+	const Common::Rect &textRect = layout.textRect;
+	Common::Rect boxRect = layout.boxRect;
+	const Common::Rect overlayBounds(overlayWidth, overlayHeight);
+
+	if (background) {
+		drawLabelBox(surface, boxRect, overlayWidth, overlayHeight, format);
+		if (_style.labelBorder)
+			boxRect.grow(MAX(1, (int)(_style.borderThickness * _sizeScale)) - 1);
+		boxRect.clip(overlayBounds);
+		addDrawRect(boxRect);
+		return;
+	}
+	Common::Rect bounds = textRect;
+	bounds.clip(overlayBounds);
+	addDrawRect(bounds);
+	const uint32 textColor = format.RGBToColor(_style.textColor >> 16, _style.textColor >> 8, _style.textColor);
+	if (textRect.width() == baseTextWidth && textRect.height() == baseTextHeight &&
+		textRect.left >= 0 && textRect.top >= 0 && textRect.right <= overlayWidth && textRect.bottom <= overlayHeight) {
+		Surface textArea = surface->getSubArea(textRect);
+		font->drawString(&textArea, label, 0, 0, baseTextWidth, textColor, kTextAlignLeft);
+		return;
+	}
+	Common::Rect clipped = textRect;
+	clipped.clip(overlayBounds);
+	if (clipped.isEmpty())
+		return;
+	LabelMask scratch;
+	const LabelMask &mask = getLabelMask(label, font, textRect.width(), textRect.height(), baseTextWidth, baseTextHeight, format, scratch);
+	const int offsetX = clipped.left - textRect.left;
+	const int offsetY = clipped.top - textRect.top;
+	const int rowWidth = clipped.width();
+	for (int row = 0; row < clipped.height(); ++row) {
+		const byte *bits = &mask.bits[(offsetY + row) * mask.stride];
+		const int py = clipped.top + row;
+		int x = 0;
+		while (x < rowWidth) {
+			const int bit = offsetX + x;
+			const byte b = bits[bit >> 3];
+			if (!b) {
+				// whole byte is empty glyph coverage; skip 8 pixels at once
+				x += 8 - (bit & 7);
 				continue;
-			surface->setPixel(px, py, pixel);
+			}
+			if ((b >> (bit & 7)) & 1) {
+				const int px = clipped.left + x;
+				if (format.bytesPerPixel == 2)
+					*(uint16 *)surface->getBasePtr(px, py) = (uint16)textColor;
+				else if (format.bytesPerPixel == 4)
+					*(uint32 *)surface->getBasePtr(px, py) = textColor;
+				else
+					surface->setPixel(px, py, textColor);
+			}
+			++x;
 		}
 	}
-
-	scaledText->free();
-	delete scaledText;
 }
 
-void HotspotRenderer::drawCrosshairMarker(Surface *surface, int x, int y,
-		int width, int height, const PixelFormat &format) {
+void HotspotRenderer::drawCrosshairMarker(Surface *surface, int x, int y, const PixelFormat &format) {
 	const int lineLength = (_markerSize / 2) - 1;
-
-	drawLine(surface, x - lineLength, y, x + lineLength, y, width, height, format, _lineThickness);
-	drawLine(surface, x, y - lineLength, x, y + lineLength, width, height, format, _lineThickness);
+	const int t = _lineThickness;
+	fillBlendRect(surface, Common::Rect(x - lineLength, y - t + 1, x + lineLength + 1, y + t),
+		format, _style.markerColor, _style.markerOpacity);
+	fillBlendRect(surface, Common::Rect(x - t + 1, y - lineLength, x + t, y + lineLength + 1),
+		format, _style.markerColor, _style.markerOpacity);
 }
 
-void HotspotRenderer::drawSquareMarker(Surface *surface, int x, int y,
-		int width, int height, const PixelFormat &format) {
-	Common::Rect markerRect((int16)_markerSize, (int16)_markerSize);
-	markerRect.moveTo(x - _markerSize / 2, y - _markerSize / 2);
-
-	drawRect(surface, markerRect, width, height, format);
+void HotspotRenderer::drawSquareMarker(Surface *surface, int x, int y, const PixelFormat &format) {
+	Common::Rect rect((int16)_markerSize, (int16)_markerSize);
+	rect.moveTo(x - _markerSize / 2, y - _markerSize / 2);
+	const int t = _lineThickness;
+	fillBlendRect(surface, Common::Rect(rect.left, rect.top - t + 1, rect.right, rect.top + t),
+		format, _style.markerColor, _style.markerOpacity);
+	fillBlendRect(surface, Common::Rect(rect.left, rect.bottom - t, rect.right, rect.bottom + t - 1),
+		format, _style.markerColor, _style.markerOpacity);
+	fillBlendRect(surface, Common::Rect(rect.left - t + 1, rect.top + 1, rect.left + t, rect.bottom - 1),
+		format, _style.markerColor, _style.markerOpacity);
+	fillBlendRect(surface, Common::Rect(rect.right - t, rect.top + 1, rect.right + t - 1, rect.bottom - 1),
+		format, _style.markerColor, _style.markerOpacity);
 }
 
 void HotspotRenderer::drawPointMarker(Surface *surface, int x, int y,
@@ -185,9 +445,8 @@ void HotspotRenderer::drawPointMarker(Surface *surface, int x, int y,
 				continue;
 
 			int distSq = dx * dx + dy * dy;
-			int dist = (int)(sqrtf((float)distSq) + 0.5f);
-
-			blendPixel(surface, px, py, format, dist, _pointRadius);
+			if (distSq < _pointRadius * _pointRadius - _pointRadius + 1)
+				blendPixel(surface, px, py, format, 0, _pointRadius);
 		}
 	}
 }
@@ -197,90 +456,61 @@ void HotspotRenderer::blendPixel(Surface *surface, int px, int py,
 	// Convert solidSize to maximum solid distance (solidSize=1 means center pixel only, distance=0)
 	int maxSolidDistance = solidSize - 1;
 
-	if (distance > maxSolidDistance)
+	if (!_style.markerOpacity || distance > maxSolidDistance)
 		return;
 
+	if (format.aBits()) {
+		surface->setPixel(px, py, _markerBlendColor);
+		return;
+	}
+
 	byte bgR, bgG, bgB, bgA;
 	format.colorToARGB(surface->getPixel(px, py), bgA, bgR, bgG, bgB);
 
-	const int solidBeta = 180;
+	const int solidBeta = _style.markerOpacity;
 	const int solidAlpha = 255 - solidBeta;
-	byte r = ((255 * solidBeta) + (bgR * solidAlpha)) / 255;
-	byte g = ((255 * solidBeta) + (bgG * solidAlpha)) / 255;
-	byte b = ((255 * solidBeta) + (bgB * solidAlpha)) / 255;
+	byte r = ((((_style.markerColor >> 16) & 255) * solidBeta) + (bgR * solidAlpha)) / 255;
+	byte g = ((((_style.markerColor >> 8) & 255) * solidBeta) + (bgG * solidAlpha)) / 255;
+	byte b = ((((_style.markerColor >> 0) & 255) * solidBeta) + (bgB * solidAlpha)) / 255;
 	surface->setPixel(px, py, format.RGBToColor(r, g, b));
 }
 
-void HotspotRenderer::drawLine(Surface *surface, int x1, int y1, int x2, int y2,
-		int width, int height, const PixelFormat &format, int lineThickness) {
-	bool isHorizontal = (y1 == y2);
-	bool isVertical = (x1 == x2);
-
-	if (!isHorizontal && !isVertical)
+void HotspotRenderer::fillBlendRect(Surface *surface, Common::Rect rect,
+		const PixelFormat &format, uint32 color, byte opacity) {
+	if (!opacity)
+		return;
+	rect.clip(Common::Rect(surface->w, surface->h));
+	if (format.aBits()) {
+		surface->fillRect(rect, format.ARGBToColor(opacity, color >> 16, color >> 8, color));
 		return;
-
-	int alongMin, alongMax, acrossFixed;
-
-	if (isHorizontal) {
-		alongMin = MIN(x1, x2);
-		alongMax = MAX(x1, x2);
-		acrossFixed = y1;
-	} else {
-		alongMin = MIN(y1, y2);
-		alongMax = MAX(y1, y2);
-		acrossFixed = x1;
 	}
-
-	for (int along = alongMin; along <= alongMax; along++) {
-		for (int thickness = -lineThickness; thickness <= lineThickness; thickness++) {
-			int px, py;
-			if (isHorizontal) {
-				px = along;
-				py = acrossFixed + thickness;
-			} else {
-				px = acrossFixed + thickness;
-				py = along;
-			}
-
-			if (px < 0 || px >= width || py < 0 || py >= height)
-				continue;
-
-			int distFromCenter = ABS(thickness);
-			blendPixel(surface, px, py, format, distFromCenter, lineThickness);
+	const int alpha = 255 - opacity;
+	const int red = ((color >> 16) & 255) * opacity;
+	const int green = ((color >> 8) & 255) * opacity;
+	const int blue = (color & 255) * opacity;
+	for (int py = rect.top; py < rect.bottom; ++py) {
+		for (int px = rect.left; px < rect.right; ++px) {
+			byte r, g, b;
+			format.colorToRGB(surface->getPixel(px, py), r, g, b);
+			surface->setPixel(px, py, format.RGBToColor((r * alpha + red) / 255, (g * alpha + green) / 255, (b * alpha + blue) / 255));
 		}
 	}
 }
 
-void HotspotRenderer::drawRect(Surface *surface, const Common::Rect &rect,
-		int overlayWidth, int overlayHeight, const PixelFormat &format) {
-	drawLine(surface, rect.left, rect.top, rect.right - 1, rect.top,
-			overlayWidth, overlayHeight, format, _lineThickness);
-	drawLine(surface, rect.left, rect.bottom - 1, rect.right - 1, rect.bottom - 1,
-			overlayWidth, overlayHeight, format, _lineThickness);
-	drawLine(surface, rect.left, rect.top + 1, rect.left, rect.bottom - 2,
-			overlayWidth, overlayHeight, format, _lineThickness);
-	drawLine(surface, rect.right - 1, rect.top + 1, rect.right - 1, rect.bottom - 2,
-			overlayWidth, overlayHeight, format, _lineThickness);
-}
-
 void HotspotRenderer::drawLabelBox(Surface *surface, const Common::Rect &rect,
 		int overlayWidth, int overlayHeight, const PixelFormat &format) {
-	for (int py = rect.top + 1; py < rect.bottom - 1; py++) {
-		for (int px = rect.left + 1; px < rect.right - 1; px++) {
-			if (px < 0 || px >= overlayWidth || py < 0 || py >= overlayHeight)
-				continue;
-
-			byte bgR, bgG, bgB, bgA;
-			format.colorToARGB(surface->getPixel(px, py), bgA, bgR, bgG, bgB);
-
-			byte r = (bgR * 40) / 100;
-			byte g = (bgG * 40) / 100;
-			byte b = (bgB * 40) / 100;
-			surface->setPixel(px, py, format.RGBToColor(r, g, b));
-		}
+	if (rect.width() > 2 && rect.height() > 2)
+		fillBlendRect(surface, Common::Rect(rect.left + 1, rect.top + 1, rect.right - 1, rect.bottom - 1),
+			format, _style.backgroundColor, _style.backgroundOpacity);
+	if (!_style.labelBorder || rect.isEmpty())
+		return;
+	const int thickness = MAX(1, (int)(_style.borderThickness * _sizeScale));
+	fillBlendRect(surface, Common::Rect(rect.left, rect.top - thickness + 1, rect.right, rect.top + thickness), format, _style.borderColor, _style.borderOpacity);
+	fillBlendRect(surface, Common::Rect(rect.left, rect.bottom - thickness, rect.right, rect.bottom + thickness - 1), format, _style.borderColor, _style.borderOpacity);
+	if (rect.height() > 2) {
+		fillBlendRect(surface, Common::Rect(rect.left - thickness + 1, rect.top + 1, rect.left + thickness, rect.bottom - 1), format, _style.borderColor, _style.borderOpacity);
+		fillBlendRect(surface, Common::Rect(rect.right - thickness, rect.top + 1, rect.right + thickness - 1, rect.bottom - 1), format, _style.borderColor, _style.borderOpacity);
 	}
-
-	drawRect(surface, rect, overlayWidth, overlayHeight, format);
 }
 
 } // End of namespace Graphics
diff --git a/graphics/hotspot_renderer.h b/graphics/hotspot_renderer.h
index 7bd69f0e12f..c39755c3f4e 100644
--- a/graphics/hotspot_renderer.h
+++ b/graphics/hotspot_renderer.h
@@ -25,6 +25,9 @@
 #include "common/array.h"
 #include "common/rect.h"
 #include "common/ustr.h"
+#include "graphics/surface.h"
+
+class OSystem;
 
 namespace Graphics {
 
@@ -65,13 +68,44 @@ enum MarkerShape {
 	kMarkerPoint
 };
 
+const int kMarkerDefault = -1;
+
+struct HotspotStyle {
+	int markerSize = 10;
+	int pointRadius = 3;
+	int lineThickness = 1;
+	int borderThickness = 1;
+	int labelGap = 8;
+	int paddingX = 4;
+	int paddingY = 2;
+	uint32 markerColor = 0xffffff;
+	uint32 textColor = 0xffffff;
+	uint32 backgroundColor = 0;
+	uint32 borderColor = 0xffffff;
+	byte markerOpacity = 180;
+	byte backgroundOpacity = 153;
+	byte borderOpacity = 180;
+	bool labelBorder = true;
+	bool scaleWithGame = true;
+	const Font *font = nullptr;
+};
+
 /**
  * Renderer for drawing hotspot markers on overlay surface.
  */
 class HotspotRenderer {
 public:
 	HotspotRenderer();
-	~HotspotRenderer();
+	virtual ~HotspotRenderer();
+
+	const HotspotStyle &getStyle() const { return _style; }
+	void setStyle(const HotspotStyle &style);
+	void invalidate();
+	void releaseResources();
+	bool empty() const { return _hotspots.empty(); }
+	void setHotspots(Common::Array<HotspotInfo> hotspots);
+	void exchangeHotspots(Common::Array<HotspotInfo> &hotspots);
+	void renderOverlay(OSystem &system, MarkerShape markerShape, bool showText, bool forceRedraw = false);
 
 	/**
 	 * Render hotspot markers on an overlay surface.
@@ -94,34 +128,78 @@ public:
 		MarkerShape markerShape,
 		bool showText);
 
-private:
-	enum {
-		kBaseMarkerSize = 10,  ///< Size of square and crosshair markers at 1x scale
-		kBasePointRadius = 3,  ///< Radius of point marker at 1x scale
-		kBaseLineThickness = 1 ///< Thickness of lines for markers and text boxes at 1x scale
+protected:
+	struct LabelLayout {
+		Common::Rect textRect, boxRect;
+		int baseTextWidth = 0, baseTextHeight = 0;
 	};
+	virtual LabelLayout layoutHotspotLabel(const HotspotInfo &hotspot, Common::Point position,
+		int overlayWidth, int overlayHeight, const Font *font) const;
+	void addDrawRect(Common::Rect rect);
+	virtual void prepareRender(const Common::Array<HotspotInfo> &hotspots,
+		int gameWidth, int gameHeight, int overlayWidth, int overlayHeight) {}
+	virtual Common::Point placeHotspot(const HotspotInfo &hotspot, Common::Point position) { return position; }
+	virtual void drawMarker(Surface *surface, const HotspotInfo &hotspot, int x, int y, const PixelFormat &format, MarkerShape markerShape);
+	virtual void drawHotspotLabelBackground(Surface *surface, const LabelLayout &layout, const HotspotInfo &hotspot,
+		int overlayWidth, int overlayHeight, const PixelFormat &format, const Font *font);
+	virtual void drawHotspotLabel(Surface *surface, const LabelLayout &layout, const HotspotInfo &hotspot,
+		int overlayWidth, int overlayHeight, const PixelFormat &format, const Font *font);
+	void blendPixel(Surface *surface, int px, int py, const PixelFormat &format,
+		int distance, int solidSize);
 
+private:
+	HotspotRenderer(const HotspotRenderer &) = delete;
+	HotspotRenderer &operator=(const HotspotRenderer &) = delete;
+	/** 1-bit-per-pixel glyph coverage mask, packed 8 pixels per byte (LSB first) per row. */
+	struct LabelMask {
+		Common::Array<byte> bits;
+		int width = 0, height = 0, stride = 0;
+	};
+	struct CachedLabel {
+		Common::U32String text;
+		LabelMask mask;
+		const Font *font;
+		PixelFormat format;
+		uint64 lastUse;
+	};
+	Common::Array<CachedLabel> _labels;
+	uint _labelBytes = 0;
+	uint64 _labelClock = 0;
+	Common::Array<Common::Point> _positions;
+	Common::Array<LabelLayout> _labelLayouts;
+	void clearLabels();
+	static void packLabelMask(const Surface &scaled, LabelMask &mask);
+	const LabelMask &getLabelMask(const Common::U32String &text, const Font *font,
+		int width, int height, int baseWidth, int baseHeight, const PixelFormat &format, LabelMask &scratch);
+	Common::Rect _drawBounds, _previousBounds;
+	Surface _overlay;
+	Common::Array<HotspotInfo> _hotspots;
+	bool _dirty = true;
+	Common::Point _gameSize;
+	int _screenChangeID = -1;
+	MarkerShape _shape = kMarkerCrosshair;
+	const Font *_font = nullptr;
+	void drawLabel(Surface *surface, const LabelLayout &layout, const HotspotInfo &hotspot,
+		int overlayWidth, int overlayHeight, const PixelFormat &format, const Font *font, bool background);
+	void renderFrame(Surface *surface, const Common::Array<HotspotInfo> &hotspots,
+		int gameWidth, int gameHeight, int overlayWidth, int overlayHeight,
+		const PixelFormat &format, MarkerShape markerShape, const Font *font);
+
+protected:
+	HotspotStyle _style;
 	float _sizeScale;   ///< Overlay/game scale factor applied to marker geometry
 	int _markerSize;    ///< Scaled size of square and crosshair markers
 	int _pointRadius;   ///< Scaled radius of point marker
 	int _lineThickness; ///< Scaled thickness of lines for markers and text boxes
+	uint32 _markerBlendColor; ///< Precomputed ARGBToColor(markerOpacity, markerColor) for alpha-blended overlays, recomputed once per renderFrame()
 
-	void drawMarker(Surface *surface, int x, int y, const PixelFormat &format, MarkerShape markerShape);
-	void drawCrosshairMarker(Surface *surface, int x, int y, int width, int height, const PixelFormat &format);
-	void drawSquareMarker(Surface *surface, int x, int y, int width, int height, const PixelFormat &format);
+	void drawCrosshairMarker(Surface *surface, int x, int y, const PixelFormat &format);
+	void drawSquareMarker(Surface *surface, int x, int y, const PixelFormat &format);
 	void drawPointMarker(Surface *surface, int x, int y, int width, int height, const PixelFormat &format);
-	void drawHotspotLabel(Surface *surface, int overlayX, int overlayY, const Common::U32String &label,
-		int overlayWidth, int overlayHeight, const PixelFormat &format, const Font *font);
+	void fillBlendRect(Surface *surface, Common::Rect rect, const PixelFormat &format, uint32 color, byte opacity);
 	void drawLabelBox(Surface *surface, const Common::Rect &rect,
 		int overlayWidth, int overlayHeight, const PixelFormat &format);
-	void drawRect(Surface *surface, const Common::Rect &rect,
-		int overlayWidth, int overlayHeight, const PixelFormat &format);
-
-	void drawLine(Surface *surface, int x1, int y1, int x2, int y2,
-		int width, int height, const PixelFormat &format, int lineThickness);
 
-	void blendPixel(Surface *surface, int px, int py, const PixelFormat &format,
-		int distance, int solidSize);
 };
 
 } // End of namespace Graphics
diff --git a/gui/editgamedialog.cpp b/gui/editgamedialog.cpp
index d21ccc7e184..e357613d2d3 100644
--- a/gui/editgamedialog.cpp
+++ b/gui/editgamedialog.cpp
@@ -338,10 +338,12 @@ EditGameDialog::EditGameDialog(const Common::String &domain)
 	_hotspotMarkerPopUpDesc = new StaticTextWidget(miscContainer,
 			"GameOptions_Misc_Container.HotspotMarkerPopupDesc", _("Hotspot marker:"));
 	_hotspotMarkerPopUp = new PopUpWidget(miscContainer, "GameOptions_Misc_Container.HotspotMarkerPopup");
+	_hotspotMarkerPopUp->appendEntry(_("<default>"), (uint32)Graphics::kMarkerDefault);
 	_hotspotMarkerPopUp->appendEntry(_("Point"), Graphics::kMarkerPoint);
 	_hotspotMarkerPopUp->appendEntry(_("Square"), Graphics::kMarkerSquare);
 	_hotspotMarkerPopUp->appendEntry(_("Crosshair"), Graphics::kMarkerCrosshair);
-	_hotspotMarkerPopUp->setSelectedTag(ConfMan.getInt("hotspot_marker", _domain));
+	_hotspotMarkerPopUp->setSelectedTag(ConfMan.hasKey("hotspot_marker", _domain) ?
+		(uint32)ConfMan.getInt("hotspot_marker", _domain) : (uint32)Graphics::kMarkerDefault);
 	_hotspotMarkerPopUp->setEnabled(hotspotsEnabled);
 	_hotspotMarkerPopUpDesc->setEnabled(hotspotsEnabled);
 




More information about the Scummvm-git-logs mailing list