1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00

Can draw the terrain

This commit is contained in:
nordsoft 2022-08-30 04:15:54 +04:00
parent 107709d728
commit 822e6a858b
5 changed files with 61 additions and 61 deletions

View File

@ -43,13 +43,13 @@ private:
std::map<size_t, std::vector <size_t> > offset; std::map<size_t, std::vector <size_t> > offset;
std::unique_ptr<ui8[]> data; std::unique_ptr<ui8[]> data;
std::unique_ptr<QRgb[]> palette; std::unique_ptr<QVector<QRgb>> palette;
public: public:
DefFile(std::string Name); DefFile(std::string Name);
~DefFile(); ~DefFile();
QImage loadFrame(size_t frame, size_t group) const; std::shared_ptr<QImage> loadFrame(size_t frame, size_t group) const;
const std::map<size_t, size_t> getEntries() const; const std::map<size_t, size_t> getEntries() const;
}; };
@ -137,8 +137,7 @@ static FileCache animationCache;
*************************************************************************/ *************************************************************************/
DefFile::DefFile(std::string Name): DefFile::DefFile(std::string Name):
data(nullptr), data(nullptr)
palette(nullptr)
{ {
#if 0 #if 0
@ -169,7 +168,7 @@ DefFile::DefFile(std::string Name):
}; };
data = animationCache.getCachedFile(ResourceID(std::string("SPRITES/") + Name, EResType::ANIMATION)); data = animationCache.getCachedFile(ResourceID(std::string("SPRITES/") + Name, EResType::ANIMATION));
palette = std::unique_ptr<QRgb[]>(new QRgb[256]); palette = std::make_unique<QVector<QRgb>>(256);
int it = 0; int it = 0;
ui32 type = read_le_u32(data.get() + it); ui32 type = read_le_u32(data.get() + it);
@ -186,55 +185,55 @@ DefFile::DefFile(std::string Name):
c[0] = data[it++]; c[0] = data[it++];
c[1] = data[it++]; c[1] = data[it++];
c[2] = data[it++]; c[2] = data[it++];
palette[i] = qRgba(c[0], c[1], c[2], 255); (*palette)[i] = qRgba(c[0], c[1], c[2], 255);
} }
switch(static_cast<DefType>(type)) switch(static_cast<DefType>(type))
{ {
case DefType::SPELL: case DefType::SPELL:
palette[0] = H3Palette[0]; (*palette)[0] = H3Palette[0];
break; break;
case DefType::SPRITE: case DefType::SPRITE:
case DefType::SPRITE_FRAME: case DefType::SPRITE_FRAME:
for(ui32 i= 0; i<8; i++) for(ui32 i= 0; i<8; i++)
palette[i] = H3Palette[i]; (*palette)[i] = H3Palette[i];
break; break;
case DefType::CREATURE: case DefType::CREATURE:
palette[0] = H3Palette[0]; (*palette)[0] = H3Palette[0];
palette[1] = H3Palette[1]; (*palette)[1] = H3Palette[1];
palette[4] = H3Palette[4]; (*palette)[4] = H3Palette[4];
palette[5] = H3Palette[5]; (*palette)[5] = H3Palette[5];
palette[6] = H3Palette[6]; (*palette)[6] = H3Palette[6];
palette[7] = H3Palette[7]; (*palette)[7] = H3Palette[7];
break; break;
case DefType::MAP: case DefType::MAP:
case DefType::MAP_HERO: case DefType::MAP_HERO:
palette[0] = H3Palette[0]; (*palette)[0] = H3Palette[0];
palette[1] = H3Palette[1]; (*palette)[1] = H3Palette[1];
palette[4] = H3Palette[4]; (*palette)[4] = H3Palette[4];
//5 = owner flag, handled separately //5 = owner flag, handled separately
break; break;
case DefType::TERRAIN: case DefType::TERRAIN:
palette[0] = H3Palette[0]; (*palette)[0] = H3Palette[0];
palette[1] = H3Palette[1]; (*palette)[1] = H3Palette[1];
palette[2] = H3Palette[2]; (*palette)[2] = H3Palette[2];
palette[3] = H3Palette[3]; (*palette)[3] = H3Palette[3];
palette[4] = H3Palette[4]; (*palette)[4] = H3Palette[4];
break; break;
case DefType::CURSOR: case DefType::CURSOR:
palette[0] = H3Palette[0]; (*palette)[0] = H3Palette[0];
break; break;
case DefType::INTERFACE: case DefType::INTERFACE:
palette[0] = H3Palette[0]; (*palette)[0] = H3Palette[0];
palette[1] = H3Palette[1]; (*palette)[1] = H3Palette[1];
palette[4] = H3Palette[4]; (*palette)[4] = H3Palette[4];
//player colors handled separately //player colors handled separately
//TODO: disallow colorizing other def types //TODO: disallow colorizing other def types
break; break;
case DefType::BATTLE_HERO: case DefType::BATTLE_HERO:
palette[0] = H3Palette[0]; (*palette)[0] = H3Palette[0];
palette[1] = H3Palette[1]; (*palette)[1] = H3Palette[1];
palette[4] = H3Palette[4]; (*palette)[4] = H3Palette[4];
break; break;
default: default:
logAnim->error("Unknown def type %d in %s", type, Name); logAnim->error("Unknown def type %d in %s", type, Name);
@ -262,7 +261,7 @@ DefFile::DefFile(std::string Name):
} }
} }
QImage DefFile::loadFrame(size_t frame, size_t group) const std::shared_ptr<QImage> DefFile::loadFrame(size_t frame, size_t group) const
{ {
std::map<size_t, std::vector <size_t> >::const_iterator it; std::map<size_t, std::vector <size_t> >::const_iterator it;
it = offset.find(group); it = offset.find(group);
@ -298,8 +297,11 @@ QImage DefFile::loadFrame(size_t frame, size_t group) const
const ui32 BaseOffset = currentOffset; const ui32 BaseOffset = currentOffset;
QImage img(sprite.fullWidth, sprite.height, QImage::Format_MonoLSB); std::shared_ptr<QImage> img = std::make_shared<QImage>(sprite.width, sprite.height, QImage::Format_Indexed8);
ImageLoader loader(&img); if(!img)
throw std::runtime_error("Image memory cannot be allocated");
ImageLoader loader(img.get());
//loader.init(QPoint(sprite.width, sprite.height), //loader.init(QPoint(sprite.width, sprite.height),
// QPoint(sprite.leftMargin, sprite.topMargin), // QPoint(sprite.leftMargin, sprite.topMargin),
// QPoint(sprite.fullWidth, sprite.fullHeight), palette.get()); // QPoint(sprite.fullWidth, sprite.fullHeight), palette.get());
@ -413,7 +415,7 @@ QImage DefFile::loadFrame(size_t frame, size_t group) const
} }
//TODO: SET PALETTE img->setColorTable(*palette);
return img; return img;
} }
@ -437,6 +439,7 @@ ImageLoader::ImageLoader(QImage * Img):
lineStart(Img->bits()), lineStart(Img->bits()),
position(Img->bits()) position(Img->bits())
{ {
} }
void ImageLoader::init(QPoint SpriteSize, QPoint Margins, QPoint FullSize, QRgb * pal) void ImageLoader::init(QPoint SpriteSize, QPoint Margins, QPoint FullSize, QRgb * pal)
@ -477,8 +480,8 @@ inline void ImageLoader::Load(size_t size, ui8 color)
inline void ImageLoader::EndLine() inline void ImageLoader::EndLine()
{ {
lineStart += 0;//image->surf->pitch; //lineStart += 0;//image->surf->pitch;
position = lineStart; //position = lineStart;
} }
ImageLoader::~ImageLoader() ImageLoader::~ImageLoader()
@ -537,10 +540,11 @@ bool Animation::loadFrame(size_t frame, size_t group)
if(vstd::contains(frameList, group) && frameList.at(group) > frame) // frame is present if(vstd::contains(frameList, group) && frameList.at(group) > frame) // frame is present
{ {
images[group][frame] = std::make_shared<QImage>(defFile->loadFrame(frame, group)); images[group][frame] = defFile->loadFrame(frame, group);
return true; return true;
} }
} }
return false;
// still here? image is missing // still here? image is missing
printError(frame, group, "LoadFrame"); printError(frame, group, "LoadFrame");

