-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sprite-piece.cpp
42 lines (34 loc) · 1.45 KB
/
sprite-piece.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "sprite-piece.h"
void iterateTiles(const SpritePiece &piece, const std::function<void(const SpritePiece::Tile&)> &callback)
{
int current_tile_index = piece.tile_index;
for (int tile_x = 0; tile_x < piece.width; ++tile_x)
{
const int tile_x_corrected = (piece.x_flip ? piece.width - tile_x - 1 : tile_x) * TileManager::TILE_WIDTH;
for (int tile_y = 0; tile_y < piece.height; ++tile_y)
{
const int tile_y_corrected = (piece.y_flip ? piece.height - tile_y - 1 : tile_y) * TileManager::TILE_HEIGHT;
callback({current_tile_index, piece.x + tile_x_corrected, piece.y + tile_y_corrected, piece.palette_line, piece.x_flip, piece.y_flip});
++current_tile_index;
}
}
}
void draw(const SpritePiece::Tile &tile, QPainter &painter, const TileManager &tile_manager, const TileManager::PixmapType effect)
{
const QRect rect(
tile.x,
tile.y,
TileManager::TILE_WIDTH,
TileManager::TILE_HEIGHT
);
painter.drawPixmap(rect, tile_manager.pixmaps(tile.index, tile.palette_line, effect).transformed(QTransform::fromScale(tile.x_flip ? -1 : 1, tile.y_flip ? -1 : 1)), QRectF(0, 0, TileManager::TILE_WIDTH, TileManager::TILE_HEIGHT));
}
void draw(const SpritePiece &piece, QPainter &painter, const TileManager &tile_manager, const TileManager::PixmapType effect)
{
iterateTiles(piece,
[&painter, &tile_manager, effect](const SpritePiece::Tile &tile)
{
draw(tile, painter, tile_manager, effect);
}
);
}