-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
1706 lines (1451 loc) · 45.1 KB
/
Copy pathButton.cpp
File metadata and controls
1706 lines (1451 loc) · 45.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
#include <cstring>
#include <random>
#include <cstdarg>
#include "Button.h"
#include "assets.h"
Button* Button::pressed_button = NULL;
int Button::pressed_button_mouse_button = 0;
Button::Button(double tile_x_, double tile_y_, double tile_w_, double tile_h_) {
x = tile_x_;
y = tile_y_;
w = tile_w_;
h = tile_h_;
currently_active = was_just_activated = false;
collection = NULL;
}
bool Button::is_active() {
return true;
}
void Button::resize() {
}
void ButtonCollection::add(Button* new_button) {
if (new_button->collection != NULL) {
PRINT_DEBUG("a button can only be part of one collection");
throw std::exception("ButtonCollection::add()");
}
new_button->collection = this;
if (first == NULL) {
first = new Node(new_button);
last = first;
}
else {
last->next = new Node(new_button);
last = last->next;
}
}
void ButtonCollection::clear() {
is_cleared = true;
while (first != NULL) {
Node* node = first;
if (node->button == Button::pressed_button) {
Button::pressed_button = NULL;
}
delete node->button;
first = first->next;
delete node;
}
last = NULL;
}
std::ostream& operator<<(std::ostream& os, ButtonCollection::Node* node) {
if (node== NULL) {
os << ";\n";
}
else {
os << (void*)node << " -> ";
os << node->next;
}
return os;
}
void ButtonCollection::draw() {
Node* tmp;
#if SHOW_BUTTON_HITBOX
static int counter = 0;
#endif
for (Node* current = first; current != NULL; current = tmp) {
tmp = current->next;
if (current->button->is_active()) {
if (not current->button->currently_active) {
current->button->currently_active = true;
current->button->was_just_activated = true;
current->button->activate();
}
else
current->button->was_just_activated = false;
#if SHOW_BUTTON_HITBOX
counter++;
current->button->draw();
if (counter % 2)
game.draw_rect(Color::red, current->button->x, current->button->y, current->button->w, current->button->h);
#else
current->button->draw();
#endif
}
else
current->button->currently_active = false;
}
}
void ButtonCollection::resize() {
for (Node* current = first; current != NULL; current = current->next)
current->button->resize();
}
bool ButtonCollection::click(int mouse_button, double x, double y) {
is_cleared = false;
for (Node* current = first; current != NULL; current = current->next) {
if (current->button->is_active()) {
double x_rel = x - current->button->x; // x coordinate relative to the top left corner of the button
double y_rel = y - current->button->y; // y coordinate relative to the top left corner of the button
if (0 <= x_rel and 0 <= y_rel and x_rel < current->button->w and y_rel < current->button->h) { // check wether the x and y coordinates are withing the button rect
if (current->button->is_on_button(mouse_button, x_rel, y_rel)) {
current->button->effect(mouse_button, x_rel, y_rel);
if (not is_cleared) {
Button::pressed_button = current->button;
Button::pressed_button_mouse_button = mouse_button;
}
return true;
}
}
}
}
return false;
}
void Button::kill() {
if (this == Button::pressed_button) {
Button::pressed_button = NULL;
}
ButtonCollection::Node* previous = NULL;
ButtonCollection::Node* next = NULL;
for (ButtonCollection::Node* current = collection->first; current != NULL; previous = current, current = next) {
next = current->next;
if (current->button == this) {
if (previous == NULL) {
collection->first = current->next;
if (collection->first == NULL) {
collection->last = NULL;
}
}
else {
previous->next = current->next;
if (previous->next == NULL) {
collection->last = previous;
}
}
current->next = NULL;
delete current;
break;
}
}
}
BeginGameButton::BeginGameButton() : Button(7.5, 5.5, 3.0, 1.0) {
}
bool BeginGameButton::is_active() {
return not game.show_type_chart;
}
void BeginGameButton::draw() {
game.draw(start_button, 9.0, 6.0, center);
}
void BeginGameButton::effect(int, double, double) {
if (game.board.with_typing) {
if (game.board.with_random_battle) {
RandomTypingButton button = RandomTypingButton(0, 0);
game.type_selection = true;
button.effect(SDL_BUTTON_LEFT, 0, 0);
game.type_selection = false;
if (game.board.with_items)
button.effect(SDL_BUTTON_LEFT, 0, 0);
game.board.active_player = black;
game.type_selection = true;
button.effect(SDL_BUTTON_LEFT, 0, 0);
game.type_selection = false;
if (game.board.with_items)
button.effect(SDL_BUTTON_LEFT, 0, 0);
game.board.active_player = white;
game.board.with_check = false;
game.to_game();
}
else {
game.to_selection();
}
}
else {
game.board.with_check = true;
game.to_game();
}
}
EndOfGameButton::EndOfGameButton() : Button(7.5, 5.5, 3.0, 1.0) {
memset(message, '\0', 16);
}
void EndOfGameButton::activate() {
switch (game.board.winner) {
case white:
bg_color = Color::white;
txt_color = Color::black;
memcpy(message, "White Wins!", 12);
break;
case black:
bg_color = Color::black;
txt_color = Color::white;
memcpy(message, "Black Wins!", 12);
break;
case no_color:
bg_color = Color::grey;
txt_color = Color::black;
memcpy(message, "Draw", 5);
break;
}
}
void EndOfGameButton::draw() {
game.draw_rect(bg_color, x, y, w, h);
Surface txt_surface = CSM_font_array[35].render_shaded(message, txt_color, bg_color);
if (txt_surface != NULL)
game.draw(txt_surface, 9.0 - (double)txt_surface->w / 2.0 / TILE_SIZE, 6.0 - (double)txt_surface->h / 2.0 / TILE_SIZE);
}
void EndOfGameButton::effect(int mouse_button, double, double) {
if (mouse_button == SDL_BUTTON_LEFT)
game.to_menu();
}
ExitGameButton::ExitGameButton(double x_, double y_) : Button(x_, y_, 1.0, 1.0) {
}
bool ExitGameButton::is_active() {
return game.state != in_menu and game.state != end_of_game and game.state != in_settings;
}
void ExitGameButton::draw() {
game.draw(icon_sheet, x, y, 0, 0, 1, 1);
}
void ExitGameButton::effect(int mouse_button, double, double) {
if (mouse_button == SDL_BUTTON_LEFT)
game.to_menu();
}
/*PromotionButton::PromotionButton(std::function<Piece* (Board&, piece_color, Square*, typing, PokeItem)> constructor, double x_, double y_) :
Button(x_, y_, 1.0, 1.0), piece_constructor(constructor)
{
}*/
void PromotionButton::resize() {
activate();
}
void PromotionButton::activate() {
Piece piece = *game.board.promoting_piece;
piece.in_limbo = true;
piece.promote(game.board, promotion_id);
sprite = Surface::createRGBA(TILE_SIZE, TILE_SIZE);
piece.draw(game.board, sprite, NULL);
}
void PromotionButton::draw() {
game.draw(sprite, x, y);
}
void PromotionButton::effect(int mouse_button, double, double) {
if (mouse_button != SDL_BUTTON_LEFT)
return;
if (not game.is_a_bot[game.board.active_player])
game.promote(promotion_id);
}
TypingSelectionButton::TypingSelectionButton(double x_, double y_, typing type_) : Button(x_, y_, 1.0, 1.0) {
type = type_;
}
bool TypingSelectionButton::is_active() {
return game.type_selection and (game.is_type_avaible & (1 << type)) != 0;
}
void TypingSelectionButton::draw() {
game.draw(typing_icon[type], x, y);
}
void TypingSelectionButton::effect(int mouse_button, double x_, double y_){
if (mouse_button != SDL_BUTTON_LEFT)
return;
if (game.selected_type != typeless) {
game.is_type_avaible |= (1 << game.selected_type);
}
if ((game.is_type_avaible >> type) & 1) {
// the type is still avaible
if (not game.board.with_AG)
game.is_type_avaible &= ~(1 << type); // makes the typing no longer avaible
game.is_holding_something = true;
game.selected_type = type;
game.selected_thing_sprite = typing_icon[type].scale_to(TILE_SIZE / 2, TILE_SIZE / 2, 1);
game.selected_thing_sprite_x_offset = x_ / 2;
game.selected_thing_sprite_y_offset = y_ / 2;
game.phone_displayed_item = NO_ITEM;
game.phone_displayed_piece = NULL;
game.phone_displayed_type = type;
}
}
ItemSelectionButton::ItemSelectionButton(double x_, double y_, PokeItem item_) : Button(x_, y_, 0.5, 0.5), item(item_) {
}
bool ItemSelectionButton::is_active() {
return not game.type_selection and not game.unavaible_items.contains(item);
}
bool ItemSelectionButton::is_on_button(int mouse_button, double x, double y) {
return (mouse_button == SDL_BUTTON_LEFT) or (mouse_button == SDL_BUTTON_RIGHT);
}
void ItemSelectionButton::draw() {
SDL_Rect rect((int)(x * TILE_SIZE), (int)(y * TILE_SIZE), ITEM_SIZE, ITEM_SIZE);
item.draw(game.drawing_board, &rect);
}
Surface ItemSelectionButton::surface = Surface::createRGBA(ITEM_SIZE, ITEM_SIZE);
void ItemSelectionButton::effect(int mouse_click, double x_, double y_) {
if (game.selected_item) {
game.unavaible_items.erase(game.selected_item);
game.is_holding_something = false;
game.selected_item = NO_ITEM;
}
switch (mouse_click) {
case SDL_BUTTON_LEFT:
game.selected_item = item;
if (not game.board.with_AG)
game.unavaible_items.insert(item);
game.is_holding_something = true;
game.selected_thing_sprite_x_offset = x_;
game.selected_thing_sprite_y_offset = y_;
if (surface == NULL or surface->w != ITEM_SIZE) {
surface = Surface::createRGBA(ITEM_SIZE, ITEM_SIZE);
}
surface.clear();
SDL_Rect rect(0, 0, ITEM_SIZE, ITEM_SIZE);
item.draw(surface, &rect);
game.selected_thing_sprite = surface;
if (game.show_phone) {
game.phone_displayed_item = item;
game.phone_displayed_piece = NULL;
}
if (false and item.get_desc(game.language).first != NULL)
std::cout << item.get_desc(game.language).first << '\n';
break;
}
}
ConfirmSelectionButton::ConfirmSelectionButton(double x_, double y_) : Button(x_, y_, 1.0, 1.0) {}
void ConfirmSelectionButton::draw() {
game.draw(icon_sheet, x, y, 1, 0, 1, 1);
}
bool ConfirmSelectionButton::is_active() {
return game.nb_of_piece_with_type == 16;
}
void ConfirmSelectionButton::effect(int mouse_button, double, double) {
if (mouse_button != SDL_BUTTON_LEFT)
return;
game.is_type_avaible = 0xFFFFFFFF;
game.type_selection = true;
game.nb_of_piece_with_type = 0;
game.unavaible_items.clear();
game.phone_displayed_item = NO_ITEM;
game.phone_displayed_type = typeless;
game.phone_displayed_piece = NULL;
if ((game.board.active_player = -game.board.active_player) == white){
game.to_game();
}
}
RandomTypingButton::RandomTypingButton(double x_, double y_) : Button(x_, y_, 1.0, 1.0) {
counter = 0;
current_sprite = 0;
}
void RandomTypingButton::draw() {
counter++;
if (counter >= period) {
counter = 0;
current_sprite++;
current_sprite %= 16;
}
game.draw(unown_questionmark_animated, x, y, (double)current_sprite, 0.0, 1.0, 1.0);
}
void RandomTypingButton::effect(int mouse_button, double, double) {
game.is_type_avaible = 0xFFFFFFFF; // makes all the types avaible again
std::random_device rd;
#if USE_FIXED_RNG_SEED
std::mt19937 gen(game.board.active_player);
#else
std::mt19937 gen(rd());
#endif
if (game.type_selection) {
game.selected_type = typeless;
game.is_holding_something = false;
typing list[18];
if (game.board.with_AG) {
for (int i = 0; i < 16; i++) {
list[i] = (typing)(gen() % 18);
}
}
else {
iter_typing(t) {
list[t] = t;
}
std::shuffle(&list[normal], &list[fairy] + 1, gen);
}
if (game.board.active_player == white)
for (int i = 0; i < 16; i++) {
Square& square = game.board[i];
if (square.piece) {
square.piece->set_type(game.board, list[i]);
if (not game.board.with_AG)
game.is_type_avaible &= ~(1 << list[i]); // makes the type not avaible
}
}
else
for (int i = 0; i < 16; i++) {
Square& square = game.board[i + 48];
if (square.piece) {
square.piece->set_type(game.board, list[i]);
if (not game.board.with_AG)
game.is_type_avaible &= ~(1 << list[i]); // makes the type not avaible
}
}
game.nb_of_piece_with_type = 16;
}
else {
int pos = (game.board.active_player == white) ? 0 : 48;
int j = 16;
PokeItem list[ITEM_TABLE_LEN];
int current = 0;
for (PokeItem item : item_table) {
if (item)
list[current++] = item;
}
game.unavaible_items.clear();
number_of_drawed_terashard = number_of_drawed_resistance_berry = 0;
while (j > 0) {
Piece* piece = game.board[pos].piece;
std::shuffle(list, list + ITEM_TABLE_LEN, gen);
std::stable_sort(
list,
list + ITEM_TABLE_LEN,
[&piece]
(PokeItem x, PokeItem y) -> bool {
return x.usefulness_tier(game.board, piece) > y.usefulness_tier(game.board, piece);
}
);
for (PokeItem item : list) {
if (item and (game.board.with_RNG or not rng_dependant_items.contains(item)) and not game.unavaible_items.contains(item)) {
if (not game.board.with_AG)
game.unavaible_items.insert(item);
piece->set_item(game.board, item);
pos++;
j--;
break;
}
}
}
}
}
SwitchSelectionButton::SwitchSelectionButton(double x_, double y_) : Button(x_, y_, 3, 1) {
;
}
void SwitchSelectionButton::draw() {
const char* txt = "";
if (game.board.with_items) {
switch (game.language) {
case LANGUAGE::ENGLISH:
txt = (game.type_selection) ? "Items" : "Types";
break;
case LANGUAGE::FRENCH:
txt = (game.type_selection) ? "Objets" : "Types";
break;
case LANGUAGE::GERMAN:
txt = (game.type_selection) ? "Objekte" : "Typen";
break;
case LANGUAGE::SPANISH:
txt = (game.type_selection) ? "Objetos" : "Typos";
break;
}
game.draw(CSM_font_array[TILE_SIZE / 2].render_shaded(txt, Color::black, game.bg_color), x, y);
}
}
void SwitchSelectionButton::effect(int, double, double) {
if (not game.board.with_items) {
return;
}
game.type_selection = not game.type_selection;
if (game.type_selection) {
game.unavaible_items.erase(game.selected_item);
}
else {
if (game.selected_type != typeless) {
game.is_type_avaible |= (1 << game.selected_type);
}
}
game.is_holding_something = false;
}
TextBoxDisplay::TextBoxDisplay(const char* text, bool _is_first) : Button(5.0, 0.0, 8.0, textbox_frame->h * 8.0 / textbox_frame->w) {
sprite = textbox_frame.copy();
visual_index = text_index = 0;
timer = 0;
shift_timer = 0;
shift_delay = 0;
is_on_second_line = false;
x = 5.0;
side = black;
y = 2.0;
strcpy_s(message, text);
is_first = _is_first;
}
bool TextBoxDisplay::is_active() {
return is_first;
}
void TextBoxDisplay::activate() {
move_data const last_move_data = game.board.get_last_nonduck_move();
if (last_move_data.begin_pos > 31) {
y = 10;
side = white;
}
else {
y = 2;
side = black;
}
timer = duration;
sprite.blit(textbox_frame, NULL, NULL);
is_on_second_line = false;
visual_index = text_index = 0;
}
void TextBoxDisplay::destroy_all() {
if (next != NULL) {
next->destroy_all();
}
kill();
delete this;
}
void TextBoxDisplay::draw() {
if (shift_delay > 0) {
shift_delay--;
}
else if (shift_timer > 0) {
shift_timer--;
SDL_Rect dest(begin_x, begin_y, 0, 0);
SDL_Rect area(begin_x, begin_y + 4, char_per_line * char_width, char_width + y_pixel_increment);
sprite.blit(sprite, &dest, &area);
}
else {
char displayed_char = (text_index >= BUFFER_SIZE) ? '\0' : message[text_index];
if (displayed_char != '\0') {
if (displayed_char == '\n' or visual_index == char_per_line) {
visual_index = 0;
if (not is_on_second_line)
is_on_second_line = true;
else {
shift_delay = FPS / 4;
shift_timer = y_pixel_increment / 4;
}
}
else if (displayed_char < 128) {
SDL_Rect r;
r.x = begin_x + char_width * visual_index - (displayed_char == '\'') * 2;
r.y = begin_y + y_pixel_increment * is_on_second_line;
Surface char_surface = poke_charset.chop({ (displayed_char % 16) * char_width, (displayed_char / 16) * char_width, char_width, char_width });
char_surface.set_colorkey(char_surface.map_rgba(255, 255, 255, 255), true);
sprite.blit(char_surface, &r, NULL);
if (displayed_char != '\'')
visual_index++;
}
text_index++;
}
else {
timer--;
if (timer < 0) {
kill();
if (next == NULL) {
game.active_textbox = game.last_textbox = NULL;
}
else {
game.active_textbox = next;
game.active_textbox->is_first = true;
}
delete this;
return;
}
}
}
SDL_Rect dest = {5 * TILE_SIZE, 2 * TILE_SIZE, 8*TILE_SIZE, (int)(sprite->h * 8. * (double)TILE_SIZE / sprite->w)};
if (side == white) {
dest.y += 8 * TILE_SIZE - dest.h;
}
SDL_BlitSurfaceScaled(sprite, NULL, game.drawing_board, &dest, SDL_SCALEMODE_LINEAR);
}
TextBoxDisplay::~TextBoxDisplay() {
}
SkipBonusMoveButton::SkipBonusMoveButton(double x_, double y_) : Button(x_ - skip_button->w / 2.0 / TILE_SIZE, y_ - skip_button->h / 2.0 / TILE_SIZE, (double)skip_button->w / TILE_SIZE, (double)skip_button->h / TILE_SIZE) {
}
bool SkipBonusMoveButton::is_active() { return game.board.in_bonus_move and not game.is_a_bot[game.board.active_player]; }
void SkipBonusMoveButton::draw() {
game.draw(skip_button, x, y);
}
void SkipBonusMoveButton::effect(int mouse_button, double, double) {
game.skip_bonus_turn();
}
StatBoostDisplay::StatBoostDisplay(Piece* boosted_piece, pokestat boosted_stat, int delay_, int boost_or_debuff_) : Button(0.0, 0.0, 0.0, 0.0) {
piece = boosted_piece;
arrow_offset = 0;
stat = boosted_stat;
delay = delay_;
counter = TextBoxDisplay::duration;
boost_or_debuff = boost_or_debuff_;
}
void StatBoostDisplay::draw() {
if (delay > 0) {
delay--;
if (delay == 0) {
if (boost_or_debuff == 1) {
Mix_PlayChannel(1, stat_increase_effect, 0);
}
}
else {
return;
}
}
else {
counter--;
arrow_offset += 0.75 / 16.0;
if (counter < 0) {
kill();
delete this;
return;
}
}
if (delay > 0 or piece == NULL or piece->is_dead)
return;
Surface surface = game.drawing_board;
Surface arrow = boost_arrows.chop({ TILE_SIZE * (int)stat, 0, TILE_SIZE, TILE_SIZE }).inplace_toric_scroll(0, (int)(- arrow_offset * TILE_SIZE * boost_or_debuff));
if (surface.MUSTLOCK())
surface.lock();
Uint8* arrow_pixels = (Uint8*)arrow->pixels;
auto surface_format = SDL_GetPixelFormatDetails(surface.get_format());
auto arrow_format = SDL_GetPixelFormatDetails(arrow.get_format());
int arrow_pitch = arrow->pitch;
int surface_pitch = surface->pitch;
Uint32* pixels = (Uint32*)surface->pixels;
//Uint32* begin = &pixels[0];
Uint32* begin = &pixels[TILE_SIZE * ((7 - (piece->pos >> 3)) + 2) * surface_pitch/4 + TILE_SIZE * ((piece->pos & 0b111) + 5)];
Uint32* end = &pixels[TILE_SIZE * ((7 - (piece->pos >> 3)) + 3) * surface_pitch/4 + TILE_SIZE * ((piece->pos & 0b111) + 5)];
Uint32 bg_color = begin[0];
Uint32* row = begin;
Uint8 arrow_R;
Uint8 arrow_G;
Uint8 arrow_B;
Uint8 surface_R;
Uint8 surface_G;
Uint8 surface_B;
for (int y = 0; y < TILE_SIZE; y++) {
for (int x = 0; x < TILE_SIZE; x++) {
Uint32 surface_px;
if ((surface_px = row[x]) != bg_color) {
Uint8 arrow_px = arrow_pixels[y * arrow_pitch + x];
SDL_GetRGB(arrow_px, arrow_format, SDL_GetSurfacePalette(arrow), &arrow_R, &arrow_G, &arrow_B);
SDL_GetRGB(surface_px, surface_format, SDL_GetSurfacePalette(surface), &surface_R, &surface_G, &surface_B);
row[x] = SDL_MapRGB(surface_format, SDL_GetSurfacePalette(surface),
(Uint8)(arrow_R * 0.5 + surface_R * 0.5),
(Uint8)(arrow_G * 0.5 + surface_G * 0.5),
(Uint8)(arrow_B * 0.5 + surface_B * 0.5));
}
}
row += surface_pitch/4;
}
if (surface.MUSTLOCK())
surface.unlock();
}
ShowTypechartButton::ShowTypechartButton(double x_, double y_) : Button(x_, y_, 1.0, 1.0) {
int a = (TILE_SIZE - 19) / 18;
resize();
}
bool ShowTypechartButton::is_active() {
return game.board.with_typing and not (game.state == in_settings);
}
void ShowTypechartButton::resize() {
int a = (TILE_SIZE - 19) / 18;
sprite = Surface::createRGBA(18 * a + 19, 18 * a + 19);
sprite.fill(Color::black);
SDL_Rect r;
r.w = r.h = a;
r.x = 1;
iter_typing(i) {
r.y = 1;
iter_typing(j) {
switch (typechart[j][i]) {
case neutral:
SDL_FillSurfaceRect(sprite, &r, 0);
break;
case super_effective:
SDL_FillSurfaceRect(sprite, &r, Color::green);
break;
case not_very_effective:
SDL_FillSurfaceRect(sprite, &r, Color::red);
break;
case immune:
SDL_FillSurfaceRect(sprite, &r, Color::black);
break;
}
r.y += a + 1;
}
r.x += a + 1;
}
}
void ShowTypechartButton::draw() {
game.draw(sprite, x, y);
}
void ShowTypechartButton::effect(int mouse_button, double, double) {
if (mouse_button == SDL_BUTTON_LEFT)
game.show_type_chart = not game.show_type_chart;
}
TypechartDisplay::TypechartDisplay() : Button(5.0, 2.0, 8.0, 8.0) {
;
}
bool TypechartDisplay::is_active() {
return game.show_type_chart;
}
void TypechartDisplay::draw() {
int a = TILE_SIZE * 8 / 19;
int cx = (TILE_SIZE * 8 - 19 * a) / 2;
int cy = cx;
SDL_Rect rect;
game.draw(CSM_font_array[a].render_shaded("Offensive Typing", Color::black, game.bg_color).rotate90(), 5.0, 6.0, middle_right);
game.draw(CSM_font_array[a].render_shaded("Defensive Typing", Color::black, game.bg_color), 9.0, 2.0, bottom_middle);
game.drawing_board.draw_disk(5 * TILE_SIZE + a / 2, 10 * TILE_SIZE + 11 * a / 4, 3 * a / 8, Color::black);
rect.x = 5 * TILE_SIZE + a;
rect.y = 10 * TILE_SIZE + 2 * a;
game.drawing_board.blit(CSM_font_array[7 * a / 8].render_shaded("immune", Color::black, game.bg_color), &rect);
game.drawing_board.draw_disk(5 * TILE_SIZE + a / 2, 10 * TILE_SIZE + 7 * a / 4, 3 * a / 8, Color::red);
rect.x = 5 * TILE_SIZE + a;
rect.y = 10 * TILE_SIZE + a;
game.drawing_board.blit(CSM_font_array[7 * a / 8].render_shaded("not very effective", Color::black, game.bg_color), &rect);
game.drawing_board.draw_disk(5 * TILE_SIZE + a / 2, 10 * TILE_SIZE + 3 * a / 4, 3 * a / 8, Color::green);
rect.x = 5 * TILE_SIZE + a;
rect.y = 10 * TILE_SIZE;
game.drawing_board.blit(CSM_font_array[7 * a / 8].render_shaded("super effective", Color::black, game.bg_color), &rect);
game.draw_rect(Color::silver, 5.0, 2.0, 8.0, 8.0);
for (typing t = normal; t <= fairy; t++) {
Surface icon = typing_icon[t].scale_to(a, a, true);
SDL_Rect r(5 * TILE_SIZE + (t+1) * a + cx, 2*TILE_SIZE + cy, a, a);
game.drawing_board.blit(icon, &r);
r.w = 1;
r.h = 19 * a;
SDL_FillSurfaceRect(game.drawing_board, &r, Color::black);
r.x = 5 * TILE_SIZE + cx;
r.y += (t + 1) * a;
game.drawing_board.blit(icon, &r);
r.h = 1;
r.w = 19 * a;
SDL_FillSurfaceRect(game.drawing_board, &r, Color::black);
r.x = cx + a + a/2 + 5 * TILE_SIZE;
r.y = cx + a * (t + 1) + a/2 + 2 * TILE_SIZE;
for (typing j = normal; j <= fairy; j++) {
Uint32 color = 0;
switch (typechart[t][j]) {
case immune:
color = Color::black;
break;
case not_very_effective:
color = Color::red;
break;
case neutral:
color = 0;
break;
case super_effective:
color = Color::green;
break;
}
if (color != 0)
game.drawing_board.draw_disk(r.x, r.y, 3*a/8, color);
r.x += a;
}
}
}
SettingsButton::SettingsButton(double x_, double y_) : Button(x_, y_, 1.0, 1.0) {
current_sprite = 0;
counter = 0;
}
void SettingsButton::draw() {
counter++;
if (counter >= period) {
counter = 0;
current_sprite++;
current_sprite %= 16;
}
game.draw(clink_animated, x, y, (double)current_sprite, 0.0, 1.0, 1.0);
}
void SettingsButton::effect(int, double, double) {
if (game.state == in_settings) {
game.exit_settings();
}
else {
game.to_settings();
}
}
DisableSoundsButton::DisableSoundsButton(double x_, double y_) : Button(x_, y_, 3.0, 1.0) {
;
}
void DisableSoundsButton::draw() {
game.draw(CSM_font_array[TILE_SIZE / 2].render_shaded("Sounds: ", Color::black, game.bg_color), x, y+0.5, middle_left);
game.draw(megaphone_img, x+2.0, y);
game.draw(icon_sheet, x+2.0, y, game.with_sounds, 0, 1, 1);
}
bool DisableSoundsButton::is_on_button(int mouse_button, double x_, double y_) {
return mouse_button == SDL_BUTTON_LEFT and x_ >= 2.0;
}
void DisableSoundsButton::effect(int mouse_button, double, double) {
game.with_sounds = not game.with_sounds;
Mix_VolumeMusic(game.with_sounds ? game.music_volume : 0);
Mix_Volume(-1, game.with_sounds ? game.music_volume : 0);
}
VolumeSlider::VolumeSlider(double x_, double y_) : Button(x_, y_, 6.5, 1.0) {
}
bool VolumeSlider::is_on_button(int mouse_button, double x_, double y_) {
return true;
}
void VolumeSlider::draw() {
game.draw(CSM_font_array[TILE_SIZE / 2].render_shaded("Volume:", Color::black, game.bg_color), x, y+0.5, middle_left);
game.draw_rect(Color::silver, x + 2.25, y+1./3., 2.5, 1./3.);
double rate = (double)game.music_volume / 128.0;
game.draw_rect(Color::black, x + 2.0 + rate * 2.5, y + 0.25, 0.5, 0.5);
game.draw(CSM_font_array[TILE_SIZE / 2].render_shaded((std::to_string((int)(rate * 100)) += '%').c_str(), Color::black, game.bg_color), x + 5.1, y + 0.5, middle_left);
}
double clamp(double x, double min, double max) {
return x < min ? min : x > max ? max : x;
}
void VolumeSlider::hold(int mouse_button, double x_, double) {
game.music_volume = (short)(128 * (clamp(x_-2.25, 0.0, 2.5) / 2.5));
if (game.with_sounds) {
Mix_VolumeMusic(game.music_volume);
Mix_Volume(-1, game.music_volume);
}
}
BoardButton::BoardButton(Board& b) : board(b), Button(5, 2, 8, 8) {
is_first_unhold = true;
no_unclick = false;
}
bool BoardButton::is_active() {
return game.state != in_settings;
}
void BoardButton::draw() {
//game.draw(board.surface, x, y, top_left);
int pos = 0;
for (Square& square : board) {
square.draw(game, game.board, pos);
pos++;
}
}
void BoardButton::effect(int mouse_button, double x, double y) {
int tile_x = (int)x;
int tile_y = 7 - (int)y;
int pos = tile_x | (tile_y << 3);
Square& selected_square = game.board[pos];
Piece* piece = selected_square.piece;
switch (game.state) {
case in_game:
if ((game.accessible_mask & get_square_mask(pos)) and game.selected_piece != NULL) {
game.move_selected_piece_to(pos);
}
else {
if (game.selected_piece != NULL and game.selected_piece == piece) {