From 9505e3789cc9db6a10a68b9794a586604271b76f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 23 Aug 2011 01:48:25 +0300 Subject: Add protection against infinite loop in viewport and add debug message if some thing wrong happend. --- src/gui/viewport.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/gui/viewport.cpp') diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index 70d02b016..a5b541a8e 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -145,8 +145,10 @@ void Viewport::draw(gcn::Graphics *gcnGraphics) if (mScrollLaziness < 1) mScrollLaziness = 1; // Avoids division by zero + int cnt = 0; + // Apply lazy scrolling - while (lastTick < tick_time) + while (lastTick < tick_time && cnt < 32) { if (player_x > static_cast(mPixelViewX) + mScrollRadius) { @@ -172,19 +174,26 @@ void Viewport::draw(gcn::Graphics *gcnGraphics) - static_cast(mPixelViewY) + mScrollRadius) / static_cast(mScrollLaziness); } - lastTick++; + lastTick ++; + cnt ++; } // Auto center when player is off screen - if (player_x - static_cast(mPixelViewX) > graphics->mWidth / 2 - || static_cast(mPixelViewX) - player_x > graphics->mWidth / 2 - || static_cast(mPixelViewY) - player_y - > graphics->getHeight() / 2 - || player_y - static_cast(mPixelViewY) - > graphics->getHeight() / 2) + if (cnt > 30 || player_x - static_cast(mPixelViewX) + > graphics->mWidth / 2 || static_cast(mPixelViewX) + - player_x > graphics->mWidth / 2 || static_cast(mPixelViewY) + - player_y > graphics->getHeight() / 2 || player_y + - static_cast(mPixelViewY) > graphics->getHeight() / 2) { mPixelViewX = static_cast(player_x); mPixelViewY = static_cast(player_y); + if (player_x <= 0 || player_y <= 0) + { + if (debugChatTab) + debugChatTab->chatLog("incorrect player position!"); + logger->log("incorrect player position: %d, %d", + player_x, player_y); + } }; // Don't move camera so that the end of the map is on screen -- cgit v1.2.3-60-g2f50