forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simgraph16.cc
4180 lines (3598 loc) · 105 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
/*
* Copyright 2010 Simutrans contributors
* Available under the Artistic License (see license.txt)
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "macros.h"
#include "simtypes.h"
#include "font.h"
#include "pathes.h"
#include "simconst.h"
#include "simsys.h"
#include "simmem.h"
#include "simdebug.h"
#include "besch/bild_besch.h"
#include "unicode.h"
#include "simticker.h"
#ifdef _MSC_VER
# include <io.h>
# define W_OK 2
#else
# include <unistd.h>
#endif
// first: find out, which copy routines we may use!
#ifndef __GNUC__
# undef USE_C
# define USE_C
# ifndef _M_IX86
# define ALIGN_COPY
# endif
#else
# if defined(USE_C) || !defined(__i386__)
# undef USE_C
# define USE_C
# if GCC_ATLEAST(4, 2) || !defined(__i386__)
# define ALIGN_COPY
# warning "Needs to use slower copy with GCC > 4.2.x"
# endif
# endif
#endif
#if MULTI_THREAD>1
#include <pthread.h>
// currently just redrawing/rezooming
static pthread_mutex_t rezoom_recode_img_mutex;
#endif
#include "simgraph.h"
// undefine for debugging the update routines
//#define DEBUG_FLUSH_BUFFER
/*
* Hajo: RGB 1555
*/
typedef uint16 PIXVAL;
#ifdef USE_SOFTPOINTER
static int softpointer = -1;
#endif
static int standard_pointer = -1;
#ifdef USE_SOFTPOINTER
/*
* Die Icon Leiste muss neu gezeichnet werden wenn der Mauszeiger darueber
* schwebt
*/
int old_my = -1;
#endif
/*
* Hajo: Current clipping rectangle
*/
static clip_dimension clip_rect;
// and the variables for polygon clipping
/*
* struct to hold the information about visible area
* at screen line y
* associated to some clipline
*/
struct xrange {
int sx,sy;
KOORD_VAL y;
bool non_convex_active;
};
#define MAX_POLY_CLIPS 6
static xrange xranges[MAX_POLY_CLIPS];
static uint8 clip_ribi[MAX_POLY_CLIPS];
static int number_of_clips =0;
static uint8 active_ribi = 15; // set all to active
/* Flag, if we have Unicode font => do unicode (UTF8) support! *
* @author prissi
* @date 29.11.04
*/
static bool has_unicode = false;
static font_type large_font = { 0, 0, 0, NULL, NULL };
// needed for resizing gui
int large_font_ascent = 9;
int large_font_total_height = 11;
#define MAX_PLAYER_COUNT (16)
#define RGBMAPSIZE (0x8000+LIGHT_COUNT+16)
/*
* Hajo: 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 - : Day&Night special colors
*/
static PIXVAL rgbmap_day_night[RGBMAPSIZE];
/*
* Hajo: same as rgbmap_day_night, but always daytime colors
*/
static PIXVAL rgbmap_all_day[RGBMAPSIZE];
/**
* Hajo:used by pixel copy functions, is one of rgbmap_day_night
* rgbmap_all_day
*/
static PIXVAL *rgbmap_current = 0;
/*
* Hajo: mapping table for specialcolors (AI player colors)
* to actual output format - day&night mode
* 16 sets of 16 colors
*/
static PIXVAL specialcolormap_day_night[256];
/*
* Hajo: mapping table for specialcolors (AI player colors)
* to actual output format - all day mode
* 16 sets of 16 colors
*/
static PIXVAL specialcolormap_all_day[256];
// offsets of first and second comany color
static uint8 player_offsets[MAX_PLAYER_COUNT][2];
/*
* Hajo: Image map descriptor struture
*/
struct imd {
sint16 base_x; // min x offset
sint16 base_y; // min y offset
sint16 base_w; // width
sint16 base_h; // height
sint16 x; // current (zoomed) min x offset
sint16 y; // current (zoomed) min y offset
sint16 w; // current (zoomed) width
sint16 h; // current (zoomed) height
uint32 len; // base image data size (used for allocation purposes only)
uint8 recode_flags; // divers flags for recoding
uint8 player_flags; // 128 = free/needs recode, otherwise coded to this color in player_data
PIXVAL* data; // current data, zoomed and adapted to output format RGB 555 or RGB 565
PIXVAL* zoom_data; // zoomed original data
PIXVAL* base_data; // original image data
PIXVAL* player_data; // current data coded for player1 (since many building belong to him)
};
// flags for recoding
#define FLAG_ZOOMABLE (1)
#define FLAG_PLAYERCOLOR (2)
#define FLAG_REZOOM (4)
#define FLAG_NORMAL_RECODE (8)
#define FLAG_POSITION_CHANGED (16)
#define NEED_PLAYER_RECODE (128)
static KOORD_VAL disp_width = 640;
static KOORD_VAL disp_actual_width = 640;
static KOORD_VAL disp_height = 480;
/*
* 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;
/*
* Hajo: dirty tile management strcutures
*/
#define DIRTY_TILE_SIZE 16
#define DIRTY_TILE_SHIFT 4
static unsigned char *tile_dirty = NULL;
static unsigned char *tile_dirty_old = NULL;
static int tiles_per_line = 0;
static int tile_lines = 0;
static int tile_buffer_length = 0;
static int light_level = 0;
static int night_shift = -1;
/*
* Hajo: speical colors during daytime
*/
COLOR_VAL 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
};
/*
* Hajo: speical colors during nighttime
*/
COLOR_VAL 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 eight colors are corresponding to a player color
static const COLOR_VAL special_pal[224*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,
};
/*
* Hajo: tile raster width
*/
KOORD_VAL tile_raster_width = 16; // zoomed
KOORD_VAL base_tile_raster_width = 16; // original
// Knightly : 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;
signed short current_tile_raster_width = 0;
/*
* Hajo: Zoom factor
*/
#define MAX_ZOOM_FACTOR (9)
static uint32 zoom_factor = 3;
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 };
/* changes the raster width after loading */
KOORD_VAL display_set_base_raster_width(KOORD_VAL new_raster)
{
KOORD_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 ------------------------------------------
sint16 display_get_width(void)
{
return disp_actual_width;
}
// only use, if you are really really sure!
void display_set_actual_width(KOORD_VAL w)
{
disp_actual_width = w;
}
sint16 display_get_height(void)
{
return disp_height;
}
void display_set_height(KOORD_VAL const h)
{
disp_height = h;
}
/**
* this sets width < 0 if the range is out of bounds
* so check the value after calling and before displaying
* @author Hj. Malthaner
*/
static int clip_wh(KOORD_VAL *x, KOORD_VAL *width, const KOORD_VAL min_width, const KOORD_VAL max_width)
{
if (*width <= 0) {
*width = 0;
return 0;
}
if (*x + *width > max_width) {
*width = max_width - *x;
}
if (*x < min_width) {
const KOORD_VAL xoff = min_width - *x;
*width += *x-min_width;
*x = min_width;
return xoff;
}
return 0;
}
/**
* places x and w within bounds left and right
* if nothing to show, returns false
* @author Niels Roest
*/
static bool clip_lr(KOORD_VAL *x, KOORD_VAL *w, const KOORD_VAL left, const KOORD_VAL right)
{
const KOORD_VAL l = *x; // leftmost pixel
const sint32 r = (sint32)*x + (sint32)*w; // rightmost pixel
if (*w <= 0 || l >= right || r <= left) {
*w = 0;
return false;
}
// there is something to show.
if (l < left) {
*w -= left - l;
*x = left;
}
if (r > right) {
*w -= (KOORD_VAL)(r - right);
}
return *w > 0;
}
/**
* Ermittelt Clipping Rechteck
* @author Hj. Malthaner
*/
clip_dimension display_get_clip_wh()
{
return clip_rect;
}
/**
* Setzt Clipping Rechteck
* @author Hj. Malthaner
*
* 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(KOORD_VAL x, KOORD_VAL y, KOORD_VAL w, KOORD_VAL h)
{
clip_wh(&x, &w, 0, disp_width);
clip_wh(&y, &h, 0, disp_height);
clip_rect.x = x;
clip_rect.y = y;
clip_rect.w = w;
clip_rect.h = h;
clip_rect.xx = x + w; // watch out, clips to KOORD_VAL max
clip_rect.yy = y + h; // watch out, clips to KOORD_VAL max
}
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,sdy,sdx;
int 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 = (dx << 16) / steps;
sdy = (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 = (dx << 16)/dy;
}
}
else if (dy<0) {
if (dy < dx) {
inc = 0; // (+1)-1 << 16;
}
else {
inc = (1 << 16) - (dx << 16) / dy;
}
}
}
// 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(KOORD_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;
}
else if (dy != 0) {
// init Bresenham algorithm
const int t = ((y-y0) << 16) / sdy;
// sx >> 16 = x
// sy >> 16 = y
r.sx = t * sdx + inc + (x0 << 16);
r.sy = t * sdy + (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;
if (dy != 0) {
// init Bresenham algorithm
const int t = ((r.y-y0) << 16) / sdy;
// sx >> 16 = x
// sy >> 16 = y
r.sx = t * sdx + inc + (x0 << 16);
r.sy = t * sdy + (y0 << 16);
if (dy > 0) {
const int r_xmin = r.sx >> 16;
if (xmin < r_xmin) xmin = r_xmin;
}
else {
const int r_xmax = r.sx >> 16;
if (xmax > r_xmax) xmax = r_xmax;
}
}
}
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;
}
}
}
// go along the ray, Bresenham
else 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;
}
}
// horicontal clip
else {
const bool clip = dx*(r.y-y0)>0;
if (clip) {
// invisible row
xmin = +1;
xmax = -1;
}
}
}
};
static clip_line_t poly_clips[MAX_POLY_CLIPS];
/*
* 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)
{
if (number_of_clips < MAX_POLY_CLIPS) {
poly_clips[number_of_clips].clip_from_to(x0,y0,x1,y1,ribi&16);
clip_ribi[number_of_clips] = ribi&15;
number_of_clips++;
}
}
/*
* Clears all clipping lines
*/
void clear_all_poly_clip()
{
number_of_clips = 0;
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)
{
active_ribi = ribi;
}
/*
* Initialize clipping region for image starting at screen line y
*/
static inline void init_ranges(int y)
{
for (uint8 i=0; i<number_of_clips; i++) {
if (clip_ribi[i] & active_ribi) {
poly_clips[i].get_x_range(y,xranges[i], 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)
{
xmin = clip_rect.x;
xmax = clip_rect.xx;
for (uint8 i=0; i<number_of_clips; i++) {
if (clip_ribi[i] & active_ribi) {
poly_clips[i].inc_y(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 * tiles_per_line;
#if 0
assert(bit / 8 < tile_buffer_length);
#endif
tile_dirty[bit >> 3] |= 1 << (bit & 7);
}
static inline void mark_tiles_dirty(const int x1, const int x2, const int y)
{
int bit = y * tiles_per_line + x1;
const int end = bit + x2 - x1;
do {
#if 0
assert(bit / 8 < tile_buffer_length);
#endif
tile_dirty[bit >> 3] |= 1 << (bit & 7);
} while (++bit <= end);
}
static inline int is_tile_dirty(const int x, const int y)
{
const int bit = x + y * tiles_per_line;
const int bita = bit >> 3;
const int bitb = 1 << (bit & 7);
return (tile_dirty[bita] | tile_dirty_old[bita]) & bitb;
}
/**
* Markiert ein Tile as schmutzig, ohne Clipping
* @author Hj. Malthaner
*/
static void mark_rect_dirty_nc(KOORD_VAL x1, KOORD_VAL y1, KOORD_VAL x2, KOORD_VAL y2)
{
// floor to tile size
x1 >>= DIRTY_TILE_SHIFT;
y1 >>= DIRTY_TILE_SHIFT;
x2 >>= DIRTY_TILE_SHIFT;
y2 >>= DIRTY_TILE_SHIFT;
#if 0
assert(x1 >= 0);
assert(x1 < tiles_per_line);
assert(y1 >= 0);
assert(y1 < tile_lines);
assert(x2 >= 0);
assert(x2 < tiles_per_line);
assert(y2 >= 0);
assert(y2 < tile_lines);
#endif
for (; y1 <= y2; y1++) {
mark_tiles_dirty(x1, x2, y1);
}
}
/**
* Markiert ein Tile as schmutzig, mit Clipping
* @author Hj. Malthaner
*/
void mark_rect_dirty_wc(KOORD_VAL x1, KOORD_VAL y1, KOORD_VAL x2, KOORD_VAL y2)
{
// inside display?
if (x2 >= 0 && y2 >= 0 && x1 < disp_width && y1 < disp_height) {
if (x1 < 0) {
x1 = 0;
}
if (y1 < 0) {
y1 = 0;
}
if (x2 >= disp_width) {
x2 = disp_width - 1;
}
if (y2 >= disp_height) {
y2 = disp_height - 1;
}
mark_rect_dirty_nc(x1, y1, x2, y2);
}
}
/**
* the area of this image need update
* @author Hj. Malthaner
*/
void display_mark_img_dirty(unsigned bild, KOORD_VAL xp, KOORD_VAL yp)
{
if (bild < anz_images) {
mark_rect_dirty_wc(
xp + images[bild].x,
yp + images[bild].y,
xp + images[bild].x + images[bild].w - 1,
yp + images[bild].y + images[bild].h - 1
);
}
}
// ------------------------- rendering images for display --------------------------------
/*
* Simutrans caches two player colored images, to allow faster drawing of them
* They are derived from a base image, which may need zooming too
*/