/
simgraph16.cc
5554 lines (4806 loc) · 162 KB
/
simgraph16.cc
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
/*
* This file is part of the Simutrans project under the Artistic License.
* (see LICENSE.txt)
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include "../macros.h"
#include "../simtypes.h"
#include "font.h"
#include "../pathes.h"
#include "../simconst.h"
#include "../sys/simsys.h"
#include "../simmem.h"
#include "../simdebug.h"
#include "../descriptor/image.h"
#include "../dataobj/environment.h"
#include "../dataobj/translator.h"
#include "../unicode.h"
#include "../simticker.h"
#include "../utils/simstring.h"
#include "../io/raw_image.h"
#include "../gui/simwin.h"
#include "../dataobj/environment.h"
#include "simgraph.h"
#include "../obj/roadsign.h" // for signal status indicator
#ifdef _MSC_VER
# include <io.h>
# define W_OK 2
#else
# include <unistd.h>
#endif
#ifdef MULTI_THREAD
#include "../utils/simthread.h"
// currently just redrawing/rezooming
static pthread_mutex_t rezoom_img_mutex[MAX_THREADS];
static pthread_mutex_t recode_img_mutex;
#endif
// to pass the extra clipnum when not needed use this
#ifdef MULTI_THREAD
#define CLIPNUM_IGNORE , 0
#else
#define CLIPNUM_IGNORE
#endif
#include "simgraph.h"
// undefine for debugging the update routines
//#define DEBUG_FLUSH_BUFFER
#ifdef USE_SOFTPOINTER
static int softpointer = -1;
#endif
static int standard_pointer = -1;
#ifdef USE_SOFTPOINTER
/*
* Icon bar needs to redrawn on mouse hover
*/
int old_my = -1;
#endif
/*
* struct to hold the information about visible area
* at screen line y
* associated to some clipline
*/
struct xrange {
sint64 sx, sy;
scr_coord_val y;
bool non_convex_active;
};
class clip_line_t {
private:
// line from (x0,y0) to (x1 y1)
// clip (do not draw) everything right from the ray (x0,y0)->(x1,y1)
// pixels on the ray are not drawn
// (not_convex) if y0>=y1 then clip along the path (x0,-inf)->(x0,y0)->(x1,y1)
// (not_convex) if y0<y1 then clip along the path (x0,y0)->(x1,y1)->(x1,+inf)
int x0, y0;
int dx, dy;
sint64 sdy, sdx;
sint64 inc;
bool non_convex;
public:
void clip_from_to(int x0_, int y0_, int x1, int y1, bool non_convex_) {
x0 = x0_;
dx = x1 - x0;
y0 = y0_;
dy = y1 - y0;
non_convex = non_convex_;
int steps = (abs(dx) > abs(dy) ? abs(dx) : abs(dy));
if( steps == 0 ) {
return;
}
sdx = ((sint64)dx << 16) / steps;
sdy = ((sint64)dy << 16) / steps;
// to stay right from the line
// left border: xmin <= x
// right border: x < xmax
if( dy > 0 ) {
if( dy > dx ) {
inc = 1 << 16;
}
else {
inc = ((sint64)dx << 16) / dy - (1 << 16);
}
}
else if( dy < 0 ) {
if( dy < dx ) {
inc = 0; // (+1)-1 << 16;
}
else {
inc = 0;
}
}
}
// clip if
// ( x-x0) . ( y1-y0 )
// ( y-y0) . (-(x1-x0)) < 0
// -- initialize the clipping
// has to be called before image will be drawn
// return interval for x coordinate
inline void get_x_range(scr_coord_val y, xrange &r, bool use_non_convex) const {
// do everything for the previous row
y--;
r.y = y;
r.non_convex_active = false;
if( non_convex && use_non_convex && y < y0 && y < (y0 + dy) ) {
r.non_convex_active = true;
y = min(y0, y0+dy) - 1;
}
if( dy != 0 ) {
// init Bresenham algorithm
const sint64 t = (((sint64)y - y0) << 16) / sdy;
// sx >> 16 = x
// sy >> 16 = y
r.sx = t * sdx + inc + ((sint64)x0 << 16);
r.sy = t * sdy + ((sint64)y0 << 16);
}
}
// -- step one line down, return interval for x coordinate
inline void inc_y(xrange &r, int &xmin, int &xmax) const {
r.y++;
// switch between clip vertical and along ray
if( r.non_convex_active ) {
if( r.y == min( y0, y0 + dy ) ) {
r.non_convex_active = false;
}
else {
if( dy < 0 ) {
const int r_xmax = x0 + dx;
if( xmax > r_xmax ) {
xmax = r_xmax;
}
}
else {
const int r_xmin = x0 + 1;
if( xmin < r_xmin ) {
xmin = r_xmin;
}
}
return;
}
}
// go along the ray, Bresenham
if( dy != 0 ) {
if( dy > 0 ) {
do {
r.sx += sdx;
r.sy += sdy;
} while( (r.sy >> 16) < r.y );
const int r_xmin = r.sx >> 16;
if( xmin < r_xmin ) {
xmin = r_xmin;
}
}
else {
do {
r.sx -= sdx;
r.sy -= sdy;
} while( (r.sy >> 16) < r.y );
const int r_xmax = r.sx >> 16;
if( xmax > r_xmax ) {
xmax = r_xmax;
}
}
}
// horizontal clip
else {
const bool clip = dx * (r.y - y0) > 0;
if( clip ) {
// invisible row
xmin = +1;
xmax = -1;
}
}
}
};
#define MAX_POLY_CLIPS 6
MSVC_ALIGN(64) struct clipping_info_t {
// current clipping rectangle
clip_dimension clip_rect;
// clipping rectangle to be swapped by display_clip_wh_toggle
clip_dimension clip_rect_swap;
bool swap_active;
// poly clipping variables
int number_of_clips;
uint8 active_ribi;
uint8 clip_ribi[MAX_POLY_CLIPS];
clip_line_t poly_clips[MAX_POLY_CLIPS];
xrange xranges[MAX_POLY_CLIPS];
} GCC_ALIGN(64); // aligned to separate cachelines
#ifdef MULTI_THREAD
clipping_info_t clips[MAX_THREADS];
#define CR0 clips[0]
#else
clipping_info_t clips;
#define CR0 clips
#endif
#define CR clips CLIP_NUM_INDEX
static font_t default_font;
// needed for resizing gui
int default_font_ascent = 0;
int default_font_linespace = 0;
#define MAX_PLAYER_COUNT (16)
#define RGBMAPSIZE (0x8000+LIGHT_COUNT+MAX_PLAYER_COUNT)
/*
* mapping tables for RGB 555 to actual output format
* plus the special (player, day&night) colors appended
*
* 0x0000 - 0x7FFF: RGB colors
* 0x8000 - 0x800F: Player colors
* 0x8010 - 0x001F: Day&Night special colors
* The following transparent colors are not in the colortable
* 0x8020 - 0xFFE1: 3 4 3 RGB transparent colors in 31 transparency levels
*/
static PIXVAL rgbmap_day_night[RGBMAPSIZE];
/*
* same as rgbmap_day_night, but always daytime colors
*/
static PIXVAL rgbmap_all_day[RGBMAPSIZE];
/*
* used by pixel copy functions, is one of rgbmap_day_night
* rgbmap_all_day
*/
static PIXVAL *rgbmap_current = 0;
/*
* mapping table for special-colors (AI player colors)
* to actual output format - day&night mode
* 16 sets of 16 colors
*/
static PIXVAL specialcolormap_day_night[256];
/*
* mapping table for special-colors (AI player colors)
* to actual output format - all day mode
* 16 sets of 16 colors
*/
PIXVAL specialcolormap_all_day[256];
/*
* contains all color conversions for transparency
* 16 player colors, 15 special colors and 1024 3 4 3 encoded colors for transparent base
*/
static PIXVAL transparent_map_day_night[MAX_PLAYER_COUNT+LIGHT_COUNT+1024];
static PIXVAL transparent_map_all_day[MAX_PLAYER_COUNT+LIGHT_COUNT+1024];
//static PIXVAL *transparent_map_current;
/*
* contains all color conversions for transparency
* 16 player colors, 15 special colors and 1024 3 4 3 encoded colors for transparent base
*/
static uint8 transparent_map_day_night_rgb[(MAX_PLAYER_COUNT+LIGHT_COUNT+1024)*4];
static uint8 transparent_map_all_day_rgb[(MAX_PLAYER_COUNT+LIGHT_COUNT+1024)*4];
//static uint8 *transparent_map_current_rgb;
// offsets of first and second company color
static uint8 player_offsets[MAX_PLAYER_COUNT][2];
/*
* Image map descriptor structure
*/
struct imd {
sint16 x; // current (zoomed) min x offset
sint16 y; // current (zoomed) min y offset
sint16 w; // current (zoomed) width
sint16 h; // current (zoomed) height
uint8 recode_flags;
uint16 player_flags; // bit # is player number, ==1 cache image needs recoding
PIXVAL* data[MAX_PLAYER_COUNT]; // current data - zoomed and recolored (player + daynight)
PIXVAL* zoom_data; // zoomed original data
uint32 len; // current zoom image data size (or base if not zoomed) (used for allocation purposes only)
sint16 base_x; // min x offset
sint16 base_y; // min y offset
sint16 base_w; // width
sint16 base_h; // height
PIXVAL* base_data; // original image data
};
// Flags for recoding
#define FLAG_HAS_PLAYER_COLOR (1)
#define FLAG_HAS_TRANSPARENT_COLOR (2)
#define FLAG_ZOOMABLE (4)
#define FLAG_REZOOM (8)
//#define FLAG_POSITION_CHANGED (16)
#define TRANSPARENT_RUN (0x8000u)
// different masks needed for RGB 555 and RGB 565 for blending
#define ONE_OUT_16 (0x7bef)
#define TWO_OUT_16 (0x39E7)
#define ONE_OUT_15 (0x3DEF)
#define TWO_OUT_15 (0x1CE7)
static int bitdepth = 16;
static scr_coord_val disp_width = 640;
static scr_coord_val disp_actual_width = 640;
static scr_coord_val disp_height = 480;
/*
* Static buffers for rezoom_img()
*/
static uint8 *rezoom_baseimage[MAX_THREADS];
static PIXVAL *rezoom_baseimage2[MAX_THREADS];
static size_t rezoom_size[MAX_THREADS];
/*
* Image table
*/
static struct imd* images = NULL;
/*
* Number of loaded images
*/
static image_id anz_images = 0;
/*
* Number of allocated entries for images
* (>= anz_images)
*/
static image_id alloc_images = 0;
/*
* Output framebuffer
*/
static PIXVAL* textur = NULL;
/*
* dirty tile management structures
*/
#define DIRTY_TILE_SIZE 16
#define DIRTY_TILE_SHIFT 4
static uint32 *tile_dirty = NULL;
static uint32 *tile_dirty_old = NULL;
static int tiles_per_line = 0;
static int tile_buffer_per_line = 0; // number of tiles that fit the allocated buffer per line - maintain alignment - x=0 is always first bit in a word for each row
static int tile_lines = 0;
static int tile_buffer_length = 0;
static int light_level = 0;
static int night_shift = -1;
/*
* special colors during daytime
*/
uint8 display_day_lights[LIGHT_COUNT*3] = {
0x57, 0x65, 0x6F, // Dark windows, lit yellowish at night
0x7F, 0x9B, 0xF1, // Lighter windows, lit blueish at night
0xFF, 0xFF, 0x53, // Yellow light
0xFF, 0x21, 0x1D, // Red light
0x01, 0xDD, 0x01, // Green light
0x6B, 0x6B, 0x6B, // Non-darkening grey 1 (menus)
0x9B, 0x9B, 0x9B, // Non-darkening grey 2 (menus)
0xB3, 0xB3, 0xB3, // non-darkening grey 3 (menus)
0xC9, 0xC9, 0xC9, // Non-darkening grey 4 (menus)
0xDF, 0xDF, 0xDF, // Non-darkening grey 5 (menus)
0xE3, 0xE3, 0xFF, // Nearly white light at day, yellowish light at night
0xC1, 0xB1, 0xD1, // Windows, lit yellow
0x4D, 0x4D, 0x4D, // Windows, lit yellow
0xE1, 0x00, 0xE1, // purple light for signals
0x01, 0x01, 0xFF, // blue light
};
/*
* special colors during nighttime
*/
uint8 display_night_lights[LIGHT_COUNT*3] = {
0xD3, 0xC3, 0x80, // Dark windows, lit yellowish at night
0x80, 0xC3, 0xD3, // Lighter windows, lit blueish at night
0xFF, 0xFF, 0x53, // Yellow light
0xFF, 0x21, 0x1D, // Red light
0x01, 0xDD, 0x01, // Green light
0x6B, 0x6B, 0x6B, // Non-darkening grey 1 (menus)
0x9B, 0x9B, 0x9B, // Non-darkening grey 2 (menus)
0xB3, 0xB3, 0xB3, // non-darkening grey 3 (menus)
0xC9, 0xC9, 0xC9, // Non-darkening grey 4 (menus)
0xDF, 0xDF, 0xDF, // Non-darkening grey 5 (menus)
0xFF, 0xFF, 0xE3, // Nearly white light at day, yellowish light at night
0xD3, 0xC3, 0x80, // Windows, lit yellow
0xD3, 0xC3, 0x80, // Windows, lit yellow
0xE1, 0x00, 0xE1, // purple light for signals
0x01, 0x01, 0xFF, // blue light
};
// the players colors and colors for simple drawing operations
// each three values correspond to a color, each 8 colors are a player color
static const uint8 special_pal[SPECIAL_COLOR_COUNT*3] =
{
36, 75, 103,
57, 94, 124,
76, 113, 145,
96, 132, 167,
116, 151, 189,
136, 171, 211,
156, 190, 233,
176, 210, 255,
88, 88, 88,
107, 107, 107,
125, 125, 125,
144, 144, 144,
162, 162, 162,
181, 181, 181,
200, 200, 200,
219, 219, 219,
17, 55, 133,
27, 71, 150,
37, 86, 167,
48, 102, 185,
58, 117, 202,
69, 133, 220,
79, 149, 237,
90, 165, 255,
123, 88, 3,
142, 111, 4,
161, 134, 5,
180, 157, 7,
198, 180, 8,
217, 203, 10,
236, 226, 11,
255, 249, 13,
86, 32, 14,
110, 40, 16,
134, 48, 18,
158, 57, 20,
182, 65, 22,
206, 74, 24,
230, 82, 26,
255, 91, 28,
34, 59, 10,
44, 80, 14,
53, 101, 18,
63, 122, 22,
77, 143, 29,
92, 164, 37,
106, 185, 44,
121, 207, 52,
0, 86, 78,
0, 108, 98,
0, 130, 118,
0, 152, 138,
0, 174, 158,
0, 196, 178,
0, 218, 198,
0, 241, 219,
74, 7, 122,
95, 21, 139,
116, 37, 156,
138, 53, 173,
160, 69, 191,
181, 85, 208,
203, 101, 225,
225, 117, 243,
59, 41, 0,
83, 55, 0,
107, 69, 0,
131, 84, 0,
155, 98, 0,
179, 113, 0,
203, 128, 0,
227, 143, 0,
87, 0, 43,
111, 11, 69,
135, 28, 92,
159, 45, 115,
183, 62, 138,
230, 74, 174,
245, 121, 194,
255, 156, 209,
20, 48, 10,
44, 74, 28,
68, 99, 45,
93, 124, 62,
118, 149, 79,
143, 174, 96,
168, 199, 113,
193, 225, 130,
54, 19, 29,
82, 44, 44,
110, 69, 58,
139, 95, 72,
168, 121, 86,
197, 147, 101,
226, 173, 115,
255, 199, 130,
8, 11, 100,
14, 22, 116,
20, 33, 139,
26, 44, 162,
41, 74, 185,
57, 104, 208,
76, 132, 231,
96, 160, 255,
43, 30, 46,
68, 50, 85,
93, 70, 110,
118, 91, 130,
143, 111, 170,
168, 132, 190,
193, 153, 210,
219, 174, 230,
63, 18, 12,
90, 38, 30,
117, 58, 42,
145, 78, 55,
172, 98, 67,
200, 118, 80,
227, 138, 92,
255, 159, 105,
11, 68, 30,
33, 94, 56,
54, 120, 81,
76, 147, 106,
98, 174, 131,
120, 201, 156,
142, 228, 181,
164, 255, 207,
64, 0, 0,
96, 0, 0,
128, 0, 0,
192, 0, 0,
255, 0, 0,
255, 64, 64,
255, 96, 96,
255, 128, 128,
0, 128, 0,
0, 196, 0,
0, 225, 0,
0, 240, 0,
0, 255, 0,
64, 255, 64,
94, 255, 94,
128, 255, 128,
0, 0, 128,
0, 0, 192,
0, 0, 224,
0, 0, 255,
0, 64, 255,
0, 94, 255,
0, 106, 255,
0, 128, 255,
128, 64, 0,
193, 97, 0,
215, 107, 0,
255, 128, 0,
255, 128, 0,
255, 149, 43,
255, 170, 85,
255, 193, 132,
8, 52, 0,
16, 64, 0,
32, 80, 4,
48, 96, 4,
64, 112, 12,
84, 132, 20,
104, 148, 28,
128, 168, 44,
164, 164, 0,
193, 193, 0,
215, 215, 0,
255, 255, 0,
255, 255, 32,
255, 255, 64,
255, 255, 128,
255, 255, 172,
32, 4, 0,
64, 20, 8,
84, 28, 16,
108, 44, 28,
128, 56, 40,
148, 72, 56,
168, 92, 76,
184, 108, 88,
64, 0, 0,
96, 8, 0,
112, 16, 0,
120, 32, 8,
138, 64, 16,
156, 72, 32,
174, 96, 48,
192, 128, 64,
32, 32, 0,
64, 64, 0,
96, 96, 0,
128, 128, 0,
144, 144, 0,
172, 172, 0,
192, 192, 0,
224, 224, 0,
64, 96, 8,
80, 108, 32,
96, 120, 48,
112, 144, 56,
128, 172, 64,
150, 210, 68,
172, 238, 80,
192, 255, 96,
32, 32, 32,
48, 48, 48,
64, 64, 64,
80, 80, 80,
96, 96, 96,
172, 172, 172,
236, 236, 236,
255, 255, 255,
41, 41, 54,
60, 45, 70,
75, 62, 108,
95, 77, 136,
113, 105, 150,
135, 120, 176,
165, 145, 218,
198, 191, 232,
};
/*
* tile raster width
*/
scr_coord_val tile_raster_width = 16; // zoomed
scr_coord_val base_tile_raster_width = 16; // original
// variables for storing currently used image procedure set and tile raster width
display_image_proc display_normal = NULL;
display_image_proc display_color = NULL;
display_blend_proc display_blend = NULL;
display_alpha_proc display_alpha = NULL;
signed short current_tile_raster_width = 0;
/*
* Zoom factor
*/
#define MAX_ZOOM_FACTOR (9)
#define ZOOM_NEUTRAL (3)
static uint32 zoom_factor = ZOOM_NEUTRAL;
static sint32 zoom_num[MAX_ZOOM_FACTOR+1] = { 2, 3, 4, 1, 3, 5, 1, 3, 1, 1 };
static sint32 zoom_den[MAX_ZOOM_FACTOR+1] = { 1, 2, 3, 1, 4, 8, 2, 8, 4, 8 };
/*
* Gets a colour index and returns RGB888
*/
uint32 get_color_rgb(uint8 idx)
{
// special_pal has 224 rgb colors
if (idx < SPECIAL_COLOR_COUNT) {
return special_pal[idx*3 + 0]<<16 | special_pal[idx*3 + 1]<<8 | special_pal[idx*3 + 2];
}
// if it uses one of the special light colours it's under display_day_lights
if (idx < SPECIAL_COLOR_COUNT + LIGHT_COUNT) {
const uint8 lidx = idx - SPECIAL_COLOR_COUNT;
return display_day_lights[lidx*3 + 0]<<16 | display_day_lights[lidx*3 + 1]<<8 | display_day_lights[lidx*3 + 2];
}
// Return black for anything else
return 0;
}
/**
* Convert indexed colors to rgb and back
*/
PIXVAL color_idx_to_rgb(PIXVAL idx)
{
return (specialcolormap_all_day[(idx)&0x00FF]);
}
PIXVAL color_rgb_to_idx(PIXVAL color)
{
for(PIXVAL i=0; i<=0xff; i++) {
if (specialcolormap_all_day[i] == color) {
return i;
}
}
return 0;
}
/*
* Convert env_t colours from RGB888 to the system format
*/
void env_t_rgb_to_system_colors()
{
// get system colours for the default colours or settings.xml
uint32 rgb = env_t::default_window_title_color_rgb;
env_t::default_window_title_color = get_system_color( rgb>>16, (rgb>>8)&0xFF, rgb&0xFF );
rgb = env_t::front_window_text_color_rgb;
env_t::front_window_text_color = get_system_color( rgb>>16, (rgb>>8)&0xFF, rgb&0xFF );
rgb = env_t::bottom_window_text_color_rgb;
env_t::bottom_window_text_color = get_system_color( rgb>>16, (rgb>>8)&0xFF, rgb&0xFF );
rgb = env_t::tooltip_color_rgb;
env_t::tooltip_color = get_system_color( rgb>>16, (rgb>>8)&0xFF, rgb&0xFF );
rgb = env_t::tooltip_textcolor_rgb;
env_t::tooltip_textcolor = get_system_color( rgb>>16, (rgb>>8)&0xFF, rgb&0xFF );
rgb = env_t::cursor_overlay_color_rgb;
env_t::cursor_overlay_color = get_system_color( rgb>>16, (rgb>>8)&0xFF, rgb&0xFF );
rgb = env_t::background_color_rgb;
env_t::background_color = get_system_color( rgb>>16, (rgb>>8)&0xFF, rgb&0xFF );
}
/* changes the raster width after loading */
scr_coord_val display_set_base_raster_width(scr_coord_val new_raster)
{
scr_coord_val old = base_tile_raster_width;
base_tile_raster_width = new_raster;
tile_raster_width = (new_raster * zoom_num[zoom_factor]) / zoom_den[zoom_factor];
return old;
}
// ----------------------------------- clipping routines ------------------------------------------
scr_coord_val display_get_width()
{
return disp_actual_width;
}
// only use, if you are really really sure!
void display_set_actual_width(scr_coord_val w)
{
disp_actual_width = w;
}
scr_coord_val display_get_height()
{
return disp_height;
}
void display_set_height(scr_coord_val const h)
{
disp_height = h;
}
/**
* Clips intervall [x,x+w] such that left <= x and x+w <= right.
* If @p w is negative, it stays negative.
* @returns difference between old and new value of @p x.
*/
inline int clip_intv(scr_coord_val &x, scr_coord_val &w, const scr_coord_val left, const scr_coord_val right)
{
scr_coord_val xx = min(x+w, right);
scr_coord_val xoff = left - x;
if (xoff > 0) { // equivalent to x < left
x = left;
}
else {
xoff = 0;
}
w = xx - x;
return xoff;
}
/// wrapper for clip_intv
static int clip_wh(scr_coord_val *x, scr_coord_val *w, const scr_coord_val left, const scr_coord_val right)
{
return clip_intv(*x, *w, left, right);
}
/// wrapper for clip_intv, @returns whether @p w is positive
static bool clip_lr(scr_coord_val *x, scr_coord_val *w, const scr_coord_val left, const scr_coord_val right)
{
clip_intv(*x, *w, left, right);
return *w > 0;
}
/**
* Get the clipping rectangle dimensions
*/
clip_dimension display_get_clip_wh(CLIP_NUM_DEF0)
{
return CR.clip_rect;
}
/**
* Set the clipping rectangle dimensions
*
* here, a pixel at coordinate xp is displayed if
* clip. x <= xp < clip.xx
* the right-most pixel of an image located at xp with width w is displayed if
* clip.x < xp+w <= clip.xx
* analogously for the y coordinate
*/
void display_set_clip_wh(scr_coord_val x, scr_coord_val y, scr_coord_val w, scr_coord_val h CLIP_NUM_DEF, bool fit)
{
if (!fit) {
clip_wh( &x, &w, 0, disp_width);
clip_wh( &y, &h, 0, disp_height);
}
else {
clip_wh( &x, &w, CR.clip_rect.x, CR.clip_rect.xx);
clip_wh( &y, &h, CR.clip_rect.y, CR.clip_rect.yy);
}
CR.clip_rect.x = x;
CR.clip_rect.y = y;
CR.clip_rect.w = w;
CR.clip_rect.h = h;
CR.clip_rect.xx = x + w; // watch out, clips to scr_coord_val max
CR.clip_rect.yy = y + h; // watch out, clips to scr_coord_val max
}
void display_push_clip_wh(scr_coord_val x, scr_coord_val y, scr_coord_val w, scr_coord_val h CLIP_NUM_DEF)
{
assert(!CR.swap_active);
// save active clipping rectangle
CR.clip_rect_swap = CR.clip_rect;
// active rectangle provided by parameters
display_set_clip_wh(x, y, w, h CLIP_NUM_PAR);
CR.swap_active = true;
}
void display_swap_clip_wh(CLIP_NUM_DEF0)
{
if (CR.swap_active) {
// swap clipping rectangles
clip_dimension save = CR.clip_rect;
CR.clip_rect = CR.clip_rect_swap;
CR.clip_rect_swap = save;
}
}
void display_pop_clip_wh(CLIP_NUM_DEF0)
{
if (CR.swap_active) {
// swap original clipping rectangle back
CR.clip_rect = CR.clip_rect_swap;
CR.swap_active = false;
}
}
/*
* Add clipping line through (x0,y0) and (x1,y1)
* with associated ribi
* if ribi & 16 then non-convex clipping.
*/
void add_poly_clip(int x0,int y0, int x1, int y1, int ribi CLIP_NUM_DEF)
{
if( CR.number_of_clips < MAX_POLY_CLIPS ) {
CR.poly_clips[CR.number_of_clips].clip_from_to( x0, y0, x1, y1, ribi&16 );
CR.clip_ribi[CR.number_of_clips] = ribi&15;
CR.number_of_clips++;
}
}
/*
* Clears all clipping lines
*/
void clear_all_poly_clip(CLIP_NUM_DEF0)
{
CR.number_of_clips = 0;
CR.active_ribi = 15; // set all to active
}
/*
* Activates clipping lines associated with ribi
* ie if clip_ribi[i] & active_ribi
*/
void activate_ribi_clip(int ribi CLIP_NUM_DEF)
{
CR.active_ribi = ribi;
}
/*
* Initialize clipping region for image starting at screen line y
*/
static inline void init_ranges(int y CLIP_NUM_DEF)
{
for( uint8 i = 0; i < CR.number_of_clips; i++ ) {
if( (CR.clip_ribi[i] & CR.active_ribi) ) {
CR.poly_clips[i].get_x_range( y, CR.xranges[i], CR.active_ribi & 16 );
}
}
}
/*
* Returns left/right border of visible area
* Computes l/r border for the next line (ie y+1)
* takes also clipping rectangle into account
*/
inline void get_xrange_and_step_y(int &xmin, int &xmax CLIP_NUM_DEF)
{
xmin = CR.clip_rect.x;
xmax = CR.clip_rect.xx;
for( uint8 i = 0; i < CR.number_of_clips; i++ ) {
if( (CR.clip_ribi[i] & CR.active_ribi) ) {
CR.poly_clips[i].inc_y( CR.xranges[i], xmin, xmax );
}
}
}
// ------------------------------ dirty tile stuff --------------------------------
/*
* Simutrans keeps a list of dirty areas, i.e. places that received new graphics
* and must be copied to the screen after an update
*/
static inline void mark_tile_dirty(const int x, const int y)
{
const int bit = x + y * tile_buffer_per_line;
#if 0
assert(bit / 8 < tile_buffer_length);
#endif
tile_dirty[bit >> 5] |= 1 << (bit & 31);
}