summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-04-27 23:03:43 +0300
committerAndrei Karas <akaras@inbox.ru>2012-04-27 23:39:57 +0300
commitcaf62e4d1c039f8d4d33bfd05f476890fdc79fcf (patch)
tree0d3e455994185ffdf04212f3284797a899931744 /src
parent7f0fa9b82fc4deb28d61fc0d2fc503f0e48747c1 (diff)
downloadmanaplus-caf62e4d1c039f8d4d33bfd05f476890fdc79fcf.tar.gz
manaplus-caf62e4d1c039f8d4d33bfd05f476890fdc79fcf.tar.bz2
manaplus-caf62e4d1c039f8d4d33bfd05f476890fdc79fcf.tar.xz
manaplus-caf62e4d1c039f8d4d33bfd05f476890fdc79fcf.zip
Improve screen position calculation.
Diffstat (limited to 'src')
-rw-r--r--src/gui/viewport.cpp101
-rw-r--r--src/gui/viewport.h10
-rw-r--r--src/net/ea/playerhandler.cpp18
-rw-r--r--src/net/manaserv/playerhandler.cpp7
4 files changed, 60 insertions, 76 deletions
diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp
index 35382bfdf..d07f80e1c 100644
--- a/src/gui/viewport.cpp
+++ b/src/gui/viewport.cpp
@@ -61,8 +61,8 @@ Viewport::Viewport():
mMap(nullptr),
mMouseX(0),
mMouseY(0),
- mPixelViewX(0.0f),
- mPixelViewY(0.0f),
+ mPixelViewX(0),
+ mPixelViewY(0),
mShowDebugPath(false),
mCameraMode(0),
mPlayerFollowMouse(false),
@@ -156,28 +156,28 @@ void Viewport::draw(gcn::Graphics *gcnGraphics)
// Apply lazy scrolling
while (lastTick < tick_time && cnt < 32)
{
- if (player_x > static_cast<int>(mPixelViewX) + mScrollRadius)
+ if (player_x > mPixelViewX + mScrollRadius)
{
mPixelViewX += static_cast<float>(player_x
- - static_cast<int>(mPixelViewX) - mScrollRadius) /
+ - mPixelViewX - mScrollRadius) /
static_cast<float>(mScrollLaziness);
}
- if (player_x < static_cast<int>(mPixelViewX) - mScrollRadius)
+ if (player_x < mPixelViewX - mScrollRadius)
{
mPixelViewX += static_cast<float>(player_x
- - static_cast<int>(mPixelViewX) + mScrollRadius) /
+ - mPixelViewX + mScrollRadius) /
static_cast<float>(mScrollLaziness);
}
- if (player_y > static_cast<int>(mPixelViewY) + mScrollRadius)
+ if (player_y > mPixelViewY + mScrollRadius)
{
mPixelViewY += static_cast<float>(player_y
- - static_cast<int>(mPixelViewY) - mScrollRadius) /
+ - mPixelViewY - mScrollRadius) /
static_cast<float>(mScrollLaziness);
}
- if (player_y < static_cast<int>(mPixelViewY) - mScrollRadius)
+ if (player_y < mPixelViewY - mScrollRadius)
{
mPixelViewY += static_cast<float>(player_y
- - static_cast<int>(mPixelViewY) + mScrollRadius) /
+ - mPixelViewY + mScrollRadius) /
static_cast<float>(mScrollLaziness);
}
lastTick ++;
@@ -185,33 +185,32 @@ void Viewport::draw(gcn::Graphics *gcnGraphics)
}
// Auto center when player is off screen
- if (cnt > 30 || player_x - static_cast<int>(mPixelViewX)
- > graphics->mWidth / 2 || static_cast<int>(mPixelViewX)
- - player_x > graphics->mWidth / 2 || static_cast<int>(mPixelViewY)
+ if (cnt > 30 || player_x - mPixelViewX
+ > graphics->mWidth / 2 || mPixelViewX
+ - player_x > graphics->mWidth / 2 || mPixelViewY
- player_y > graphics->getHeight() / 2 || player_y
- - static_cast<int>(mPixelViewY) > graphics->getHeight() / 2)
+ - mPixelViewY > graphics->getHeight() / 2)
{
if (player_x <= 0 || player_y <= 0)
{
// if (debugChatTab)
// debugChatTab->chatLog("incorrect player position!");
logger->log("incorrect player position: %d, %d, %d, %d",
- player_x, player_y, static_cast<int>(mPixelViewX),
- static_cast<int>(mPixelViewY));
+ player_x, player_y, mPixelViewX,mPixelViewY);
if (player_node)
{
logger->log("tile position: %d, %d",
player_node->getTileX(), player_node->getTileY());
}
}
- mPixelViewX = static_cast<float>(player_x);
- mPixelViewY = static_cast<float>(player_y);
+ mPixelViewX = player_x;
+ mPixelViewY = player_y;
}
}
else
{
- mPixelViewX = static_cast<float>(player_x);
- mPixelViewY = static_cast<float>(player_y);
+ mPixelViewX = player_x;
+ mPixelViewY = player_y;
}
// Don't move camera so that the end of the map is on screen
@@ -226,20 +225,17 @@ void Viewport::draw(gcn::Graphics *gcnGraphics)
if (mPixelViewY < 0)
mPixelViewY = 0;
if (mPixelViewX > viewXmax)
- mPixelViewX = static_cast<float>(viewXmax);
+ mPixelViewX = viewXmax;
if (mPixelViewY > viewYmax)
- mPixelViewY = static_cast<float>(viewYmax);
+ mPixelViewY = viewYmax;
// Draw tiles and sprites
- mMap->draw(graphics, static_cast<int>(mPixelViewX),
- static_cast<int>(mPixelViewY));
+ mMap->draw(graphics, mPixelViewX, mPixelViewY);
if (mShowDebugPath)
{
- mMap->drawCollision(graphics,
- static_cast<int>(mPixelViewX),
- static_cast<int>(mPixelViewY),
- mShowDebugPath);
+ mMap->drawCollision(graphics, mPixelViewX,
+ mPixelViewY, mShowDebugPath);
if (mShowDebugPath == Map::MAP_DEBUG)
_drawDebugPath(graphics);
}
@@ -254,8 +250,7 @@ void Viewport::draw(gcn::Graphics *gcnGraphics)
// Draw text
if (textManager)
{
- textManager->draw(graphics, static_cast<int>(mPixelViewX),
- static_cast<int>(mPixelViewY));
+ textManager->draw(graphics, mPixelViewX, mPixelViewY);
}
// Draw player names, speech, and emotion sprite as needed
@@ -267,10 +262,8 @@ void Viewport::draw(gcn::Graphics *gcnGraphics)
continue;
Being *b = static_cast<Being*>(*it);
- b->drawSpeech(static_cast<int>(mPixelViewX),
- static_cast<int>(mPixelViewY));
- b->drawEmotion(graphics, static_cast<int>(mPixelViewX),
- static_cast<int>(mPixelViewY));
+ b->drawSpeech(mPixelViewX, mPixelViewY);
+ b->drawEmotion(graphics, mPixelViewX, mPixelViewY);
}
if (miniStatusWindow)
@@ -322,8 +315,8 @@ void Viewport::_drawDebugPath(Graphics *graphics)
static Path debugPath;
static Vector lastMouseDestination = Vector(0.0f, 0.0f);
- const int mousePosX = mMouseX + static_cast<int>(mPixelViewX);
- const int mousePosY = mMouseY + static_cast<int>(mPixelViewY);
+ const int mousePosX = mMouseX + mPixelViewX;
+ const int mousePosY = mMouseY + mPixelViewY;
Vector mouseDestination(mousePosX, mousePosY);
if (mouseDestination.x != lastMouseDestination.x
@@ -371,8 +364,8 @@ void Viewport::_drawPath(Graphics *graphics, const Path &path,
for (Path::const_iterator i = path.begin(), i_end = path.end();
i != i_end; ++i)
{
- int squareX = i->x * 32 - static_cast<int>(mPixelViewX) + 12;
- int squareY = i->y * 32 - static_cast<int>(mPixelViewY) + 12;
+ int squareX = i->x * 32 - mPixelViewX + 12;
+ int squareY = i->y * 32 - mPixelViewY + 12;
graphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8));
if (mMap)
@@ -389,8 +382,8 @@ void Viewport::_drawPath(Graphics *graphics, const Path &path,
for (Path::const_iterator i = path.begin(), i_end = path.end();
i != i_end; ++i)
{
- int squareX = i->x - static_cast<int>(mPixelViewX);
- int squareY = i->y - static_cast<int>(mPixelViewY);
+ int squareX = i->x - mPixelViewX;
+ int squareY = i->y - mPixelViewY;
graphics->fillRectangle(gcn::Rectangle(squareX - 4, squareY - 4,
8, 8));
@@ -419,8 +412,8 @@ void Viewport::mousePressed(gcn::MouseEvent &event)
if (Being::isTalking())
return;
- const int pixelX = event.getX() + static_cast<int>(mPixelViewX);
- const int pixelY = event.getY() + static_cast<int>(mPixelViewY);
+ const int pixelX = event.getX() + mPixelViewX;
+ const int pixelY = event.getY() + mPixelViewY;
// Right click might open a popup
if (event.getButton() == gcn::MouseEvent::RIGHT)
@@ -432,8 +425,8 @@ void Viewport::mousePressed(gcn::MouseEvent &event)
if (actorSpriteManager)
{
std::vector<ActorSprite*> beings;
- const int x = getMouseX() + static_cast<int>(mPixelViewX);
- const int y = getMouseY() + static_cast<int>(mPixelViewY);
+ const int x = getMouseX() + mPixelViewX;
+ const int y = getMouseY() + mPixelViewY;
actorSpriteManager->findBeingsByPixel(beings, x, y, true);
if (beings.size() > 1)
{
@@ -577,10 +570,8 @@ void Viewport::mouseDragged(gcn::MouseEvent &event)
{
mLocalWalkTime = tick_time;
player_node->unSetPickUpTarget();
- player_node->setDestination(event.getX()
- + static_cast<int>(mPixelViewX),
- event.getY()
- + static_cast<int>(mPixelViewY));
+ player_node->setDestination(event.getX() + mPixelViewX,
+ event.getY() + mPixelViewY);
player_node->pathSetByMouse();
}
}
@@ -590,12 +581,10 @@ void Viewport::mouseDragged(gcn::MouseEvent &event)
if (mLocalWalkTime != player_node->getActionTime())
{
mLocalWalkTime = cur_time;
- int destX = static_cast<int>((static_cast<float>(event.getX())
- + mPixelViewX)
- / static_cast<float>(mMap->getTileWidth()));
- int destY = static_cast<int>((static_cast<float>(event.getY())
- + mPixelViewY)
- / static_cast<float>(mMap->getTileHeight()));
+ int destX = (event.getX() + mPixelViewX)
+ / static_cast<float>(mMap->getTileWidth());
+ int destY = (event.getY() + mPixelViewY)
+ / static_cast<float>(mMap->getTileHeight());
player_node->unSetPickUpTarget();
if (!player_node->navigateTo(destX, destY))
{
@@ -751,8 +740,8 @@ void Viewport::mouseMoved(gcn::MouseEvent &event A_UNUSED)
if (!mMap || !player_node || !actorSpriteManager)
return;
- const int x = getMouseX() + static_cast<int>(mPixelViewX);
- const int y = getMouseY() + static_cast<int>(mPixelViewY);
+ const int x = getMouseX() + mPixelViewX;
+ const int y = getMouseY() + mPixelViewY;
mHoverBeing = actorSpriteManager->findBeingByPixel(x, y, true);
if (mHoverBeing && (mHoverBeing->getType() == Being::PLAYER
diff --git a/src/gui/viewport.h b/src/gui/viewport.h
index 43f61bcfc..3031a181e 100644
--- a/src/gui/viewport.h
+++ b/src/gui/viewport.h
@@ -190,13 +190,13 @@ class Viewport : public WindowContainer, public gcn::MouseListener,
* Returns camera x offset in pixels.
*/
int getCameraX() const
- { return static_cast<int>(mPixelViewX); }
+ { return mPixelViewX; }
/**
* Returns camera y offset in pixels.
*/
int getCameraY() const
- { return static_cast<int>(mPixelViewY); }
+ { return mPixelViewY; }
/**
* Returns mouse x in pixels.
@@ -213,7 +213,7 @@ class Viewport : public WindowContainer, public gcn::MouseListener,
/**
* Changes viewpoint by relative pixel coordinates.
*/
- void scrollBy(float x, float y)
+ void scrollBy(int x, int y)
{ mPixelViewX += x; mPixelViewY += y; }
/**
@@ -305,8 +305,8 @@ class Viewport : public WindowContainer, public gcn::MouseListener,
int mScrollCenterOffsetY;
int mMouseX; /**< Current mouse position in pixels. */
int mMouseY; /**< Current mouse position in pixels. */
- float mPixelViewX; /**< Current viewpoint in pixels. */
- float mPixelViewY; /**< Current viewpoint in pixels. */
+ int mPixelViewX; /**< Current viewpoint in pixels. */
+ int mPixelViewY; /**< Current viewpoint in pixels. */
int mShowDebugPath; /**< Show a path from player to pointer. */
int mCameraMode; /**< Camera mode. */
diff --git a/src/net/ea/playerhandler.cpp b/src/net/ea/playerhandler.cpp
index bfbef0798..8f4cfd411 100644
--- a/src/net/ea/playerhandler.cpp
+++ b/src/net/ea/playerhandler.cpp
@@ -216,8 +216,8 @@ void PlayerHandler::processPlayerWarp(Net::MessageIn &msg)
mapPath = mapPath.substr(0, mapPath.rfind("."));
game->changeMap(mapPath);
- float scrollOffsetX = 0.0f;
- float scrollOffsetY = 0.0f;
+ int scrollOffsetX = 0;
+ int scrollOffsetY = 0;
if (player_node)
{
@@ -239,12 +239,10 @@ void PlayerHandler::processPlayerWarp(Net::MessageIn &msg)
|| (abs(y - player_node->getTileY())
> MAP_TELEPORT_SCROLL_DISTANCE))
{
- scrollOffsetX = static_cast<float>((x
- - player_node->getTileX())
- * map->getTileWidth());
- scrollOffsetY = static_cast<float>((y
- - player_node->getTileY())
- * map->getTileHeight());
+ scrollOffsetX = (x - player_node->getTileX())
+ * map->getTileWidth();
+ scrollOffsetY = (y - player_node->getTileY())
+ * map->getTileHeight();
}
}
@@ -253,9 +251,7 @@ void PlayerHandler::processPlayerWarp(Net::MessageIn &msg)
player_node->navigateClean();
}
- logger->log("Adjust scrolling by %d:%d",
- static_cast<int>(scrollOffsetX),
- static_cast<int>(scrollOffsetY));
+ logger->log("Adjust scrolling by %d:%d", scrollOffsetX, scrollOffsetY);
if (viewport)
{
diff --git a/src/net/manaserv/playerhandler.cpp b/src/net/manaserv/playerhandler.cpp
index b2609cf02..eeb597908 100644
--- a/src/net/manaserv/playerhandler.cpp
+++ b/src/net/manaserv/playerhandler.cpp
@@ -306,8 +306,8 @@ void PlayerHandler::handleMapChangeMessage(Net::MessageIn &msg)
game->changeMap(mapName);
const Vector &playerPos = player_node->getPosition();
- float scrollOffsetX = 0.0f;
- float scrollOffsetY = 0.0f;
+ int scrollOffsetX = 0;
+ int scrollOffsetY = 0;
/* Scroll if neccessary */
if (!sameMap
@@ -322,8 +322,7 @@ void PlayerHandler::handleMapChangeMessage(Net::MessageIn &msg)
player_node->setPosition(x, y);
player_node->setDestination(x, y);
- logger->log("Adjust scrolling by %d,%d", (int) scrollOffsetX,
- (int) scrollOffsetY);
+ logger->log("Adjust scrolling by %d,%d", scrollOffsetX, scrollOffsetY);
viewport->scrollBy(scrollOffsetX, scrollOffsetY);
}