-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsoleApplication9.cpp
1992 lines (1949 loc) · 84.6 KB
/
ConsoleApplication9.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ConsoleApplication9.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#if __cplusplus < 201703L
#error "C++17 or later is required to build the project."
#endif
#if !defined(SFML_STATIC)
#define STB_IMAGE_IMPLEMENTATION
#endif
#include "BTRCommon.h"
#include "SoundPlayback.h"
#include <random>
#include <mutex>
#if __has_include(<sys/param.h>) && (defined(__unix__) || defined(__APPLE__))
#include <sys/param.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <float.h>
#endif
#define SI_SUPPORT_IOSTREAMS
#include "SimpleIni.h"
#include <signal.h>
//#include <immintrin.h>
#include <algorithm>
int lives = 2;
BTRsprite *ball = NULL;
//std::vector<BTRball*> balls;
std::vector<BTRSpark> sparks;
int wallWidth = 0;
int score = 0;
int frameCnt = 0;
int fireBrickFrame = 64;
unsigned char brickID = 1;
std::chrono::steady_clock clocktime;
std::chrono::steady_clock::time_point now;
int64_t sec = 0;
bool endofgame = false;
extern void ParseMidsFile(std::string filename);
extern void StartMidiPlayback();
#ifdef WIN32
extern void CALLBACK MidiCallBack(HMIDIOUT hmo, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2);
extern HMIDISTRM midiDev;
HMIDIOUT midiOutDev;
#endif // WIN32
extern bool eot;
extern bool oneshotplay;
extern bool gameSound;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-5, 5);
bool fadeOut = false;
bool ballLost = false;
std::vector<BTRExplodingBricks> explodingBricks;
std::vector<BTRExplosion> explosions;
std::vector<BTRButton> btns;
std::vector<BTRButton> levEditBtns;
int framesPassedlastPowerup;
std::thread *thread = NULL;
std::vector<std::pair<std::string, int>> scoresAndNames;
BTRChompTeeth *chompteeth = NULL;
BTRPlayArea *playArea = NULL;
std::vector<unsigned char> randPlayedLevels;
int64_t randPlayedSet;
btr::Vector2i lastTouchPosition = btr::Vector2i(0,0);
#ifdef BTR_USE_SDL
namespace btr
{
SDL_Renderer* context = nullptr;
}
#endif
#ifndef BTR_USE_SDL
static inline void AppendVerticesFromRect(sf::IntRect& rect, btr::Vector2f pos, sf::VertexArray& vertArray)
{
vertArray.append(sf::Vertex(pos,btr::Vector2f(rect.left,rect.top)));
vertArray.append(sf::Vertex(pos + btr::Vector2f(rect.width,0),btr::Vector2f(sf::Vector2i(rect.left,rect.top) + sf::Vector2i(rect.width,0))));
vertArray.append(sf::Vertex(pos + btr::Vector2f(0,rect.height),btr::Vector2f(sf::Vector2i(rect.left,rect.top) + sf::Vector2i(0,rect.height))));
vertArray.append(sf::Vertex(pos + btr::Vector2f(0,rect.height),btr::Vector2f(sf::Vector2i(rect.left,rect.top) + sf::Vector2i(0,rect.height))));
vertArray.append(sf::Vertex(pos + btr::Vector2f(rect.width,rect.height),btr::Vector2f(sf::Vector2i(rect.left,rect.top) + sf::Vector2i(rect.width,rect.height))));
vertArray.append(sf::Vertex(pos + btr::Vector2f(rect.width,0),btr::Vector2f(sf::Vector2i(rect.left,rect.top) + sf::Vector2i(rect.width,0))));
}
#endif
bool sortScoreList(const std::pair<std::string, int> t1, const std::pair<std::string, int> t2)
{
if (std::get<1>(t1) > std::get<1>(t2))
return true;
return false;
}
extern void StopMidiPlayback();
extern std::string &GetCurPlayingFilename();
unsigned int devID = 0;
bool loadedEndMusic = false;
bool gameMusic = true;
void loadMusic(std::string mdsfilename, bool oneshot = false)
{
if (!gameMusic)
return;
if (GetCurPlayingFilename() == mdsfilename)
{
return; // Let it continue;
}
if (thread)
{
eot = true;
if (thread->joinable())
thread->join();
eot = false;
delete thread;
}
#if defined(WIN32)
// Completely close the stream and then reopen to avoid weird bugs.
midiStreamStop(midiDev);
midiOutReset((HMIDIOUT)midiDev);
midiStreamClose(midiDev);
//unsigned int devID = 0;
midiStreamOpen(&midiDev, &devID, 1, 0, 0, 0);
#endif
oneshotplay = oneshot;
ParseMidsFile(mdsfilename);
thread = new std::thread(StartMidiPlayback);
}
extern void DownBricks(BTRPlayArea &area);
//using namespace luabridge;
auto ActivatePowerup(int powerupID)
{
return BTRpowerup::PowerupHandle(*playArea, powerupID);
}
std::vector<BTRMovingText> BuildTextFromString(std::string buildStr, BTRFont*& font, int baseYPos = 0)
{
bool flipVal = 0;
std::stringstream stream(buildStr);
BTRMovingText moveText;
std::vector<BTRMovingText> btrmoveTexts;
while (!stream.eof())
{
std::getline(stream,moveText.movingText);
flipVal ^= 1;
moveText.toPos = btr::Vector2f(BTRWINDOWWIDTH / 2 - font->GetSizeOfText(moveText.movingText).x / 2, baseYPos * font->genCharHeight);
moveText.pos = moveText.orgPos = btr::Vector2f(-font->GetSizeOfText(moveText.movingText).x + (BTRWINDOWWIDTH * flipVal), baseYPos++ * font->genCharHeight);
btrmoveTexts.push_back(moveText);
}
return btrmoveTexts;
}
void DoFuncForDuration(unsigned long long sec, std::function<void()> func)
{
auto time = std::chrono::duration_cast<std::chrono::seconds>(clocktime.now().time_since_epoch());
while ((std::chrono::duration_cast<std::chrono::seconds>(clocktime.now().time_since_epoch()) - time).count() <= sec)
{
func();
}
}
const std::string gammaShaderCode =
"#version 120"
"\n"
""
"uniform sampler2D texture;"
"uniform float gamma; "
"void main()"
"{"
" vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);"
" gl_FragColor = pow(pixel, vec4(vec3(1.0f / gamma),1.0) );"
"}";
const std::string colorShaderCode=
"#version 120"
"\n"
"\n"
"uniform sampler2D texture;"
"uniform vec4 col;"
"void main()"
"{"
" vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);"
" float grayCol = (pixel.r + pixel.g + pixel.b) / 3.0;"
" gl_FragColor = gl_Color * vec4(grayCol,grayCol,grayCol,1.0) * col;"
"}";
bool firstRun = true;
bool vertvelpowerup = false;
bool backgrndcol = false;
bool isFullscreen = false;
bool demo = false;
bool simpleSparks = false;
float backgrndr = 1.0f;
float backgrndg = 1.0f;
float backgrndb = 1.0f;
char* homeprefix;
#if defined(__ANDROID__) || defined(ANDROID)
#include <android/log.h>
#endif
int main(int argc, char *argv[])
{
int randNum = time(NULL);
std::vector<int> randNums;
for (int i = 0; i < lrint(2492.125); i++)
{
randNum = rd();
randNums.push_back(randNum);
}
std::seed_seq* seq = new std::seed_seq(randNums.begin(), randNums.end());
gen.seed(*seq);
delete seq;
CSimpleIniA ini;
ini.SetUnicode(true);
ini.Reset();
#if defined(__ANDROID__) || defined(ANDROID)
int chdirErr = chdir("/storage/emulated/0/BlastThruRebornData");
if (chdirErr == -1)
{
__android_log_print(ANDROID_LOG_INFO, "BlastThruReborn", "Failed to chdir into /storage/emulated/0/BlastThruRebornData");
exit(-1);
}
#endif
SI_Error inierr = (SI_Error)ini.LoadFile("./bt.ini");
if (!(inierr < 0))
{
auto section = ini.GetSection("bt.ini");
std::string scorestr;
std::string sectionName = "score0";
while (sectionName != "score1:")
{
scorestr = ini.GetValue("bt.ini", sectionName.c_str());
std::string name;
auto res = scorestr.find_first_of(';');
if (res == std::string::npos)
{
std::cout << "Invalid score string encountered!" << std::endl;
}
else
{
name = scorestr.substr(0, res);
auto scoreint = std::stoi(scorestr.substr(res + 1, scorestr.size() - res));
std::cout << sectionName << ": " << name << ", " << scoreint << std::endl;
scoresAndNames.push_back(std::make_pair(name, scoreint));
};
if (sectionName == "score9")
{
sectionName.pop_back();
sectionName += "10";
}
else
sectionName[sectionName.size() - 1]++;
}
std::sort(scoresAndNames.begin(), scoresAndNames.end(), &sortScoreList);
}
else
{
ini.SetValue("bt.ini", 0, 0);
int cnt = 0;
while (cnt < 20)
{
auto scorestr = std::string("score") + std::to_string(cnt);
ini.SetValue("bt.ini", scorestr.c_str(), ";0");
cnt++;
}
ini.SetValue("bt.ini", "scheck", "0");
ini.SetValue("bt.ini", "gmididevselect", "off");
ini.SetValue("bt.ini", "gmididevnum", "0");
ini.SetValue("bt.ini", "ggamemusic", "on");
ini.SetValue("bt.ini", "ggamesound", "on");
ini.SetValue("bt.ini", "gvertvelpowerup", "off");
ini.SetValue("bt.ini", "runcount", "0");
ini.SetValue("bt.ini", "runsuccess", "0");
ini.SetValue("bt.ini", "backgrndred", "0.0");
ini.SetValue("bt.ini", "backgrndgreen", "0.0");
ini.SetValue("bt.ini", "backgrndblue", "0.0");
ini.SetBoolValue("bt.ini", "gfullscreen",0);
ini.SetBoolValue("bt.ini", "backgrndcol", false);
ini.SetValue("bt.ini","gamename", "Nameless Wonder");
#if defined(__ANDROID__) || defined(ANDROID)
ini.SetBoolValue("bt.ini", "gmididevselect",1);
ini.SetBoolValue("bt.ini", "gfullscreen",0);
ini.SetLongValue("bt.ini", "gmididevnum", 0);
devID = 0;
#endif
ini.SaveFile("./bt.ini", 0);
}
std::string name;
if (ini.GetValue("bt.ini", "gamename") == NULL)
{
std::cout << "Enter name to be saved in the Hall Of Fame: ";
std::cin >> name;
ini.SetValue("bt.ini", "gamename", name.c_str());
}
name = ini.GetValue("bt.ini", "gamename");
gameMusic = ini.GetBoolValue("bt.ini", "ggamemusic");
gameSound = ini.GetBoolValue("bt.ini", "ggamesound");
simpleSparks = ini.GetBoolValue("bt.ini","gsimplesparks");
backgrndcol = ini.GetBoolValue("bt.ini", "backgrndcol",false);
backgrndr = ini.GetDoubleValue("bt.ini", "backgrndred",1);
backgrndg = ini.GetDoubleValue("bt.ini", "backgrndgreen",1);
backgrndb = ini.GetDoubleValue("bt.ini", "backgrndblue",1);
ini.SetDoubleValue("bt.ini", "backgrndred", backgrndr);
ini.SetDoubleValue("bt.ini", "backgrndgreen", backgrndg);
ini.SetDoubleValue("bt.ini", "backgrndblue", backgrndb);
ini.SetBoolValue("bt.ini", "backgrndcol", backgrndcol);
ini.SetBoolValue("bt.ini","gsimplesparks",simpleSparks);
bool mididevselect = ini.GetBoolValue("bt.ini", "gmididevselect");
isFullscreen = ini.GetBoolValue("bt.ini","gfullscreen");
InitOpenAL();
if (!mididevselect)
{
SelectMidiDevice();
ini.SetValue("bt.ini", "gmididevselect", "on");
ini.SetLongValue("bt.ini", "gmididevnum", devID);
}
else
SelectMidiDevice(ini.GetLongValue("bt.ini", "gmididevnum"));
vertvelpowerup = ini.GetBoolValue("bt.ini", "gvertvelpowerup");
for (int i = 0; i < argc; i++)
{
if (strncmp("vertvelpowerup", argv[i], strlen("vertvelpowerup")) == 0)
{
vertvelpowerup = true;
std::cout << "vertvelpowerup = true" << std::endl;
}
if (strncmp("-vertvelpowerup", argv[i], strlen("-vertvelpowerup")) == 0)
{
vertvelpowerup = false;
std::cout << "vertvelpowerup = false" << std::endl;
}
ini.SetBoolValue("bt.ini", "gvertvelpowerup", vertvelpowerup);
if (strncmp("extractdata", argv[i], strlen("extractdata")) == 0)
{
int ret = system("./GloDecrypt.exe ./ball.glo ./balldecrypt.glo extract ./ball/");
if (ret == -1)
system("./GloDecrypt ./ball.glo ./balldecrypt.glo extract ./ball/");
}
}
std::map<std::string, int> cheatKeys =
{
{"inc me", 14},
{"let me hear you say fired up", 6},
{"my velcro shoes", 3},
{"no ska no swing", 9},
{"not a small thing", 0},
{"sidewinders", 10},
{"detrimental abundancy", 22},
{"mixed blessings", 12},
{"beneficial abundancy", 11},
{"through holy faith", 15},
{"sweet", 8},
{"funyons", 30},
{"i never prosper", 31}
};
#ifndef BTR_USE_SDL
sf::Shader gammaShader;
auto loaded = gammaShader.loadFromMemory(gammaShaderCode, sf::Shader::Fragment);
if (!loaded)
{
std::cout << "Loading gamma shader failed" << std::endl;
}
sf::Shader colShader;
loaded = colShader.loadFromMemory(colorShaderCode, sf::Shader::Fragment);
if (!loaded)
{
std::cout << "Loading color shader failed" << std::endl;
}
#endif
#if 0
L = lua_open();
luaL_openlibs(L);
getGlobalNamespace(L)
.beginNamespace("registeredBallTicks")
.endNamespace()
.beginClass<BTRball>("BTRball")
.addProperty("x", &BTRball::x)
.addProperty("y", &BTRball::y)
.addProperty("velX",&BTRball::velX)
.addProperty("velY",&BTRball::velY)
.addProperty("isFireball",&BTRball::isFireball)
.addProperty("invisibleSparkling",&BTRball::invisibleSparkling)
.endClass()
.beginClass<BTRPaddle>("BTRpaddle")
.endClass()
.beginNamespace("audio")
.addFunction("PlaySound",&BTRPlaySound)
.endNamespace();
luaL_dostring(L, R"(
function registerBallTickHandler(func)
table.insert(registeredBallTicks,func);
end
function tickBallHandlers(ball)
for i,v in ipairs(registeredBallTicks) do
v(ball);
end
end
)");
#endif
std::vector<std::string> musics = {"./ball/coolin.mds", "./ball/samba.mds", "./ball/piano.mds", "./ball/rush.mds"};
std::cout.setf(std::ios_base::boolalpha);
//for (int i = 0; i < 19937 / 8 * 4; i++) gen.seed(rd());
std::uniform_int_distribution<> dis(-5, 5);
std::uniform_int_distribution<> fuzzrand(0, 2);
std::uniform_int_distribution<> mdsrand(0, musics.size() - 1);
unsigned int devID = 0;
#if defined(WIN32)
//midiStreamOpen(&midiDev, &devID, 1, 0, 0, 0);
#endif
int x, y, n;
auto retval = stbi_load("./ball/back.png", &x, &y, &n, 4);
if (retval == NULL)
{
std::cerr << "Failed to load image: " << std::endl;
std::cerr << stbi_failure_reason() << std::endl;
return -1;
}
btr::RenderWindow *window = new btr::RenderWindow(btr::VideoMode(BTRWINDOWWIDTH, BTRWINDOWHEIGHT), "Blast Thru Reborn",
isFullscreen ? btr::Style::Fullscreen : btr::Style::Titlebar | btr::Style::Close);
btr::Texture tex;
tex.create(x, y);
tex.update(retval);
free(retval);
int wallHeight = 0;
auto wallPixels = stbi_load("./ball/walls.png", &wallWidth, &wallHeight, &n, 4);
if (wallPixels == NULL)
{
std::cerr << "Failed to load image: " << std::endl;
std::cerr << stbi_failure_reason() << std::endl;
return -1;
}
btr::Texture wallTex;
wallTex.create(wallWidth, wallHeight);
wallTex.update(wallPixels);
wallTex.setSmooth(1);
free(wallPixels);
btr::Sprite wallSprite;
wallSprite.setPosition(btr::Vector2f(wallWidth / -2, 0));
wallSprite.setTexture(wallTex);
wallSprite.setScale(1, 24);
ball = new BTRsprite("./ball/ball.png", 32);
auto loadSplashSprite = new BTRsprite("./art/romtech.png", 1, false, 1);
std::stack<BTRMenuUIWindow> menuWindowStack;
static const std::function<void()> closeMenuWindow = [&]()
{
menuWindowStack.pop();
};
btr::Texture windowTexture;
windowTexture.create(window->getSize().x,window->getSize().y);
btr::Sprite windowSprite;
windowSprite.setTexture(windowTexture, true);
window->setFramerateLimit(40);
window->clear();
window->draw(*loadSplashSprite);
windowTexture.update(*window);
window->display();
auto fadeToColor = [&](btr::Color fadeColor) {
uint32_t framerate = 60;
#if defined WIN32 && !defined BTR_USE_SDL
framerate = GetDeviceCaps(GetDC(window->getSystemHandle()), VREFRESH);
#endif
window->setFramerateLimit(framerate);
windowTexture.update(*window);
double alpha = 1.0;
int localFrameCnt = 0;
while (localFrameCnt < framerate)
{
#ifndef BTR_USE_SDL
if (fadeColor == btr::Color(0, 0, 0, 255) && btr::Shader::isAvailable())
{
gammaShader.setUniform("texture", windowTexture);
gammaShader.setUniform("gamma", (float)alpha);
window->clear();
window->draw(windowSprite, &gammaShader);
window->display();
alpha = std::lerp(alpha, 0, ((double)framerate / 10.) / framerate);
localFrameCnt++;
continue;
}
#endif
btr::RectangleShape shape;
shape.setSize(btr::Vector2f(window->getSize().x,window->getSize().y));
shape.setFillColor(btr::Color(fadeColor.r,fadeColor.g,fadeColor.b,255 * (1.f - alpha)));
shape.setPosition(0.f,0.f);
windowSprite.setColor(btr::Color(255, 255, 255, 255));
window->clear();
window->draw(windowSprite);
window->draw(shape);
window->display();
alpha -= 1. / framerate;
localFrameCnt++;
}
window->setFramerateLimit(40);
};
BTRPlaySound("./sound/intro.wav");
window->requestFocus();
btr::Event event;
DoFuncForDuration(3, [&]()
{
window->pollEvent(event);
window->clear();
window->draw(*loadSplashSprite);
window->display();
});
fadeToColor(btr::Color(0, 0, 0, 255));
BTRPlaySound("./ball/grow.wav");
window->setFramerateLimit(40);
//loadMusic(musics[mdsrand(gen)]);
now = clocktime.now();
auto font = new BTRFont("./ball/fontcool.png");
auto largeFont = new BTRFont("./ball/fontlarge.png", BTRFont::FontType::BTR_FONTLARGE);
auto largeFont2 = new BTRFont("./ball/fontlarge2.png", BTRFont::FontType::BTR_FONTLARGE2);
playArea = new BTRPlayArea("./lev/0.lev");
//playArea->paddle.sprite = new BTRsprite("./ball/rockpaddle.png", 1, true, 32);
long double fade = 1.0;
btr::RectangleShape scrRect((btr::Vector2f)window->getSize());
scrRect.setFillColor(btr::Color(0, 0, 0, 255));
bool cursorVisible = true;
auto cursor = new BTRsprite("./ball/cursor.png", 1, false, 1);
auto sparkSprite = new BTRsprite("./ball/sparks.png", 15, false, 3);
auto powerupSprite = new BTRsprite("./ball/powerup.png", 16, false, 2);
auto pausedSprite = new BTRsprite("./ball/paused.png", 1, false, 1);
auto magnetSprite = new BTRsprite("./ball/magnet.png", 16, false, 4);
auto tractorSprite = new BTRsprite("./ball/power.png", 1, 0);
auto explSprite = new BTRsprite("./ball/explosion.png", 8, false, 4);
tractorSprite->SetSpriteIndex(1);
pausedSprite->SetTexRect(0, 0);
powerupSprite->Animate();
framesPassedlastPowerup = 40 * 5;
bool paused = false;
bool highScore = false;
bool cheatText = false;
bool exitingFromHighScore = false;
bool menu = false;
int magnetHeldBall = false;
bool drawCornerText = 1;
int cornerTextBlinkTime = 0;
std::string textToDraw = "Welcome to Blast Thru Reborn!";
std::string cheatstr = "";
menu = true;
paused = true;
PauseMidiPlayback();
BTRPlaySound("./sound/menu.wav", 1, 0, 1);
auto DrawBackground = [&]() {
btr::Sprite sprite;
sprite.setTexture(tex, true);
window->clear();
for (int posY = 0; posY < BTRWINDOWHEIGHT; posY += y)
for (int posX = 0; posX < BTRWINDOWWIDTH; posX += x)
{
sprite.setPosition(btr::Vector2f(posX, posY));
//gammaShader.setUniform("gamma", 1.f);
//gammaShader.setUniform("texture", tex);
#ifdef BTR_USE_SDL
window->draw(sprite);
#else
colShader.setUniform("texture",tex);
colShader.setUniform("col",sf::Glsl::Vec4(backgrndr,backgrndg,backgrndb,1));
window->draw(sprite, backgrndcol ? &colShader : NULL /*,&gammaShader*/);
#endif
}
wallSprite.setPosition(BTRWINDOWWIDTH - wallWidth / 2, 0);
window->draw(wallSprite);
wallSprite.setPosition(btr::Vector2f(wallWidth / -2, 0));
window->draw(wallSprite);
};
DrawBackground();
windowTexture.update(*window);
window->display();
auto removeSparkObject = [](BTRSpark &spark) {
if (spark.sparkRect.left >= 15 * 3 || spark.y > BTRWINDOWHEIGHT)
{
return true;
}
return false;
};
auto drawPaddle = [&](bool changeToCursor = true) {
if (changeToCursor)
playArea->paddle.sprite->sprite.setPosition(std::clamp(cursor->sprite.getPosition().x, wallWidth / 2.f, BTRWINDOWWIDTH - wallWidth / 2.f - (float)playArea->paddle.paddleRadius), BTRWINDOWHEIGHT - 30);
//paddle.sprite->Animate();
window->draw(playArea->paddle.sprite->sprite);
playArea->paddle.sprite->sprite.setTextureRect(btr::IntRect(btr::Vector2i(0, playArea->paddle.sprite->realHeightPerTile * (playArea->paddle.sprite->animFramePos - 1)), btr::Vector2i(3, playArea->paddle.sprite->realHeightPerTile)));
auto orgpos = playArea->paddle.sprite->sprite.getPosition();
playArea->paddle.sprite->sprite.setPosition(orgpos.x - 3, orgpos.y);
window->draw(playArea->paddle.sprite->sprite);
playArea->paddle.sprite->sprite.setTextureRect(btr::IntRect(btr::Vector2i(playArea->paddle.sprite->width - 3, playArea->paddle.sprite->realHeightPerTile * (playArea->paddle.sprite->animFramePos - 1)), btr::Vector2i(3, playArea->paddle.sprite->realHeightPerTile)));
playArea->paddle.sprite->sprite.setPosition(orgpos.x + playArea->paddle.paddleRadius, orgpos.y);
window->draw(playArea->paddle.sprite->sprite);
playArea->paddle.sprite->sprite.setPosition(orgpos);
};
auto drawPaddleXY = [&](float x, float y, int width) {
auto orgRad = playArea->paddle.paddleRadius;
auto orgPos = playArea->paddle.sprite->sprite.getPosition();
playArea->paddle.sprite->sprite.setPosition(x, y);
playArea->paddle.paddleRadius = width;
playArea->paddle.sprite->sprite.setTextureRect(btr::IntRect(btr::Vector2i(playArea->paddle.sprite->width / 2 - playArea->paddle.paddleRadius / 2, playArea->paddle.sprite->realHeightPerTile * (playArea->paddle.sprite->animFramePos - 1)), btr::Vector2i(playArea->paddle.paddleRadius, playArea->paddle.sprite->realHeightPerTile)));
drawPaddle(false);
playArea->paddle.paddleRadius = orgRad;
playArea->paddle.sprite->sprite.setPosition(orgPos);
};
btr::Texture highScoreTexture;
highScoreTexture.create(BTRWINDOWWIDTH, BTRWINDOWHEIGHT);
btr::Sprite highScoreSprite;
highScoreSprite.setTexture(highScoreTexture, true);
std::vector<BTRMovingText> scoreTexts;
std::vector<BTRMovingText> moveTexts;
moveTexts = BuildTextFromString(
"Congratulations!\n"
"You have entered the Hall of Scores!\n"
"Enter your name in the box below:\n",
font,3);
int highScoreWidth, highScoreHeight, colchan;
auto highScoreImage = stbi_load("./ball/halloffaith.png", &highScoreWidth, &highScoreHeight, &colchan, 4);
window->setKeyRepeatEnabled(true);
if (highScoreImage)
{
highScoreTexture.update(highScoreImage);
free(highScoreImage);
}
auto winBoxImage = new BTRsprite("./ball/winbox.png", 1, 0, 1);
auto winBoxImage2 = new BTRsprite("./ball/winbox2.png", 1, 0, 1);
auto titleImage = new BTRsprite("./ball/bibleball.png", 1, false, 1);
auto wincornerImage = new BTRsprite("./ball/wincorner.png", 14, 0, 1);
auto winButtonImage = new BTRsprite("./ball/winbutton2.png", 1);
winButtonImage->SetTexRect(0,0);
auto winButtonSmallImage = new BTRsprite("./ball/winbutton.png", 1);
winButtonSmallImage->SetTexRect(0,0);
wincornerImage->sprite.setOrigin(btr::Vector2f(wincornerImage->realWidthPerTile / 2, wincornerImage->height / 2));
titleImage->SetTexRect(0, 0);
titleImage->texture.setRepeated(true);
bool isLevEdit = false;
auto DrawFrame = [&](btr::RenderWindow *window, btr::Vector2f pos, btr::Vector2f size) {
btr::RectangleShape rect;
rect.setFillColor(btr::Color(16, 24, 32));
rect.setPosition(pos);
rect.setSize(size);
window->draw(rect);
auto orgPosX = pos.x;
auto orgPosY = pos.y;
for (; pos.x < orgPosX + size.x; pos.x += wincornerImage->realWidthPerTile)
{
wincornerImage->SetTexRect(1, 0);
wincornerImage->sprite.setPosition(pos);
window->draw(*wincornerImage);
}
pos.x = orgPosX;
pos.y += size.y;
for (; pos.x < orgPosX + size.x; pos.x += wincornerImage->realWidthPerTile)
{
wincornerImage->SetTexRect(1, 0);
wincornerImage->sprite.setPosition(pos);
window->draw(*wincornerImage);
}
pos = btr::Vector2f(orgPosX, orgPosY);
for (; pos.y < orgPosY + size.y; pos.y += wincornerImage->height)
{
wincornerImage->SetTexRect(3, 0);
wincornerImage->sprite.setPosition(pos);
window->draw(*wincornerImage);
wincornerImage->SetTexRect(0, 0);
}
pos = btr::Vector2f(orgPosX + size.x, orgPosY);
for (; pos.y < orgPosY + size.y; pos.y += wincornerImage->height)
{
wincornerImage->SetTexRect(3, 0);
wincornerImage->sprite.setPosition(pos);
window->draw(*wincornerImage);
wincornerImage->SetTexRect(0, 0);
}
pos = btr::Vector2f(orgPosX, orgPosY);
wincornerImage->SetTexRect(0, 0);
wincornerImage->sprite.setPosition(pos);
window->draw(*wincornerImage);
wincornerImage->sprite.setPosition(pos + btr::Vector2f(size.x, 0));
window->draw(*wincornerImage);
wincornerImage->sprite.setPosition(pos + size);
window->draw(*wincornerImage);
wincornerImage->sprite.setPosition(pos + btr::Vector2f(0, size.y));
window->draw(*wincornerImage);
};
auto editPlayArea = new BTRPlayArea();
auto flipPaused = [&](btr::Event event, bool musRestart = true) {
paused ^= 1;
BTRStopAllSounds();
PauseMidiPlayback();
windowTexture.update(*window);
if (paused)
{
BTRPlaySound("./sound/menu.wav", 1, 0, 1);
if (event.key.code == btr::Keyboard::Escape)
{
menu = true;
}
else
menu = false;
}
else
{
BTRStopAllSounds();
if (musRestart)
ContinueMidiPlayback();
menu = false;
}
};
int newScoreAt = -1;
auto updateScoreList = [&]()
{
{
scoreTexts.clear();
newScoreAt = -1;
auto scorePair = std::make_pair("", score);
scoresAndNames.push_back(scorePair);
std::sort(scoresAndNames.begin(), scoresAndNames.end(), &sortScoreList);
scoresAndNames.pop_back();
bool scoreFound = false;
int scoreY = 3;
int scoreList = 1;
bool flipVal = false;
for (int i = 0; i < scoresAndNames.size(); i++)
{
auto &curScore = scoresAndNames[i];
if (std::get<int>(curScore) == score)
{
newScoreAt = i;
for (auto& curMovingText : moveTexts)
{
curMovingText.pos = curMovingText.orgPos;
}
}
std::stringstream istr;
std::string str = std::to_string(scoreList) + ". " + std::get<std::string>(curScore);
istr << str;
istr.setf(std::ios::right, std::ios::adjustfield);
str += " ";
str.resize(str.size() - std::to_string(std::get<int>(curScore)).size());
str += std::to_string(std::get<int>(curScore));
istr << std::to_string(std::get<int>(curScore));
auto moveText = BTRMovingText();
flipVal ^= 1;
moveText.toPos = btr::Vector2f(90, scoreY * font->genCharHeight);
moveText.pos = btr::Vector2f(-font->GetSizeOfText(str).x + (BTRWINDOWWIDTH * flipVal), scoreY * font->genCharHeight);
scoreY++;
moveText.movingText = str;
scoreTexts.push_back(moveText);
scoreList++;
}
}
};
// Game Menu buttons.
BTRButton retToGame;
retToGame.clickedFunc = [&]() {
if (firstRun)
{
return;
}
btr::Event event;
event.type = btr::Event::KeyPressed;
event.key.code = btr::Keyboard::Escape;
flipPaused(event);
//BTRPlaySound("./ball/editselect.wav");
};
retToGame.str = "Return to Game";
retToGame.pos = btr::Vector2f(640 / 2 - winButtonImage->width / 2, BTRWINDOWHEIGHT - 100);
BTRButton quitGame;
quitGame.clickedFunc = [&]() {
BTRMenuUIWindow menuWindow;
menuWindow.nameOfWindow = "Quit Game";
//menuWindow.staticTexts.push(std::make_pair());
window->display();
windowTexture.update(*window); // Flip off the buffers.
fadeToColor(btr::Color(0, 0, 0, 255));
window->close();
};
quitGame.pos = btr::Vector2f(640 / 2 - winButtonImage->width / 2, BTRWINDOWHEIGHT - 80);
quitGame.str = "Quit Game";
BTRButton randomLevel;
randomLevel.clickedFunc = [&]() {
fadeOut = true;
demo = false;
score = 0;
lives = 2;
playArea->levnum = std::uniform_int_distribution<int>(1, 40)(rd) - 1;
btr::Event event;
event.type = btr::Event::KeyPressed;
event.key.code = btr::Keyboard::Escape;
flipPaused(event, false);
GetCurPlayingFilename() = "";
//BTRPlaySound("./ball/editselect.wav");
firstRun = false;
fade = 1;
playArea->randomPlay = true;
isLevEdit = false;
};
randomLevel.str = "Random Play";
randomLevel.pos = btr::Vector2f(640 / 2 - winButtonImage->width / 2, BTRWINDOWHEIGHT - 340);
btns.push_back(randomLevel);
BTRButton singlePlay;
singlePlay.str = "Single Play";
singlePlay.pos = btr::Vector2f(randomLevel.pos.x, randomLevel.pos.y - 20);
singlePlay.clickedFunc = [&]() {
btns[0].clickedFunc();
playArea->levnum = 0;
playArea->randomPlay = false;
};
btns.push_back(retToGame);
btns.push_back(quitGame);
btns.push_back(singlePlay);
BTRButton levEdit;
levEdit.clickedFunc = [&]() {
isLevEdit = true;
//editPlayArea->texLoaded = false;
//editPlayArea->LoadBrickTex();
btr::Event event;
event.type = btr::Event::KeyPressed;
event.key.code = btr::Keyboard::Escape;
flipPaused(event, false);
};
levEdit.str = "Level Editor";
levEdit.pos = btr::Vector2f(randomLevel.pos.x, randomLevel.pos.y + 20);
btns.push_back(levEdit);
BTRButton highScoreEnter;
highScoreEnter.str = "High Scores";
highScoreEnter.pos = btr::Vector2f(randomLevel.pos.x, randomLevel.pos.y + 40);
highScoreEnter.clickedFunc = [&]() {
btr::Event event;
event.type = btr::Event::KeyPressed;
event.key.code = btr::Keyboard::Escape;
flipPaused(event, false);
BTRPlaySound("./ball/whoosh.wav");
score = 0;
updateScoreList();
newScoreAt = -1;
highScore = true;
ballLost = false;
fadeOut = false;
loadMusic("./ball/hghscr.mds");
};
btns.push_back(highScoreEnter);
// Level Editor stuff.
BTRButton levEditMenu;
levEditMenu.clickedFunc = [&]() {
btr::Event event;
event.type = btr::Event::KeyPressed;
event.key.code = btr::Keyboard::Escape;
flipPaused(event);
//BTRPlaySound("./ball/editselect.wav");
};
levEditMenu.pos = btr::Vector2f(BTRWINDOWWIDTH - winButtonSmallImage->width - 20 - wallWidth / 2, 20);
levEditMenu.str = "Menu";
levEditBtns.push_back(levEditMenu);
BTRButton levEditPlay;
levEditPlay.clickedFunc = [&]() {
*playArea = *editPlayArea;
singlePlay.clickedFunc();
btr::Event event;
event.type = btr::Event::KeyPressed;
event.key.code = btr::Keyboard::Escape;
flipPaused(event, false);
playArea->levnum = -1;
playArea->SpawnInitialBall();
};
levEditPlay.smallButton = 1;
levEditPlay.str = "Play";
levEditPlay.pos = btr::Vector2f(levEditMenu.pos.x - winButtonSmallImage->width - 20, 20);
levEditBtns.push_back(levEditPlay);
BTRButton levEditLoad;
levEditLoad.clickedFunc = [&]() {
editPlayArea->bricks.clear();
auto file = std::ifstream();
file.open("./lev/cust.btrlev");
if (!file.is_open()) return;
char* header = new char[6];
file.read (header,6);
if (strncmp(header,"BTRLEV",6) == 0)
{
unsigned char endianness = 0;
file.read((char*)&endianness,1);
while(!file.eof())
{
int curBrickID = 0;
BTRbrick curBrick;
file.read((char*)&curBrickID,sizeof(int));
int x,y;
file.read((char*)&x,sizeof(x));
file.read((char*)&y,sizeof(y));
curBrick.x = x;
curBrick.y = y;
curBrick.brickID = curBrickID;
if (curBrick.brickID >= 64) curBrick.isFireball = true;
editPlayArea->bricks.push_back(curBrick);
}
}
};
levEditLoad.str = "Load";
levEditLoad.pos = btr::Vector2f(levEditPlay.pos.x - winButtonSmallImage->width - 20, 20);
levEditBtns.push_back(levEditLoad);
BTRButton levEditSave;
levEditSave.clickedFunc = [editPlayArea]() {
editPlayArea->ExportBricks();
};
levEditSave.smallButton = 1;
levEditSave.str = "Save";
levEditSave.pos = btr::Vector2f(levEditLoad.pos.x - winButtonSmallImage->width - 20, 20);
levEditBtns.push_back(levEditSave);
BTRButton levEditNew;
levEditNew.clickedFunc = [&editPlayArea]() {
editPlayArea->bricks.clear();
};
levEditNew.smallButton = 1;
levEditNew.str = "New";
levEditNew.pos = btr::Vector2f(levEditSave.pos.x - winButtonSmallImage->width - 20, 20);
levEditBtns.push_back(levEditNew);
// In-game menu stuff.
BTRButton testMenuWindow;
testMenuWindow.clickedFunc = [&]() {
BTRMenuUIWindow menuWindow;
BTRButton menuWindowQuitButton;
menuWindowQuitButton.clickedFunc = closeMenuWindow;
menuWindowQuitButton.str = "Close";
menuWindowQuitButton.pos = btr::Vector2f(0,0);
menuWindow.buttons.push_back(menuWindowQuitButton);
menuWindow.position = btr::Vector2f(320,240);
menuWindow.size = btr::Vector2f(100,100);
menuWindow.nameOfWindow = "TestMenuWindow";
menuWindowStack.push(menuWindow);
};
testMenuWindow.pos = btr::Vector2f(highScoreEnter.pos.x, highScoreEnter.pos.y + 40);
testMenuWindow.str = "testMenuWindow";
btns.push_back(testMenuWindow);
btr::Sprite brickSprite;
playArea->LoadBrickTex();
brickSprite.setTexture(playArea->brickTexture, true);
auto drawBricks = [&]() {
btr::Sprite brickSprite;
playArea->LoadBrickTex();
brickSprite.setTexture(playArea->brickTexture, true);
#ifndef BTR_USE_SDL
btr::VertexArray brickVertArray;
brickVertArray.setPrimitiveType(btr::PrimitiveType::Triangles);
#endif
for (auto &curBrick : playArea->bricks)
{
if (curBrick.brickID != 63 || (curBrick.brickID == 63 && isLevEdit))
{
#ifndef BTR_USE_SDL
auto& curBrickIntRect = playArea->brickTexRects[curBrick.isFireball ? fireBrickFrame : curBrick.brickID - 1];
AppendVerticesFromRect(curBrickIntRect,btr::Vector2f(curBrick.x,curBrick.y),brickVertArray);
// Below ifdef'd code is the more higher-level representation of the above.
#else
if (curBrick.isFireball)
{
brickSprite.setTextureRect(playArea->brickTexRects[fireBrickFrame]);
}
else brickSprite.setTextureRect(playArea->brickTexRects[curBrick.brickID - 1]);
brickSprite.setPosition(btr::Vector2f(curBrick.x, curBrick.y));
window->draw(brickSprite);
#endif
}
}
for (auto &curExplBrick : explodingBricks)
{
if (curExplBrick.frameOffset >= 7)
{
curExplBrick.frameOffset = 0;
curExplBrick.loop++;
}
#ifndef BTR_USE_SDL
AppendVerticesFromRect(playArea->brickTexRects[128ll + curExplBrick.frameOffset],curExplBrick.pos,brickVertArray);
#else
brickSprite.setTextureRect(playArea->brickTexRects[128ll + curExplBrick.frameOffset]);
brickSprite.setPosition(curExplBrick.pos);
window->draw(brickSprite);
#endif
curExplBrick.frameOffset++;
}
#ifndef BTR_USE_SDL
btr::RenderStates vertexRenderstate(&playArea->brickTexture);
window->draw(brickVertArray,vertexRenderstate);
#endif
#if __cplusplus <= 201703L
for (int i = 0; i < explodingBricks.size(); i++)
{
if (explodingBricks[i].loop > 4)
{