View File

@ -95,7 +95,7 @@ MainWindow::~MainWindow()
void MainWindow::on_actionOpen_triggered() void MainWindow::on_actionOpen_triggered()
{ {
auto filename = QFileDialog::getOpenFileName(this, tr("Open Image"), QString::fromStdString(VCMIDirs::get().userCachePath().native()), tr("Homm3 Files (*.vmap *.h3m)")); /*auto filename = QFileDialog::getOpenFileName(this, tr("Open Image"), QString::fromStdString(VCMIDirs::get().userCachePath().native()), tr("Homm3 Files (*.vmap *.h3m)"));
if(filename.isNull()) if(filename.isNull())
return; return;
@ -103,7 +103,8 @@ void MainWindow::on_actionOpen_triggered()
QFileInfo fi(filename); QFileInfo fi(filename);
std::string fname = fi.fileName().toStdString(); std::string fname = fi.fileName().toStdString();
std::string fdir = fi.dir().path().toStdString(); std::string fdir = fi.dir().path().toStdString();
ResourceID resId("MAPS/" + fname, EResType::MAP); ResourceID resId("MAPS/" + fname, EResType::MAP);*/
ResourceID resId("MAPS/SomeMap.vmap", EResType::MAP);
if(!CResourceHandler::get()->existsResource(resId)) if(!CResourceHandler::get()->existsResource(resId))
{ {
@ -121,28 +122,18 @@ void MainWindow::on_actionOpen_triggered()
QMessageBox::critical(this, "Failed to open map", e.what()); QMessageBox::critical(this, "Failed to open map", e.what());
} }
const int tileSize = 32; MapHandler mapHandler(map.get());
mapHandler.map = map.get();
mapHandler.init();
QPixmap pixmap(32 * map->width, 32 * map->height);
QPainter painter(&pixmap);
for(int j = 0; j < map->height; ++j) for(int j = 0; j < map->height; ++j)
{ {
for(int i = 0; i < map->width; ++i) for(int i = 0; i < map->width; ++i)
{ {
auto img = mapHandler.drawTileTerrain(map->getTile(int3(1, 1, 0))); mapHandler.drawTerrainTile(i, j, map->getTile((int3(i, j, 0))));
if(!img)
continue;
painter.drawImage(i * 32, j * 32, *img);
} }
} }
scene->clear(); scene->clear();
scene->addPixmap(pixmap); scene->addPixmap(mapHandler.surface);
} }

