-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
1816 lines (1574 loc) · 51.5 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
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
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions"
/*
* SDL2Sand
*
* Copyright © 2006 Thomas RenÈ Sidor, Kristian Jensen
* Copyright © 2014 Artur Rojek
* Copyright © 2022 Volodymyr Atamanenko
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include "SDL.h"
#include "CmdLine.h"
#ifdef __vita__
#include <psp2/power.h>
#endif
#define FASTRAND_MAX 32767
static unsigned int g_seed;
inline void fast_srand( int seed ) {
g_seed = seed;
}
inline int fastrand() {
g_seed = (214013*g_seed+2531011);
return (g_seed>>16)&0x7FFF;
}
//The application time based timer
class LTimer
{
public:
//Initializes variables
LTimer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
Uint32 getTicks();
//Checks the status of the timer
bool isStarted();
bool isPaused();
private:
//The clock time when the timer started
Uint32 mStartTicks;
//The ticks stored when the timer was paused
Uint32 mPausedTicks;
//The timer status
bool mPaused;
bool mStarted;
};
LTimer::LTimer()
{
//Initialize the variables
mStartTicks = 0;
mPausedTicks = 0;
mPaused = false;
mStarted = false;
}
void LTimer::start()
{
//Start the timer
mStarted = true;
//Unpause the timer
mPaused = false;
//Get the current clock time
mStartTicks = SDL_GetTicks();
mPausedTicks = 0;
}
void LTimer::stop()
{
//Stop the timer
mStarted = false;
//Unpause the timer
mPaused = false;
//Clear tick variables
mStartTicks = 0;
mPausedTicks = 0;
}
void LTimer::pause()
{
//If the timer is running and isn't already paused
if( mStarted && !mPaused )
{
//Pause the timer
mPaused = true;
//Calculate the paused ticks
mPausedTicks = SDL_GetTicks() - mStartTicks;
mStartTicks = 0;
}
}
void LTimer::unpause()
{
//If the timer is running and paused
if( mStarted && mPaused )
{
//Unpause the timer
mPaused = false;
//Reset the starting ticks
mStartTicks = SDL_GetTicks() - mPausedTicks;
//Reset the paused ticks
mPausedTicks = 0;
}
}
Uint32 LTimer::getTicks()
{
//The actual timer time
Uint32 time = 0;
//If the timer is running
if( mStarted )
{
//If the timer is paused
if( mPaused )
{
//Return the number of ticks when the timer was paused
time = mPausedTicks;
}
else
{
//Return the current time minus the start time
time = SDL_GetTicks() - mStartTicks;
}
}
return time;
}
bool LTimer::isStarted()
{
//Timer is running and paused or unpaused
return mStarted;
}
bool LTimer::isPaused()
{
//Timer is running and paused
return mPaused && mStarted;
}
int JOY_DEADZONE = 500;
//Screen size
int WIDTH;
int HEIGHT;
// FPS
const int SCREEN_FPS = 30;
const int SCREEN_TICKS_PER_FRAME = 1000 / SCREEN_FPS;
//Button sizes
int BUTTON_SIZE = 10;
int UPPER_ROW_Y;
int MIDDLE_ROW_Y;
int LOWER_ROW_Y;
const int DASHBOARD_HEIGHT = BUTTON_SIZE + 2;
int particleCount = 0;
int penSize = 2;
int mbx;
int mby;
int canMoveX = 0;
int canMoveY = 0;
int speedX = 0;
int speedY = 0;
//Init and declare ParticleSwapping
bool implementParticleSwaps = true;
/*
Enumerating conventions
-----------------------
Stillborn: between STILLBORN_UPPER_BOUND and STILLBORN_LOWER_BOUND
Floating: between FLOATING_UPPER_BOUND and FLOATING_LOWER_BOUND
*/
const int STILLBORN_UPPER_BOUND = 14;
const int STILLBORN_LOWER_BOUND = 1;
const int FLOATING_UPPER_BOUND = 35;
const int FLOATING_LOWER_BOUND = 32;
enum ParticleType
{
// STILLBORN
NOTHING = 0,
WALL = 1,
IRONWALL = 2,
TORCH = 3,
//x = 4,
STOVE = 5,
ICE = 6,
RUST = 7,
EMBER = 8,
PLANT = 9,
VOID = 10,
//SPOUTS
WATERSPOUT = 11,
SANDSPOUT = 12,
SALTSPOUT = 13,
OILSPOUT = 14,
//x = 15,
//ELEMENTAL
WATER = 16,
MOVEDWATER = 17,
DIRT = 18,
MOVEDDIRT = 19,
SALT = 20,
MOVEDSALT = 21,
OIL = 22,
MOVEDOIL = 23,
SAND = 24,
MOVEDSAND = 25,
//COMBINED
SALTWATER = 26,
MOVEDSALTWATER = 27,
MUD = 28,
MOVEDMUD = 29,
ACID = 30,
MOVEDACID = 31,
//FLOATING
STEAM = 32,
MOVEDSTEAM = 33,
FIRE = 34,
MOVEDFIRE = 35,
//ELECTRICITY
ELEC = 36,
MOVEDELEC = 37
};
// Instead of using a two-dimensional array
// we'll use a simple array to improve speed
// vs = virtual screen
ParticleType *vs;
// The current brush type
ParticleType CurrentParticleType = WALL;
ParticleType LastParticleType = NOTHING;
//The number of buttons
const int BUTTON_COUNT = 19;
// Button rectangle struct
typedef struct
{
SDL_Rect rect;
ParticleType particleType;
} ButtonRect;
//An array of the buttons
ButtonRect Button[BUTTON_COUNT];
// The particle system play area
SDL_Rect scene;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *scene_texture;
uint32_t *screen_buffer;
//Checks wether a given particle type is a stillborn element
static inline bool IsStillborn(ParticleType t)
{
return (t >= STILLBORN_LOWER_BOUND && t <= STILLBORN_UPPER_BOUND);
}
//Checks wether a given particle type is a floting type - like FIRE and STEAM
static inline bool IsFloating(ParticleType t)
{
return (t >= FLOATING_LOWER_BOUND && t <= FLOATING_UPPER_BOUND);
}
//Checks wether a given particle type is burnable - like PLANT and OIL
static inline bool IsBurnable(ParticleType t)
{
return (t == PLANT || t == OIL || t == MOVEDOIL);
}
//Checks wether a given particle type is burnable - like PLANT and OIL
static inline bool BurnsAsEmber(ParticleType t)
{
return (t == PLANT); //Maybe we'll add a FUSE or WOOD
}
std::map<ParticleType, SDL_Color> colors;
// Initializing colors
void initColors()
{
//STILLBORN
colors[SAND] = { 238, 204, 128, 255};
colors[WALL] = { 100, 100, 100, 255};
colors[VOID] = { 60, 60, 60, 255};
colors[IRONWALL] = { 110, 110, 110, 255};
colors[TORCH] = { 139, 69, 19, 255};
colors[STOVE] = { 74, 74, 74, 255};
colors[ICE] = { 175, 238, 238, 255};
colors[PLANT] = { 0, 150, 0, 255};
colors[EMBER] = { 127, 25, 25, 255};
colors[RUST] = { 110, 40, 10, 255};
//ELEMENTAL
colors[WATER] = { 32, 32, 255, 255};
colors[DIRT] = { 205, 175, 149, 255};
colors[SALT] = { 255, 255, 255, 255};
colors[OIL] = { 128, 64, 64, 255};
//COMBINED
colors[MUD] = { 139, 69, 19, 255};
colors[SALTWATER] = { 65, 105, 225, 255};
colors[STEAM] = { 95, 158, 160, 255};
//EXTRA
colors[ACID] = { 173, 255, 47, 255};
colors[FIRE] = { 255, 50, 50, 255};
colors[ELEC] = { 255, 255, 0, 255};
//SPOUTS
colors[WATERSPOUT] = { 0, 0, 128, 255};
colors[SANDSPOUT] = { 240, 230, 140, 255};
colors[SALTSPOUT] = { 238, 233, 233, 255};
colors[OILSPOUT] = { 108, 44, 44, 255};
}
//Drawing our virtual screen to the real screen
static void DrawScene()
{
particleCount = 0;
size_t framebuf_size = scene.w * scene.h * 3 * sizeof(Uint8);
auto* pixels = static_cast<Uint8 *>(malloc(framebuf_size));
memset(pixels, 0, framebuf_size);
//Iterating through each pixel height first
for(int y=scene.h;y--;)
{
//Width
for(int x=scene.w;x--;)
{
const unsigned int offset = ( scene.w * 3 * y ) + x * 3;
int index = x+(scene.w*y);
ParticleType same = vs[index];
if(same != NOTHING)
{
if(IsStillborn(same)) {
pixels[ offset + 0 ] = colors[same].r;
pixels[ offset + 1 ] = colors[same].g;
pixels[ offset + 2 ] = colors[same].b;
} else
{
particleCount++;
if(same % 2 == 1) //Moved
{
pixels[ offset + 0 ] = colors[(ParticleType)(same-1)].r;
pixels[ offset + 1 ] = colors[(ParticleType)(same-1)].g;
pixels[ offset + 2 ] = colors[(ParticleType)(same-1)].b;
vs[index] = (ParticleType)(same-1); //Set it to not moved
}
else { //Not moved
pixels[ offset + 0 ] = colors[same].r;
pixels[ offset + 1 ] = colors[same].g;
pixels[ offset + 2 ] = colors[same].b;
}
}
}
}
}
SDL_UpdateTexture(scene_texture, nullptr, pixels, scene.w * 3);
free(pixels);
SDL_RenderCopy(renderer, scene_texture, nullptr, &scene);
}
// Emitting a given particletype at (x,o) width pixels wide and
// with a p density (probability that a given pixel will be drawn
// at a given position withing the width)
void Emit(int x, int width, ParticleType type, float p)
{
for (int i = x - width/2; i < x + width/2; i++)
{
if ( fastrand() < (int)(FASTRAND_MAX * p) ) vs[i+WIDTH] = type;
}
}
//Performs logic of stillborn particles
void StillbornParticleLogic(int x,int y,ParticleType type)
{
int index, above, left, right, below, same, abovetwo;
switch(type)
{
case VOID:
above = x+((y-1)*WIDTH);
left = (x+1)+(y*WIDTH);
right = (x-1)+(y*WIDTH);
below = x+((y+1)*WIDTH);
if(vs[above] != NOTHING)
vs[above] = NOTHING;
if(vs[below] != NOTHING)
vs[below] = NOTHING;
if(vs[left] != NOTHING)
vs[left] = NOTHING;
if(vs[right] != NOTHING)
vs[right] = NOTHING;
break;
case IRONWALL:
above = x+((y-1)*WIDTH);
left = (x+1)+(y*WIDTH);
right = (x-1)+(y*WIDTH);
if(fastrand()%200 == 0 && (vs[above] == RUST || vs[left] == RUST || vs[right] == RUST))
vs[x+(y*WIDTH)] = RUST;
break;
case TORCH:
above = x+((y-1)*WIDTH);
left = (x+1)+(y*WIDTH);
right = (x-1)+(y*WIDTH);
if(fastrand()%2 == 0) // Spawns fire
{
if(vs[above] == NOTHING || vs[above] == MOVEDFIRE) //Fire above
vs[above] = MOVEDFIRE;
if(vs[right] == NOTHING || vs[right] == MOVEDFIRE) //Fire to the right
vs[right] = MOVEDFIRE;
if(vs[left] == NOTHING || vs[left] == MOVEDFIRE) //Fire to the left
vs[left] = MOVEDFIRE;
}
if(vs[above] == MOVEDWATER || vs[above] == WATER) //Fire above
vs[above] = MOVEDSTEAM;
if(vs[right] == MOVEDWATER || vs[right] == WATER) //Fire to the right
vs[right] = MOVEDSTEAM;
if(vs[left] == MOVEDWATER || vs[left] == WATER) //Fire to the left
vs[left] = MOVEDSTEAM;
break;
case PLANT:
if(fastrand()%2 == 0) //Making the plant grow slowly
{
index = 0;
switch(fastrand()%4)
{
case 0: index = (x-1)+(y*WIDTH); break;
case 1: index = x+((y-1)*WIDTH); break;
case 2: index = (x+1)+(y*WIDTH); break;
case 3: index = x+((y+1)*WIDTH); break;
}
if(vs[index] == WATER)
vs[index] = PLANT;
}
break;
case EMBER:
below = x+((y+1)*WIDTH);
if(vs[below] == NOTHING || IsBurnable(vs[below]))
vs[below] = FIRE;
index = 0;
switch(fastrand()%4)
{
case 0: index = (x-1)+(y*WIDTH); break;
case 1: index = x+((y-1)*WIDTH); break;
case 2: index = (x+1)+(y*WIDTH); break;
case 3: index = x+((y+1)*WIDTH); break;
}
if(vs[index] == PLANT)
vs[index] = FIRE;
if(fastrand()%18 == 0) // Making ember burn out slowly
vs[x+(y*WIDTH)] = NOTHING;
break;
case STOVE:
above = x+((y-1)*WIDTH);
abovetwo = x+((y-2)*WIDTH);
if(fastrand()%4 == 0 && vs[above] == WATER) // Boil the water
vs[above] = STEAM;
if(fastrand()%4 == 0 && vs[above] == SALTWATER) // Saltwater separates
{
vs[above] = SALT;
vs[abovetwo] = STEAM;
}
if(fastrand()%8 == 0 && vs[above] == OIL) // Set oil aflame
vs[above] = EMBER;
break;
case RUST:
if(fastrand()%7000 == 0)//Deteriate rust
vs[x+(y*WIDTH)] = NOTHING;
break;
//####################### SPOUTS #######################
case WATERSPOUT:
if(fastrand()%6 == 0) // Take it easy on the spout
{
below = x+((y+1)*WIDTH);
if (vs[below] == NOTHING)
vs[below] = MOVEDWATER;
}
break;
case SANDSPOUT:
if(fastrand()%6 == 0) // Take it easy on the spout
{
below = x+((y+1)*WIDTH);
if (vs[below] == NOTHING)
vs[below] = MOVEDSAND;
}
break;
case SALTSPOUT:
if(fastrand()%6 == 0) // Take it easy on the spout
{
below = x+((y+1)*WIDTH);
if (vs[below] == NOTHING)
vs[below] = MOVEDSALT;
if(vs[below] == WATER || vs[below] == MOVEDWATER)
vs[below] = MOVEDSALTWATER;
}
break;
case OILSPOUT:
if(fastrand()%6 == 0) // Take it easy on the spout
{
below = x+((y+1)*WIDTH);
if (vs[below] == NOTHING)
vs[below] = MOVEDOIL;
}
break;
default:
break;
}
}
// Performing the movement logic of a given particle. The argument 'type'
// is passed so that we don't need a table lookup when determining the
// type to set the given particle to - i.e. if the particle is SAND then the
// passed type will be MOVEDSAND
inline void MoveParticle(int x, int y, ParticleType type)
{
type = (ParticleType)(type+1);
int above = x+((y-1)*WIDTH);
int same = x+(WIDTH*y);
int below = x+((y+1)*WIDTH);
//If nothing below then just fall (gravity)
if(!IsFloating(type))
{
if ( (vs[below] == NOTHING) && (fastrand() % 8)) //fastrand() % 8 makes it spread
{
vs[below] = type;
vs[same] = NOTHING;
return;
}
}
else
{
if(fastrand()%3 == 0) //Slow down please
return;
//If nothing above then rise (floating - or reverse gravity? ;))
if ((vs[above] == NOTHING || vs[above] == FIRE) && (fastrand() % 8) && (vs[same] != ELEC) && (vs[same] != MOVEDELEC)) //fastrand() % 8 makes it spread
{
if (type == MOVEDFIRE && fastrand()%20 == 0)
vs[same] = NOTHING;
else
{
vs[above] = vs[same];
vs[same] = NOTHING;
}
return;
}
}
//Randomly select right or left first
int sign = fastrand() % 2 == 0 ? -1 : 1;
// We'll only calculate these indicies once for optimization purpose
int first = (x+sign)+(WIDTH*y);
int second = (x-sign)+(WIDTH*y);
int index = 0;
//Particle type specific logic
switch(type)
{
case MOVEDELEC:
if(fastrand()%2 == 0)
vs[same] = NOTHING;
break;
case MOVEDSTEAM:
if(fastrand()%1000 == 0)
{
vs[same] = MOVEDWATER;
return;
}
if(fastrand()%500 == 0)
{
vs[same] = NOTHING;
return;
}
if(!IsStillborn(vs[above]) && !IsFloating(vs[above]))
{
if(fastrand()%15 == 0)
{
vs[same] = NOTHING;
return;
}
else
{
vs[same] = vs[above];
vs[above] = MOVEDSTEAM;
return;
}
}
break;
case MOVEDFIRE:
if(!IsBurnable(vs[above]) && fastrand()%10 == 0)
{
vs[same] = NOTHING;
return;
}
// Let the snowman melt!
if(fastrand()%4 == 0)
{
if (vs[above] == ICE)
{
vs[above] = WATER;
vs[same] = NOTHING;
}
if (vs[below] == ICE)
{
vs[below] = WATER;
vs[same] = NOTHING;
}
if (vs[first] == ICE)
{
vs[first] = WATER;
vs[same] = NOTHING;
}
if (vs[second] == ICE)
{
vs[second] = WATER;
vs[same] = NOTHING;
}
}
//Let's burn whatever we can!
index = 0;
switch(fastrand()%4)
{
case 0: index = above; break;
case 1: index = below; break;
case 2: index = first; break;
case 3: index = second; break;
}
if(IsBurnable(vs[index]))
{
if(BurnsAsEmber(vs[index]))
vs[index] = EMBER;
else
vs[index] = FIRE;
}
break;
case MOVEDWATER:
if(fastrand()%200 == 0 && vs[below] == IRONWALL)
vs[below] = RUST;
if(vs[below] == FIRE || vs[above] == FIRE || vs[first] == FIRE || vs[second] == FIRE)
vs[same] = MOVEDSTEAM;
//Making water+dirt into dirt
if(vs[below] == DIRT)
{
vs[below] = MOVEDMUD;
vs[same] = NOTHING;
}
if(vs[above] == DIRT)
{
vs[above] = MOVEDMUD;
vs[same] = NOTHING;
}
//Making water+salt into saltwater
if(vs[above] == SALT || vs[above] == MOVEDSALT)
{
vs[above] = MOVEDSALTWATER;
vs[same] = NOTHING;
}
if(vs[below] == SALT || vs[below] == MOVEDSALT)
{
vs[below] = MOVEDSALTWATER;
vs[same] = NOTHING;
}
if(fastrand()%60 == 0) //Melting ice
{
switch(fastrand()%4)
{
case 0: index = above; break;
case 1: index = below; break;
case 2: index = first; break;
case 3: index = second; break;
}
if(vs[index] == ICE)vs[index] = WATER; //--
}
break;
case MOVEDACID:
switch(fastrand()%4)
{
case 0: index = above; break;
case 1: index = below; break;
case 2: index = first; break;
case 3: index = second; break;
}
if(vs[index] != WALL && vs[index] != IRONWALL && vs[index] != WATER && vs[index] != MOVEDWATER && vs[index] != ACID && vs[index] != MOVEDACID) vs[index] = NOTHING; break;
break;
case MOVEDSALT:
if(fastrand()%20 == 0)
{
switch(fastrand()%4)
{
case 0: index = above; break;
case 1: index = below; break;
case 2: index = first; break;
case 3: index = second; break;
}
if(vs[index] == ICE)vs[index] = WATER; //--
}
break;
case MOVEDSALTWATER:
//Saltwater separated by heat
// if (vs[above] == FIRE || vs[below] == FIRE || vs[first] == FIRE || vs[second] == FIRE || vs[above] == STOVE || vs[below] == STOVE || vs[first] == STOVE || vs[second] == STOVE)
// {
// vs[same] = SALT;
// vs[above] = STEAM;
// }
if(fastrand()%40 == 0) //Saltwater dissolves ice more slowly than pure salt
{
switch(fastrand()%4)
{
case 0: index = above; break;
case 1: index = below; break;
case 2: index = first; break;
case 3: index = second; break;
}
if(vs[index] == ICE)vs[index] = WATER;
}
break;
case MOVEDOIL:
switch(fastrand()%4)
{
case 0: index = above; break;
case 1: index = below; break;
case 2: index = first; break;
case 3: index = second; break;
}
if(vs[index] == FIRE)
vs[same] = FIRE;
break;
default:
break;
}
//Peform 'realism' logic?
// When adding dynamics to this part please use the following structure:
// If a particle A is ligther than particle B then add vs[above] == B to the condition in case A (case MOVED_A)
if(implementParticleSwaps)
{
switch(type)
{
case MOVEDWATER:
if(vs[above] == SAND || vs[above] == MUD || vs[above] == SALTWATER && fastrand()%3 == 0)
{
vs[same] = vs[above];
vs[above] = type;
return;
}
break;
case MOVEDOIL:
if(vs[above] == WATER && fastrand()%3 == 0)
{
vs[same] = vs[above];
vs[above] = type;
return;
}
break;
case MOVEDSALTWATER:
if(vs[above] == DIRT || vs[above] == MUD || vs[above] == SAND && fastrand()%3 == 0)
{
vs[same] = vs[above];
vs[above] = type;
return;
}
break;
default:
break;
}
}
// The place below (x,y+1) is filled with something, then check (x+sign,y+1) and (x-sign,y+1)
// We chose sign randomly to randomly check eigther left or right
// This is for elements that fall downward
if (!IsFloating(type))
{
int firstdown = (x+sign)+((y+1)*WIDTH);
int seconddown = (x-sign)+((y+1)*WIDTH);
if ( vs[firstdown] == NOTHING)
{
vs[firstdown] = type;
vs[same] = NOTHING;
}
else if ( vs[seconddown] == NOTHING)
{
vs[seconddown] = type;
vs[same] = NOTHING;
}
//If (x+sign,y+1) is filled then try (x+sign,y) and (x-sign,y)
else if (vs[first] == NOTHING)
{
vs[first] = type;
vs[same] = NOTHING;
}
else if (vs[second] == NOTHING)
{
vs[second] = type;
vs[same] = NOTHING;
}
}
// Make steam move
else if(type == MOVEDSTEAM)
{
int firstup = (x+sign)+((y-1)*WIDTH);
int secondup = (x-sign)+((y-1)*WIDTH);
if ( vs[firstup] == NOTHING)
{
vs[firstup] = type;
vs[same] = NOTHING;
}
else if ( vs[secondup] == NOTHING)
{
vs[secondup] = type;
vs[same] = NOTHING;
}
//If (x+sign,y+1) is filled then try (x+sign,y) and (x-sign,y)
else if (vs[first] == NOTHING)
{
vs[first] = type;
vs[same] = NOTHING;
}
else if (vs[second] == NOTHING)
{
vs[second] = type;
vs[same] = NOTHING;
}
}
}
//Drawing a filled circle at a given position with a given radius and a given partice type
void DrawParticles(int xpos, int ypos, int radius, ParticleType type)
{
for (int x = ((xpos - radius - 1) < 0) ? 0 : (xpos - radius - 1); x <= xpos + radius && x < WIDTH; x++) {
for (int y = ((ypos - radius - 1) < 0) ? 0 : (ypos - radius - 1); y <= ypos + radius && y < HEIGHT; y++)
{
if ((x-xpos)*(x-xpos) + (y-ypos)*(y-ypos) <= radius*radius) vs[x+(WIDTH*y)] = type;
}
}
}
// Drawing a line
void DrawLine(int newx, int newy, int oldx, int oldy)
{
if(newx == oldx && newy == oldy)
{
DrawParticles(newx,newy,penSize,CurrentParticleType);
}
else
{
float step = 1.0f / ((abs(newx-oldx)>abs(newy-oldy)) ? abs(newx-oldx) : abs(newy-oldy));
for (float a = 0; a < 1; a+=step)
DrawParticles(a*newx+(1-a)*oldx,a*newy+(1-a)*oldy,penSize,CurrentParticleType);
}
}
// Updating a virtual pixel
inline void UpdateVirtualPixel(int x, int y)
{
ParticleType same = vs[x+(WIDTH*y)];
if(same != NOTHING)
{
if(IsStillborn(same))
StillbornParticleLogic(x,y,same);
else
if ( fastrand() >= FASTRAND_MAX / 13 && same % 2 == 0) MoveParticle(x,y,same); //THe rand condition makes the particles fall unevenly
}
}
// Updating the particle system (virtual screen) pixel by pixel
inline void UpdateVirtualScreen()
{
for(int y =0; y< HEIGHT-DASHBOARD_HEIGHT; y++)
{
// Due to biasing when iterating through the scanline from left to right,
// we now chose our direction randomly per scanline.
if (fastrand() % 2 == 0)
for(int x = WIDTH-2; x--;) UpdateVirtualPixel(x,y);
else
for(int x = 1; x < WIDTH - 1; x++) UpdateVirtualPixel(x,y);
}
}
//Cearing the particle system
void Clear()
{
for(int w = 0; w < WIDTH ; w++)
{
for(int h = 0; h < HEIGHT; h++)
{
vs[w+(WIDTH*h)] = NOTHING;
}
}
}
void InitButtons()
{
// Update dashboard
SDL_Rect dashboard ;
dashboard.x = 0;
dashboard.y = HEIGHT-DASHBOARD_HEIGHT;
dashboard.w = WIDTH;
dashboard.h = DASHBOARD_HEIGHT;
SDL_SetRenderDrawColor( renderer, 155, 155, 155, 255 );
SDL_RenderFillRect( renderer, &dashboard );
// ?? SDL_RenderPresent(_renderer);
//SDL_UpdateRect ( screen , dashboard.x , dashboard.y , dashboard.w , dashboard.h ) ;
//set up water emit button
SDL_Rect wateroutput ;
wateroutput.x = 1;
wateroutput.y = UPPER_ROW_Y;
wateroutput.w = BUTTON_SIZE;