1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/mapeditor/maphandler.h

122 lines
4.0 KiB
C++
Raw Normal View History

2022-10-12 23:51:55 +02:00
/*
* maphandler.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
*
*/
2022-09-24 22:55:05 +02:00
#pragma once
2022-10-12 23:51:55 +02:00
//code is copied from vcmiclient/mapHandler.h with minimal changes
2022-09-18 01:23:17 +02:00
#include "StdInc.h"
2023-05-24 01:05:59 +02:00
#include "../lib/int3.h"
2022-09-18 01:23:17 +02:00
#include "Animation.h"
#include <QImage>
#include <QPixmap>
#include <QRect>
VCMI_LIB_NAMESPACE_BEGIN
2022-09-18 01:23:17 +02:00
class CGObjectInstance;
2023-05-24 01:05:59 +02:00
class CGHeroInstance;
2022-09-18 01:23:17 +02:00
class CGBoat;
2023-05-24 01:05:59 +02:00
class CMap;
2022-09-18 01:23:17 +02:00
class PlayerColor;
VCMI_LIB_NAMESPACE_END
2023-10-20 01:25:06 +02:00
struct ObjectRect
2022-09-18 01:23:17 +02:00
{
2023-10-20 01:25:06 +02:00
const CGObjectInstance * obj;
2022-09-18 01:23:17 +02:00
QRect rect;
2023-10-20 01:25:06 +02:00
ObjectRect(const CGObjectInstance * obj_, QRect rect_);
~ObjectRect();
2022-09-18 01:23:17 +02:00
};
2023-10-20 01:25:06 +02:00
using TileObjects = std::vector<ObjectRect>; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
2022-09-18 01:23:17 +02:00
class MapHandler
{
public:
2023-10-20 01:25:06 +02:00
struct BitmapHolder
2022-09-18 01:23:17 +02:00
{
std::shared_ptr<QImage> objBitmap; // main object bitmap
std::shared_ptr<QImage> flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
2023-10-20 01:25:06 +02:00
BitmapHolder(std::shared_ptr<QImage> objBitmap_ = nullptr, std::shared_ptr<QImage> flagBitmap_ = nullptr)
2022-09-18 01:23:17 +02:00
: objBitmap(objBitmap_),
flagBitmap(flagBitmap_)
{}
};
private:
int index(int x, int y, int z) const;
int index(const int3 &) const;
std::shared_ptr<QImage> findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const;
std::shared_ptr<QImage> findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor color, int group) const;
2023-10-20 01:25:06 +02:00
BitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim, int group = 0) const;
2022-09-18 01:23:17 +02:00
//FIXME: unique_ptr should be enough, but fails to compile in MSVS 2013
typedef std::map<std::string, std::shared_ptr<Animation>> TFlippedAnimations; //[type, rotation]
typedef std::map<std::string, std::vector<std::shared_ptr<QImage>>> TFlippedCache;//[type, view type, rotation]
TFlippedAnimations terrainAnimations;//[terrain type, rotation]
TFlippedCache terrainImages;//[terrain type, view type, rotation]
TFlippedAnimations roadAnimations;//[road type, rotation]
TFlippedCache roadImages;//[road type, view type, rotation]
TFlippedAnimations riverAnimations;//[river type, rotation]
TFlippedCache riverImages;//[river type, view type, rotation]
2023-10-20 01:25:06 +02:00
std::vector<TileObjects> tileObjects; //informations about map tiles
std::map<const CGObjectInstance *, std::set<int3>> tilesCache; //set of tiles beloging to object
const CMap * map = nullptr;
2022-09-18 01:23:17 +02:00
void initObjectRects();
void initTerrainGraphics();
QRgb getTileColor(int x, int y, int z);
2023-10-20 01:25:06 +02:00
std::shared_ptr<QImage> getObjectImage(const CGObjectInstance * obj);
2022-09-18 01:23:17 +02:00
public:
MapHandler();
~MapHandler() = default;
void reset(const CMap * Map);
void drawTerrainTile(QPainter & painter, int x, int y, int z);
/// draws a river segment on current tile
void drawRiver(QPainter & painter, int x, int y, int z);
/// draws a road segment on current tile
void drawRoad(QPainter & painter, int x, int y, int z);
2023-10-20 01:25:06 +02:00
std::set<int3> invalidate(const CGObjectInstance *); //invalidates object rects
2022-09-18 01:23:17 +02:00
void invalidateObjects(); //invalidates all objects on the map
2023-10-20 01:25:06 +02:00
const std::set<int3> & getTilesUnderObject(const CGObjectInstance *) const;
//get objects at position
std::vector<ObjectRect> & getObjects(const int3 & tile);
std::vector<ObjectRect> & getObjects(int x, int y, int z);
//returns set of tiles to draw
std::set<int3> removeObject(const CGObjectInstance * object);
std::set<int3> addObject(const CGObjectInstance * object);
2022-09-18 01:23:17 +02:00
/// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
2023-10-13 05:21:09 +02:00
void drawObjects(QPainter & painter, int x, int y, int z, const std::set<const CGObjectInstance *> & locked);
2022-09-18 01:23:17 +02:00
void drawObjectAt(QPainter & painter, const CGObjectInstance * object, int x, int y);
void drawMinimapTile(QPainter & painter, int x, int y, int z);
static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
};