-
Notifications
You must be signed in to change notification settings - Fork 10
/
demo.cpp
694 lines (664 loc) · 22.2 KB
/
demo.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#include <iostream>
#include <iomanip>
#include <memory>
#include <ctime>
#include "Candle/geometry/Polygon.hpp"
#include "Candle/graphics/VertexArray.hpp"
#include "Candle/Constants.hpp"
#include "Candle/LightingArea.hpp"
#include "Candle/LightSource.hpp"
#include "Candle/RadialLight.hpp"
#include "Candle/DirectedLight.hpp"
/*
* AUXILIAR
*/
float clamp(float x){
return std::max(0.f, std::min(1.f, x));
}
struct App{
/*
* WINDOW
*/
constexpr static const float WIDTH = 700.f;
constexpr static const float HEIGHT = 700.f;
constexpr static const float MENU_W = HEIGHT/8.f;
sf::RenderWindow w;
sf::View sandboxView, menuView;
/*
* LIGHTING
*/
candle::LightingArea lighting;
bool glow = true;
bool persistent_fog = true;
std::vector<std::shared_ptr<candle::LightSource>> lights1; // all
std::vector<std::shared_ptr<candle::LightSource>> lights2; // glowing
candle::EdgeVector edgePool;
sf::VertexArray edgeVertices;
sf::Texture fogTex;
/*
* BACKGROUND
*/
static const int ROWS = 16;
static const int COLS = 16;
float CELL_W = WIDTH / COLS;
float CELL_H = HEIGHT / ROWS;
sf::VertexArray background;
/*
* INTERACTIVITY - Brushes - Lights
*/
enum Brush{
NONE = -1,
RADIAL = 0,
DIRECTED = 1,
BLOCK = 2,
LINE = 3};
Brush brush;
bool lineStarted;
sf::VertexArray mouseBlock;
float blockSize;
candle::RadialLight radialLight;
candle::DirectedLight directedLight;
bool control, shift, alt;
/*
* INTERACTIVITY - Menu
*/
struct Button: sf::Drawable, sf::Transformable{
static sf::Color buttonA1;
static sf::Color buttonA2;
static sf::Color buttonZ1;
static sf::Color buttonZ2;
static int BC;
sf::RectangleShape rect;
sf::Drawable* icon;
void (*function)(App*);
Button(sf::Drawable* d, void (*f)(App*)){
float bw = (MENU_W-4);
rect.setFillColor(buttonZ1);
rect.setOutlineColor(buttonZ2);
rect.setOutlineThickness(2);
rect.setSize({bw, bw});
setPosition(2, 2 + (BC)*(bw + 4));
icon = d;
function = f;
BC++;
}
~Button(){
// delete icon;
}
bool contains(const sf::Vector2f& p) const{
return getTransform().transformRect(rect.getGlobalBounds()).contains(p);
}
void draw(sf::RenderTarget& t, sf::RenderStates st) const{
st.transform = this->getTransform();
t.draw(rect, st);
if(icon != NULL)
t.draw(*icon, st);
}
};
std::vector<Button> buttons;
/*
* SUBROUTINES
*/
sf::Vector2f getMousePosition(){
sf::Vector2f mp = w.mapPixelToCoords(sf::Mouse::getPosition(w));
if(control){
mp = {
CELL_W * (0.5f + std::round(mp.x/CELL_W - 0.5f)),
CELL_H * (0.5f + std::round(mp.y/CELL_H - 0.5f))
};
}
return mp;
}
App()
: lighting(candle::LightingArea::FOG, {0.f,0.f}, {WIDTH, HEIGHT})
{
w.create(sf::VideoMode(WIDTH+MENU_W, HEIGHT), "Candle - demo");
w.setFramerateLimit(60);
float totalWidth = WIDTH + MENU_W;
edgeVertices.setPrimitiveType(sf::Lines);
background.setPrimitiveType(sf::Quads);
background.resize(ROWS * COLS * 4);
if(fogTex.loadFromFile("texture.png")){
lighting.setAreaTexture(&fogTex);
lighting.scale(WIDTH/fogTex.getSize().x, HEIGHT/fogTex.getSize().y);
}else{
std::cout << "No texture detected" << std::endl;
lighting.setAreaColor(sf::Color::Black);
}
lighting.clear();
mouseBlock.setPrimitiveType(sf::Lines);
mouseBlock.resize(8);
sandboxView.setSize(WIDTH, HEIGHT);
sandboxView.setCenter(WIDTH/2.f, HEIGHT/2.f);
sandboxView.setViewport({0.f, 0.f, WIDTH/totalWidth, 1.f});
menuView.setSize(MENU_W, HEIGHT);
menuView.setCenter(MENU_W/2, HEIGHT/2);
menuView.setViewport({WIDTH/totalWidth, 0.f, MENU_W/totalWidth, 1.f});
radialLight.setRange(100.f);
directedLight.setRange(200.f);
directedLight.setBeamWidth(200.f);
static const sf::Color BG_COLORS[] = {
sf::Color::Green,
sf::Color::Blue,
sf::Color::Red
};
int colors = sizeof(BG_COLORS) / sizeof(*BG_COLORS);
for (int i = 0; i < COLS * ROWS; i++){
int p1 = i*4;
int p2 = p1+1;
int p3 = p1+2;
int p4 = p1+3;
float x = CELL_W * (i % COLS);
float y = CELL_H * (i / COLS);
background[p1].color =
background[p2].color =
background[p3].color =
background[p4].color = BG_COLORS[i % colors];
background[p1].position = {x, y};
background[p2].position = {x, y + CELL_H};
background[p3].position = {x + CELL_W, y + CELL_H};
background[p4].position = {x + CELL_W, y};
}
setMouseBlockSize(CELL_W);
brush = NONE;
lineStarted = false;
control = false;
shift = false;
alt = false;
auto i1 = new sf::CircleShape(MENU_W/3, 60);
i1->setFillColor({255,255,150,255});
i1->setOutlineColor(sf::Color::White);
i1->setOutlineThickness(2);
i1->move(MENU_W/6, MENU_W/6);
buttons.emplace_back(i1, [](App* app){ app->setBrush(RADIAL); });
auto i2 = new sf::RectangleShape({MENU_W*2/3, MENU_W/2});
i2->move(MENU_W/6, MENU_W/4);
i2->setFillColor({255,255,150,255});
i2->setOutlineColor(sf::Color::White);
i2->setOutlineThickness(1);
buttons.emplace_back(i2, [](App* app){ app->setBrush(DIRECTED); });
auto i3 = new sf::RectangleShape({MENU_W/2, MENU_W/2});
i3->move(MENU_W/4, MENU_W/4);
i3->setFillColor({25,25,25,255});
buttons.emplace_back(i3, [](App* app){ app->setBrush(BLOCK); });
auto i4 = new sf::RectangleShape({MENU_W, 3});
i4->move(MENU_W*2/15, MENU_W*2/15);
i4->rotate(45.f);
i4->setFillColor({25,25,25,255});
buttons.emplace_back(i4, [](App* app){ app->setBrush(LINE); });
auto i7 = new sf::VertexArray(sf::Quads, 12);
for(int i=0; i < 12; i++){
(*i7)[i] = background[i];
}
sfu::transform(*i7, {MENU_W*2/7/CELL_W, 0, MENU_W/14 - 1,
0, MENU_W*2/7/CELL_H, MENU_W*5/14 - 1,
0, 0, 1
});
sfu::darken(*i7, 0.2);
buttons.emplace_back(i7, [](App *app){ app->lighting.setAreaOpacity(clamp(app->lighting.getAreaOpacity() - 0.1)); });
auto i8 = new sf::VertexArray(*i7);
sfu::darken(*i8, 0.5);
buttons.emplace_back(i8, [](App *app){ app->lighting.setAreaOpacity(clamp(app->lighting.getAreaOpacity() + 0.1)); });
auto i5 = new sf::VertexArray(sf::Quads, 8);
(*i5)[0].position = {1, 0};
(*i5)[1].position = {2, 0};
(*i5)[2].position = {2, 3};
(*i5)[3].position = {1, 3};
(*i5)[4].position = {0, 1};
(*i5)[5].position = {3, 1};
(*i5)[6].position = {3, 2};
(*i5)[7].position = {0, 2};
sfu::setColor(*i5, {255,255,150,255});
float a = sfu::PI/4;
float s = MENU_W*2/9;
sfu::transform(*i5, {s*std::cos(a), s*-std::sin(a), MENU_W/2,
s*std::sin(a), s*std::cos(a), 0,
0, 0, 1
});
buttons.emplace_back(i5, [](App* app){ app->clearLights(); });
auto i6 = new sf::VertexArray(*i5);
sfu::setColor(*i6, {25,25,25,255});
buttons.emplace_back(i6, [](App *app){ app->clearEdges(); app->castAllLights(); });
}
void capture(){
sf::Texture tex;
tex.create(w.getSize().x, w.getSize().y);
tex.update(w);
sf::RenderTexture rt;
rt.create(WIDTH, HEIGHT);
rt.setView(sf::View({WIDTH/2, HEIGHT/2}, {WIDTH, HEIGHT}));
rt.draw(sf::Sprite(tex));
rt.display();
std::string name = "candle-capture-";
char timestr[13];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(timestr,13,"%y%m%d%H%M%S",timeinfo);
name += timestr;
name += ".png";
if(!rt.getTexture().copyToImage().saveToFile(name)){
exit(1);
}
}
void setMouseBlockSize(float size){
if(size > 0){
mouseBlock[7].position =
mouseBlock[0].position = {-size/2, -size/2};
mouseBlock[1].position =
mouseBlock[2].position = {size/2, -size/2};
mouseBlock[3].position =
mouseBlock[4].position = {size/2, size/2};
mouseBlock[5].position =
mouseBlock[6].position = {-size/2, size/2};
blockSize = size;
if(brush == BLOCK){
popBlock();
pushBlock(getMousePosition());
castAllLights();
}
}
}
void pushEdge(const sfu::Line& edge){
edgePool.push_back(edge);
edgeVertices.append(sf::Vertex(edge.m_origin));
edgeVertices.append(sf::Vertex(edge.point(1.f)));
}
void popEdge(){
edgePool.pop_back();
edgeVertices.resize(edgeVertices.getVertexCount() - 2);
}
void pushBlock(const sf::Vector2f& pos){
const sf::Vector2f points[] = {
pos + mouseBlock[0].position,
pos + mouseBlock[2].position,
pos + mouseBlock[4].position,
pos + mouseBlock[6].position,
};
sfu::Polygon p(points, 4);
for(auto& l: p.lines){
pushEdge(l);
}
}
void popBlock(){
for(int i = 0; i < 4; i++){
popEdge();
}
}
void drawBrush(){
sf::Transform t;
t.translate(getMousePosition());
switch(brush){
case LINE:
w.draw(sf::CircleShape(1.5f), sf::RenderStates(t));
break;
default:
break;
}
}
void setBrush(Brush b){
if(b != brush){
if(b == BLOCK){
pushBlock(getMousePosition());
}
if(brush == BLOCK){
popBlock();
castAllLights();
}
if(lineStarted){
popEdge();
castAllLights();
lineStarted = false;
}
brush = b;
updateOnMouseMove();
}
}
void castAllLights(){
for(auto& l: lights1){
l -> castLight(edgePool.begin(), edgePool.end());
}
}
void click(){
sf::Vector2f mp = getMousePosition();
if(mp.x > WIDTH){
mp.x -= WIDTH;
bool press=false;
for(auto& button: buttons){
if(button.contains(mp)){
button.rect.setFillColor(sf::Color(Button::buttonA1));
button.rect.setOutlineColor(sf::Color(Button::buttonA2));
button.function(this);
press = true;
}else{
button.rect.setFillColor(sf::Color(Button::buttonZ1));
button.rect.setOutlineColor(sf::Color(Button::buttonZ2));
}
}
if(!press) setBrush(NONE);
}else{
switch(brush){
case RADIAL:{
std::shared_ptr<candle::LightSource> nl(new candle::RadialLight(radialLight));
lights1.push_back(nl);
if(glow){
lights2.push_back(nl);
}
}
break;
case DIRECTED:{
std::shared_ptr<candle::LightSource> nl(new candle::DirectedLight(directedLight));
lights1.push_back(nl);
if(glow){
lights2.push_back(nl);
}
}
break;
case LINE:
pushEdge(sfu::Line(mp, 0.f));
lineStarted = true;
break;
case BLOCK:
pushBlock(mp);
break;
default:
break;
}
}
}
void updateOnMouseMove(){
sf::Vector2f mp = getMousePosition();
if(mp.x < WIDTH){
switch(brush){
case BLOCK:
popBlock();
pushBlock(mp);
castAllLights();
break;
case RADIAL:
radialLight.setPosition(mp);
radialLight.castLight(edgePool.begin(), edgePool.end());
break;
case DIRECTED:
directedLight.setPosition(mp);
directedLight.castLight(edgePool.begin(), edgePool.end());
break;
case LINE:
if(lineStarted){
int n = edgePool.size();
sf::Vector2f orig = edgePool[n-1].m_origin;
popEdge();
pushEdge(sfu::Line(orig, mp));
castAllLights();
}
default:
break;
}
}
}
void updateOnMouseScroll(int d){
if(getMousePosition().x < WIDTH){
switch(brush){
case RADIAL:
if(alt){
radialLight.rotate(d*6);
}else if(shift){
radialLight.setBeamAngle(radialLight.getBeamAngle() + d*5);
}else{
radialLight.setRange(std::max(0.f, radialLight.getRange() + d*10));
}
radialLight.castLight(edgePool.begin(), edgePool.end());
break;
case DIRECTED:
if(alt){
directedLight.rotate(6 * d);
}else if(shift){
directedLight.setBeamWidth(directedLight.getBeamWidth() + d*5);
}else{
directedLight.setRange(std::max(0.f, directedLight.getRange() + d*10));
}
directedLight.castLight(edgePool.begin(), edgePool.end());
break;
case BLOCK:
setMouseBlockSize(blockSize + d*CELL_W);
break;
default:
break;
}
}
}
void updateOnPressKey(sf::Keyboard::Key k){
switch(k){
case sf::Keyboard::M:{
bool textured = lighting.getAreaTexture() != nullptr;
if(lighting.getMode() == candle::LightingArea::FOG){
lighting.setMode(candle::LightingArea::AMBIENT);
lighting.setAreaColor(textured? sf::Color::White: sf::Color::Yellow);
}else{
lighting.setMode(candle::LightingArea::FOG);
lighting.setAreaColor(textured? sf::Color::White: sf::Color::Black);
}
}
break;
case sf::Keyboard::T:
persistent_fog = !persistent_fog;
break;
case sf::Keyboard::P:
capture();
break;
case sf::Keyboard::Q:
case sf::Keyboard::Escape:
w.close();
break;
case sf::Keyboard::LControl:
control = true;
break;
case sf::Keyboard::LAlt:
alt = true;
break;
case sf::Keyboard::LShift:
shift = true;
break;
case sf::Keyboard::R:
setBrush(RADIAL);
break;
case sf::Keyboard::D:
setBrush(DIRECTED);
break;
case sf::Keyboard::B:
setBrush(BLOCK);
break;
case sf::Keyboard::L:
setBrush(LINE);
break;
case sf::Keyboard::A:
lighting.setAreaOpacity(clamp(lighting.getAreaOpacity()+0.1));
break;
case sf::Keyboard::Z:
lighting.setAreaOpacity(clamp(lighting.getAreaOpacity()-0.1));
break;
case sf::Keyboard::S:
if(brush == RADIAL || brush == DIRECTED){
radialLight.setIntensity(clamp(radialLight.getIntensity()+0.1));
directedLight.setIntensity(clamp(directedLight.getIntensity()+0.1));
}
break;
case sf::Keyboard::X:
if(brush == RADIAL || brush == DIRECTED){
radialLight.setIntensity(clamp(radialLight.getIntensity()-0.1));
directedLight.setIntensity(clamp(directedLight.getIntensity()-0.1));
}
break;
case sf::Keyboard::G:
if(brush == RADIAL || brush == DIRECTED){
glow = !glow;
}
break;
case sf::Keyboard::F:
if(brush == RADIAL || brush == DIRECTED){
radialLight.setFade(!radialLight.getFade());
directedLight.setFade(!directedLight.getFade());
}
break;
case sf::Keyboard::C:
if(brush == RADIAL || brush == DIRECTED){
static const sf::Color L_COLORS[] = {
sf::Color::White,
sf::Color::Magenta,
sf::Color::Cyan,
sf::Color::Yellow
};
static int color_i = 0;
int n = sizeof(L_COLORS)/sizeof(*L_COLORS);
color_i = (color_i+1) % n;
radialLight.setColor(L_COLORS[color_i]);
directedLight.setColor(L_COLORS[color_i]);
}
break;
case sf::Keyboard::Space:
lineStarted = false;
if(alt){
clearEdges();
}else if(shift){
clearLights();
}else{
clearAll();
}
castAllLights();
break;
default:
break;
}
}
void updateOnReleaseKey(sf::Keyboard::Key k){
switch(k){
case sf::Keyboard::LControl:
control = false;
break;
case sf::Keyboard::LAlt:
alt = false;
break;
case sf::Keyboard::LShift:
shift = false;
break;
default:
break;
}
}
void clearLights(){
lights1.clear();
lights2.clear();
}
void clearEdges(){
edgePool.clear();
edgeVertices.clear();
lineStarted = false;
if(brush == BLOCK) pushBlock(getMousePosition());
}
void clearAll(){
clearLights();
clearEdges();
}
void mainLoop(){
//candle::initializeTextures();
sf::Clock clock;
while(w.isOpen()){
sf::Event e;
while(w.pollEvent(e)){
switch(e.type){
case sf::Event::Closed:
w.close();
break;
case sf::Event::MouseMoved:
updateOnMouseMove();
break;
case sf::Event::MouseWheelScrolled:
updateOnMouseScroll((0 < e.mouseWheelScroll.delta) - (e.mouseWheelScroll.delta < 0));
break;
case sf::Event::KeyPressed:
updateOnPressKey(e.key.code);
break;
case sf::Event::KeyReleased:
updateOnReleaseKey(e.key.code);
break;
case sf::Event::MouseButtonPressed:
if(e.mouseButton.button == sf::Mouse::Left){
click();
}else{
setBrush(NONE);
}
break;
case sf::Event::MouseButtonReleased:
if(e.mouseButton.button == sf::Mouse::Left){
lineStarted = false;
for(auto& b: buttons){
b.rect.setFillColor(Button::buttonZ1);
b.rect.setOutlineColor(Button::buttonZ2);
}
}
break;
default:
break;
}
}
if(persistent_fog){
lighting.clear();
}
for(auto& l: lights1){
lighting.draw(*l);
}
if(brush == RADIAL){
lighting.draw(radialLight);
}else if(brush == DIRECTED){
lighting.draw(directedLight);
}
lighting.display();
w.clear();
w.setView(menuView);
for(auto& b: buttons){
w.draw(b);
}
w.setView(sandboxView);
w.draw(background);
w.draw(lighting);
for(auto& l: lights2){
w.draw(*l);
}
if(glow){
if(brush == RADIAL){
w.draw(radialLight);
}else if(brush == DIRECTED){
w.draw(directedLight);
}
}
w.draw(edgeVertices);
drawBrush();
w.display();
sf::Time dt = clock.restart();
int fps = int(std::round(1.f/dt.asSeconds()));
w.setTitle("Candle demo ["
+ std::to_string(fps)
+ " fps: "
+ std::to_string(dt.asMilliseconds())
+ " ms] ("
+ std::to_string(lights1.size() + (brush==RADIAL || brush==DIRECTED))
+ " Light/s "
+ std::to_string(edgePool.size())
+ " Edge/s)");
}
}
};
int App::Button::BC = 0;
sf::Color App::Button::buttonA1({50,50,250,255});
sf::Color App::Button::buttonA2({40,40,40,255});
sf::Color App::Button::buttonZ1({50,50,50,255});
sf::Color App::Button::buttonZ2({20,20,20,255});
/*
* MAIN
*/
int main(){
App app;
app.mainLoop();
return 0;
}