[Scummvm-cvs-logs] SF.net SVN: scummvm:[45542] scummvm/trunk/engines/sci/gui

thebluegr at users.sourceforge.net thebluegr at users.sourceforge.net
Fri Oct 30 18:38:11 CET 2009


Revision: 45542
          http://scummvm.svn.sourceforge.net/scummvm/?rev=45542&view=rev
Author:   thebluegr
Date:     2009-10-30 17:38:11 +0000 (Fri, 30 Oct 2009)

Log Message:
-----------
Added caching of views

Modified Paths:
--------------
    scummvm/trunk/engines/sci/gui/gui.cpp
    scummvm/trunk/engines/sci/gui/gui_animate.cpp
    scummvm/trunk/engines/sci/gui/gui_gfx.cpp
    scummvm/trunk/engines/sci/gui/gui_gfx.h

Modified: scummvm/trunk/engines/sci/gui/gui.cpp
===================================================================
--- scummvm/trunk/engines/sci/gui/gui.cpp	2009-10-30 17:13:25 UTC (rev 45541)
+++ scummvm/trunk/engines/sci/gui/gui.cpp	2009-10-30 17:38:11 UTC (rev 45542)
@@ -644,13 +644,12 @@
 }
 
 bool SciGui::isItSkip(GuiResourceId viewId, int16 loopNo, int16 celNo, Common::Point position) {
-	SciGuiView *tmpView = new SciGuiView(_s->resMan, NULL, NULL, viewId);
+	SciGuiView *tmpView = _gfx->getView(viewId);
 	sciViewCelInfo *celInfo = tmpView->getCelInfo(loopNo, celNo);
 	position.x = CLIP<int>(position.x, 0, celInfo->width - 1);
 	position.y = CLIP<int>(position.y, 0, celInfo->height - 1);
 	byte *celData = tmpView->getBitmap(loopNo, celNo);
 	bool result = (celData[position.y * celInfo->width + position.x] == celInfo->clearKey);
-	delete tmpView;
 	return result;
 }
 
@@ -664,7 +663,7 @@
 		int16 loopNo = GET_SEL32V(_s->_segMan, object, loop);
 		int16 celNo = GET_SEL32V(_s->_segMan, object, cel);
 
-		SciGuiView *tmpView = new SciGuiView(_s->resMan, NULL, NULL, viewId);
+		SciGuiView *tmpView = _gfx->getView(viewId);
 		sciViewCelInfo *celInfo = tmpView->getCelInfo(loopNo, celNo);
 		int16 left = x + celInfo->displaceX - (celInfo->width >> 1);
 		int16 right = left + celInfo->width;
@@ -674,8 +673,6 @@
 		debugC(2, kDebugLevelBaseSetter, "(%d,%d)+/-(%d,%d), (%d x %d) -> (%d, %d) to (%d, %d)\n",
 				x, y, celInfo->displaceX, celInfo->displaceY, celInfo->width, celInfo->height, left, top, bottom, right);
 
-		delete tmpView;
-
 		PUT_SEL32V(_s->_segMan, object, brLeft, left);
 		PUT_SEL32V(_s->_segMan, object, brRight, right);
 		PUT_SEL32V(_s->_segMan, object, brTop, top);
