Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ set(client_SOURCES ${client_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/towns.h
${CMAKE_CURRENT_LIST_DIR}/creatures.cpp
${CMAKE_CURRENT_LIST_DIR}/creatures.h
${CMAKE_CURRENT_LIST_DIR}/viewportcontrol.cpp
${CMAKE_CURRENT_LIST_DIR}/viewportcontrol.h

# lua
${CMAKE_CURRENT_LIST_DIR}/luavaluecasts.cpp
Expand Down
9 changes: 5 additions & 4 deletions src/client/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "statictext.h"
#include "mapview.h"
#include "minimap.h"
#include "viewportcontrol.h"

#include <framework/core/eventdispatcher.h>
#include <framework/core/application.h>
Expand Down Expand Up @@ -689,10 +690,10 @@ void Map::setAwareRange(const AwareRange& range)
void Map::resetAwareRange()
{
AwareRange range;
range.left = 8;
range.top = 6;
range.bottom = 7;
range.right = 9;
range.left = ViewportControl::maxViewportX;
range.top = ViewportControl::maxViewportY;
range.bottom = range.top + 1;
range.right = range.left + 1;
setAwareRange(range);
}

Expand Down
12 changes: 12 additions & 0 deletions src/client/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ MapView::MapView()
setVisibleDimension(Size(15, 11));

m_shader = g_shaders.getDefaultMapShader();

for(int dir = Otc::North; dir < Otc::InvalidDirection; ++dir) {
ViewportControl viewport = ViewportControl((Otc::Direction)dir);
m_viewportControl[dir] = viewport;
}
}

MapView::~MapView()
Expand Down Expand Up @@ -127,6 +132,10 @@ void MapView::draw(const Rect& rect)
}
g_painter->setColor(Color::white);

const LocalPlayerPtr player = g_game.getLocalPlayer();
const bool isWalking = player->isWalking() || player->isPreWalking() || player->isServerWalking();
const auto& viewport = isWalking ? m_viewportControl[player->getDirection()] : m_viewportControl[Otc::InvalidDirection];

auto it = m_cachedVisibleTiles.begin();
auto end = m_cachedVisibleTiles.end();
for(int z=m_cachedLastVisibleFloor;z>=m_cachedFirstVisibleFloor;--z) {
Expand All @@ -139,6 +148,9 @@ void MapView::draw(const Rect& rect)
else
++it;

if(!viewport.isValid(tile, cameraPosition))
continue;

if (g_map.isCovered(tilePos, m_cachedFirstVisibleFloor))
tile->draw(transformPositionTo2D(tilePos, cameraPosition), scaleFactor, drawFlags);
else
Expand Down
2 changes: 2 additions & 0 deletions src/client/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <framework/luaengine/luaobject.h>
#include <framework/core/declarations.h>
#include "lightview.h"
#include "viewportcontrol.h"

// @bindclass
class MapView : public LuaObject
Expand Down Expand Up @@ -162,6 +163,7 @@ class MapView : public LuaObject
stdext::boolean<true> m_follow;
std::vector<TilePtr> m_cachedVisibleTiles;
std::vector<CreaturePtr> m_cachedFloorVisibleCreatures;
std::array<ViewportControl, Otc::InvalidDirection + 1> m_viewportControl;
CreaturePtr m_followingCreature;
FrameBufferPtr m_framebuffer;
PainterShaderProgramPtr m_shader;
Expand Down
95 changes: 95 additions & 0 deletions src/client/viewportcontrol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "viewportcontrol.h"
#include "map.h"

ViewportControl::ViewportControl(const Otc::Direction directionWalking)
{
m_top = maxViewportY;
m_right = maxViewportX;
m_bottom = m_top;
m_left = m_right;

switch(directionWalking) {
case Otc::North:
m_top += 1;
m_bottom += 1;
break;
case Otc::East:
m_right += 1;
m_left += 1;
break;
case Otc::South:
m_top += 1;
m_bottom += 1;
break;
case Otc::West:
m_left += 1;
m_right += 1;
break;
case Otc::NorthEast:
m_left += 1;
m_bottom += 1;

m_top += 1;
m_right += 1;
break;
case Otc::SouthEast:
m_right += 1;
m_bottom += 1;

m_top += 1;
m_left += 1;
break;
case Otc::SouthWest:
m_top += 1;
m_right += 1;

m_left += 1;
m_bottom += 1;
break;
case Otc::NorthWest:
m_right += 1;
m_bottom += 1;

m_top += 1;
m_left += 1;
break;
case Otc::InvalidDirection:
break;
}
}

bool ViewportControl::isValid(const TilePtr& tile, const Position cameraPosition) const
{
const Position tilePos = tile->getPosition();
const int dz = tilePos.z - cameraPosition.z;
Position checkPos = tilePos.translated(dz, dz);

if(cameraPosition.x - checkPos.x >= m_left || cameraPosition.y - checkPos.y >= m_top)
return false;
else if((checkPos.x - cameraPosition.x >= m_right || checkPos.y - cameraPosition.y >= m_bottom) && tile->isSingleDimension())
return false;

return true;
}
50 changes: 50 additions & 0 deletions src/client/viewportcontrol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef VIEWPORT_OPTIMIZED_H
#define VIEWPORT_OPTIMIZED_H

#include "declarations.h"
#include "const.h"

class ViewportControl {
public:
static const int32_t maxViewportX = 8;
static const int32_t maxViewportY = 6;

ViewportControl(const Otc::Direction directionWalking = Otc::InvalidDirection);

bool isValid(const TilePtr& tile, const Position cameraPosition) const;

int top() const { return m_top; }
int right() const { return m_right; }
int bottom() const { return m_bottom; }
int left() const { return m_left; }

private:
int m_top;
int m_right;
int m_bottom;
int m_left;
};

#endif
4 changes: 3 additions & 1 deletion vc14/otclient.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
<ClCompile Include="..\src\client\uiminimap.cpp" />
<ClCompile Include="..\src\client\uiprogressrect.cpp" />
<ClCompile Include="..\src\client\uisprite.cpp" />
<ClCompile Include="..\src\client\viewportcontrol.cpp" />
<ClCompile Include="..\src\framework\core\adaptativeframecounter.cpp" />
<ClCompile Include="..\src\framework\core\application.cpp" />
<ClCompile Include="..\src\framework\core\asyncdispatcher.cpp" />
Expand Down Expand Up @@ -335,6 +336,7 @@
<ClInclude Include="..\src\client\uiminimap.h" />
<ClInclude Include="..\src\client\uiprogressrect.h" />
<ClInclude Include="..\src\client\uisprite.h" />
<ClInclude Include="..\src\client\viewportcontrol.h" />
<ClInclude Include="..\src\framework\const.h" />
<ClInclude Include="..\src\framework\core\adaptativeframecounter.h" />
<ClInclude Include="..\src\framework\core\application.h" />
Expand Down Expand Up @@ -474,4 +476,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
3 changes: 3 additions & 0 deletions vc14/otclient.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@
<ClCompile Include="..\src\client\uisprite.cpp">
<Filter>Source Files\client</Filter>
</ClCompile>
<ClCompile Include="..\src\client\viewportcontrol.cpp">
<Filter>Source Files\client</Filter>
</ClCompile>
<ClCompile Include="..\src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down