10#include <cmrc/cmrc.hpp>
12#include <opencv2/core/mat.hpp>
13#include <opencv2/core.hpp>
14#include <opencv2/imgcodecs.hpp>
15#include <opencv2/highgui.hpp>
19using UnitStateData = std::unordered_map<Constants::State, std::vector<std::tuple<int, int, bool>>>;
20using UnitData = std::unordered_map<Constants::Direction, UnitStateData>;
21using Color = std::tuple<int, int, int>;
26 static BLImage
subImage(
const BLImage& src,
int x,
int y,
int w,
int h, BLFormat format=BL_FORMAT_PRGB32){
27 BLImage img(w, h, format);
29 ctx.setCompOp(BLCompOp::BL_COMP_OP_SRC_COPY);
30 ctx.blitImage(BLPointI(0, 0), src, BLRectI(x, y, w, h));
33 static void blit(
int x,
int y, BLContext& ctx,
const BLImage& src){
34 ctx.blitImage(BLPointI(x, y), src);
46 std::vector<BLImage>
Combat;
47 std::vector<BLImage>
Dead;
48 std::vector<BLImage> Base;
49 std::vector<BLImage>
Idle;
52 std::vector<BLImage> *container;
87 void load(
const BLImage& src,
int tileSize,
int width,
int height,
const UnitStateData& stateData){
88 for(
const auto & pair: stateData){
89 std::vector<BLImage> *container = getContainer(pair.first);
90 for(
const auto& frame: pair.second){
91 auto [x, y, flip] = frame;
99 std::vector<BLImage> *container = getContainer(state);
100 auto idx = animationCounter % container->size();
101 return container->at(idx);
120 return Down.
get(animationCounter, state);
122 return Up.
get(animationCounter, state);
124 return Left.
get(animationCounter, state);;
126 return Right.
get(animationCounter, state);
132 return UpLeft.
get(animationCounter, state);
137 throw std::runtime_error(
"Should not come here.");
142 const std::string asset_path;
153 : asset_path(std::move(path))
157 , data(std::move(data))
159 auto [r, g, b] = color;
160 auto fs = cmrc::DeepRTSAssets::get_filesystem();
161 auto tileData = fs.open(asset_path);
162 cv::Mat rawData( 1, tileData.size(), CV_8UC1, (
void*)tileData.begin() );
163 cv::Mat decodedImage = cv::imdecode(rawData, cv::ImreadModes::IMREAD_UNCHANGED);
165 cv::inRange(decodedImage, cv::Scalar(150, 150, 150, 150), cv::Scalar(255, 255, 255, 255), mask);
166 decodedImage.setTo(cv::Scalar(r, g, b, 255), mask);
168 std::vector<uchar> buf;
169 cv::imencode(
".png", decodedImage, buf, std::vector<int>() );
175 if ( decodedImage.data == NULL )
180 auto err = image.readFromData(buf.data(), buf.size());
183 throw std::runtime_error(
"Could not load the tileset texture");
191 for(
auto& pair: data){
194 Down.
load(image, tileSize, width, height, pair.second);
197 Up.
load(image, tileSize, width, height, pair.second);
200 Left.
load(image, tileSize, width, height, pair.second);
203 Right.
load(image, tileSize, width, height, pair.second);
206 DownLeft.
load(image, tileSize, width, height, pair.second);
212 UpLeft.
load(image, tileSize, width, height, pair.second);
215 UpRight.
load(image, tileSize, width, height, pair.second);
233 tileSize, width, height,
234 "assets/textures/farm.png",
345 tileSize, width, height,
346 "assets/textures/peasant.png",
351 {
Constants::State::Walking, {{0, 0,
false}, {32, 0,
false}, {64, 0,
false}, {96, 0,
false},
354 {
Constants::State::Harvesting, {{0, 288,
false}, {32, 288,
false}, {64, 288,
false}, {96, 288,
false},
356 {
Constants::State::Building, {{0, 288,
false}, {32, 288,
false}, {64, 288,
false}, {96, 288,
false},
358 {
Constants::State::Combat, {{0, 288,
false}, {32, 288,
false}, {64, 288,
false}, {96, 288,
false},
367 {
Constants::State::Walking, {{0, 32,
false}, {32, 32,
false}, {64, 32,
false}, {96, 32,
false},
370 {
Constants::State::Harvesting, {{0, 352,
false}, {32, 352,
false}, {64, 352,
false}, {96, 352,
false},
372 {
Constants::State::Building, {{0, 352,
false}, {32, 352,
false}, {64, 352,
false}, {96, 352,
false},
374 {
Constants::State::Combat, {{0, 352,
false}, {32, 352,
false}, {64, 352,
false}, {96, 352,
false},
383 {
Constants::State::Walking, {{0, 64,
false}, {32, 64,
false}, {64, 64,
false}, {96, 64,
false},
386 {
Constants::State::Harvesting, {{0, 320,
false}, {32, 320,
false}, {64, 320,
false}, {96, 320,
false}}},
387 {
Constants::State::Building, {{0, 320,
false}, {32, 320,
false}, {64, 320,
false}, {96, 320,
false},
389 {
Constants::State::Combat, {{0, 320,
false}, {32, 320,
false}, {64, 320,
false}, {96, 320,
false}}},
397 {
Constants::State::Walking, {{0, 96,
false}, {32, 96,
false}, {64, 96,
false}, {96, 96,
false},
400 {
Constants::State::Harvesting, {{0, 320,
true}, {32, 320,
true}, {64, 320,
true}, {96, 320,
true}}},
401 {
Constants::State::Building, {{0, 320,
true}, {32, 320,
true}, {64, 320,
true}, {96, 320,
true},
403 {
Constants::State::Combat, {{0, 320,
true}, {32, 320,
true}, {64, 320,
true}, {96, 320,
true}}},
411 {
Constants::State::Walking, {{0, 128,
false}, {32, 128,
false}, {64, 128,
false}, {96, 128,
false},
414 {
Constants::State::Harvesting, {{0, 256,
false}, {32, 256,
false}, {64, 256,
false}}},
415 {
Constants::State::Building, {{0, 256,
false}, {32, 256,
false}, {64, 256,
false}}},
416 {
Constants::State::Combat, {{0, 256,
false}, {32, 256,
false}, {64, 256,
false}}},
424 {
Constants::State::Walking, {{0, 160,
false}, {32, 160,
false}, {64, 160,
false}, {96, 160,
false},
427 {
Constants::State::Harvesting, {{0, 256,
true}, {32, 256,
true}, {64, 256,
true}}},
428 {
Constants::State::Building, {{0, 256,
true}, {32, 256,
true}, {64, 256,
true}}},
429 {
Constants::State::Combat, {{0, 256,
true}, {32, 256,
true}, {64, 256,
true}}},
437 {
Constants::State::Walking, {{0, 224,
false}, {32, 224,
false}, {64, 224,
false}, {96, 224,
false},
440 {
Constants::State::Harvesting, {{32, 320,
true}, {64, 352,
true}, {96, 352,
true}, {128, 352,
true}}},
442 {
Constants::State::Combat, {{32, 320,
true}, {64, 352,
true}, {96, 352,
true}, {128, 352,
true}}},
450 {
Constants::State::Walking, {{0, 192,
false}, {32, 192,
false}, {64, 192,
false}, {96, 192,
false},
453 {
Constants::State::Harvesting, {{32, 320,
false}, {64, 352,
false}, {96, 352,
false},
456 {
Constants::State::Combat, {{32, 320,
false}, {64, 352,
false}, {96, 352,
false}, {128, 352,
false}}},
471 tileSize, width, height,
472 "assets/textures/footman.png",
478 {
Constants::State::Walking, {{0, 0,
false}, {32, 0,
false}, {64, 0,
false}, {96, 0,
false},
483 {
Constants::State::Combat, {{0, 160,
false}, {32, 160,
false}, {64, 160,
false}, {96, 160,
false}}},
491 {
Constants::State::Walking, {{0, 128,
false}, {32, 128,
false}, {64, 128,
false}, {96, 128,
false},
496 {
Constants::State::Combat, {{0, 288,
false}, {32, 288,
false}, {64, 288,
false}, {96, 288,
false}}},
504 {
Constants::State::Walking, {{0, 64,
false}, {32, 64,
false}, {64, 64,
false}, {96, 64,
false},
509 {
Constants::State::Combat, {{0, 224,
false}, {32, 224,
false}, {64, 224,
false}, {96, 224,
false}}},
517 {
Constants::State::Walking, {{0, 64,
true}, {32, 64,
true}, {64, 64,
true}, {96, 64,
true},
522 {
Constants::State::Combat, {{0, 224,
true}, {32, 224,
true}, {64, 224,
true}, {96, 224,
true}}},
530 {
Constants::State::Walking, {{0, 32,
false}, {32, 32,
false}, {64, 32,
false}, {96, 32,
false},
535 {
Constants::State::Combat, {{0, 192,
false}, {32, 192,
false}, {64, 192,
false}}},
543 {
Constants::State::Walking, {{0, 32,
true}, {32, 32,
true}, {64, 32,
true}, {96, 32,
true},
548 {
Constants::State::Combat, {{0, 192,
true}, {32, 192,
true}, {64, 192,
true}}},
556 {
Constants::State::Walking, {{0, 96,
false}, {32, 96,
false}, {64, 96,
false}, {96, 96,
false},
560 {
Constants::State::Combat, {{0, 256,
false}, {32, 256,
false}, {64, 256,
false}, {96, 256,
false}}},
568 {
Constants::State::Walking, {{0, 96,
true}, {32, 96,
true}, {64, 96,
true}, {96, 96,
true},
573 {
Constants::State::Combat, {{0, 256,
true}, {32, 256,
true}, {64, 256,
true}, {96, 256,
true}}},
587 tileSize, width, height,
588 "assets/textures/barracks.png",
695 tileSize, width, height,
696 "assets/textures/archer.png",
701 {
Constants::State::Spawning, {{0, 32,
false}, {32, 32,
false}, {64, 32,
false}, {96, 32,
false}}},
714 {
Constants::State::Walking, {{0, 160,
false}, {32, 160,
false}, {64, 160,
false}, {96, 160,
false},
727 {
Constants::State::Walking, {{0, 96,
false}, {32, 96,
false}, {64, 96,
false}, {96, 96,
false}}},
739 {
Constants::State::Walking, {{0, 96,
true}, {32, 96,
true}, {64, 96,
true}, {96, 96,
true}}},
751 {
Constants::State::Walking, {{0, 32,
false}, {32, 32,
false}, {64, 32,
false}, {96, 32,
false},
764 {
Constants::State::Walking, {{0, 32,
true}, {32, 32,
true}, {64, 32,
true}, {96, 32,
true},
777 {
Constants::State::Walking, {{0, 128,
true}, {32, 128,
true}, {64, 128,
true}, {96, 128,
true},
789 {
Constants::State::Walking, {{0, 128,
false}, {32, 128,
false}, {64, 128,
false}, {96, 128,
false},
808 tileSize, width, height,
809 "assets/textures/town_hall.png",
934 std::vector<Blend2DTile> tiles;
936 const int tileHeight;
939 explicit Blend2DTileset(
int tileWidth,
int tileHeight,
const std::string& srcPath)
940 : tileWidth(tileWidth)
941 , tileHeight(tileHeight)
944 auto fs = cmrc::DeepRTSAssets::get_filesystem();
945 auto tileData = fs.open(srcPath);
946 auto err = tileset.readFromData(tileData.begin(), tileData.size());
948 throw std::runtime_error(
"Could not load the tileset texture");
952 codec.findByName(
"BMP");
954 tileset.writeToFile(
"lol.bmp", codec);
960 auto n_cols = tileset.width() / tileWidth;
961 auto n_rows = tileset.height() / tileHeight;
962 auto total_items = n_cols * n_rows;
963 for(
int i = 0; i < total_items; i++){
966 auto x_start = (x * tileWidth) + x;
967 auto y_start = (y * tileHeight) + y;
981 [[nodiscard]]
const BLImage &
at(
int index)
const{
982 return tiles[index].tile;
992 BLImageData imageData;
994 std::vector<std::unordered_map<Constants::Unit, Blend2DUnit>> units;
996 const cv::Mat& render()
const override;
997 void onTileChange(
const Tile& tile)
override;
std::tuple< int, int, int > Color
Definition: Blend2DGUI.h:21
std::unordered_map< Constants::Direction, UnitStateData > UnitData
Definition: Blend2DGUI.h:20
CMRC_DECLARE(DeepRTSAssets)
std::unordered_map< Constants::State, std::vector< std::tuple< int, int, bool > > > UnitStateData
Definition: Blend2DGUI.h:19
Definition: Blend2DGUI.h:105
const BLImage & get(int animationCounter, Constants::Direction direction, Constants::State state)
Definition: Blend2DGUI.h:117
Blend2DUnitState Up
Definition: Blend2DGUI.h:107
Blend2DUnitState Left
Definition: Blend2DGUI.h:108
Blend2DUnitState DownRight
Definition: Blend2DGUI.h:114
Blend2DUnitState Right
Definition: Blend2DGUI.h:110
Blend2DUnitState UpLeft
Definition: Blend2DGUI.h:111
Blend2DUnitState UpRight
Definition: Blend2DGUI.h:112
Blend2DUnitState DownLeft
Definition: Blend2DGUI.h:113
Blend2DUnitState Down
Definition: Blend2DGUI.h:109
const Game & game
Definition: BaseGUI.h:17
Definition: Blend2DGUI.h:691
Blend2DArcher(const Color &color, int tileSize, int width, int height)
Definition: Blend2DGUI.h:693
Definition: Blend2DGUI.h:583
Blend2DBarracks(const Color &color, int tileSize, int width, int height)
Definition: Blend2DGUI.h:585
Definition: Blend2DGUI.h:229
Blend2DFarm(const Color &color, int tileSize, int width, int height)
Definition: Blend2DGUI.h:231
Definition: Blend2DGUI.h:988
Blend2DGUI(Game &game)
Definition: Blend2DGUI.cpp:62
void renderMap()
Definition: Blend2DGUI.cpp:49
Definition: Blend2DGUI.h:340
Blend2DPeasant(const Color &color, int tileSize, int width, int height)
Definition: Blend2DGUI.h:343
Definition: Blend2DGUI.h:337
Definition: Blend2DGUI.h:918
Blend2DTile(BLImage tile)
Definition: Blend2DGUI.h:923
BLImage tile
Definition: Blend2DGUI.h:928
Definition: Blend2DGUI.h:932
void load(BLImage &tileset)
Definition: Blend2DGUI.h:959
const BLImage & at(int index) const
Definition: Blend2DGUI.h:981
Blend2DTileset(int tileWidth, int tileHeight, const std::string &srcPath)
Definition: Blend2DGUI.h:939
Definition: Blend2DGUI.h:804
Blend2DTownHall(const Color &color, int tileSize, int width, int height)
Definition: Blend2DGUI.h:806
Definition: Blend2DGUI.h:40
void load(const BLImage &src, int tileSize, int width, int height, const UnitStateData &stateData)
Definition: Blend2DGUI.h:87
const BLImage & get(int animationCounter, Constants::State state)
Definition: Blend2DGUI.h:98
Definition: Blend2DGUI.h:141
Blend2DUnit(const Color &color, int tileSize, int width, int height, std::string path, UnitData data)
Definition: Blend2DGUI.h:152
void load()
Definition: Blend2DGUI.h:190
Definition: Blend2DGUI.h:24
static void blit(int x, int y, BLContext &ctx, const BLImage &src)
Definition: Blend2DGUI.h:33
static BLImage subImage(const BLImage &src, int x, int y, int w, int h, BLFormat format=BL_FORMAT_PRGB32)
Definition: Blend2DGUI.h:26
Definition: Building.h:11
Definition: Despawned.h:13
Definition: Harvesting.h:13
Definition: Spawning.h:12
State
Definition: Constants.h:64
@ Harvesting
Definition: Constants.h:68
@ Despawned
Definition: Constants.h:67
@ Spawning
Definition: Constants.h:65
@ Idle
Definition: Constants.h:72
@ Combat
Definition: Constants.h:70
@ Walking
Definition: Constants.h:66
@ Building
Definition: Constants.h:69
@ Base
Definition: Constants.h:73
@ Dead
Definition: Constants.h:71
Direction
Definition: Constants.h:83
@ UpRight
Definition: Constants.h:91
@ Down
Definition: Constants.h:84
@ Up
Definition: Constants.h:85
@ DownLeft
Definition: Constants.h:88
@ DownRight
Definition: Constants.h:89
@ Right
Definition: Constants.h:87
@ UpLeft
Definition: Constants.h:90
@ Left
Definition: Constants.h:86