@@ -725,43 +722,19 @@
 }
 
 int16 SciGui::getCelWidth(GuiResourceId viewId, int16 loopNo, int16 celNo) {
-	SciGuiView *tmpView = new SciGuiView(_s->resMan, _screen, _palette, viewId);
-	sciViewCelInfo *celInfo = tmpView->getCelInfo(loopNo, celNo);
-	int16 celWidth = celInfo->width;
-	delete tmpView;
-
-	return celWidth;
+	return _gfx->getView(viewId)->getCelInfo(loopNo, celNo)->width;
 }
 
 int16 SciGui::getCelHeight(GuiResourceId viewId, int16 loopNo, int16 celNo) {
-	SciGuiView *tmpView = new SciGuiView(_s->resMan, _screen, _palette, viewId);
-	sciViewCelInfo *celInfo = tmpView->getCelInfo(loopNo, celNo);
-	int16 celHeight = celInfo->height;
-	delete tmpView;
-
-	return celHeight;
+	return _gfx->getView(viewId)->getCelInfo(loopNo, celNo)->height;
 }
 
 int16 SciGui::getLoopCount(GuiResourceId viewId) {
-	SciGuiView *tmpView = new SciGuiView(_s->resMan, _screen, _palette, viewId);
-	if (!tmpView)
-		return -1;
-
-	uint16 loopCount = tmpView->getLoopCount();
-	delete tmpView;
-
-	return loopCount;
+	return _gfx->getView(viewId)->getLoopCount();
 }
 
 int16 SciGui::getCelCount(GuiResourceId viewId, int16 loopNo) {
-	SciGuiView *tmpView = new SciGuiView(_s->resMan, _screen, _palette, viewId);
-	if (!tmpView)
-		return -1;
-
-	uint16 celCount = tmpView->getLoopInfo(loopNo)->celCount;
-	delete tmpView;
-
-	return celCount;
+	return _gfx->getView(viewId)->getLoopInfo(loopNo)->celCount;
 }
 
 bool SciGui::debugUndither(bool flag) {

Modified: scummvm/trunk/engines/sci/gui/gui_animate.cpp
===================================================================
--- scummvm/trunk/engines/sci/gui/gui_animate.cpp	2009-10-30 17:13:25 UTC (rev 45541)
+++ scummvm/trunk/engines/sci/gui/gui_animate.cpp	2009-10-30 17:38:11 UTC (rev 45542)
@@ -196,7 +196,7 @@
 		curObject = listEntry->object;
 
 		// Get the corresponding view
-		view = new SciGuiView(_s->resMan, _screen, _palette, listEntry->viewId);
+		view = _gfx->getView(listEntry->viewId);
 		
 		// adjust loop and cel, if any of those is invalid
 		if (listEntry->loopNo >= view->getLoopCount()) {
@@ -238,8 +238,6 @@
 		listEntry->signal = signal;
 
 		listIterator++;
-
-		delete view;
 	}
 }
 
@@ -544,7 +542,7 @@
 			listEntry->priority = _gfx->CoordinateToPriority(listEntry->y);
 
 		// Get the corresponding view
-		view = new SciGuiView(_s->resMan, _screen, _palette, listEntry->viewId);
+		view = _gfx->getView(listEntry->viewId);
 
 		// Create rect according to coordinates and given cel
 		view->getCelRect(listEntry->loopNo, listEntry->celNo, listEntry->x, listEntry->y, listEntry->z, &listEntry->celRect);
@@ -557,22 +555,16 @@
 		}
 
 		listIterator++;
-
-		delete view;
 	}
 }
 
 void SciGuiAnimate::addToPicDrawView(GuiResourceId viewId, GuiViewLoopNo loopNo, GuiViewCelNo celNo, int16 leftPos, int16 topPos, int16 priority, int16 control) {
-	SciGuiView *view = NULL;
+	SciGuiView *view = _gfx->getView(viewId);
 	Common::Rect celRect;
 
-	view = new SciGuiView(_s->resMan, _screen, _palette, viewId);
-
 	// Create rect according to coordinates and given cel
 	view->getCelRect(loopNo, celNo, leftPos, topPos, priority, &celRect);
 	_gfx->drawCel(view, loopNo, celNo, celRect, priority, 0);
-
-	delete view;
 }
 
 } // End of namespace Sci

Modified: scummvm/trunk/engines/sci/gui/gui_gfx.cpp
===================================================================
--- scummvm/trunk/engines/sci/gui/gui_gfx.cpp	2009-10-30 17:13:25 UTC (rev 45541)
+++ scummvm/trunk/engines/sci/gui/gui_gfx.cpp	2009-10-30 17:38:11 UTC (rev 45542)
@@ -45,6 +45,8 @@
 }
 
 SciGuiGfx::~SciGuiGfx() {
+	purgeCache();
+
 	delete _mainPort;
 	delete _menuPort;
 }
@@ -66,6 +68,25 @@
 	_menuRect = Common::Rect(0, 0, _screen->_width, 9);
 }
 
