2017-12-31 21:51:57 +01:00
|
|
|
#include "SpriteAtlas.h"
|
2017-09-17 13:43:13 +02:00
|
|
|
#include "Renderer.h"
|
|
|
|
|
2017-12-31 21:51:57 +01:00
|
|
|
SpriteAtlas::SpriteAtlas(Renderer* renderer, std::string imgPath, int imgWidth, int imgHeight, int tileWidth, int tileHeight) {
|
2017-09-17 13:43:13 +02:00
|
|
|
int tilesX = imgWidth / tileWidth;
|
|
|
|
int tilesY = imgHeight / tileHeight;
|
2017-09-23 22:06:36 +02:00
|
|
|
tile_width = tileWidth;
|
|
|
|
tile_height = tileHeight;
|
2017-09-17 13:43:13 +02:00
|
|
|
amount = tilesX*tilesY;
|
|
|
|
sprites = new Sprite[amount];
|
|
|
|
if (renderer->LoadTexture(imgPath) != nullptr) {
|
|
|
|
for (int i = 0; i < amount; i++) {
|
|
|
|
int x = i%tilesX * tileWidth;
|
|
|
|
int y = (i / tilesX) * tileHeight;
|
|
|
|
sprites[i] = renderer->CreateSprite(imgPath, x, y, tileWidth, tileHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
amount = -1; // Prevents it from returning non-existing sprite objects.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 21:51:57 +01:00
|
|
|
SpriteAtlas::~SpriteAtlas() {
|
2017-09-17 13:43:13 +02:00
|
|
|
delete sprites;
|
|
|
|
}
|
|
|
|
|
2017-12-31 21:51:57 +01:00
|
|
|
int SpriteAtlas::get_amount() {
|
2017-09-17 13:43:13 +02:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2017-12-31 21:51:57 +01:00
|
|
|
Sprite * SpriteAtlas::get_sprite(int tileId) {
|
2017-09-17 13:43:13 +02:00
|
|
|
if (tileId >= 0 && tileId < amount) {
|
|
|
|
return &sprites[tileId];
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-23 22:06:36 +02:00
|
|
|
|
2017-12-31 21:51:57 +01:00
|
|
|
int SpriteAtlas::get_tile_width() {
|
2017-09-23 22:06:36 +02:00
|
|
|
return tile_width;
|
|
|
|
}
|
|
|
|
|
2017-12-31 21:51:57 +01:00
|
|
|
int SpriteAtlas::get_tile_height() {
|
2017-09-23 22:06:36 +02:00
|
|
|
return tile_height;
|
|
|
|
}
|