-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundTilePreviewer.cpp
More file actions
172 lines (141 loc) · 4.9 KB
/
BackgroundTilePreviewer.cpp
File metadata and controls
172 lines (141 loc) · 4.9 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "BackgroundTilePreviewer.h"
void BackgroundTilePreviewer::initWindow()
{
windowSize.x = 1280;
windowSize.y = 720;
title = "Background tile previewer";
vm = sf::VideoMode(sf::Vector2u(windowSize));
window.create(vm, title);
window.setFramerateLimit(60);
window.clear();
}
void BackgroundTilePreviewer::pollEvents()
{
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>() ||
(event->is<sf::Event::KeyPressed>() &&
event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape))
window.close();
else if ((event->is<sf::Event::MouseButtonPressed>() && event->getIf<sf::Event::MouseButtonPressed>()->button == sf::Mouse::Button::Left) ||
(event->is<sf::Event::KeyPressed>() &&
event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Space)) //go to next image if left mouse button clicked or Spacebar
{
moveNext();
}
}
}
void BackgroundTilePreviewer::initFullTexture(std::string file_addr)
{
if (!fullImageTexture.loadFromFile(file_addr)) {
std::cerr << "ERROR::BACKGROUNDTILEPREVIEWER::initTexture::COULD NOT LOAD FILE::" << file_addr << std::endl;
}
}
void BackgroundTilePreviewer::setTileTexture()
{
sf::IntRect tileRect(texCoord, tileSize);
if (!tileTexture.loadFromImage(fullImageTexture.copyToImage(), false, tileRect)) {
std::cerr << "ERROR::BACKGROUNDTILEPREVIEWER::setTileTexture::COULD NOT COPY RECT TO IMAGE" << std::endl;
}
tileTexture.setRepeated(true);
}
void BackgroundTilePreviewer::initSprite()
{
int windowSizeX = static_cast<int>(windowSize.x), windowSizeY = static_cast<int>(windowSize.y);
tileSprite.setTexture(tileTexture);
tileSprite.setTextureRect(sf::IntRect({ 0,0 }, { windowSizeX, windowSizeY }));
}
void BackgroundTilePreviewer::initAboutText()
{
if (!aboutFont.openFromFile(fontFileAddr)) {
std::cerr << "ERROR::BACKGROUNDTILEPREVIEWER::initAboutText::COULD NOT LOAD FONT FILE::" << fontFileAddr << std::endl;
}
aboutText.setFont(aboutFont);
aboutText.setFillColor(sf::Color::Yellow);
aboutText.setCharacterSize(20);
setAboutText();
}
void BackgroundTilePreviewer::initAll(std::string file_addr, int x_tile_size, int y_tile_size, int x_offset, int y_offset, int x_offset_between, int y_offset_between)
{
tileSize = { x_tile_size, y_tile_size };
xOffset = x_offset;
xOffsetBetween = x_offset_between;
yOffset = y_offset;
yOffsetBetween = y_offset_between;
texCoord = { xOffset, yOffset };
initWindow();
initFullTexture(file_addr);
setTileTexture();
initSprite();
initAboutText();
}
void BackgroundTilePreviewer::setAboutText()
{
std::string about, xIdxS, yIdxS, nIdxS, xCoordS, yCoordS;
float textWidth;
xCoordS = std::to_string(texCoord.x);
yCoordS = std::to_string(texCoord.y);
xIdxS = std::to_string(xIndex + 1);
yIdxS = std::to_string(yIndex + 1);
about = "Image, Row " + yIdxS + ", Col " + xIdxS + ". Coords: {" + xCoordS + ", " + yCoordS + "}";
aboutText.setString(about);
textWidth = aboutText.getLocalBounds().size.x;
aboutText.setPosition({ static_cast<float>(windowSize.x) - (textWidth + 20.0f), 20.0f });
}
void BackgroundTilePreviewer::moveNext()
{
sf::Vector2u imgSize;
imgSize = fullImageTexture.getSize();
texCoord.x += tileSize.x + xOffsetBetween;
xIndex++;
if ((texCoord.x + tileSize.x) > (imgSize.x - xOffset))
{
texCoord.x = xOffset; //go back to the beginning (after any left margin)
xIndex = 0;
// added: allow for multirow sheets (if at the end of a row, go to beginning of next row. If below the bottom of the image, go back to the beginning)
texCoord.y += tileSize.y + yOffsetBetween;
yIndex++;
if ((texCoord.y + tileSize.y) > (imgSize.y - yOffset))
{
texCoord.y = yOffset;
yIndex = 0;
}
}
setTileTexture();
setAboutText();
}
void BackgroundTilePreviewer::updating()
{
pollEvents();
}
void BackgroundTilePreviewer::rendering()
{
//render
window.clear();
//draw here!!
window.draw(tileSprite);
window.draw(aboutText);
//show window
window.display();
}
BackgroundTilePreviewer::BackgroundTilePreviewer(std::string file_addr, int x_tile_size, int y_tile_size)
: fullImageTexture(file_addr), tileSprite(tileTexture), aboutFont(fontFileAddr), aboutText(aboutFont, "")
{
initAll(file_addr, x_tile_size, y_tile_size, 0, 0, 0, 0);
}
BackgroundTilePreviewer::BackgroundTilePreviewer(std::string file_addr, int x_tile_size, int y_tile_size, int x_offset, int y_offset, int x_offset_between, int y_offset_between)
: fullImageTexture(file_addr), tileSprite(tileTexture), aboutFont(fontFileAddr), aboutText(aboutFont, "")
{
initAll(file_addr, x_tile_size, y_tile_size, x_offset, y_offset, x_offset_between, y_offset_between);
}
BackgroundTilePreviewer::~BackgroundTilePreviewer()
{
}
void BackgroundTilePreviewer::running()
{
while (window.isOpen())
{
updating();
rendering();
}
}