-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
479 lines (430 loc) · 17.6 KB
/
main.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
struct Tile{
enum Type : char {
O, Z, S, X
};
float x;
float y;
unsigned char type;
sf::Color colors[4];
};
class TileArray{
public:
TileArray(const sf::Vector2u & size, const sf::Vector2u & vec2){
this->size.x = size.x;
this->size.y = size.y;
Init(vec2);
pTiles = new Tile[count];
InitTiles();
}
void Init(const sf::Vector2u & vec2){
count = vec2.x * vec2.y;
rows = vec2.x;
cols = vec2.y;
width = (size.x + 1) / cols;
height = (size.y + 1) / rows;
halfWidth = width / 2;
halfHeight = height / 2;
}
void InitTiles(Tile::Type type){
float x = 0.0f, y = 0.0f;
for (int row=0; row < rows; row++){
y = size.y * row / rows;
for (int col=0; col < cols; col++){
x = size.x * col / cols;
Tile & tile = pTiles[row * cols + col];
tile.x = x;
tile.y = y;
tile.type = type;
tile.colors[0]=sf::Color::White;
tile.colors[1]=sf::Color::Red;
tile.colors[2]=sf::Color::Green;
tile.colors[3]=sf::Color::Blue;
}
}
}
void InitTiles(){
float x = 0.0f, y = 0.0f;
for (int row=0; row < rows; row++){
y = size.y * row / rows;
for (int col=0; col < cols; col++){
x = size.x * col / cols;
Tile & tile = pTiles[row * cols + col];
tile.x = x;
tile.y = y;
tile.type = Tile::O;
tile.colors[0]=sf::Color::White;
tile.colors[1]=sf::Color::Red;
tile.colors[2]=sf::Color::Green;
tile.colors[3]=sf::Color::Blue;
}
}
}
void buildTiles(const sf::Vector2u & vec2, Tile::Type type){
Init(vec2);
Tile *new_tiles = new Tile[count];
delete[] pTiles;
pTiles = new_tiles;
InitTiles(type);
}
void buildTiles(const sf::Vector2u & vec2){
Init(vec2);
Tile *new_tiles = new Tile[count];
delete[] pTiles;
pTiles = new_tiles;
InitTiles();
}
int indexTransform(int row, int col, int j, int i){
const sf::Vector2u offset = {(unsigned int)(size.x * col / cols), (unsigned int)(size.y * row / rows)};
int index = ((offset.y+j)*size.x + offset.x+i);
int maxIdx = size.x * size.y;
index = index < 0 ? 0 : index;
index = index >= maxIdx ? maxIdx-1 : index;
return index*4;
}
void renderTiles(const uint8_t *ptrColors){
int R=0,G=0,B=0,A=0;
float gradient = size.x / size.y;
int numbPixels=0;
for (int row=0; row<rows; row++){
for (int col=0; col<cols; col++){
//TOP
numbPixels=0;
R=0;G=0;B=0;A=0;
for (int j=0; j<halfHeight; j++){
for (int i=j*gradient; i<width-j*gradient; i++){
R += ptrColors[indexTransform(row, col, j, i)+0];
G += ptrColors[indexTransform(row, col, j, i)+1];
B += ptrColors[indexTransform(row, col, j, i)+2];
A += ptrColors[indexTransform(row, col, j, i)+3];
numbPixels++;
}
}
R /= numbPixels;
G /= numbPixels;
B /= numbPixels;
A /= numbPixels;
pTiles[row*cols + col].colors[0] = sf::Color(R,G,B,A);
//DOWN
numbPixels=0;
R=0;G=0;B=0;A=0;
for (int j=halfHeight; j<height; j++){
for (int i=halfWidth-(j-halfWidth)*gradient;
i<halfWidth+(j-halfWidth)*gradient; i++){
R += ptrColors[indexTransform(row, col, j, i)+0];
G += ptrColors[indexTransform(row, col, j, i)+1];
B += ptrColors[indexTransform(row, col, j, i)+2];
A += ptrColors[indexTransform(row, col, j, i)+3];
numbPixels++;
}
}
R /= numbPixels;
G /= numbPixels;
B /= numbPixels;
A /= numbPixels;
pTiles[row*cols + col].colors[1] = sf::Color(R,G,B,A);
//LEFT
numbPixels=0;
R=0;G=0;B=0;A=0;
for (int j=0; j<height; j++){
for (int i=0; i<j*gradient && j<halfHeight; i++){
R += ptrColors[indexTransform(row, col, j, i)+0];
G += ptrColors[indexTransform(row, col, j, i)+1];
B += ptrColors[indexTransform(row, col, j, i)+2];
A += ptrColors[indexTransform(row, col, j, i)+3];
numbPixels++;
}
for (int i=halfHeight; i<(j-halfHeight)*gradient && j<height; i++){
int i2, j2;
R += ptrColors[indexTransform(row, col, j, i)+0];
G += ptrColors[indexTransform(row, col, j, i)+1];
B += ptrColors[indexTransform(row, col, j, i)+2];
A += ptrColors[indexTransform(row, col, j, i)+3];
numbPixels++;
}
}
R /= numbPixels;
G /= numbPixels;
B /= numbPixels;
A /= numbPixels;
pTiles[row*cols + col].colors[2] = sf::Color(R,G,B,A);
//RIGHT
numbPixels=0;
R=0;G=0;B=0;A=0;
for (int j=0; j<height; j++){
for (int i=width-j*gradient; i<width && j<halfHeight; i++){
R += ptrColors[indexTransform(row, col, j, i)+0];
G += ptrColors[indexTransform(row, col, j, i)+1];
B += ptrColors[indexTransform(row, col, j, i)+2];
A += ptrColors[indexTransform(row, col, j, i)+3];
numbPixels++;
}
for (int i=halfWidth+(j-halfHeight)*gradient; i<width && j<height; i++){
int i2, j2;
R += ptrColors[indexTransform(row, col, j, i)+0];
G += ptrColors[indexTransform(row, col, j, i)+1];
B += ptrColors[indexTransform(row, col, j, i)+2];
A += ptrColors[indexTransform(row, col, j, i)+3];
numbPixels++;
}
}
R /= numbPixels;
G /= numbPixels;
B /= numbPixels;
A /= numbPixels;
pTiles[row*cols + col].colors[3] = sf::Color(R,G,B,A);
}
}
}
void changeTileType(const sf::Vector2f & mpos, Tile::Type type){
int col = int(mpos.x * cols / size.x);
int row = int(mpos.y * rows / size.y);
pTiles[row * cols + col].type = type;
}
void draw(sf::RenderTarget & gfx){
sf::ConvexShape spUp, spDown, spLeft, spRight;
spUp.setPointCount(3);
spDown.setPointCount(3);
spLeft.setPointCount(3);
spRight.setPointCount(3);
spUp.setPoint(0, sf::Vector2f(0, 0));
spUp.setPoint(1, sf::Vector2f(width+1, 0));
spUp.setPoint(2, sf::Vector2f(halfWidth+1,halfHeight+1));
spDown.setPoint(0, sf::Vector2f(0, height+1));
spDown.setPoint(1, sf::Vector2f(width+1, height+1));
spDown.setPoint(2, sf::Vector2f(halfWidth+1,halfHeight+1));
spLeft.setPoint(0, sf::Vector2f(0, 0));
spLeft.setPoint(1, sf::Vector2f(0, height+1));
spLeft.setPoint(2, sf::Vector2f(halfWidth+1,halfHeight+1));
spRight.setPoint(0, sf::Vector2f(width+1, 0));
spRight.setPoint(1, sf::Vector2f(width+1, height+1));
spRight.setPoint(2, sf::Vector2f(halfWidth+1,halfHeight+1));
for (int i=0; i<count; i++){
Tile & tile = pTiles[i];
spUp.setPosition(tile.x, tile.y);
spDown.setPosition(tile.x, tile.y);
spLeft.setPosition(tile.x, tile.y);
spRight.setPosition(tile.x, tile.y);
switch(tile.type){
case Tile::O:
{
uint32_t R = (tile.colors[0].r + tile.colors[1].r + tile.colors[2].r + tile.colors[3].r) / 4;
uint32_t G = (tile.colors[0].g + tile.colors[1].g + tile.colors[2].g + tile.colors[3].g) / 4;
uint32_t B = (tile.colors[0].b + tile.colors[1].b + tile.colors[2].b + tile.colors[3].b) / 4;
uint32_t A = (tile.colors[0].a + tile.colors[1].a + tile.colors[2].a + tile.colors[3].a) / 4;
sf::Color color = sf::Color(R,G,B,A);
spUp.setFillColor(color);
spDown.setFillColor(color);
spLeft.setFillColor(color);
spRight.setFillColor(color);
break;
}
case Tile::Z:
{
uint32_t R_U = (tile.colors[0].r + tile.colors[2].r) / 2;
uint32_t G_U = (tile.colors[0].g + tile.colors[2].g) / 2;
uint32_t B_U = (tile.colors[0].b + tile.colors[2].b) / 2;
uint32_t A_U = (tile.colors[0].a + tile.colors[2].a) / 2;
sf::Color color_U = sf::Color(R_U,G_U,B_U,A_U);
uint32_t R_L = (tile.colors[1].r + tile.colors[3].r) / 2;
uint32_t G_L = (tile.colors[1].g + tile.colors[3].g) / 2;
uint32_t B_L = (tile.colors[1].b + tile.colors[3].b) / 2;
uint32_t A_L = (tile.colors[1].a + tile.colors[3].a) / 2;
sf::Color color_L = sf::Color(R_L,G_L,B_L,A_L);
spUp.setFillColor(color_U);
spDown.setFillColor(color_L);
spLeft.setFillColor(color_U);
spRight.setFillColor(color_L);
break;
}
case Tile::S:
{
uint32_t R_U = (tile.colors[0].r + tile.colors[3].r) / 2;
uint32_t G_U = (tile.colors[0].g + tile.colors[3].g) / 2;
uint32_t B_U = (tile.colors[0].b + tile.colors[3].b) / 2;
uint32_t A_U = (tile.colors[0].a + tile.colors[3].a) / 2;
sf::Color color_U = sf::Color(R_U,G_U,B_U,A_U);
uint32_t R_L = (tile.colors[1].r + tile.colors[2].r) / 2;
uint32_t G_L = (tile.colors[1].g + tile.colors[2].g) / 2;
uint32_t B_L = (tile.colors[1].b + tile.colors[2].b) / 2;
uint32_t A_L = (tile.colors[1].a + tile.colors[2].a) / 2;
sf::Color color_L = sf::Color(R_L,G_L,B_L,A_L);
spUp.setFillColor(color_U);
spDown.setFillColor(color_L);
spLeft.setFillColor(color_L);
spRight.setFillColor(color_U);
break;
}
case Tile::X:
default:
spUp.setFillColor(tile.colors[0]);
spDown.setFillColor(tile.colors[1]);
spLeft.setFillColor(tile.colors[2]);
spRight.setFillColor(tile.colors[3]);
}
gfx.draw(spUp);
gfx.draw(spDown);
gfx.draw(spLeft);
gfx.draw(spRight);
}
}
private:
Tile *pTiles;
unsigned int count;
unsigned int rows;
unsigned int cols;
float width, height, halfWidth, halfHeight;
sf::Vector2u size;
};
void drawGrid(sf::RenderTarget & gfx, sf::Vector2u vec2){
const sf::Vector2u size = gfx.getSize();
sf::VertexArray horiLine(sf::LinesStrip, 2);
horiLine[0].position = sf::Vector2f(00.0f, 0.0f);
horiLine[1].position = sf::Vector2f(size.x, 0.0f);
sf::VertexArray vertLine(sf::LinesStrip, 2);
vertLine[0].position = sf::Vector2f(0.0f, 10.0f);
vertLine[1].position = sf::Vector2f(0.0f, size.y);
for (int row=0; row < vec2.x; row++){
for (int col=0; col < vec2.y; col++){
int x = int(size.x * col / vec2.y);
vertLine[0].position = sf::Vector2f(x, 0.0f);
vertLine[1].position = sf::Vector2f(x, size.y);
gfx.draw(vertLine);
}
int y = int(size.y * row / vec2.x);
horiLine[0].position = sf::Vector2f(0.0f, y);
horiLine[1].position = sf::Vector2f(size.x, y);
gfx.draw(horiLine);
}
}
int main(){
try{
static sf::Image image;
static sf::Texture texture;
image.loadFromFile("sample.png");
texture.loadFromImage(image);
const sf::Vector2u size = image.getSize();
sf::RenderWindow wnd(sf::VideoMode(size.x, size.y), "polyI", sf::Style::Close);
sf::RenderTexture rTexture;
rTexture.create(size.x, size.y);
sf::Sprite ppSprite(rTexture.getTexture());
sf::Sprite drawSprite(texture);
int row = 1;
int col = 1;
bool isGrid = false;
bool isTile = false;
bool isShift = false;
bool isVerbose = false;
static constexpr float coolDown = 0.1f;
float time = 0.0f;
sf::Clock clock;
clock.restart();
TileArray tileArray(size, sf::Vector2u(row,col));
tileArray.renderTiles(image.getPixelsPtr());
while(wnd.isOpen()){
static sf::Event e;
while(wnd.pollEvent(e)){
switch(e.type){
case sf::Event::Closed:
wnd.close();
break;
case sf::Event::KeyPressed:
{
if (e.key.shift)
isShift = true;
break;
}
case sf::Event::KeyReleased:
{
switch(e.key.code){
case sf::Keyboard::G:
isGrid = !isGrid;
break;
case sf::Keyboard::T:
isTile = !isTile;
break;
case sf::Keyboard::V:
isVerbose = !isVerbose;
break;
case sf::Keyboard::C:
system("clear");
break;
default:
break;
}
if (!e.key.shift)
isShift = false;
break;
}
case sf::Event::MouseWheelMoved:
{
if (isShift)
row += e.mouseWheel.delta;
else
col += e.mouseWheel.delta;
row = row < 1 ? 1 : row;
col = col < 1 ? 1 : col;
if (isVerbose)
std::cout << row << '\t' << col << std::endl;
time = coolDown;
break;
}
case sf::Event::MouseButtonPressed:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q))
tileArray.changeTileType(sf::Vector2f(e.mouseButton.x, e.mouseButton.y), Tile::O);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
tileArray.changeTileType(sf::Vector2f(e.mouseButton.x, e.mouseButton.y), Tile::S);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::E))
tileArray.changeTileType(sf::Vector2f(e.mouseButton.x, e.mouseButton.y), Tile::Z);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R))
tileArray.changeTileType(sf::Vector2f(e.mouseButton.x, e.mouseButton.y), Tile::X);
break;
default:
break;
}
}
if (time > 0.0f){
time -= clock.getElapsedTime().asSeconds();
if (time <= 0.0f){
if (isVerbose)
std::cout << "Snap" << std::endl;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q))
tileArray.buildTiles(sf::Vector2u(row, col), Tile::O);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
tileArray.buildTiles(sf::Vector2u(row, col), Tile::S);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::E))
tileArray.buildTiles(sf::Vector2u(row, col), Tile::Z);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R))
tileArray.buildTiles(sf::Vector2u(row, col), Tile::X);
else
tileArray.buildTiles(sf::Vector2u(row, col));
tileArray.renderTiles(image.getPixelsPtr());
}
}
clock.restart();
rTexture.clear();
{
rTexture.draw(drawSprite);
if (isTile)
tileArray.draw(rTexture);
if (isGrid)
drawGrid(rTexture, sf::Vector2u(row, col));
}
rTexture.display();
wnd.clear();
{
wnd.draw(ppSprite);
}
wnd.display();
}
rTexture.getTexture().copyToImage().saveToFile("out.png");
}
catch(...){
std::cout << "UNKNOW ERROR" << std::endl;
}
return 0;
}