-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1568 lines (1379 loc) · 59.3 KB
/
main.c
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
/*
** VNCSlots - a slot machine played over RFB protocol
*/
#include "image.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#define PORT "5900" // port we're listening on
#define INTERVAL (1000000 / 25)
// /////////////////////////////////
// types
enum fruit {
cherry,
orange,
plum,
bell,
bar
};
enum encoding {
Raw = 0,
CopyRect = 1,
RRE = 2,
HexTile = 4,
TRLE = 8,
ZRLE = 16,
Cursor = 32
};
// GLOBALS
// gamestate - what the slot machine is currently doing
enum {
waiting,
coin,
handle_down,
handle_up,
spin,
payout
} gamestate;
// economy
int plays;
int profit;
// which of the 20 stops is each reel on
unsigned char reel_stop[3];
// GRAPHICS -
static struct image * framebuffer;
static uint16_t * palette[3];
// actual Y position on the reel
short reel_position[3];
// temporary usage for screen drawing
short coin_y;
short handle_y;
short reel_left[3];
short payout_left;
// a slightly cooked pixel format, where the _max is converted to a _div
struct pixel_format {
uint8_t bpp;
// uint8_t depth;
uint8_t big_endian_flag;
uint8_t true_color_flag;
uint16_t red_div;
uint16_t green_div;
uint16_t blue_div;
uint8_t red_shift;
uint8_t green_shift;
uint8_t blue_shift;
};
// LINKED LISTS for network sockets
struct listener {
int fd;
struct listener * next;
};
struct client {
int fd;
struct client * next;
enum {
none = 0,
handshake_protocolversion,
handshake_security,
handshake_securityresult,
init_client,
client_message,
client_message_setpixelformat,
client_message_setencodings_0,
client_message_setencodings_n,
client_message_framebufferupdaterequest,
client_message_keyevent,
client_message_pointerevent,
client_message_clientcuttext_0,
client_message_clientcuttext_n
} state;
// data read from the TCP socket
unsigned int read, needed, extra;
unsigned char buffer[20];
// statistics
unsigned int bytes_sent;
// client state
struct pixel_format format;
// bit field of encodings supported
uint8_t encodings;
// key-down status
uint8_t key_down;
// mouse-down hotspots
uint8_t mouse_down;
// ready for update?
uint8_t ready;
// some indicators of Last Time Things Happened, which tells us when they need a Rectangle update
unsigned char sent_cursor;
unsigned char sent_palette;
short coin_y;
short handle_y;
short reel_position[3];
int plays, profit;
};
// /////////////////////////////////
// Helper functions
static unsigned char * encode_pixel(unsigned char * p, const struct pixel_format * f, const uint8_t color)
{
uint32_t pixel = ((palette[0][color] / f->red_div) << f->red_shift) |
((palette[1][color] / f->green_div) << f->green_shift) |
((palette[2][color] / f->blue_div) << f->blue_shift);
if (f->bpp == 8) {
*p = (pixel & 0xFF);
p ++;
} else if (f->bpp == 16) {
if (f->big_endian_flag) {
*p = (pixel & 0xFF00) >> 8;
p ++;
*p = (pixel & 0xFF);
p ++;
} else {
*p = (pixel & 0xFF);
p ++;
*p = (pixel & 0xFF00) >> 8;
p ++;
}
} else {
if (f->big_endian_flag) {
*p = (pixel & 0xFF000000) >> 24;
p ++;
*p = (pixel & 0xFF0000) >> 16;
p ++;
*p = (pixel & 0xFF00) >> 8;
p ++;
*p = (pixel & 0xFF);
p ++;
} else {
*p = (pixel & 0xFF);
p ++;
*p = (pixel & 0xFF00) >> 8;
p ++;
*p = (pixel & 0xFF0000) >> 16;
p ++;
*p = (pixel & 0xFF000000) >> 24;
p ++;
}
}
return p;
}
static unsigned char * encode_hextile(unsigned char * p, struct pixel_format * f, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
// some enums
enum {
H_None = 0,
H_Raw = 1,
H_BGSpec = 2,
H_FGSpec = 4,
H_AnySub = 8,
H_SubColor = 16
};
short background = -1;
short foreground = -1;
// we break the area into 16x16 tiles and analyze them
while (h > 0) {
int th = h > 16 ? 16 : h;
int dx = x;
int w_left = w;
while (w_left > 0) {
int tw = w_left > 16 ? 16 : w_left;
// histogram of the colors
// for 8bpp we can use pigeonhole
unsigned short colors[256] = { 0 };
for (int j = 0; j < th; j ++)
for (int i = 0; i < tw; i ++)
colors[framebuffer->data[(y + j) * 512 + dx + i]] ++;
short newbg = -1;
short newfg = -1;
unsigned short color_count = 0;
for (int i = 0; i < 256; i ++) {
if (colors[i] > 0) {
color_count ++;
if (newbg < 0 || colors[i] > colors[newbg]) {
newfg = newbg;
newbg = i;
} else if (newfg < 0 || colors[i] > colors[newfg]) {
newfg = i;
}
}
}
// log this. if we do the work and find this is more expensive than raw encoding the whole tile,
// backtrack to this point and do raw.
unsigned char * tile_start = p;
// ok! we have determined a count of how many colors are in the tile,
// the most common (newbg) and the second-most-common (newfg)
if (color_count == 1) {
// solid colored tile
if (newbg == background) {
// can carry over color from before
*p = H_None;
p ++;
} else {
*p = H_BGSpec;
p ++;
p = encode_pixel(p, f, newbg);
background = newbg;
}
} else {
if (color_count == 2) {
// two-tone tile
if (newbg == background && newfg == foreground) {
*p = H_AnySub;
p ++;
} else if (newbg != background && newfg == foreground) {
*p = H_AnySub | H_BGSpec;
p ++;
p = encode_pixel(p, f, newbg);
background = newbg;
} else if (newbg == background && newfg != foreground) {
*p = H_AnySub | H_FGSpec;
p ++;
p = encode_pixel(p, f, newfg);
foreground = newfg;
} else {
*p = H_AnySub | H_FGSpec | H_BGSpec;
p ++;
p = encode_pixel(p, f, newbg);
background = newbg;
p = encode_pixel(p, f, newfg);
foreground = newfg;
}
} else {
if (newbg == background) {
// can carry over color from before
*p = H_AnySub | H_SubColor;
p ++;
} else {
*p = H_AnySub | H_SubColor | H_BGSpec;
p ++;
p = encode_pixel(p, f, newbg);
background = newbg;
}
foreground = -1;
}
unsigned char * rect_count = p;
p ++;
*rect_count = 0;
// at last do RRE on the tile
unsigned char coverage[16][16] = { 0 };
for (int j = 0; j < th; j ++) {
for (int i = 0; i < tw; i ++) {
// square already "covered", skip
if (coverage[j][i]) continue;
// mark it now
coverage[j][i] = 1;
// check for background-color
unsigned char color = framebuffer->data[(y + j) * 512 + dx + i];
if (color == background) continue;
// an uncovered, new color.
*rect_count += 1;
// try to expand our ending box as far right as we can
int i2 = i + 1;
while (i2 < tw && framebuffer->data[(y + j) * 512 + dx + i2] == color)
{
coverage[j][i2] = 1;
i2 ++;
}
// and now a check to see how tall we can make the box
int j2 = j + 1;
while (j2 < th) {
unsigned char full_row = 1;
// check the row first
for (int q = i; q < i2; q ++) {
if (color != framebuffer->data[(y + j2) * 512 + dx + q]) {
full_row = 0;
break;
}
}
if (! full_row) break;
// mark the row now
for (int q = i; q < i2; q ++)
coverage[j2][q] = 1;
j2 ++;
}
// ok we have everything we need! send the rectangle
if (color_count > 2) p = encode_pixel(p, f, color);
*p = ((i & 0xF) << 4) | (j & 0xF);
p ++;
*p = (((i2 - i - 1) & 0xF) << 4) | ((j2 - j - 1) & 0xF);
p ++;
}
}
}
// RAW ENCODE THE TILE
if (p - tile_start > tw * th * f->bpp / 8) {
p = tile_start;
*p = 1;
p ++;
for (int j = 0; j < th; j ++)
for (int i = 0; i < tw; i ++)
p = encode_pixel(p, f, framebuffer->data[(y + j) * 512 + dx + i]);
background = foreground = -1;
}
//printf("%2x ", *tile_start);
dx += tw;
w_left -= tw;
}
//printf("\n");
y += th;
h -= th;
}
return p;
}
static unsigned char * encode_rre(unsigned char * p, struct pixel_format * f, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
// we need this to go update the subrectangle count later
unsigned char * start = p;
p += 4;
unsigned int subrects = 0;
// find background color
// this is a histogram across the region, tracking each pixel and its frequency
// for 8bpp we can use pigeonhole
unsigned char max_color = 0;
unsigned int colors[256] = { 0 };
for (int src_y = y; src_y < y + h; src_y ++) {
int i = src_y * 512;
for (int src_x = x; src_x < x + w; src_x ++) {
unsigned char color = framebuffer->data[i + src_x];
colors[color] ++;
if (colors[color] > colors[max_color])
max_color = color;
}
}
p = encode_pixel(p, f, max_color);
// now we calloc a region and then walk it trying to build RRE blocks and send them
unsigned char * coverage = calloc(h, w);
if (coverage == NULL) {
perror("calloc coverage");
exit(EXIT_FAILURE);
}
for (int src_y = 0; src_y < h; src_y ++) {
int i = (y + src_y) * 512;
int j = src_y * w;
for (int src_x = 0; src_x < w; src_x ++) {
// square already "covered", skip
if (coverage[j + src_x]) continue;
// mark it now
coverage[j + src_x] = 1;
// check for background-color
unsigned char color = framebuffer->data[i + x + src_x];
if (color == max_color) continue;
// an uncovered, new color.
subrects ++;
// try to expand our ending box as far right as we can
int src_x2 = src_x + 1;
while (src_x2 < w && framebuffer->data[i + x + src_x2] == color)
{
coverage[j + src_x2] = 1;
src_x2 ++;
}
// and now a check to see how tall we can make the box
int src_y2 = src_y + 1;
while (src_y2 < h) {
int k = (y + src_y2) * 512;
unsigned char full_row = 1;
// check the row first
for (int l = src_x; l < src_x2; l ++) {
if (color != framebuffer->data[l + x + k]) {
full_row = 0;
break;
}
}
if (! full_row) break;
// mark the row now
k = src_y2 * w;
for (int l = src_x; l < src_x2; l ++)
coverage[k + l] = 1;
src_y2 ++;
}
// ok we have everything we need! send the rectangle
p = encode_pixel(p, f, color);
*p = src_x / 256;
p ++;
*p = src_x % 256;
p ++;
*p = src_y / 256;
p ++;
*p = src_y % 256;
p ++;
*p = (src_x2 - src_x) / 256;
p ++;
*p = (src_x2 - src_x) % 256;
p ++;
*p = (src_y2 - src_y) / 256;
p ++;
*p = (src_y2 - src_y) % 256;
p ++;
}
}
*start = (subrects & 0xFF000000) >> 24;
start ++;
*start = (subrects & 0xFF0000) >> 16;
start ++;
*start = (subrects & 0xFF00) >> 8;
start ++;
*start = subrects & 0xFF;
free(coverage);
return p;
}
static unsigned char * encode_raw(unsigned char * p, const struct pixel_format * f, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
// if their format exactly matches ours, we can just send the pixels directly
if (f->bpp == 8 && (f->true_color_flag == 0 || (f->red_div == (65536 / 8) && f->red_shift == 0 && f->green_div == (65536 / 8) && f->green_shift == 3 && f->blue_div == (65536 / 4) && f->blue_shift == 6))) {
for (int row = y; row < y + h; row ++) {
memcpy(p, & framebuffer->data[512 * row + x], w);
p += w;
}
} else {
// hmm ok A Conversion Is Needed
for (int src_y = y; src_y < y + h; src_y ++) {
int i = src_y * 512;
for (int src_x = x; src_x < x + w; src_x ++)
p = encode_pixel(p, f, framebuffer->data[i + src_x]);
}
}
return p;
}
static unsigned char * encode_cursor(unsigned char * p, const struct pixel_format * f)
{
// the cursor shape - Windows "hand"
static const unsigned char cursor_colormap[47] = { 0x00, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x30, 0x00, 0x1b, 0x00, 0x0d, 0xb0, 0x06, 0xda, 0x03, 0x6d, 0x99, 0xfe, 0xce, 0xff, 0xe3, 0x7f, 0xf0, 0xbf, 0xf8, 0x7f, 0xfc, 0x1f, 0xfe, 0x0f, 0xfe, 0x03, 0xff, 0x01, 0xff, 0x80, 0x7f, 0x80, 0x3f, 0xc0, 0x00, 0x00 };
static const unsigned char cursor_tmap[66] = { 0x06, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x0f, 0xf8, 0x00, 0x0f, 0xfe, 0x00, 0x0f, 0xff, 0x00, 0xef, 0xff, 0x80, 0xff, 0xff, 0x80, 0xff, 0xff, 0x80, 0x7f, 0xff, 0x80, 0x3f, 0xff, 0x80, 0x3f, 0xff, 0x80, 0x1f, 0xff, 0x80, 0x1f, 0xff, 0x00, 0x0f, 0xff, 0x00, 0x0f, 0xff, 0x00, 0x07, 0xfe, 0x00, 0x07, 0xfe, 0x00, 0x07, 0xfe, 0x00 };
// rectangle header
p[0] = p[2] = p[4] = p[6] = 0;
// hotspot
p[1] = 5;
p[3] = 1;
// cursor size
p[5] = 17;
p[7] = 22;
// encoding
p[8] = p[9] = p[10] = 0xFF;
p[11] = 0x11;
p += 12;
// decode the cursor colormap
char bit = 7;
int byte = 0;
for (int i = 0; i < 17 * 22; i ++) {
p = encode_pixel(p, f, cursor_colormap[byte] & (1 << bit) ? 0xFF : 0);
bit --;
if (bit < 0) {
byte ++;
bit = 7;
}
}
// dump the transparency map
memcpy(p, cursor_tmap, 66);
p += 66;
return p;
}
// Sends a consolidated Update packet to the client.
static unsigned char * encode(unsigned char * p, struct client * c, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
// pack an update
// x
p[0] = (x / 256);
p[1] = (x % 256);
p[2] = (y / 256);
p[3] = (y % 256);
p[4] = (w / 256);
p[5] = (w % 256);
p[6] = (h / 256);
p[7] = (h % 256);
// encoding
p[8] = p[9] = p[10] = 0;
p += 11;
if (c->encodings & HexTile) {
*p = 5;
unsigned char * hextile = encode_hextile(p + 1, & c->format, x, y, w, h);
if (hextile - p <= w * h * (c->format.bpp / 8)) return hextile;
// it seems HexTile made it worse than Raw so toss that encoding attempt
}
if (c->encodings & RRE) {
*p = 2;
unsigned char * rre = encode_rre(p + 1, & c->format, x, y, w, h);
if (rre - p <= w * h * (c->format.bpp / 8)) return rre;
// it seems RRE made it worse than Raw so toss that encoding attempt
}
// RAW encoding
*p = 0;
return encode_raw(p + 1, & c->format, x, y, w, h);
}
// Sends a consolidated Update packet to the client.
static int update(struct client * c, uint16_t x, uint16_t y, uint16_t w, uint16_t h, unsigned char incremental)
{
// cap the region to just our screen limits
if (x > 511) x = 511;
if (y > 383) y = 383;
if (x + w > 512) w = 512 - x;
if (y + h > 384) h = 384 - y;
// this is a staging area for building the complete packet we'll send
// the largest possible packet is a full screen 32-bit refresh, plus maybe a cursor
static unsigned char * packet = NULL, *p;
if (packet == NULL) {
packet = malloc(4 +
12 + 512 * 384 * 4 +
12 + 17 * 22 * 4 + 3 * 22);
if (packet == NULL) {
perror("packet malloc");
exit(EXIT_FAILURE);
}
}
// paletted modes should get a copy of the palette on first update
if (! c->format.true_color_flag && ! c->sent_palette) {
// type + padding
packet[1] = packet[2] = packet[3] = packet[5] = 0;
packet[0] = packet[4] = 1;
p = &packet[6];
for (int i = 0; i < 256; i ++) {
for (int c = 0; c < 3; c ++) {
*p = (palette[c][i] >> 8) & 0xFF;
p ++;
*p = palette[c][i] & 0xFF;
p ++;
}
}
c->bytes_sent += (p - packet);
//printf("Sending %d bytes\n", p - packet);
if ( send(c->fd, packet, p - packet, 0) == -1) {
perror("send");
return 0;
}
c->sent_palette = 1;
}
// framebuffer update
// type + padding
packet[0] = packet[1] = 0;
p = &packet[4];
unsigned short rectangle_count = 0;
// Incremental update can take just the changes in the area
if (incremental)
{
// coin drop
if (c->coin_y != coin_y) {
rectangle_count ++;
p = encode(p, c, 388, 185, 29, 37);
}
// handle
if (c->handle_y != handle_y) {
rectangle_count ++;
int skip = (c->handle_y < handle_y ? c->handle_y : handle_y);
p = encode(p, c, 447, 73 + skip, 40, 248 - skip);
}
// reels
for (int i = 0; i < 3; i ++) {
if (c->reel_position[i] != reel_position[i]) {
rectangle_count ++;
p = encode(p, c, 222 + 50 * i, 67, 32, 114);
}
}
// scoreboard
if (c->profit - c->plays != profit - plays) {
rectangle_count ++;
p = encode(p, c, 19, 353, 63, 11);
}
if (c->plays != plays) {
rectangle_count ++;
p = encode(p, c, 19, 293, 63, 11);
}
if (c->profit != profit) {
rectangle_count ++;
p = encode(p, c, 19, 323, 63, 11);
// ding!
*p = 0x02;
p ++;
}
// nothing to do! don't send anything.
if (rectangle_count == 0) return 1;
} else {
// encode the entire region
rectangle_count ++;
p = encode(p, c, x, y, w, h);
}
if ((c->encodings & Cursor) && ! c->sent_cursor)
{
rectangle_count ++;
p = encode_cursor(p, &c->format);
}
packet[2] = rectangle_count / 256;
packet[3] = rectangle_count % 256;
// All done! Send the packet.
c->bytes_sent += (p - packet);
//printf("Sending %d bytes\n", p - packet);
if ( send(c->fd, packet, p - packet, 0) == -1) {
perror("send");
return 0;
}
c->sent_cursor = 1;
c->coin_y = coin_y;
c->handle_y = handle_y;
for (int i = 0; i < 3; i ++)
c->reel_position[i] = reel_position[i];
c->plays = plays;
c->profit = profit;
c->ready = 0;
return 1;
}
// special draw functions
static void draw_number(struct image * dst, const struct image * src, int number, unsigned short dst_x, unsigned short dst_y)
{
static char num[12] = "";
sprintf(num, "%8d", number);
for (int i = 0; i < 8; i ++) {
if (num[i] >= '0' && num[i] <= '9') {
blit_special(src, 0, 11 * (num[i] - '0'), dst, dst_x, dst_y, src->width, 11, 0, (number < 0 ? 7 : 0));
} else if (num[i] == '-') {
blit_special(src, 0, 110, dst, dst_x, dst_y, src->width, 11, 0, (number < 0 ? 7 : 0));
} else {
// white square
fill(dst, dst_x, dst_y, 6, 11, 0xFF);
}
dst_x += 8;
}
}
static void darken_row(struct image * dst, unsigned short x, unsigned short y, unsigned short w, unsigned char amount)
{
unsigned char *p = &dst->data[y * dst->width + x];
while (w > 0) {
short b = ((*p & 0xC0) >> 6) - (amount >> 1);
if (b < 0) b = 0;
short g = ((*p & 0x38) >> 3) - amount;
if (g < 0) g = 0;
short r = (*p & 0x07) - amount;
if (r < 0) r = 0;
*p = (b << 6) | (g << 3) | r;
p ++;
w --;
}
}
static void draw_reel(struct image * dst, const struct image * src, short reel_position, unsigned short dst_x, unsigned short dst_y)
{
// reel is 114 pixels high
short dst_h = 114;
if (reel_position + dst_h > src->height) {
// reel won't fit
int h = src->height - reel_position;
blit_simple(src, 0, reel_position, dst, dst_x, dst_y, src->width, h);
blit_simple(src, 0, 0, dst, dst_x, dst_y + h, src->width, dst_h - h);
} else {
blit_simple(src, 0, reel_position, dst, dst_x, dst_y, src->width, dst_h);
}
// darken top and bottom
for (int y = 0; y < 14; y ++) {
darken_row(dst, dst_x, dst_y + y, 32, (14 - y) >> 1);
darken_row(dst, dst_x, dst_y + dst_h - y - 1, 32, (14 - y) >> 1);
}
}
static void draw_handle(struct image * dst, const struct image * img_background, const struct image * img_handle, const struct image * img_ball, int scale)
{
blit_simple(img_background, 447, 73, dst, 447, 73, 40, img_ball->height + img_handle->height);
blit_special(img_ball, 0, 0, dst, 451, 73 + scale, img_ball->height, img_ball->width, 0xFF, 0);
blit_scaled(img_handle, 0, 0, img_handle->height, dst, 447, img_ball->height + 73 + scale, img_handle->height - scale, img_handle->width, 0xFF);
}
// get sockaddr, IPv4 or IPv6:
static const void *get_in_addr(const struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in *)sa)->sin_addr);
}
return &(((struct sockaddr_in6 *)sa)->sin6_addr);
}
/*
static const char * en2s(int fruit)
{
if (fruit == bar) return "BAR";
if (fruit == bell) return "BELL";
if (fruit == plum) return "PLUM";
if (fruit == orange) return "ORANGE";
if (fruit == cherry) return "CHERRY";
return "UNKNOWN";
}
*/
// /////////////////////////////////
int main(void)
{
puts("VNCSlots - starting up!");
// local constants
static const uint8_t reels[3][20] = {
{ orange, bar, plum, cherry, plum, orange, bell, plum, orange, cherry, orange, bar, orange, plum, orange, plum, cherry, bar, orange, plum },
{ bell, cherry, bell, cherry, bell, cherry, bell, orange, bell, cherry, bell, cherry, bell, bar, bell, cherry, bell, cherry, bell, plum },
{ orange, cherry, orange, plum, orange, bar, orange, plum, orange, bell, orange, cherry, orange, plum, orange, plum, orange, cherry, orange, plum }
};
// important variables
plays = 0;
profit = 0;
FILE * stats = fopen("stats.ini", "r");
if (stats != NULL) {
fscanf(stats, "%d %d\n", &plays, &profit);
fclose(stats);
}
// reel positions
reel_stop[0] = reel_stop[1] = reel_stop[2] = 0;
reel_position[0] = reel_position[1] = reel_position[2] = 960 - 57 + 16;
// linked lists of listeners and clients
struct listener * listeners = NULL;
struct client * clients = NULL;
// nEtwork socketstuff
int listener_fd_max = 0; // maximum file descriptor number
fd_set master; // master file descriptor list
FD_ZERO(&master); // clear the master set
// images
puts("Loading images...");
const struct image * img_background = read_image("background.bin");
const struct image * img_digits = read_image("digits.bin");
const struct image * img_ball = read_image("ball.bin");
const struct image * img_handle = read_image("handle.bin");
const struct image * img_coin = read_image("coin.bin");
const struct image * img_coinslot = read_image("coinslot.bin");
struct image * img_fruit = read_image("fruit.bin");
// build three large reel images
struct image * img_reels[3];
for (int i = 0; i < 3; i ++) {
img_reels[i] = make_image(32, 48 * 20);
for (int k = 0; k < 20; k ++) {
blit_simple(img_fruit, 0, 32 * reels[i][k], img_reels[i], 0, 48 * k, 32, 32);
fill(img_reels[i], 0, 48 * k + 32, 32, 16, 0xFF);
}
}
free_image(img_fruit);
// build BGR233 palette
// the format of a RFB palette is uint16
for (int i = 0; i < 3; i ++)
{
palette[i] = malloc(256 * sizeof(uint16_t));
if (palette[i] == NULL) {
perror("malloc palette");
return EXIT_FAILURE;
}
}
int i = 0;
for (int b = 0; b < 4; b ++)
{
for (int g = 0; g < 8; g ++)
{
for (int r = 0; r < 8; r ++)
{
palette[0][i] = (r << 13) | (r << 10) | (r << 7) | (r << 4) | (r << 1) | (r >> 2);
palette[1][i] = (g << 13) | (g << 10) | (g << 7) | (g << 4) | (g << 1) | (g >> 2);
palette[2][i] = (b << 14) | (b << 12) | (b << 10) | (b << 8) | (b << 6) | (b << 4) | (b << 2) | b;
i ++;
}
}
}
// BUILD FRAMEBUFFER
framebuffer = make_image(512, 384);
// blit
blit_simple(img_background, 0, 0, framebuffer, 0, 0, img_background->width, img_background->height);
draw_handle(framebuffer, img_background, img_handle, img_ball, 0);
draw_number(framebuffer, img_digits, plays, 19, 293);
draw_number(framebuffer, img_digits, profit, 19, 323);
draw_number(framebuffer, img_digits, profit - plays, 19, 353);
// visuals - the center position is 57 pixels down but then we also have to remove 16px for the top half of the fruit
draw_reel(framebuffer, img_reels[0], reel_position[0], 222, 67);
draw_reel(framebuffer, img_reels[1], reel_position[1], 272, 67);
draw_reel(framebuffer, img_reels[2], reel_position[2], 322, 67);
// BIND LISTENERS
puts("Binding listen sockets (port " PORT ")...");
{
// get us a socket and bind it - any family, TCP / stream
static const struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP,
.ai_flags = AI_ADDRCONFIG | AI_PASSIVE
};
struct addrinfo *ai;
int rv = getaddrinfo(NULL, PORT, &hints, &ai);
if (rv != 0) {
fputs("getaddrinfo: ", stderr);
fputs(gai_strerror(rv), stderr);
return EXIT_FAILURE;
}
for(struct addrinfo * p = ai; p != NULL; p = p->ai_next) {
int fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (fd < 0) {
perror("socket");
continue;
}
// lose the pesky "address already in use" error message
static const int yes=1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(fd, p->ai_addr, p->ai_addrlen)) {
perror("bind");
close(fd);
continue;
}
if (listen(fd, 1)) {
perror("listen");
close(fd);
continue;
}
// looks good! print some info and put this into the master set
char ip[INET6_ADDRSTRLEN];
inet_ntop(p->ai_addr->sa_family,
get_in_addr(p->ai_addr),
ip, INET6_ADDRSTRLEN);
printf(" . Bound to %s on socket %d\n", ip, fd);
// add the listener to the master set
FD_SET(fd, &master);
// keep track of the biggest file descriptor
if (fd > listener_fd_max) listener_fd_max = fd;
struct listener * l = malloc(sizeof(struct listener));
l->fd = fd;
l->next = listeners;
listeners = l;
}
freeaddrinfo(ai); // all done with this
}
// if we got here, it means we didn't get bound
if (listeners == NULL) {
fputs("selectserver: failed to bind to any sockets\n", stderr);
return EXIT_FAILURE;
}
puts("Ready to accept new connections!");
// state info about the game display
gamestate = waiting;
struct timeval tv_now, tv_next;
/*
gettimeofday(&tv_now, NULL);
//printf("The time is %d.%06d sec\n", tv_now.tv_sec, tv_now.tv_usec);
tv_next.tv_sec = tv_now.tv_sec;
tv_next.tv_usec = tv_now.tv_usec + INTERVAL;
if (tv_next.tv_usec > 999999) {
tv_next.tv_sec ++;
tv_next.tv_usec -= 1000000;
}
*/
// main loop
int fd_max = listener_fd_max;
for(;;) {
// temp file descriptor list for select()
fd_set read_fds = master;