/
draw.cpp
2547 lines (2111 loc) · 83.1 KB
/
draw.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
//=============================================================================
//
// Adventure Game Studio (AGS)
//
// Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
// The full list of copyright holders can be found in the Copyright.txt
// file, which is part of this source code distribution.
//
// The AGS source code is provided under the Artistic License 2.0.
// A copy of this license can be found in the file License.txt and at
// http://www.opensource.org/licenses/artistic-license-2.0.php
//
//=============================================================================
#include "aastr.h"
#include "ac/common.h"
#include "util/compress.h"
#include "ac/view.h"
#include "ac/charactercache.h"
#include "ac/characterextras.h"
#include "ac/characterinfo.h"
#include "ac/display.h"
#include "ac/draw.h"
#include "ac/gamesetup.h"
#include "ac/gamesetupstruct.h"
#include "ac/gamestate.h"
#include "ac/global_game.h"
#include "ac/global_gui.h"
#include "ac/global_region.h"
#include "ac/gui.h"
#include "ac/mouse.h"
#include "ac/objectcache.h"
#include "ac/overlay.h"
#include "ac/record.h"
#include "ac/roomobject.h"
#include "ac/roomstatus.h"
#include "ac/roomstruct.h"
#include "ac/runtime_defines.h"
#include "ac/screenoverlay.h"
#include "ac/spritelistentry.h"
#include "ac/string.h"
#include "ac/viewframe.h"
#include "ac/viewport.h"
#include "ac/walkablearea.h"
#include "ac/walkbehind.h"
#include "ac/dynobj/scriptsystem.h"
#include "debug/debugger.h"
#include "debug/debug_log.h"
#include "font/fonts.h"
#include "gui/guimain.h"
#include "media/audio/audio.h"
#include "media/audio/soundclip.h"
#include "platform/base/agsplatformdriver.h"
#include "plugin/agsplugin.h"
#include "ac/spritecache.h"
#include "gfx/gfx_util.h"
#include "gfx/graphicsdriver.h"
#include "gfx/ali3dexception.h"
#include "gfx/blender.h"
#include "main/graphics_mode.h"
using namespace AGS::Common;
using namespace AGS::Engine;
#if defined(ANDROID_VERSION)
#include <sys/stat.h>
#include <android/log.h>
extern "C" void android_render();
#endif
#if defined(IOS_VERSION)
extern "C" void ios_render();
#endif
extern GameSetup usetup;
extern GameSetupStruct game;
extern GameState play;
extern int current_screen_resolution_multiplier;
extern ScriptSystem scsystem;
extern AGSPlatformDriver *platform;
extern roomstruct thisroom;
extern char noWalkBehindsAtAll;
extern unsigned int loopcounter;
extern char *walkBehindExists; // whether a WB area is in this column
extern int *walkBehindStartY, *walkBehindEndY;
extern int walkBehindLeft[MAX_OBJ], walkBehindTop[MAX_OBJ];
extern int walkBehindRight[MAX_OBJ], walkBehindBottom[MAX_OBJ];
extern IDriverDependantBitmap *walkBehindBitmap[MAX_OBJ];
extern int walkBehindsCachedForBgNum;
extern WalkBehindMethodEnum walkBehindMethod;
extern int walk_behind_baselines_changed;
extern int spritewidth[MAX_SPRITES],spriteheight[MAX_SPRITES];
extern SpriteCache spriteset;
extern RoomStatus*croom;
extern int our_eip;
extern int in_new_room;
extern RoomObject*objs;
extern ViewStruct*views;
extern CharacterCache *charcache;
extern ObjectCache objcache[MAX_INIT_SPR];
extern int displayed_room;
extern CharacterExtras *charextra;
extern CharacterInfo*playerchar;
extern int eip_guinum;
extern ScreenOverlay screenover[MAX_SCREEN_OVERLAYS];
extern int numscreenover;
extern int screen_reset;
extern int is_complete_overlay;
extern int cur_mode,cur_cursor;
extern int mouse_frame,mouse_delay;
extern int lastmx,lastmy;
extern int replay_time;
extern int mouse_on_iface_button;
extern IDriverDependantBitmap *mouseCursor;
extern int hotx,hoty;
extern int bg_just_changed;
// How is this actually used??
// We need COLOR_DEPTH_24 to allow it to load the preload PCX in hi-col
BEGIN_COLOR_DEPTH_LIST
COLOR_DEPTH_8
COLOR_DEPTH_15
COLOR_DEPTH_16
COLOR_DEPTH_24
COLOR_DEPTH_32
END_COLOR_DEPTH_LIST
color palette[256];
COLOR_MAP maincoltable;
IGraphicsDriver *gfxDriver;
IDriverDependantBitmap *blankImage = NULL;
IDriverDependantBitmap *blankSidebarImage = NULL;
IDriverDependantBitmap *debugConsole = NULL;
// actsps is used for temporary storage of the bitamp image
// of the latest version of the sprite
int actSpsCount = 0;
Bitmap **actsps;
IDriverDependantBitmap* *actspsbmp;
// temporary cache of walk-behind for this actsps image
Bitmap **actspswb;
IDriverDependantBitmap* *actspswbbmp;
CachedActSpsData* actspswbcache;
Bitmap *virtual_screen;
bool current_background_is_dirty = false;
Bitmap *_old_screen=NULL;
Bitmap *_sub_screen=NULL;
int offsetx = 0, offsety = 0;
IDriverDependantBitmap* roomBackgroundBmp = NULL;
#define MAX_SPRITES_ON_SCREEN 76
SpriteListEntry sprlist[MAX_SPRITES_ON_SCREEN];
int sprlistsize=0;
#define MAX_THINGS_TO_DRAW 125
SpriteListEntry thingsToDrawList[MAX_THINGS_TO_DRAW];
int thingsToDrawSize = 0;
//GUIMain dummygui;
//GUIButton dummyguicontrol;
Bitmap **guibg = NULL;
IDriverDependantBitmap **guibgbmp = NULL;
Bitmap *debugConsoleBuffer = NULL;
// whether there are currently remnants of a DisplaySpeech
int screen_is_dirty = 0;
Bitmap *raw_saved_screen = NULL;
Bitmap *dynamicallyCreatedSurfaces[MAX_DYNAMIC_SURFACES];
// TODO: move to test unit
extern Bitmap *test_allegro_bitmap;
extern IDriverDependantBitmap *test_allegro_ddb;
void allegro_bitmap_test_draw()
{
if (test_allegro_bitmap)
{
test_allegro_bitmap->FillTransparent();
test_allegro_bitmap->FillRect(Rect(50,50,150,150), 15);
if (test_allegro_ddb == NULL)
{
test_allegro_ddb = gfxDriver->CreateDDBFromBitmap(test_allegro_bitmap, false, true);
}
else
{
gfxDriver->UpdateDDBFromBitmap(test_allegro_ddb, test_allegro_bitmap, false);
}
gfxDriver->DrawSprite(-offsetx, -offsety, test_allegro_ddb);
}
}
void setpal() {
set_palette_range(palette, 0, 255, 0);
}
#ifdef USE_15BIT_FIX
extern "C" {
AL_FUNC(GFX_VTABLE *, _get_vtable, (int color_depth));
}
Bitmap *convert_16_to_15(Bitmap *iii) {
// int xx,yy,rpix;
int iwid = iii->GetWidth(), ihit = iii->GetHeight();
int x,y;
if (iii->GetColorDepth() > 16) {
// we want a 32-to-24 conversion
Bitmap *tempbl = BitmapHelper::CreateBitmap(iwid,ihit,ScreenResolution.ColorDepth);
if (ScreenResolution.ColorDepth < 24) {
// 32-to-16
tempbl->Blit(iii, 0, 0, 0, 0, iwid, ihit);
return tempbl;
}
GFX_VTABLE *vtable = _get_vtable(ScreenResolution.ColorDepth);
if (vtable == NULL) {
quit("unable to get 24-bit bitmap vtable");
}
// TODO
((BITMAP*)tempbl->GetAllegroBitmap())->vtable = vtable;
for (y=0; y < tempbl->GetHeight(); y++) {
unsigned char*p32 = (unsigned char *)iii->GetScanLine(y);
unsigned char*p24 = (unsigned char *)tempbl->GetScanLine(y);
// strip out the alpha channel bit and copy the rest across
for (x=0; x < tempbl->GetWidth(); x++) {
memcpy(&p24[x * 3], &p32[x * 4], 3);
}
}
return tempbl;
}
// we want a 16-to-15 converstion
unsigned short c,r,ds,b;
// we do this process manually - no allegro color conversion
// because we store the RGB in a particular order in the data files
Bitmap *tempbl = BitmapHelper::CreateBitmap(iwid,ihit,15);
GFX_VTABLE *vtable = _get_vtable(15);
if (vtable == NULL) {
quit("unable to get 15-bit bitmap vtable");
}
// TODO
((BITMAP*)tempbl->GetAllegroBitmap())->vtable = vtable;
for (y=0; y < tempbl->GetHeight(); y++) {
unsigned short*p16 = (unsigned short *)iii->GetScanLine(y);
unsigned short*p15 = (unsigned short *)tempbl->GetScanLine(y);
for (x=0; x < tempbl->GetWidth(); x++) {
c = p16[x];
b = _rgb_scale_5[c & 0x1F];
ds = _rgb_scale_6[(c >> 5) & 0x3F];
r = _rgb_scale_5[(c >> 11) & 0x1F];
p15[x] = makecol15(r, ds, b);
}
}
/*
// the auto color conversion doesn't seem to work
for (xx=0;xx<iwid;xx++) {
for (yy=0;yy<ihit;yy++) {
rpix = _getpixel16(iii,xx,yy);
rpix = (rpix & 0x001f) | ((rpix >> 1) & 0x7fe0);
// again putpixel16 because the dest is actually 16bit
_putpixel15(tempbl,xx,yy,rpix);
}
}*/
return tempbl;
}
int _places_r = 3, _places_g = 2, _places_b = 3;
// convert RGB to BGR for strange graphics cards
Bitmap *convert_16_to_16bgr(Bitmap *tempbl) {
int x,y;
unsigned short c,r,ds,b;
for (y=0; y < tempbl->GetHeight(); y++) {
unsigned short*p16 = (unsigned short *)tempbl->GetScanLine(y);
for (x=0; x < tempbl->GetWidth(); x++) {
c = p16[x];
if (c != MASK_COLOR_16) {
b = _rgb_scale_5[c & 0x1F];
ds = _rgb_scale_6[(c >> 5) & 0x3F];
r = _rgb_scale_5[(c >> 11) & 0x1F];
// allegro assumes 5-6-5 for 16-bit
p16[x] = (((r >> _places_r) << _rgb_r_shift_16) |
((ds >> _places_g) << _rgb_g_shift_16) |
((b >> _places_b) << _rgb_b_shift_16));
}
}
}
return tempbl;
}
#endif
// PSP: convert 32 bit RGB to BGR.
Bitmap *convert_32_to_32bgr(Bitmap *tempbl) {
int i = 0;
int j = 0;
unsigned char* current;
while (i < tempbl->GetHeight())
{
current = tempbl->GetScanLineForWriting(i);
while (j < tempbl->GetWidth())
{
current[0] ^= current[2];
current[2] ^= current[0];
current[0] ^= current[2];
current += 4;
j++;
}
i++;
j = 0;
}
return tempbl;
}
// Begin resolution system functions
// Multiplies up the number of pixels depending on the current
// resolution, to give a relatively fixed size at any game res
AGS_INLINE int get_fixed_pixel_size(int pixels)
{
return pixels * current_screen_resolution_multiplier;
}
AGS_INLINE int convert_to_low_res(int coord)
{
if (game.options[OPT_NATIVECOORDINATES] == 0)
return coord;
else
return coord / current_screen_resolution_multiplier;
}
AGS_INLINE int convert_back_to_high_res(int coord)
{
if (game.options[OPT_NATIVECOORDINATES] == 0)
return coord;
else
return coord * current_screen_resolution_multiplier;
}
AGS_INLINE int multiply_up_coordinate(int coord)
{
if (game.options[OPT_NATIVECOORDINATES] == 0)
return coord * current_screen_resolution_multiplier;
else
return coord;
}
AGS_INLINE void multiply_up_coordinates(int *x, int *y)
{
if (game.options[OPT_NATIVECOORDINATES] == 0)
{
x[0] *= current_screen_resolution_multiplier;
y[0] *= current_screen_resolution_multiplier;
}
}
AGS_INLINE void multiply_up_coordinates_round_up(int *x, int *y)
{
if (game.options[OPT_NATIVECOORDINATES] == 0)
{
x[0] = x[0] * current_screen_resolution_multiplier + (current_screen_resolution_multiplier - 1);
y[0] = y[0] * current_screen_resolution_multiplier + (current_screen_resolution_multiplier - 1);
}
}
AGS_INLINE int divide_down_coordinate(int coord)
{
if (game.options[OPT_NATIVECOORDINATES] == 0)
return coord / current_screen_resolution_multiplier;
else
return coord;
}
AGS_INLINE int divide_down_coordinate_round_up(int coord)
{
if (game.options[OPT_NATIVECOORDINATES] == 0)
return (coord / current_screen_resolution_multiplier) + (current_screen_resolution_multiplier - 1);
else
return coord;
}
// End resolution system functions
// ** dirty rectangle system **
#define MAXDIRTYREGIONS 25
#define WHOLESCREENDIRTY (MAXDIRTYREGIONS + 5)
#define MAX_SPANS_PER_ROW 4
struct InvalidRect {
int x1, y1, x2, y2;
};
struct IRSpan {
int x1, x2;
int mergeSpan(int tx1, int tx2);
};
struct IRRow {
IRSpan span[MAX_SPANS_PER_ROW];
int numSpans;
};
IRRow *dirtyRow = NULL;
int _dirtyRowSize;
InvalidRect dirtyRegions[MAXDIRTYREGIONS];
int numDirtyRegions = 0;
int numDirtyBytes = 0;
int IRSpan::mergeSpan(int tx1, int tx2) {
if ((tx1 > x2) || (tx2 < x1))
return 0;
// overlapping, increase the span
if (tx1 < x1)
x1 = tx1;
if (tx2 > x2)
x2 = tx2;
return 1;
}
void init_invalid_regions(int scrnHit) {
numDirtyRegions = WHOLESCREENDIRTY;
dirtyRow = (IRRow*)malloc(sizeof(IRRow) * scrnHit);
memset(dirtyRow, 0, sizeof(IRRow) * scrnHit);
for (int e = 0; e < scrnHit; e++)
dirtyRow[e].numSpans = 0;
_dirtyRowSize = scrnHit;
}
void update_invalid_region(Bitmap *ds, int x, int y, Bitmap *src) {
int i;
// convert the offsets for the destination into
// offsets into the source
x = -x;
y = -y;
if (numDirtyRegions == WHOLESCREENDIRTY) {
ds->Blit(src, x, y, 0, 0, ds->GetWidth(), ds->GetHeight());
}
else {
int k, tx1, tx2, srcdepth = src->GetColorDepth();
if ((srcdepth == ds->GetColorDepth()) && (ds->IsMemoryBitmap())) {
int bypp = src->GetBPP();
// do the fast copy
for (i = 0; i < play.viewport.GetHeight(); i++) {
const uint8_t *src_scanline = src->GetScanLine(i + y);
uint8_t *dst_scanline = ds->GetScanLineForWriting(i);
const IRRow &dirty_row = dirtyRow[i];
for (k = 0; k < dirty_row.numSpans; k++) {
tx1 = dirty_row.span[k].x1;
tx2 = dirty_row.span[k].x2;
memcpy(&dst_scanline[tx1 * bypp], &src_scanline[(tx1 + x) * bypp], ((tx2 - tx1) + 1) * bypp);
}
}
}
else {
// do the fast copy
int rowsInOne;
for (i = 0; i < play.viewport.GetHeight(); i++) {
rowsInOne = 1;
// if there are rows with identical masks, do them all in one go
while ((i+rowsInOne < play.viewport.GetHeight()) && (memcmp(&dirtyRow[i], &dirtyRow[i+rowsInOne], sizeof(IRRow)) == 0))
rowsInOne++;
const IRRow &dirty_row = dirtyRow[i];
for (k = 0; k < dirty_row.numSpans; k++) {
tx1 = dirty_row.span[k].x1;
tx2 = dirty_row.span[k].x2;
ds->Blit(src, tx1 + x, i + y, tx1, i, (tx2 - tx1) + 1, rowsInOne);
}
i += (rowsInOne - 1);
}
}
/* else {
// update the dirty regions
for (i = 0; i < numDirtyRegions; i++) {
->Blit(src, dest, x + dirtyRegions[i].x1, y + dirtyRegions[i].y1,
dirtyRegions[i].x1, dirtyRegions[i].y1,
(dirtyRegions[i].x2 - dirtyRegions[i].x1) + 1,
(dirtyRegions[i].y2 - dirtyRegions[i].y1) + 1);
}
}*/
}
}
void update_invalid_region_and_reset(Bitmap *ds, int x, int y, Bitmap *src) {
int i;
update_invalid_region(ds, x, y, src);
// screen has been updated, no longer dirty
numDirtyRegions = 0;
numDirtyBytes = 0;
for (i = 0; i < _dirtyRowSize; i++)
dirtyRow[i].numSpans = 0;
}
int combine_new_rect(InvalidRect *r1, InvalidRect *r2) {
// check if new rect is within old rect X-wise
if ((r2->x1 >= r1->x1) && (r2->x2 <= r1->x2)) {
if ((r2->y1 >= r1->y1) && (r2->y2 <= r1->y2)) {
// Y is also within the old one - scrap the new rect
return 1;
}
}
return 0;
}
void invalidate_rect(int x1, int y1, int x2, int y2) {
if (numDirtyRegions >= MAXDIRTYREGIONS) {
// too many invalid rectangles, just mark the whole thing dirty
numDirtyRegions = WHOLESCREENDIRTY;
return;
}
int a;
if (x1 >= play.viewport.GetWidth()) x1 = play.viewport.GetWidth()-1;
if (y1 >= play.viewport.GetHeight()) y1 = play.viewport.GetHeight()-1;
if (x2 >= play.viewport.GetWidth()) x2 = play.viewport.GetWidth()-1;
if (y2 >= play.viewport.GetHeight()) y2 = play.viewport.GetHeight()-1;
if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0;
if (x2 < 0) x2 = 0;
if (y2 < 0) y2 = 0;
/*
dirtyRegions[numDirtyRegions].x1 = x1;
dirtyRegions[numDirtyRegions].y1 = y1;
dirtyRegions[numDirtyRegions].x2 = x2;
dirtyRegions[numDirtyRegions].y2 = y2;
for (a = 0; a < numDirtyRegions; a++) {
// see if we can merge it into any other regions
if (combine_new_rect(&dirtyRegions[a], &dirtyRegions[numDirtyRegions]))
return;
}
numDirtyBytes += (x2 - x1) * (y2 - y1);
if (numDirtyBytes > (play.viewport.GetWidth() * play.viewport.GetHeight()) / 2)
numDirtyRegions = WHOLESCREENDIRTY;
else {*/
numDirtyRegions++;
// ** Span code
int s, foundOne;
// add this rect to the list for this row
for (a = y1; a <= y2; a++) {
foundOne = 0;
for (s = 0; s < dirtyRow[a].numSpans; s++) {
if (dirtyRow[a].span[s].mergeSpan(x1, x2)) {
foundOne = 1;
break;
}
}
if (foundOne) {
// we were merged into a span, so we're ok
int t;
// check whether now two of the spans overlap each other
// in which case merge them
for (s = 0; s < dirtyRow[a].numSpans; s++) {
for (t = s + 1; t < dirtyRow[a].numSpans; t++) {
if (dirtyRow[a].span[s].mergeSpan(dirtyRow[a].span[t].x1, dirtyRow[a].span[t].x2)) {
dirtyRow[a].numSpans--;
for (int u = t; u < dirtyRow[a].numSpans; u++)
dirtyRow[a].span[u] = dirtyRow[a].span[u + 1];
break;
}
}
}
}
else if (dirtyRow[a].numSpans < MAX_SPANS_PER_ROW) {
dirtyRow[a].span[dirtyRow[a].numSpans].x1 = x1;
dirtyRow[a].span[dirtyRow[a].numSpans].x2 = x2;
dirtyRow[a].numSpans++;
}
else {
// didn't fit in an existing span, and there are none spare
int nearestDist = 99999, nearestWas = -1, extendLeft;
int tleft, tright;
// find the nearest span, and enlarge that to include this rect
for (s = 0; s < dirtyRow[a].numSpans; s++) {
tleft = dirtyRow[a].span[s].x1 - x2;
if ((tleft > 0) && (tleft < nearestDist)) {
nearestDist = tleft;
nearestWas = s;
extendLeft = 1;
}
tright = x1 - dirtyRow[a].span[s].x2;
if ((tright > 0) && (tright < nearestDist)) {
nearestDist = tright;
nearestWas = s;
extendLeft = 0;
}
}
if (extendLeft)
dirtyRow[a].span[nearestWas].x1 = x1;
else
dirtyRow[a].span[nearestWas].x2 = x2;
}
}
// ** End span code
//}
}
void invalidate_sprite(int x1, int y1, IDriverDependantBitmap *pic) {
invalidate_rect(x1, y1, x1 + pic->GetWidth(), y1 + pic->GetHeight());
}
void draw_and_invalidate_text(Bitmap *ds, int x1, int y1, int font, color_t text_color, const char *text) {
wouttext_outline(ds, x1, y1, font, text_color, (char*)text);
invalidate_rect(x1, y1, x1 + wgettextwidth_compensate(text, font), y1 + wgetfontheight(font) + get_fixed_pixel_size(1));
}
void invalidate_screen() {
// mark the whole screen dirty
numDirtyRegions = WHOLESCREENDIRTY;
}
// ** dirty rectangle system end **
void mark_current_background_dirty()
{
current_background_is_dirty = true;
}
void render_black_borders(int atx, int aty)
{
if (!gfxDriver->UsesMemoryBackBuffer())
{
if (aty > 0)
{
// letterbox borders
blankImage->SetStretch(game.size.Width, aty);
gfxDriver->DrawSprite(-atx, -aty, blankImage);
gfxDriver->DrawSprite(0, play.viewport.GetHeight(), blankImage);
}
if (atx > 0)
{
// sidebar borders for widescreen
blankSidebarImage->SetStretch(atx, play.viewport.GetHeight());
gfxDriver->DrawSprite(-atx, 0, blankSidebarImage);
gfxDriver->DrawSprite(play.viewport.GetWidth(), 0, blankSidebarImage);
}
}
}
void render_to_screen(Bitmap *toRender, int atx, int aty) {
atx += play.viewport.Left;
aty += play.viewport.Top;
gfxDriver->SetRenderOffset(atx, aty);
render_black_borders(atx, aty);
gfxDriver->DrawSprite(AGSE_FINALSCREENDRAW, 0, NULL);
if (play.screen_is_faded_out)
{
if (gfxDriver->UsesMemoryBackBuffer())
gfxDriver->RenderToBackBuffer();
gfxDriver->ClearDrawList();
return;
}
// only vsync in full screen mode, it makes things worse
// in a window
gfxDriver->EnableVsyncBeforeRender((scsystem.vsync > 0) && (!usetup.windowed));
bool succeeded = false;
while (!succeeded)
{
try
{
gfxDriver->Render((GlobalFlipType)play.screen_flipped);
#if defined(ANDROID_VERSION)
if (game.color_depth == 1)
android_render();
#elif defined(IOS_VERSION)
if (game.color_depth == 1)
ios_render();
#endif
succeeded = true;
}
catch (Ali3DFullscreenLostException)
{
platform->Delay(500);
}
}
}
void clear_letterbox_borders() {
if (multiply_up_coordinate(thisroom.height) < game.size.Height) {
// blank out any traces in borders left by a full-screen room
gfxDriver->ClearRectangle(0, 0, _old_screen->GetWidth() - 1, play.viewport.Top - 1, NULL);
gfxDriver->ClearRectangle(0, play.viewport.Bottom + 1, _old_screen->GetWidth() - 1, game.size.Height - 1, NULL);
}
}
// writes the virtual screen to the screen, converting colours if
// necessary
void write_screen() {
if (play.fast_forward)
return;
static int wasShakingScreen = 0;
bool clearScreenBorders = false;
int at_yp = 0;
if (play.shakesc_length > 0) {
wasShakingScreen = 1;
if ( (loopcounter % play.shakesc_delay) < (play.shakesc_delay / 2) )
at_yp = multiply_up_coordinate(play.shakesc_amount);
invalidate_screen();
}
else if (wasShakingScreen) {
wasShakingScreen = 0;
if (!gfxDriver->RequiresFullRedrawEachFrame())
{
clear_letterbox_borders();
}
}
if (play.screen_tint < 1)
gfxDriver->SetScreenTint(0, 0, 0);
else
gfxDriver->SetScreenTint(play.screen_tint & 0xff, (play.screen_tint >> 8) & 0xff, (play.screen_tint >> 16) & 0xff);
render_to_screen(virtual_screen, 0, at_yp);
}
void draw_screen_callback()
{
construct_virtual_screen(false);
render_black_borders(play.viewport.Left, play.viewport.Top);
}
void putpixel_compensate (Bitmap *ds, int xx,int yy, int col) {
if ((ds->GetColorDepth() == 32) && (col != 0)) {
// ensure the alpha channel is preserved if it has one
int alphaval = geta32(ds->GetPixel(xx, yy));
col = makeacol32(getr32(col), getg32(col), getb32(col), alphaval);
}
ds->FillRect(Rect(xx, yy, xx + get_fixed_pixel_size(1) - 1, yy + get_fixed_pixel_size(1) - 1), col);
}
void draw_sprite_support_alpha(Bitmap *ds, bool ds_has_alpha, int xpos, int ypos, Bitmap *image, bool src_has_alpha,
BlendMode blend_mode, int alpha)
{
if (alpha <= 0)
return;
if (game.options[OPT_SPRITEALPHA] == kSpriteAlphaRender_Improved)
{
GfxUtil::DrawSpriteBlend(ds, Point(xpos, ypos), image, blend_mode, ds_has_alpha, src_has_alpha, alpha);
}
// Backwards-compatible drawing
else if (src_has_alpha && alpha == 0xFF)
{
set_alpha_blender();
ds->TransBlendBlt(image, xpos, ypos);
}
else
{
GfxUtil::DrawSpriteWithTransparency(ds, image, xpos, ypos, alpha);
}
}
void draw_sprite_slot_support_alpha(Bitmap *ds, bool ds_has_alpha, int xpos, int ypos, int src_slot,
BlendMode blend_mode, int alpha)
{
draw_sprite_support_alpha(ds, ds_has_alpha, xpos, ypos, spriteset[src_slot], (game.spriteflags[src_slot] & SPF_ALPHACHANNEL) != 0,
blend_mode, alpha);
}
// Avoid freeing and reallocating the memory if possible
IDriverDependantBitmap* recycle_ddb_bitmap(IDriverDependantBitmap *bimp, Bitmap *source, bool hasAlpha) {
if (bimp != NULL) {
// same colour depth, width and height -> reuse
if (((bimp->GetColorDepth() + 1) / 8 == source->GetBPP()) &&
(bimp->GetWidth() == source->GetWidth()) && (bimp->GetHeight() == source->GetHeight()))
{
gfxDriver->UpdateDDBFromBitmap(bimp, source, hasAlpha);
return bimp;
}
gfxDriver->DestroyDDB(bimp);
}
bimp = gfxDriver->CreateDDBFromBitmap(source, hasAlpha, false);
return bimp;
}
void invalidate_cached_walkbehinds()
{
memset(&actspswbcache[0], 0, sizeof(CachedActSpsData) * actSpsCount);
}
// sort_out_walk_behinds: modifies the supplied sprite by overwriting parts
// of it with transparent pixels where there are walk-behind areas
// Returns whether any pixels were updated
int sort_out_walk_behinds(Bitmap *sprit,int xx,int yy,int basel, Bitmap *copyPixelsFrom = NULL, Bitmap *checkPixelsFrom = NULL, int zoom=100) {
if (noWalkBehindsAtAll)
return 0;
if ((!thisroom.object->IsMemoryBitmap()) ||
(!sprit->IsMemoryBitmap()))
quit("!sort_out_walk_behinds: wb bitmap not linear");
int rr,tmm, toheight;//,tcol;
// precalculate this to try and shave some time off
int maskcol = sprit->GetMaskColor();
int spcoldep = sprit->GetColorDepth();
int screenhit = thisroom.object->GetHeight();
short *shptr, *shptr2;
int *loptr, *loptr2;
int pixelsChanged = 0;
int ee = 0;
if (xx < 0)
ee = 0 - xx;
if ((checkPixelsFrom != NULL) && (checkPixelsFrom->GetColorDepth() != spcoldep))
quit("sprite colour depth does not match background colour depth");
for ( ; ee < sprit->GetWidth(); ee++) {
if (ee + xx >= thisroom.object->GetWidth())
break;
if ((!walkBehindExists[ee+xx]) ||
(walkBehindEndY[ee+xx] <= yy) ||
(walkBehindStartY[ee+xx] > yy+sprit->GetHeight()))
continue;
toheight = sprit->GetHeight();
if (walkBehindStartY[ee+xx] < yy)
rr = 0;
else
rr = (walkBehindStartY[ee+xx] - yy);
// Since we will use _getpixel, ensure we only check within the screen
if (rr + yy < 0)
rr = 0 - yy;
if (toheight + yy > screenhit)
toheight = screenhit - yy;
if (toheight + yy > walkBehindEndY[ee+xx])
toheight = walkBehindEndY[ee+xx] - yy;
if (rr < 0)
rr = 0;
for ( ; rr < toheight;rr++) {
// we're ok with _getpixel because we've checked the screen edges
//tmm = _getpixel(thisroom.object,ee+xx,rr+yy);
// actually, _getpixel is well inefficient, do it ourselves
// since we know it's 8-bit bitmap
tmm = thisroom.object->GetScanLine(rr+yy)[ee+xx];
if (tmm<1) continue;
if (croom->walkbehind_base[tmm] <= basel) continue;
if (copyPixelsFrom != NULL)
{
if (spcoldep <= 8)
{
if (checkPixelsFrom->GetScanLine((rr * 100) / zoom)[(ee * 100) / zoom] != maskcol) {
sprit->GetScanLineForWriting(rr)[ee] = copyPixelsFrom->GetScanLine(rr + yy)[ee + xx];
pixelsChanged = 1;
}
}
else if (spcoldep <= 16) {
shptr = (short*)&sprit->GetScanLine(rr)[0];
shptr2 = (short*)&checkPixelsFrom->GetScanLine((rr * 100) / zoom)[0];
if (shptr2[(ee * 100) / zoom] != maskcol) {
shptr[ee] = ((short*)(©PixelsFrom->GetScanLine(rr + yy)[0]))[ee + xx];
pixelsChanged = 1;
}
}
else if (spcoldep == 24) {
char *chptr = (char*)&sprit->GetScanLine(rr)[0];
char *chptr2 = (char*)&checkPixelsFrom->GetScanLine((rr * 100) / zoom)[0];
if (memcmp(&chptr2[((ee * 100) / zoom) * 3], &maskcol, 3) != 0) {
memcpy(&chptr[ee * 3], ©PixelsFrom->GetScanLine(rr + yy)[(ee + xx) * 3], 3);
pixelsChanged = 1;
}
}
else if (spcoldep <= 32) {
loptr = (int*)&sprit->GetScanLine(rr)[0];
loptr2 = (int*)&checkPixelsFrom->GetScanLine((rr * 100) / zoom)[0];
if (loptr2[(ee * 100) / zoom] != maskcol) {
loptr[ee] = ((int*)(©PixelsFrom->GetScanLine(rr + yy)[0]))[ee + xx];
pixelsChanged = 1;
}
}
}
else
{
pixelsChanged = 1;
if (spcoldep <= 8)
sprit->GetScanLineForWriting(rr)[ee] = maskcol;
else if (spcoldep <= 16) {
shptr = (short*)&sprit->GetScanLine(rr)[0];
shptr[ee] = maskcol;
}
else if (spcoldep == 24) {
char *chptr = (char*)&sprit->GetScanLine(rr)[0];
memcpy(&chptr[ee * 3], &maskcol, 3);
}
else if (spcoldep <= 32) {
loptr = (int*)&sprit->GetScanLine(rr)[0];
loptr[ee] = maskcol;
}
else
quit("!Sprite colour depth >32 ??");
}
}
}
return pixelsChanged;
}
void sort_out_char_sprite_walk_behind(int actspsIndex, int xx, int yy, int basel, int zoom, int width, int height)
{
if (noWalkBehindsAtAll)
return;
if ((!actspswbcache[actspsIndex].valid) ||
(actspswbcache[actspsIndex].xWas != xx) ||
(actspswbcache[actspsIndex].yWas != yy) ||
(actspswbcache[actspsIndex].baselineWas != basel))
{
actspswb[actspsIndex] = recycle_bitmap(actspswb[actspsIndex], thisroom.ebscene[play.bg_frame]->GetColorDepth(), width, height, true);
Bitmap *wbSprite = actspswb[actspsIndex];
actspswbcache[actspsIndex].isWalkBehindHere = sort_out_walk_behinds(wbSprite, xx, yy, basel, thisroom.ebscene[play.bg_frame], actsps[actspsIndex], zoom);
actspswbcache[actspsIndex].xWas = xx;
actspswbcache[actspsIndex].yWas = yy;
actspswbcache[actspsIndex].baselineWas = basel;
actspswbcache[actspsIndex].valid = 1;