From 7795ce38d8a3efefec97fbb288e4a7aaa73e2105 Mon Sep 17 00:00:00 2001 From: Joseph Davies Date: Tue, 7 Jul 2009 01:07:01 +0000 Subject: [PATCH] ASYLUM: Added support for polygon testing inside bounding boxes. git-svn-id: http://asylumengine.googlecode.com/svn/trunk@228 0bfb4aae-4ea4-11de-8d8d-752d95cf3e3c --- engines/asylum/scene.cpp | 37 +++++++++++++++++++++++++++++++++---- engines/asylum/scene.h | 4 +++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/engines/asylum/scene.cpp b/engines/asylum/scene.cpp index f4344b22a95..f90087702c5 100644 --- a/engines/asylum/scene.cpp +++ b/engines/asylum/scene.cpp @@ -59,7 +59,7 @@ Scene::Scene(Screen *screen, Sound *sound, uint8 sceneIdx): _screen(screen), _so _leftClick = false; _rightButton = false; _isActive = false; - g_debugPolygons = 0; + g_debugPolygons = 1; } Scene::~Scene() { @@ -300,9 +300,11 @@ void Scene::update() { for (uint32 p = 0; p < _sceneResource->getGamePolygons()->numEntries; p++) { PolyDefinitions poly = _sceneResource->getGamePolygons()->polygons[p]; if (poly.boundingRect.contains(_mouseX + _startX, _mouseY + _startY)) { - curHotspot = (int32)p; - updateCursor(); - break; + if (pointInPoly(&poly, _mouseX + _startX, _mouseY + _startY)) { + curHotspot = (int32)p; + updateCursor(); + break; + } } } @@ -408,6 +410,33 @@ void Scene::updateBarrier(Screen *screen, ResourcePack *res, uint8 barrierIndex) delete gra; } +bool Scene::pointInPoly(PolyDefinitions *poly, int x, int y) { + // Copied from backends/vkeybd/polygon.cpp + int yflag0; + int yflag1; + bool inside_flag = false; + unsigned int pt; + + Common::Point *vtx0 = &poly->points[poly->numPoints - 1]; + Common::Point *vtx1 = &poly->points[0]; + + yflag0 = (vtx0->y >= y); + for (pt = 0; pt < poly->numPoints; pt++, vtx1++) { + yflag1 = (vtx1->y >= y); + if (yflag0 != yflag1) { + if (((vtx1->y - y) * (vtx0->x - vtx1->x) >= + (vtx1->x - x) * (vtx0->y - vtx1->y)) == yflag1) { + inside_flag = !inside_flag; + } + } + yflag0 = yflag1; + vtx0 = vtx1; + } + + return inside_flag; +} + + // POLYGONS DEBUG void Scene::ShowPolygons() { for (uint32 p = 0; p < _sceneResource->getGamePolygons()->numEntries; p++) { diff --git a/engines/asylum/scene.h b/engines/asylum/scene.h index 4634bb9c327..e923a666e32 100644 --- a/engines/asylum/scene.h +++ b/engines/asylum/scene.h @@ -40,8 +40,9 @@ class Screen; class Sound; class SceneResource; class Text; -class ActionDefinitions; class Interpreter; +class ActionDefinitions; +class PolyDefinitions; class Scene { public: @@ -99,6 +100,7 @@ private: void updateBarrier(Screen *screen, ResourcePack *res, uint8 actorIndex); void ShowPolygons(); + bool pointInPoly(PolyDefinitions *poly, int x, int y); friend class Interpreter; }; // end of class Scene