View File

@ -29,8 +29,6 @@ private:
QGraphicsScene * scene; QGraphicsScene * scene;
std::unique_ptr<CMap> map; std::unique_ptr<CMap> map;
MapHandler mapHandler;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -2,9 +2,12 @@
#include "maphandler.h" #include "maphandler.h"
#include "../lib/mapping/CMap.h" #include "../lib/mapping/CMap.h"
MapHandler::MapHandler() const int tileSize = 32;
{
MapHandler::MapHandler(const CMap * Map):
map(Map), surface(Map->width * tileSize, Map->height * tileSize), painter(&surface)
{
init();
} }
void MapHandler::init() void MapHandler::init()
@ -86,16 +89,16 @@ void MapHandler::initTerrainGraphics()
loadFlipped(riverAnimations, riverImages, RIVER_FILES); loadFlipped(riverAnimations, riverImages, RIVER_FILES);
} }
std::shared_ptr<QImage> MapHandler::drawTileTerrain(const TerrainTile & tinfo) const void MapHandler::drawTerrainTile(int x, int y, const TerrainTile & tinfo)
{ {
//Rect destRect(realTileRect); //Rect destRect(realTileRect);
ui8 rotation = tinfo.extTileFlags % 4; ui8 rotation = tinfo.extTileFlags % 4;
if(terrainImages.at(tinfo.terType).size() <= tinfo.terView) if(terrainImages.at(tinfo.terType).size() <= tinfo.terView)
return nullptr; return;
return terrainImages.at(tinfo.terType)[tinfo.terView][rotation]; painter.drawImage(x * tileSize, y * tileSize, *terrainImages.at(tinfo.terType)[tinfo.terView][rotation]);
} }
void MapHandler::initObjectRects() void MapHandler::initObjectRects()

View File

@ -6,6 +6,7 @@
#include "Animation.h" #include "Animation.h"
#include <QImage> #include <QImage>
#include <QPixmap>
class MapHandler class MapHandler
{ {
@ -20,6 +21,8 @@ public:
int3 sizes; //map size (x = width, y = height, z = number of levels) int3 sizes; //map size (x = width, y = height, z = number of levels)
const CMap * map; const CMap * map;
QPixmap surface;
QPainter painter;
//terrain graphics //terrain graphics
@ -36,18 +39,19 @@ public:
TFlippedAnimations riverAnimations;//[river type, rotation] TFlippedAnimations riverAnimations;//[river type, rotation]
TFlippedCache riverImages;//[river type, view type, rotation] TFlippedCache riverImages;//[river type, view type, rotation]
std::shared_ptr<QImage> drawTileTerrain(const TerrainTile & tinfo) const; void drawTerrainTile(int x, int y, const TerrainTile & tinfo);
mutable std::map<const CGObjectInstance*, ui8> animationPhase; mutable std::map<const CGObjectInstance*, ui8> animationPhase;
MapHandler(); MapHandler(const CMap * Map);
~MapHandler() = default; ~MapHandler() = default;
void init();
//void getTerrainDescr(const int3 & pos, std::string & out, bool isRMB) const; // isRMB = whether Right Mouse Button is clicked //void getTerrainDescr(const int3 & pos, std::string & out, bool isRMB) const; // isRMB = whether Right Mouse Button is clicked
//bool printObject(const CGObjectInstance * obj, bool fadein = false); //puts appropriate things to tiles, so obj will be visible on map //bool printObject(const CGObjectInstance * obj, bool fadein = false); //puts appropriate things to tiles, so obj will be visible on map
//bool hideObject(const CGObjectInstance * obj, bool fadeout = false); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist) //bool hideObject(const CGObjectInstance * obj, bool fadeout = false); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
//bool hasObjectHole(const int3 & pos) const; // Checks if TerrainTile2 tile has a pit remained after digging. //bool hasObjectHole(const int3 & pos) const; // Checks if TerrainTile2 tile has a pit remained after digging.
void init();
//EMapAnimRedrawStatus drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim = false); //EMapAnimRedrawStatus drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim = false);
void updateWater(); void updateWater();