+void SciGuiGfx::purgeCache() {
+	for (ViewCache::iterator iter = _cachedViews.begin(); iter != _cachedViews.end(); ++iter) {
+		delete iter->_value;
+		iter->_value = 0;
+	}
+
+	_cachedViews.clear();
+}
+
+SciGuiView *SciGuiGfx::getView(GuiResourceId viewNum) {
+	if (_cachedViews.size() >= MAX_CACHED_VIEWS)
+		purgeCache();
+
+	if (!_cachedViews.contains(viewNum))
+		_cachedViews[viewNum] = new SciGuiView(_s->resMan, _screen, _palette, viewNum);
+
+	return _cachedViews[viewNum];
+}
+
 GuiPort *SciGuiGfx::SetPort(GuiPort *newPort) {
 	GuiPort *oldPort = _curPort;
 	_curPort = newPort;
@@ -309,7 +330,7 @@
 
 // This one is the only one that updates screen!
 void SciGuiGfx::drawCel(GuiResourceId viewId, GuiViewLoopNo loopNo, GuiViewCelNo celNo, uint16 leftPos, uint16 topPos, byte priority, uint16 paletteNo) {
-	SciGuiView *view = new SciGuiView(_s->resMan, _screen, _palette, viewId);
+	SciGuiView *view = getView(viewId);
 	Common::Rect rect;
 	Common::Rect clipRect;
 	if (view) {
@@ -320,7 +341,6 @@
 		clipRect = rect;
 		clipRect.clip(_curPort->rect);
 		if (clipRect.isEmpty()) {	// nothing to draw
-			delete view;
 			return;
 		}
 
@@ -330,19 +350,16 @@
 		if (!_screen->_picNotValid)
 			BitsShow(rect);
 	}
-
-	delete view;
 }
 
 // This version of drawCel is not supposed to call BitsShow()!
 void SciGuiGfx::drawCel(GuiResourceId viewId, GuiViewLoopNo loopNo, GuiViewCelNo celNo, Common::Rect celRect, byte priority, uint16 paletteNo) {
-	SciGuiView *view = new SciGuiView(_s->resMan, _screen, _palette, viewId);
+	SciGuiView *view = getView(viewId);
 	Common::Rect clipRect;
 	if (view) {
 		clipRect = celRect;
 		clipRect.clip(_curPort->rect);
 		if (clipRect.isEmpty()) { // nothing to draw
-			delete view;
 			return;
 		}
 
@@ -350,8 +367,6 @@
 		OffsetRect(clipRectTranslated);
 		view->draw(celRect, clipRect, clipRectTranslated, loopNo, celNo, priority, paletteNo);
 	}
-
-	delete view;
 }
 
 // This version of drawCel is not supposed to call BitsShow()!
@@ -504,7 +519,7 @@
 	}
 
 	// now get cel rectangle
-	view = new SciGuiView(_s->resMan, _screen, _palette, viewId);
+	view = getView(viewId);
 	view->getCelRect(loopNo, celNo, x, y, z, &celRect);
 
 	// TODO: sometimes loop is negative. Check what it means
@@ -514,8 +529,6 @@
 		PUT_SEL32V(segMan, objectReference, nsTop, celRect.top);
 		PUT_SEL32V(segMan, objectReference, nsBottom, celRect.bottom);
 	}
-
-	delete view;
 }
 
 } // End of namespace Sci

Modified: scummvm/trunk/engines/sci/gui/gui_gfx.h
===================================================================
--- scummvm/trunk/engines/sci/gui/gui_gfx.h	2009-10-30 17:13:25 UTC (rev 45541)
+++ scummvm/trunk/engines/sci/gui/gui_gfx.h	2009-10-30 17:38:11 UTC (rev 45542)
@@ -28,17 +28,24 @@
 
 #include "sci/gui/gui.h"
 
+#include "common/hashmap.h"
+
 namespace Sci {
 
 #define SCI_TEXT_ALIGNMENT_RIGHT -1
 #define SCI_TEXT_ALIGNMENT_CENTER 1
 #define SCI_TEXT_ALIGNMENT_LEFT	0
 
+#define MAX_CACHED_VIEWS 50
+
 class SciGuiScreen;
 class SciGuiPalette;
 class SciGuiFont;
 class SciGuiPicture;
 class SciGuiView;
+
+typedef Common::HashMap<int, SciGuiView *> ViewCache;
+
 class SciGuiGfx {
 public:
 	SciGuiGfx(EngineState *state, SciGuiScreen *screen, SciGuiPalette *palette);
@@ -99,7 +106,11 @@
 	Common::Rect _menuRect;
 	GuiPort *_curPort;
 
+	SciGuiView *getView(GuiResourceId viewNum);
+
 private:
+	void purgeCache();
+
 	EngineState *_s;
 	SciGuiScreen *_screen;
 	SciGuiPalette *_palette;
@@ -111,6 +122,8 @@
 	// Priority Bands related variables
 	int16 _priorityTop, _priorityBottom, _priorityBandCount;
 	byte _priorityBands[200];
+
+	ViewCache _cachedViews;
 };
 
 } // End of namespace Sci


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list