-
Notifications
You must be signed in to change notification settings - Fork 1
/
wx_main.cpp
1904 lines (1623 loc) · 58.2 KB
/
wx_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
#include<unordered_map>
#include<ctime>
#include<iostream>
#include<sstream>
#include<memory>
#include<mutex>
#include<deque>
#include "wx/wx.h"
#include "wx/defs.h"
#include "wx/sizer.h"
#include "wx/graphics.h"
#include "wx/filename.h"
#include "wx/imagpng.h"
#include "wx/popupwin.h"
#include "wx/wxprec.h"
#include "wx/cmdline.h"
#include "game_api.h"
#include "sqlite_saved_state.h"
#include "saved_state_db_c_api.h"
#include "c_dictionary.h"
#include "wx_game_popup.h"
#include "wx_network_ui.h"
#include "wx_network.h"
#define DEFAULT_PORT (55345)
#define ALEX_CUSTOM_MSG_ID (10100)
#define ARY_LEN(x) ( sizeof(x) / sizeof((x)[0]) )
#define NOT_IMPL() do {\
fprintf(stderr, "%s not implemented %s:%d\n", __func__, __FILE__, __LINE__); \
} while (0) \
static void wx_set_game_handle(const void *L, const char *game_id);
static void wx_get_game_id(const void *L, char *game_id_out, size_t game_id_out_max_len);
static void wx_draw_graphic(const char *img_id,
int y, int x,
int width, int height,
const struct draw_graphic_params *params);
static void wx_draw_line(const char *colour_str, int line_size, int y1, int x1, int y2, int x2);
static void wx_draw_text(const char *text_str, size_t text_str_len,
const char *colour_str, size_t colour_str_len,
int y, int x, int size, int align);
static void wx_draw_rect(const char *fill_colour_str, size_t fill_colour_len,
int y_start, int x_start,
int y_end , int x_end);
static void wx_draw_triangle(const char *fill_colour_str, size_t fill_colour_len,
int y1, int x1,
int y2, int x2,
int y3, int x3);
static void wx_draw_circle(const char *fill_colour_str, size_t fill_colour_len,
const char *outline_colour_str, size_t outline_colour_len,
int y, int x, int radius, int outline_width);
static void wx_draw_clear(void);
static void wx_draw_refresh(void);
static void wx_send_message(const char *dst, size_t dst_len, const char *msg, size_t msg_len);
static void wx_create_btn(const char *btn_id_str, const char *btn_text_str, int weight);
static void wx_set_btn_enabled(const char *btn_id_str, bool enabled);
static void wx_set_btn_visible(const char *btn_id_str, bool visible);
static void wx_hide_popup(void);
static void wx_add_game_option(const char *option_id, const struct option_info *info);
static void wx_set_status_msg(const char *msg, size_t msg_len);
static void wx_set_status_err(const char *msg, size_t msg_len);
//static void wx_show_popup(const char *popup_id, size_t popup_id_str_len,
// const char *title, size_t title_len,
// const char *msg, size_t msg_len,
// const char * const *btn_str_ary, size_t ary_len);
static void wx_show_popup(void *L, const char *popup_id, size_t popup_id_str_len,
const struct popup_info *info);
static void wx_prompt_string(const char *prompt_title, size_t prompt_title_len,
const char *prompt_msg, size_t prompt_msg_len);
static int wx_update_timer_ms(int update_period_ms);
static void wx_delete_timer(int timer_handle);
static void wx_enable_evt(const char *evt_id_str, size_t evt_id_len);
static void wx_disable_evt(const char *evt_id_str, size_t evt_id_len);
static time_ms_t wx_get_time_ms(void);
static size_t wx_get_time_of_day(char *time_str, size_t max_time_str_len);
static void wx_store_data(void *L, const char *key, const uint8_t *value, size_t value_len);
static size_t wx_read_stored_data(void *L, const char *key, uint8_t *value_out, size_t max_val_len);
static int wx_get_new_session_id(void);
static int wx_get_last_session_id(const char *game_id);
static void wx_save_state(int session_id, const uint8_t *state, size_t state_len);
static bool wx_has_saved_state_offset(int session_id, int move_id_offset);
static int wx_get_saved_state_offset(int session_id, int move_id_offset, uint8_t *state_out, size_t max_sate_out);
static void wx_draw_extra_canvas(const char *img_id,
int y, int x,
int width, int height);
static void wx_new_extra_canvas(const char *canvas_id);
static void wx_set_active_canvas(const char *canvas_id);
static void wx_delete_extra_canvases(void);
static size_t wx_get_user_colour_pref(char *colour_pref_out, size_t max_colour_pref_out_len);
static bool wx_is_feature_supported(const char *feature_id, size_t feature_id_len);
static void wx_draw_graphic_internal(const char *img_id, int y, int x, int width, int height, const struct draw_graphic_params *params);
static void wx_draw_rect_internal(const char *fill_colour_str, size_t fill_colour_len,
int y_start, int x_start,
int y_end , int x_end);
static void wx_draw_text_internal(const char *text_str, size_t text_str_len,
const char *colour_str, size_t colour_str_len,
int y, int x, int size, int align);
static void wx_draw_circle_internal(const char *fill_colour_str, size_t fill_colour_len,
const char *outline_colour_str, size_t outline_colour_len,
int y, int x, int radius);
static void wx_draw_triangle_internal(const char *fill_colour_str, size_t fill_colour_len,
int y1, int x1,
int y2, int x2,
int y3, int x3);
static void wx_draw_line_internal(const char *colour_str, int line_width,
int y1, int x1,
int y2, int x2);
static void wx_destroy_all(void);
struct img_info {
const char *img_id;
const char *img_path;
};
// TODO need to convert these images to bmp
// does that mean no transparency?
static const struct img_info IMAGES_TABLE[] = {
{ "board" , "img/wooden_board.png" },
{ "piece_black" , "img/black_piece.png" },
{ "piece_white" , "img/white_piece.png" },
{ "piece_highlight" , "img/piece_highlight.png" },
{ "piece_king_icon" , "img/piece_king_icon.png" },
{ "card_diamonds" , "img/cards/diamonds.png" },
{ "card_hearts" , "img/cards/hearts.png" },
{ "card_spades" , "img/cards/spades.png" },
{ "card_clubs" , "img/cards/clubs.png" },
{ "card_blank" , "img/cards/blank_card.png" },
{ "card_facedown" , "img/cards/card_facedown.png" },
{ "card_highlight" , "img/cards/card_highlight.png" },
{ "more_info_btn" , "img/more_info_btn.png" },
{ "minesweeper_mine" , "img/minesweeper/mine.png" },
{ "minesweeper_box1" , "img/minesweeper/box1.png" },
{ "minesweeper_box2" , "img/minesweeper/box2.png" },
{ "minesweeper_box3" , "img/minesweeper/box3.png" },
{ "minesweeper_box4" , "img/minesweeper/box4.png" },
{ "minesweeper_box5" , "img/minesweeper/box5.png" },
{ "minesweeper_box6" , "img/minesweeper/box6.png" },
{ "minesweeper_box7" , "img/minesweeper/box7.png" },
{ "minesweeper_box8" , "img/minesweeper/box8.png" },
{ "minesweeper_box_unclicked" , "img/minesweeper/box_unclicked.png" },
{ "minesweeper_box_empty" , "img/minesweeper/box_empty.png" },
{ "minesweeper_box_flagged_red" , "img/minesweeper/box_flagged_red.png" },
{ "minesweeper_box_flagged_blue" , "img/minesweeper/box_flagged_blue.png" },
{ "space_ship1" , "img/space/ship1.png" },
{ "hospital_ui_dirpad" , "img/hospital/ui/dirpad.png" },
};
static const struct game_api_callbacks api = {
wx_set_game_handle,
wx_get_game_id,
wx_draw_graphic,
wx_draw_line,
wx_draw_text,
wx_draw_rect,
wx_draw_triangle,
wx_draw_circle,
wx_draw_clear,
wx_draw_refresh,
wx_send_message,
wx_create_btn,
wx_set_btn_enabled,
wx_set_btn_visible,
wx_hide_popup,
wx_add_game_option,
wx_set_status_msg,
wx_set_status_err,
wx_show_popup,
wx_prompt_string,
wx_update_timer_ms,
wx_delete_timer,
wx_enable_evt,
wx_disable_evt,
wx_get_time_ms,
wx_get_time_of_day,
wx_store_data,
wx_read_stored_data,
wx_get_new_session_id,
wx_get_last_session_id,
wx_save_state,
wx_has_saved_state_offset,
wx_get_saved_state_offset,
wx_draw_extra_canvas,
wx_new_extra_canvas,
wx_set_active_canvas,
wx_delete_extra_canvases,
wx_get_user_colour_pref,
wx_is_feature_supported,
wx_destroy_all,
};
class AlexGamesTimer;
std::mutex mutex;
void *L;
static char g_game_id[256];
static bool g_is_dark_mode = false;
static int g_timer_handle_idx = 1;
static std::unordered_map<int, AlexGamesTimer*> g_timers;
static unsigned long g_last_draw_board = 0;
static void *g_saved_state = nullptr;
static void *g_db_state_handler = NULL;
static bool g_key_enabled = false;
static bool g_mousemove_enabled = false;
static bool g_mouse_updown_enabled = false;
static void wx_mutex_take() {
mutex.lock();
}
static void wx_mutex_release() {
mutex.unlock();
}
std::unordered_map<std::string, wxBitmap> images_map(ARY_LEN(IMAGES_TABLE));
class MyFrame;
class MyCanvas: public wxScrolledWindow {
public:
MyCanvas( MyFrame *parent );
void OnPaint(wxPaintEvent &event);
void OnMouseMove(wxMouseEvent &event);
void OnMouseDown(wxMouseEvent &event);
void OnMouseUp(wxMouseEvent &event);
void handle_key_down_evt(wxKeyEvent &event);
void handle_key_up_evt(wxKeyEvent &event);
void ReDrawIfNeeded(void);
//wxMemoryDC mdc;
wxBitmap bmp;
bool is_drawing = false;
private:
bool meta_down = false;
bool alt_down = false;
wxDECLARE_EVENT_TABLE();
};
class MyApp : public wxApp {
public:
virtual bool OnInit();
virtual void OnInitCmdLine(wxCmdLineParser& parser);
virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
};
static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
{ wxCMD_LINE_SWITCH, "h", "help", "Prints help message.",
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
{ wxCMD_LINE_OPTION, "g", "game", "Chooses game. Run this program without any arguments to list available games.",
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_NONE },
};
enum {
MENU_ITEM_START_GAME = wxID_HIGHEST,
MENU_ITEM_GAME_SELECT,
MENU_ITEM_NETWORK_SETTINGS,
};
class MyFrame : public wxFrame {
public:
MyFrame(const wxString& title);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void StartGame(wxCommandEvent &event);
void StartGame();
void GameSelect(wxCommandEvent &event);
void GameSelect();
void ShowNetworkSettings();
void ShowNetworkSettings(wxCommandEvent &event);
MyCanvas *canvas;
wxTextCtrl statusText;
void new_button(const char *btn_id_str, const char *btn_text_str, int weight);
void set_btn_enabled(const char *btn_id_str, bool is_enabled);
void handle_msg_recvd(wxCommandEvent &evt);
//void handle_key_evt(wxKeyEvent &evt);
private:
wxBoxSizer *sizer;
wxDECLARE_EVENT_TABLE();
std::unordered_map<std::string, wxButton*> buttons;
wxBoxSizer *button_sizer;
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(MENU_ITEM_START_GAME, MyFrame::StartGame)
EVT_MENU(MENU_ITEM_GAME_SELECT, MyFrame::GameSelect)
EVT_MENU(MENU_ITEM_NETWORK_SETTINGS, MyFrame::ShowNetworkSettings)
//EVT_KEY_DOWN(MyFrame::handle_key_evt)
EVT_COMMAND(ALEX_CUSTOM_MSG_ID, wxEVT_COMMAND_TEXT_UPDATED, MyFrame::handle_msg_recvd)
wxEND_EVENT_TABLE()
class MyCanvas;
MyFrame *g_frame = nullptr;
NetworkPopup *g_network_popup = nullptr;
GamePopup *g_popup = nullptr;
//wxDialog *g_popup = nullptr;
MyCanvas *g_canvas = nullptr;
class MyPopup : public wxDialog {
public:
MyPopup(wxFrame *parent, wxString popup_id_arg, wxString title, wxString text,
const char * const * btn_txts, int btn_count) :
wxDialog(parent, wxID_ANY, title,
wxDefaultPosition, wxSize(300, 400),
wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP),
popup_id(popup_id_arg),
txtTitle(this, wxID_ANY, title),
txtBody(this, wxID_ANY, text),
buttons(btn_count) {
this->sizer = new wxBoxSizer(wxVERTICAL);
//txtTitle.AppendText(title);
sizer->Add(&txtTitle, 0);
sizer->Add(&txtBody, 0);
for (int i=0; i<btn_count; i++) {
buttons[i] = new wxButton(this, wxID_ANY, wxString(btn_txts[i]));
/*
buttons[i]->Connect(wxID_ANY,
wxEVT_COMMAND_BUTTON_CLICKED,
wxObjectEventFunction([this, i](wxEvent &evt) {
this->handle_button_clicked(i);
}));
*/
//function <void (wxCommandEvent &)> clickHandler( bind(
struct clickHandler {
public:
clickHandler(MyPopup *inst, int i) {
this->inst = inst;
this->i = i;
}
void operator()(wxCommandEvent &) const {
inst->handle_button_clicked(i);
}
private:
int i;
MyPopup *inst;
};
struct clickHandler handler(this, i);
buttons[i]->Bind(wxEVT_COMMAND_BUTTON_CLICKED, handler, wxID_ANY);
sizer->Add(buttons[i], 0);
}
//sizer.Layout();
this->SetSizer(sizer);
}
private:
wxStaticText txtTitle;
wxStaticText txtBody;
wxBoxSizer *sizer;
std::vector<wxButton*> buttons;
wxString popup_id;
void handle_button_clicked(int i) {
printf("popup button %d clicked\n", i);
// TODO pass popup state as last parameter once dropdowns are implemented
game_api->handle_popup_btn_clicked(L, popup_id.c_str(), i, nullptr);
g_canvas->ReDrawIfNeeded();
}
};
IMPLEMENT_APP(MyApp)
class Drawable {
public:
virtual void draw(void *L) = 0;
};
#if 0
class DrawableFromLambda : Drawable {
public:
DrawableFromLambda(void (*draw_lambda)(void *L)) {
this->draw_lambda = draw_lambda;
}
virtual void draw(void *L) {
}
private:
void (*draw_lambda)(void *L);
};
#endif
class DrawGraphicDrawable : public Drawable {
public:
DrawGraphicDrawable(const char *img_id, int y, int x, int height, int width, const struct draw_graphic_params *params) {
this->img_id = img_id;
this->y = y;
this->x = x;
this->height = height;
this->width = width;
this->params = *params;
}
virtual void draw(void *L) {
wx_draw_graphic_internal(img_id.c_str(), y, x, height, width, ¶ms);
};
private:
std::string img_id;
int y, x;
int height, width;
struct draw_graphic_params params;
};
class DrawRectDrawable : public Drawable {
public:
DrawRectDrawable(const char *fill_colour_str, size_t fill_colour_len,
int y_start, int x_start,
int y_end , int x_end) {
this->fill_colour = fill_colour_str;
this->y_start = y_start;
this->x_start = x_start;
this->y_end = y_end;
this->x_end = x_end;
}
virtual void draw(void *L) {
wx_draw_rect_internal(fill_colour.c_str(), fill_colour.length(),
y_start, x_start,
y_end, x_end);
}
private:
std::string fill_colour;
int y_start, x_start;
int y_end, x_end;
};
class DrawTextDrawable : public Drawable {
public:
DrawTextDrawable (const char *text_str,
const char *colour_str,
int y, int x, int size, int align) {
this->text_str = text_str;
this->colour_str = colour_str;
this->y = y;
this->x = x;
this->size = size;
this->align = align;
}
virtual void draw(void *L) {
wx_draw_text_internal(text_str.c_str(), text_str.length(),
colour_str.c_str(), colour_str.length(),
y, x, size, align);
}
private:
std::string text_str;
std::string colour_str;
int y, x, size, align;
};
class DrawCircleDrawable : public Drawable {
public:
DrawCircleDrawable(const char *fill_str, const char *outline_str,
int y, int x, int radius) {
this->fill = fill_str;
this->outline = outline_str;
this->y = y;
this->x = x;
this->radius = radius;
}
virtual void draw(void *L) {
wx_draw_circle_internal(fill.c_str(), fill.size(), outline.c_str(), outline.size(), y, x, radius);
}
private:
std::string fill;
std::string outline;
int y, x, radius;
};
class DrawLineDrawable : public Drawable {
public:
DrawLineDrawable(const char *colour_str, int line_size, int y1, int x1, int y2, int x2) {
this->colour = colour_str;
this->line_size = line_size;
this->y1 = y1;
this->x1 = x1;
this->y2 = y2;
this->x2 = x2;
}
virtual void draw(void *L) {
wx_draw_line_internal(colour.c_str(), line_size, y1, x1, y2, x2);
}
private:
std::string colour;
int line_size;
int y1, x1, y2, x2;
};
class DrawTriangleDrawable : public Drawable {
public:
DrawTriangleDrawable(const char *fill_str, size_t fill_str_len,
int y1, int x1,
int y2, int x2,
int y3, int x3) {
this->fill = fill_str;
this->y1 = y1;
this->x1 = x1;
this->y2 = y2;
this->x2 = x2;
this->y3 = y3;
this->x3 = x3;
}
virtual void draw(void *L) {
wx_draw_triangle_internal(fill.c_str(), fill.size(), y1, x1, y2, x2, y3, x3);
}
private:
std::string fill;
int y1, x1;
int y2, x2;
int y3, x3;
};
// TODO how do you allow the queue to automatically handle freeing these?
std::deque<Drawable*> drawable_queue;
ServerThread *g_server_thread = nullptr;
ClientThread *g_client_thread = nullptr;
static int hex_char_to_int(char c) {
if ('0' <= c && c <= '9') {
return c - '0';
} else if ('a' <= c && c <= 'f') {
return c - 'a' + 10;
} else if ('A' <= c && c <= 'F') {
return c - 'A' + 10;
} else {
fprintf(stderr, "invalid hex char %c (%x)\n", c, c);
return -1;
}
}
static char hex_char2_to_byte(char c1, char c2) {
return (hex_char_to_int(c1)<<4) | hex_char_to_int(c2);
}
static wxColour colour_str_to_wxColour(const char *colour_str) {
if (colour_str == nullptr) {
fprintf(stderr, "colour_str is null\n");
return wxNullColour;
}
if (colour_str[0] == '#') {
colour_str++;
}
int len = strlen(colour_str);
if (len == 3) {
return wxColour(hex_char_to_int(colour_str[0])*16,
hex_char_to_int(colour_str[1])*16,
hex_char_to_int(colour_str[2])*16);
} else if (len == 6) {
return wxColour(hex_char2_to_byte(colour_str[0], colour_str[1]),
hex_char2_to_byte(colour_str[2], colour_str[3]),
hex_char2_to_byte(colour_str[4], colour_str[5]));
} else if (len == 8) {
return wxColour(hex_char2_to_byte(colour_str[0], colour_str[1]),
hex_char2_to_byte(colour_str[2], colour_str[3]),
hex_char2_to_byte(colour_str[4], colour_str[5]),
hex_char2_to_byte(colour_str[6], colour_str[7]));
} else {
fprintf(stderr, "invalid colour len %d\n", len);
return wxNullColour;
}
}
static wxPen colour_str_to_wxPen(const char *colour_str, int width=1) {
return wxPen(colour_str_to_wxColour(colour_str), width);
}
static void wx_set_game_handle(const void *L, const char *game_id) {
// TODO
NOT_IMPL();
}
static void wx_get_game_id(const void *L, char *game_id_out, size_t game_id_out_max_len) {
strncpy(game_id_out, g_game_id, game_id_out_max_len);
}
static void wx_draw_graphic(const char *img_id,
int y, int x,
int width, int height,
const struct draw_graphic_params *params) {
if (g_canvas == nullptr) { return; }
Drawable *drawable = new DrawGraphicDrawable(img_id, y, x, width, height, params);
drawable_queue.push_back(drawable);
}
static void wx_draw_graphic_internal(const char *img_id, int y, int x, int width, int height, const struct draw_graphic_params *params) {
//wxPaintDC dc(g_canvas);
wxMemoryDC dc;
dc.SelectObject(g_canvas->bmp);
#if 0
std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
//wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
if (gc.get() == nullptr) {
fprintf(stderr, "could not create graphics context?\n");
return;
}
#endif
if (images_map.find(img_id) == images_map.end()) {
fprintf(stderr, "could not find image_id \"%s\"\n", img_id);
return;
}
wxBitmap &bmp = images_map.at(img_id);
//if (!bmp.isOk()) {
if (false) {
fprintf(stderr, "bmp %s is not OK\n", img_id);
return;
}
//printf("drawing graphic \"%s\"\n", img_id);
// TODO cache these reszied bitmaps?
// It seems like a waste to do this every time,
// most of the time the same resized images are drawn
wxImage image = bmp.ConvertToImage();
image.Rescale(width, height);
wxBitmap bmp_resized(image, 1);
// TODO need to handle rotation! (params->angle_degrees)
// In "31s" at least, the other player's cards should be rotated 180 degrees
// about their top left corner (I think)
// TODO need to handle params->flip_y and flip_x
//dc.DrawBitmap(bmp_resized, x - width/2, y - height/2);
dc.DrawBitmap(image, x - width/2, y - height/2);
//g_canvas->Refresh();
if (!g_canvas->is_drawing) {
g_canvas->Update();
}
}
static void wx_draw_line(const char *colour_str, int line_width,
int y1, int x1,
int y2, int x2) {
Drawable *d = new DrawLineDrawable(colour_str, line_width, y1, x1, y2, x2);
drawable_queue.push_back(d);
}
int clip(int val, int l_lim, int r_lim) {
if (val < l_lim) { return l_lim; }
if (val > r_lim) { return r_lim; }
return val;
}
static void wx_draw_line_internal(const char *colour_str, int line_width,
int y1, int x1,
int y2, int x2) {
if (g_canvas == nullptr) { return; }
//wxPaintDC dc(g_canvas);
//printf("using memory dc?\n");
//wxMemoryDC &dc = g_canvas->mdc;
wxMemoryDC dc;
dc.SelectObject(g_canvas->bmp);
wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
if (gc == nullptr) {
fprintf(stderr, "could not create graphics context?\n");
return;
}
gc->SetPen(colour_str_to_wxPen(colour_str, line_width));
//gc->SetBrush(colour_str_to_wxColour(colour_str)); // TODO I just added this to test, is it right?
//gc->SetBrush(wxBrush("pink"));
wxGraphicsPath path = gc->CreatePath();
path.MoveToPoint(x1, y1);
path.AddLineToPoint(x2, y2);
gc->StrokePath(path);
delete gc;
g_canvas->Refresh();
//g_canvas->Update();
}
static void wx_draw_text(const char *text_str, size_t text_str_len,
const char *colour_str, size_t colour_str_len,
int y, int x, int size, int align) {
Drawable *d = new DrawTextDrawable(text_str, colour_str, y, x, size, align);
drawable_queue.push_back(d);
}
static void wx_draw_text_internal(const char *text_str, size_t text_str_len,
const char *colour_str, size_t colour_str_len,
int y, int x, int size, int align) {
wxMemoryDC dc;
dc.SelectObject(g_canvas->bmp);
wxFont font(size, wxFONTFAMILY_SWISS, wxNORMAL, wxNORMAL);
dc.SetFont(font);
dc.SetBackgroundMode(wxTRANSPARENT);
wxColour colour = colour_str_to_wxColour(colour_str);
//printf("drawing_text %s with colour %s, #%02x%02x%02x%02x\n", text_str, colour_str, colour.Red(), colour.Green(), colour.Blue(), colour.Alpha());
dc.SetTextForeground(colour_str_to_wxColour(colour_str));
y -= dc.GetCharHeight(); // TODO I guess this is how it is in html?
if (align == 0) {
x -= strlen(text_str) * dc.GetCharWidth()/2;
}
wxCoord width, height;
dc.GetTextExtent(text_str, &width, &height);
#if 0
if (align == TEXT_ALIGN_LEFT) {
// do nothing, this is the default
} else if (align == TEXT_ALIGN_CENTRE) {
// TODO why does this look so bad for solitaire cards?
// is this not the definition of centre alignment?
x -= width/2;
} else if (align == TEXT_ALIGN_RIGHT) {
x -= width;
} else {
fprintf(stderr, "Unexpected text align value %d\n", align);
}
dc.DrawText(text_str, wxPoint(x, y));
#else
wxRect text_pos(x, y, width, height);
int wx_align = wxALIGN_TOP;
if (align == TEXT_ALIGN_LEFT) {
wx_align |= wxALIGN_LEFT;
} else if (align == TEXT_ALIGN_CENTRE) {
wx_align |= wxALIGN_CENTRE;
} else if (align == TEXT_ALIGN_RIGHT) {
wx_align |= wxALIGN_RIGHT;
} else {
wx_align |= wxALIGN_LEFT;
fprintf(stderr, "Unexpected text align value %d\n", align);
}
dc.DrawLabel(text_str, text_pos, wx_align);
#endif
}
static void wx_draw_rect(const char *fill_colour_str, size_t fill_colour_len,
int y_start, int x_start,
int y_end , int x_end) {
if (g_canvas == nullptr) { return; }
Drawable *drawable = new DrawRectDrawable(fill_colour_str, fill_colour_len, y_start, x_start, y_end, x_end);
drawable_queue.push_back(drawable);
}
static void wx_draw_rect_internal(const char *fill_colour_str, size_t fill_colour_len,
int y_start, int x_start,
int y_end , int x_end) {
//wxPaintDC dc(g_canvas);
//wxMemoryDC &dc = g_canvas->mdc;
wxMemoryDC dc;
dc.SelectObject(g_canvas->bmp);
wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
if (gc == nullptr) {
fprintf(stderr, "could not create graphics context?\n");
return;
}
//gc->SetPen(wxPen("navy"));
//gc->SetBrush(wxBrush("pink"));
gc->SetPen(wxNullPen);
gc->SetBrush(colour_str_to_wxColour(fill_colour_str));
// wxGraphicsPath path = gc->CreatePath();
gc->DrawRectangle(x_start, y_start, x_end - x_start, y_end - y_start);
delete gc;
g_canvas->Refresh();
//g_canvas->Update();
}
static void wx_draw_circle(const char *fill_colour_str, size_t fill_colour_len,
const char *outline_colour_str, size_t outline_colour_len,
int y, int x, int radius, int outline_width) {
Drawable *drawable = new DrawCircleDrawable(fill_colour_str, outline_colour_str, y, x, radius);
drawable_queue.push_back(drawable);
}
static void wx_draw_triangle_internal(const char *fill_colour_str, size_t fill_colour_len,
int y1, int x1,
int y2, int x2,
int y3, int x3) {
wxMemoryDC dc;
dc.SelectObject(g_canvas->bmp);
wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
if (gc == nullptr) {
fprintf(stderr, "could not create graphics context?\n");
return;
}
wxGraphicsPath path = gc->CreatePath();
path.MoveToPoint(x1, y1);
path.AddLineToPoint(x2, y2);
path.AddLineToPoint(x3, y3);
path.AddLineToPoint(x1, y1);
path.CloseSubpath();
gc->SetBrush(colour_str_to_wxColour(fill_colour_str));
gc->FillPath(path);
delete gc;
}
static void wx_draw_triangle(const char *fill_colour_str, size_t fill_colour_str_len,
int y1, int x1,
int y2, int x2,
int y3, int x3) {
Drawable *drawable = new DrawTriangleDrawable(fill_colour_str, fill_colour_str_len, y1, x1, y2, x2, y3, x3);
drawable_queue.push_back(drawable);
}
static void wx_draw_circle_internal(const char *fill_colour_str, size_t fill_colour_len,
const char *outline_colour_str, size_t outline_colour_len,
int y, int x, int radius) {
//NOT_IMPL();
wxMemoryDC dc;
dc.SelectObject(g_canvas->bmp);
wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
gc->SetBrush(colour_str_to_wxColour(fill_colour_str));
gc->SetPen(colour_str_to_wxColour(outline_colour_str));
//gc->DrawCircle(wxPoint(x, y), radius);
wxGraphicsPath path = gc->CreatePath();
path.AddCircle(x, y, radius);
gc->StrokePath(path);
gc->FillPath(path);
delete gc;
}
static void wx_draw_clear(void) {
while (!drawable_queue.empty()) {
Drawable *d = drawable_queue.front();
drawable_queue.pop_front();
delete d;
}
}
static void wx_draw_clear_internal(void) {
wxMemoryDC dc;
dc.SelectObject(g_canvas->bmp);
dc.Clear();
}
static void wx_draw_refresh(void) {
// TODO could trigger refresh here to be more efficient?
}
static void wx_send_message(const char *dst, size_t dst_len, const char *msg, size_t msg_len) {
//NOT_IMPL();
std::string dst_str(dst);
printf("Trying to send message to %s, %.*s...\n", dst, msg_len, msg);
if (g_server_thread != nullptr) {
printf("sending to server thread\n");
g_server_thread->send_message(dst, (const uint8_t *)msg, msg_len);
} else if (g_client_thread != nullptr) {
printf("sending to client thread\n");
g_client_thread->send_message(dst, (const uint8_t *)msg, msg_len);
} else {
printf("Neither client nor server thread initialized!\n");
}
}
static void wx_create_btn(const char *btn_id_str, const char *btn_text_str, int weight) {
g_frame->new_button(btn_id_str, btn_text_str, weight);
}
static void wx_set_btn_enabled(const char *btn_id_str, bool enabled) {
g_frame->set_btn_enabled(btn_id_str, enabled);
}
static void wx_set_btn_visible(const char *btn_id_str, bool visible) {
NOT_IMPL();
}
static void wx_hide_popup(void) {
if (g_popup != nullptr) {
g_popup->Show(false);
// TODO figure out how to destroy this. Calling either of the below
// result in a heap error?
//g_popup->Destroy();
//delete g_popup;
g_popup = nullptr;
}
}
static void wx_add_game_option(const char *option_id, const struct option_info *info) {
// TODO
NOT_IMPL();
}
static std::string get_time_str() {
std::time_t t = std::time(0);
std::tm* now = std::localtime(&t);
char s_buff[128];
snprintf(s_buff, sizeof(s_buff), "%02d:%02d:%02d:",
now->tm_hour, now->tm_min, now->tm_sec);
return std::string(s_buff);
}
static void wx_set_status_msg(const char *msg, size_t msg_len) {
// reset style in case we set it to red earlier
#if 1
if (!g_is_dark_mode) {
g_frame->statusText.SetDefaultStyle(wxTextAttr(*wxBLACK));
} else {
g_frame->statusText.SetDefaultStyle(wxTextAttr(*wxWHITE));
}
#else
// This doesn't actually seem to work for dark mode on macOS.
g_frame->statusText.SetDefaultStyle(g_frame->statusText.GetDefaultStyle());
#endif
g_frame->statusText.AppendText(wxT("\n"));
g_frame->statusText.AppendText(wxString(get_time_str()));
g_frame->statusText.AppendText(wxString(msg, msg_len));
//g_frame->statusText.SetDefaultStyle(wxTextAttr(*wxBLACK));
}
static void wx_set_status_err(const char *msg, size_t msg_len) {
g_frame->statusText.SetDefaultStyle(wxTextAttr(*wxRED));
g_frame->statusText.AppendText(wxT("\n"));
g_frame->statusText.AppendText(wxString(get_time_str()));
g_frame->statusText.AppendText(wxT("ERR:"));
g_frame->statusText.AppendText(wxString(msg, msg_len));
}
static void wx_handle_popup_btn_clicked(void *L, const char *popup_id, int popup_btn_id, const struct popup_state *popup_state) {
game_api->handle_popup_btn_clicked(L, popup_id, popup_btn_id, popup_state);
}
static void game_selection_wx_handle_popup_btn_clicked(void *L, const char *popup_id, int popup_btn_id, const struct popup_state *popup_state) {
strncpy(g_game_id, alex_get_game_name(popup_btn_id), sizeof(g_game_id));
printf("Game ID is: %s\n", g_game_id);
//g_popup->Show(false);
g_popup->Destroy();
g_popup = nullptr;
g_frame->StartGame();
}
static void wx_show_popup(void *L,
const char *popup_id_ptr, size_t popup_id_str_len,
const struct popup_info *info) {
// TODO implement new API that allows for more than just buttons
#if 1
//NOT_IMPL();
if (g_popup != nullptr) {
wx_hide_popup();
}
// TODO if g_popup is not null, destroy it
std::string popup_id(popup_id_ptr, popup_id_str_len);
std::cout << "Initializing game popup with lua handle " << L << std::endl;
g_popup = new GamePopup(g_frame, popup_id, info, L);
g_popup->set_popup_btn_pressed_callback(wx_handle_popup_btn_clicked);
g_popup->Show(true);
#else
//static void wx_show_popup(const char *popup_id, size_t popup_id_str_len,
// const char *title, size_t title_len,
// const char *msg, size_t msg_len,