2023-02-14 23:49:12 +02:00
|
|
|
/*
|
|
|
|
* MapView.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../gui/CIntObject.h"
|
|
|
|
|
|
|
|
#include "MapRendererContext.h"
|
|
|
|
|
|
|
|
class Canvas;
|
|
|
|
class MapRenderer;
|
|
|
|
|
|
|
|
class MapRendererContext : public IMapRendererContext
|
|
|
|
{
|
2023-02-16 21:35:15 +02:00
|
|
|
Point tileSize = Point(32, 32);
|
2023-02-14 23:49:12 +02:00
|
|
|
uint32_t animationTime = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void advanceAnimations(uint32_t ms);
|
2023-02-15 20:16:21 +02:00
|
|
|
void setTileSize(const Point & dimensions);
|
2023-02-14 23:49:12 +02:00
|
|
|
|
|
|
|
int3 getMapSize() const override;
|
|
|
|
bool isInMap(const int3 & coordinates) const override;
|
|
|
|
const TerrainTile & getMapTile(const int3 & coordinates) const override;
|
|
|
|
|
|
|
|
ObjectsVector getAllObjects() const override;
|
|
|
|
const CGObjectInstance * getObject(ObjectInstanceID objectID) const override;
|
|
|
|
|
2023-02-15 14:09:27 +02:00
|
|
|
const CGPath * currentPath() const override;
|
|
|
|
|
2023-02-14 23:49:12 +02:00
|
|
|
bool isVisible(const int3 & coordinates) const override;
|
|
|
|
|
|
|
|
uint32_t getAnimationPeriod() const override;
|
|
|
|
uint32_t getAnimationTime() const override;
|
2023-02-15 20:16:21 +02:00
|
|
|
Point getTileSize() const override;
|
2023-02-14 23:49:12 +02:00
|
|
|
|
|
|
|
bool showGrid() const override;
|
|
|
|
};
|
|
|
|
|
2023-02-15 20:16:21 +02:00
|
|
|
class MapViewModel
|
2023-02-14 23:49:12 +02:00
|
|
|
{
|
|
|
|
Point tileSize;
|
|
|
|
Point viewCenter;
|
2023-02-15 20:16:21 +02:00
|
|
|
Point viewDimensions;
|
|
|
|
|
|
|
|
int mapLevel = 0;
|
2023-02-16 21:35:15 +02:00
|
|
|
|
2023-02-15 20:16:21 +02:00
|
|
|
public:
|
2023-02-16 21:35:15 +02:00
|
|
|
void setTileSize(const Point & newValue);
|
|
|
|
void setViewCenter(const Point & newValue);
|
|
|
|
void setViewDimensions(const Point & newValue);
|
2023-02-15 20:16:21 +02:00
|
|
|
void setLevel(int newLevel);
|
|
|
|
|
|
|
|
/// returns current size of map tile in pixels
|
|
|
|
Point getSingleTileSize() const;
|
|
|
|
|
|
|
|
/// returns center point of map view, in Map coordinates
|
|
|
|
Point getMapViewCenter() const;
|
|
|
|
|
|
|
|
/// returns total number of visible tiles
|
|
|
|
Point getTilesVisibleDimensions() const;
|
|
|
|
|
|
|
|
/// returns rect encompassing all visible tiles
|
|
|
|
Rect getTilesTotalRect() const;
|
|
|
|
|
|
|
|
/// returns required area in pixels of cache canvas
|
|
|
|
Point getCacheDimensionsPixels() const;
|
|
|
|
|
|
|
|
/// returns actual player-visible area
|
|
|
|
Point getPixelsVisibleDimensions() const;
|
|
|
|
|
|
|
|
/// returns area covered by specified tile in map cache
|
|
|
|
Rect getCacheTileArea(const int3 & coordinates) const;
|
|
|
|
|
|
|
|
/// returns area covered by specified tile in target view
|
|
|
|
Rect getTargetTileArea(const int3 & coordinates) const;
|
|
|
|
|
|
|
|
int getLevel() const;
|
|
|
|
int3 getTileCenter() const;
|
|
|
|
|
|
|
|
/// returns tile under specified position in target view
|
|
|
|
int3 getTileAtPoint(const Point & position) const;
|
|
|
|
};
|
2023-02-14 23:49:12 +02:00
|
|
|
|
2023-02-15 20:16:21 +02:00
|
|
|
class MapCache
|
|
|
|
{
|
|
|
|
std::unique_ptr<Canvas> terrain;
|
|
|
|
std::shared_ptr<MapViewModel> model;
|
2023-02-14 23:49:12 +02:00
|
|
|
|
|
|
|
std::unique_ptr<MapRendererContext> context;
|
|
|
|
std::unique_ptr<MapRenderer> mapRenderer;
|
|
|
|
|
|
|
|
Canvas getTile(const int3 & coordinates);
|
|
|
|
void updateTile(const int3 & coordinates);
|
|
|
|
|
|
|
|
public:
|
2023-02-15 20:16:21 +02:00
|
|
|
explicit MapCache(const std::shared_ptr<MapViewModel> & model);
|
2023-02-14 23:49:12 +02:00
|
|
|
~MapCache();
|
|
|
|
|
|
|
|
void update(uint32_t timeDelta);
|
|
|
|
void render(Canvas & target);
|
|
|
|
};
|
|
|
|
|
|
|
|
class MapView : public CIntObject
|
|
|
|
{
|
2023-02-15 20:16:21 +02:00
|
|
|
std::shared_ptr<MapViewModel> model;
|
2023-02-14 23:49:12 +02:00
|
|
|
std::unique_ptr<MapCache> tilesCache;
|
|
|
|
|
2023-02-15 20:16:21 +02:00
|
|
|
std::shared_ptr<MapViewModel> createModel(const Point & dimensions) const;
|
2023-02-16 21:35:15 +02:00
|
|
|
|
2023-02-14 23:49:12 +02:00
|
|
|
public:
|
2023-02-15 20:16:21 +02:00
|
|
|
std::shared_ptr<const MapViewModel> getModel() const;
|
2023-02-14 23:49:12 +02:00
|
|
|
|
2023-02-15 20:16:21 +02:00
|
|
|
MapView(const Point & offset, const Point & dimensions);
|
2023-02-14 23:49:12 +02:00
|
|
|
|
|
|
|
void setViewCenter(const int3 & position);
|
|
|
|
void setViewCenter(const Point & position, int level);
|
2023-02-15 20:16:21 +02:00
|
|
|
void setTileSize(const Point & tileSize);
|
2023-02-14 23:49:12 +02:00
|
|
|
|
2023-02-16 21:35:15 +02:00
|
|
|
void moveHero();
|
|
|
|
|
2023-02-14 23:49:12 +02:00
|
|
|
void show(SDL_Surface * to) override;
|
|
|
|
void showAll(SDL_Surface * to) override;
|
|
|
|
};
|