/
gfxcore.cpp
1963 lines (1493 loc) · 39.3 KB
/
gfxcore.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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This code is based on original Tony Tough source code
*
* Copyright (c) 1997-2003 Nayma Software
*/
#include "tony/gfxengine.h"
#include "tony/mpal/mpalutils.h"
#include "tony/tony.h"
namespace Tony {
/****************************************************************************\
* RMGfxTask Methods
\****************************************************************************/
RMGfxTask::RMGfxTask() {
_nPrior = 0;
_nInList = 0;
}
int RMGfxTask::priority() {
return _nPrior;
}
void RMGfxTask::removeThis(CORO_PARAM, bool &result) {
result = true;
}
/****************************************************************************\
* RMGfxTaskSetPrior Methods
\****************************************************************************/
void RMGfxTaskSetPrior::setPriority(int nPrior) {
_nPrior = nPrior;
}
/****************************************************************************\
* RMGfxBuffer Methods
\****************************************************************************/
RMGfxBuffer::RMGfxBuffer() {
_dimx = _dimy = 0;
_bUseDDraw = false;
_origBuf = _buf = NULL;
}
RMGfxBuffer::~RMGfxBuffer() {
destroy();
}
void RMGfxBuffer::create(int dimx, int dimy, int nBpp, bool bUseDDraw) {
// Destroy the buffer it is already exists
if (_buf != NULL)
destroy();
// Copy the parameters in the private members
_dimx = dimx;
_dimy = dimy;
_bUseDDraw = bUseDDraw;
if (!_bUseDDraw) {
// Allocate a buffer
_origBuf = _buf = new byte[_dimx * _dimy * nBpp / 8];
assert(_buf != NULL);
Common::fill(_origBuf, _origBuf + _dimx * _dimy * nBpp / 8, 0);
}
}
void RMGfxBuffer::destroy(void) {
if (!_bUseDDraw) {
if (_origBuf != NULL && _origBuf == _buf) {
delete[] _origBuf;
_origBuf = _buf = NULL;
}
}
}
void RMGfxBuffer::lock(void) {
if (_bUseDDraw) {
// Manages acceleration
}
}
void RMGfxBuffer::unlock(void) {
if (_bUseDDraw) {
// Manages acceleration
}
}
void RMGfxBuffer::offsetY(int nLines, int nBpp) {
_buf += nLines * getDimx() * nBpp / 8;
}
RMGfxBuffer::operator byte *() {
return _buf;
}
RMGfxBuffer::operator void *() {
return (void *)_buf;
}
RMGfxBuffer::RMGfxBuffer(int dimx, int dimy, int nBpp, bool bUseDDraw) {
create(dimx, dimy, nBpp, bUseDDraw);
}
/****************************************************************************\
* RMGfxSourceBuffer Methods
\****************************************************************************/
int RMGfxSourceBuffer::init(const byte *buf, int dimx, int dimy, bool bLoadPalette) {
create(dimx, dimy, Bpp());
CopyMemory(_buf, buf, dimx * dimy * Bpp() / 8);
// Invokes the method for preparing the surface (inherited)
prepareImage();
return dimx * dimy * Bpp() / 8;
}
void RMGfxSourceBuffer::init(RMDataStream &ds, int dimx, int dimy, bool bLoadPalette) {
create(dimx, dimy, Bpp());
ds.Read(_buf, dimx * dimy * Bpp() / 8);
// Invokes the method for preparing the surface (inherited)
prepareImage();
}
RMGfxSourceBuffer::~RMGfxSourceBuffer() {
}
void RMGfxSourceBuffer::prepareImage(void) {
// Do nothing. Can be overloaded if necessary
}
bool RMGfxSourceBuffer::clip2D(int &x1, int &y1, int &u, int &v, int &width, int &height, bool bUseSrc, RMGfxTargetBuffer *buf) {
int destw, desth;
destw = buf->getDimx();
desth = buf->getDimy();
if (!bUseSrc) {
u = v = 0;
width = _dimx;
height = _dimy;
}
if (x1 > destw - 1)
return false;
if (y1 > desth - 1)
return false;
if (x1 < 0) {
width += x1;
if (width < 0)
return false;
u -= x1;
x1 = 0;
}
if (y1 < 0) {
height += y1;
if (height < 0)
return false;
v -= y1;
y1 = 0;
}
if (x1 + width - 1 > destw - 1)
width = destw - x1;
if (y1 + height - 1 > desth - 1)
height = desth - y1;
return true;
}
/**
* Initialises a surface by resource Id
*
* @param resID Resource ID
* @param dimx Buffer X dimension
* @param dimy Buffer Y dimension
*/
int RMGfxSourceBuffer::init(uint32 resID, int dimx, int dimy, bool bLoadPalette) {
return init(RMRes(resID), dimx, dimy, bLoadPalette);
}
/****************************************************************************\
* RMGfxWoodyBuffer Methods
\****************************************************************************/
RMGfxWoodyBuffer::~RMGfxWoodyBuffer() {
}
void RMGfxWoodyBuffer::draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
CORO_BEGIN_CONTEXT;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
// Draw the OT list
CORO_INVOKE_0(drawOT);
// Draw itself into the target buffer
CORO_INVOKE_2(RMGfxSourceBuffer16::draw, bigBuf, prim);
CORO_END_CODE;
}
RMGfxWoodyBuffer::RMGfxWoodyBuffer() {
}
RMGfxWoodyBuffer::RMGfxWoodyBuffer(int dimx, int dimy, bool bUseDDraw)
: RMGfxBuffer(dimx, dimy, 16, bUseDDraw) {
}
/****************************************************************************\
* RMGfxTargetBuffer Methods
\****************************************************************************/
RMGfxClearTask RMGfxTargetBuffer::taskClear;
RMGfxTargetBuffer::RMGfxTargetBuffer() {
otlist = NULL;
_otSize = 0;
// csModifyingOT = g_system->createMutex();
}
RMGfxTargetBuffer::~RMGfxTargetBuffer() {
clearOT();
// g_system->deleteMutex(csModifyingOT);
}
void RMGfxTargetBuffer::clearOT(void) {
OTList *cur, *n;
// g_system->lockMutex(csModifyingOT);
cur = otlist;
while (cur != NULL) {
cur->prim->_task->Unregister();
delete cur->prim;
n = cur->next;
delete cur;
cur = n;
}
otlist = NULL;
// g_system->unlockMutex(csModifyingOT);
}
void RMGfxTargetBuffer::drawOT(CORO_PARAM) {
CORO_BEGIN_CONTEXT;
OTList *cur;
OTList *prev;
OTList *next;
RMGfxPrimitive *myprim;
bool result;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
_ctx->prev = NULL;
_ctx->cur = otlist;
// Lock the buffer to access it
lock();
// g_system->lockMutex(csModifyingOT);
while (_ctx->cur != NULL) {
// Call the task Draw method, passing it a copy of the original
_ctx->myprim = _ctx->cur->prim->duplicate();
CORO_INVOKE_2(_ctx->cur->prim->_task->draw, *this, _ctx->myprim);
delete _ctx->myprim;
// Check if it's time to remove the task from the OT list
CORO_INVOKE_1(_ctx->cur->prim->_task->removeThis, _ctx->result);
if (_ctx->result) {
// De-register the task
_ctx->cur->prim->_task->Unregister();
// Delete task, freeing the memory
delete _ctx->cur->prim;
_ctx->next = _ctx->cur->next;
delete _ctx->cur;
// If it was the first item, update the list head
if (_ctx->prev == NULL)
otlist = _ctx->next;
// Otherwise update the next pinter of the previous item
else
_ctx->prev->next = _ctx->next;
_ctx->cur = _ctx->next;
} else {
// Update the pointer to the previous item, and the current to the next
_ctx->prev = _ctx->cur;
_ctx->cur = _ctx->cur->next;
}
}
// g_system->unlockMutex(csModifyingOT);
//Unlock after writing
unlock();
CORO_END_CODE;
}
void RMGfxTargetBuffer::addPrim(RMGfxPrimitive *prim) {
int nPrior;
OTList *cur, *n;
// g_system->lockMutex(csModifyingOT);
// Warn of the OT listing
prim->_task->Register();
// Check the priority
nPrior = prim->_task->priority();
n = new OTList(prim);
// Empty list
if (otlist == NULL) {
otlist = n;
otlist->next = NULL;
}
// Inclusion in the head
else if (nPrior < otlist->prim->_task->priority()) {
n->next = otlist;
otlist = n;
} else {
cur = otlist;
while (cur->next != NULL && nPrior > cur->next->prim->_task->priority())
cur = cur->next;
n->next = cur->next;
cur->next = n;
}
// g_system->unlockMutex(csModifyingOT);
}
void RMGfxTargetBuffer::addClearTask(void) {
addPrim(new RMGfxPrimitive(&taskClear));
}
/****************************************************************************\
* RMGfxSourceBufferPal Methods
\****************************************************************************/
RMGfxSourceBufferPal::~RMGfxSourceBufferPal() {
}
int RMGfxSourceBufferPal::loadPaletteWA(const byte *buf, bool bSwapped) {
int i;
if (bSwapped)
for (i = 0; i < (1 << Bpp()); i++) {
_pal[i * 3 + 0] = buf[i * 3 + 2];
_pal[i * 3 + 1] = buf[i * 3 + 1];
_pal[i * 3 + 2] = buf[i * 3 + 0];
}
else
CopyMemory(_pal, buf, (1 << Bpp()) * 3);
preparePalette();
return (1 << Bpp()) * 3;
}
int RMGfxSourceBufferPal::loadPalette(const byte *buf) {
int i;
for (i = 0; i < 256; i++)
CopyMemory(_pal + i * 3, buf + i * 4, 3);
preparePalette();
return (1 << Bpp()) * 4;
}
void RMGfxSourceBufferPal::preparePalette(void) {
int i;
for (i = 0; i < 256; i++) {
_palFinal[i] = (((int)_pal[i * 3 + 0] >> 3) << 10) |
(((int)_pal[i * 3 + 1] >> 3) << 5) |
(((int)_pal[i * 3 + 2] >> 3) << 0);
}
}
int RMGfxSourceBufferPal::init(const byte *buf, int dimx, int dimy, bool bLoadPalette) {
int read;
// Load the RAW image
read = RMGfxSourceBuffer::init(buf, dimx, dimy);
// Load the palette if necessary
if (bLoadPalette)
read += loadPaletteWA(&buf[read]);
return read;
}
void RMGfxSourceBufferPal::init(RMDataStream &ds, int dimx, int dimy, bool bLoadPalette) {
// Load the RAW image
RMGfxSourceBuffer::init(ds, dimx, dimy);
// Load the palette if necessary
if (bLoadPalette) {
byte *suxpal = new byte[256 * 3];
ds.Read(suxpal, 256 * 3);
loadPaletteWA(suxpal);
delete[] suxpal;
}
}
int RMGfxSourceBufferPal::loadPalette(uint32 resID) {
return loadPalette(RMRes(resID));
}
int RMGfxSourceBufferPal::loadPaletteWA(uint32 resID, bool bSwapped) {
return loadPaletteWA(RMRes(resID), bSwapped);
}
/****************************************************************************\
* RMGfxSourceBuffer4 Methods
\****************************************************************************/
void RMGfxSourceBuffer4::draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
}
RMGfxSourceBuffer4::RMGfxSourceBuffer4(int dimx, int dimy, bool bUseDDraw)
: RMGfxBuffer(dimx, dimy, 4, bUseDDraw) {
setPriority(0);
}
/**
* Returns the number of bits per pixel of the surface
*
* @returns Bit per pixel
*/
int RMGfxSourceBuffer4::Bpp() {
return 4;
}
void RMGfxSourceBuffer4::Create(int dimx, int dimy, bool bUseDDraw) {
RMGfxBuffer::create(dimx, dimy, 4, bUseDDraw);
}
/****************************************************************************\
* RMGfxSourceBuffer8 Methods
\****************************************************************************/
RMGfxSourceBuffer8::~RMGfxSourceBuffer8() {
}
void RMGfxSourceBuffer8::draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
int x, y, width, height, u, v;
int bufx = bigBuf.getDimx();
uint16 *buf = bigBuf;
byte *raw = _buf;
// Destination buffer
RMRect dst;
if (prim->haveDst())
dst = prim->Dst();
// Clipping
if (prim->haveSrc()) {
u = prim->Src().x1;
v = prim->Src().y1;
width = prim->Src().Width();
height = prim->Src().Height();
}
if (!clip2D(dst.x1, dst.y1, u, v, width, height, prim->haveSrc(), &bigBuf))
return;
// Starting offset into the buffer
buf += dst.y1 * bufx + dst.x1;
// Normal step
if (_bTrasp0) {
for (y = 0; y < height; y++) {
raw = _buf + (y + v) * _dimx + u;
for (x = 0; x < width; x++) {
if (*raw)
*buf = _palFinal[*raw];
buf++;
raw++;
}
buf += bufx - width;
}
} else {
for (y = 0; y < height; y++) {
raw = _buf + (y + v) * _dimx + u;
for (x = 0; x < width; x += 2) {
buf[0] = _palFinal[raw[0]];
buf[1] = _palFinal[raw[1]];
buf += 2;
raw += 2;
}
buf += bufx - width;
}
}
}
RMGfxSourceBuffer8::RMGfxSourceBuffer8(int dimx, int dimy, bool bUseDDraw)
: RMGfxBuffer(dimx, dimy, 8, bUseDDraw) {
setPriority(0);
}
RMGfxSourceBuffer8::RMGfxSourceBuffer8(bool bTrasp0) {
_bTrasp0 = bTrasp0;
}
/**
* Returns the number of bits per pixel of the surface
*
* @returns Bit per pixel
*/
int RMGfxSourceBuffer8::Bpp() {
return 8;
}
void RMGfxSourceBuffer8::create(int dimx, int dimy, bool bUseDDraw) {
RMGfxBuffer::create(dimx, dimy, 8, bUseDDraw);
}
#define GETRED(x) (((x) >> 10) & 0x1F)
#define GETGREEN(x) (((x) >> 5) & 0x1F)
#define GETBLUE(x) ((x) & 0x1F)
/****************************************************************************\
* RMGfxSourceBuffer8AB Methods
\****************************************************************************/
RMGfxSourceBuffer8AB::~RMGfxSourceBuffer8AB() {
}
int RMGfxSourceBuffer8AB::calcTrasp(int fore, int back) {
int r, g, b;
r = (GETRED(fore) >> 2) + (GETRED(back) >> 1);
g = (GETGREEN(fore) >> 2) + (GETGREEN(back) >> 1);
b = (GETBLUE(fore) >> 2) + (GETBLUE(back) >> 1);
if (r > 0x1F) r = 0x1F;
if (g > 0x1F) g = 0x1F;
if (b > 0x1F) b = 0x1F;
return (r << 10) | (g << 5) | b;
}
void RMGfxSourceBuffer8AB::draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
int x, y, width, height, u, v;
int bufx = bigBuf.getDimx();
uint16 *buf = bigBuf;
byte *raw = _buf;
// Destination buffer
RMRect dst;
if (prim->haveDst())
dst = prim->Dst();
// Clipping
if (prim->haveSrc()) {
u = prim->Src().x1;
v = prim->Src().y1;
width = prim->Src().Width();
height = prim->Src().Height();
}
if (!clip2D(dst.x1, dst.y1, u, v, width, height, prim->haveSrc(), &bigBuf))
return;
// Starting offset into the buffer
buf += dst.y1 * bufx + dst.x1;
// Passaggio normale
if (_bTrasp0) {
for (y = 0; y < height; y++) {
raw = _buf + (y + v) * _dimx + u;
for (x = 0; x < width; x++) {
if (*raw)
*buf = calcTrasp(_palFinal[*raw], *buf);
buf++;
raw++;
}
buf += bufx - width;
}
} else {
for (y = 0; y < height; y++) {
raw = _buf + (y + v) * _dimx + u;
for (x = 0; x < width; x += 2) {
buf[0] = calcTrasp(_palFinal[raw[0]], buf[0]);
buf[1] = calcTrasp(_palFinal[raw[1]], buf[1]);
buf += 2;
raw += 2;
}
buf += bufx - width;
}
}
return;
}
/****************************************************************************\
* RMGfxSourceBuffer8RLE Methods
\****************************************************************************/
byte RMGfxSourceBuffer8RLE::MegaRLEBuf[512 * 1024];
void RMGfxSourceBuffer8RLE::setAlphaBlendColor(int color) {
alphaBlendColor = color;
}
RMGfxSourceBuffer8RLE::RMGfxSourceBuffer8RLE() {
alphaBlendColor = -1;
bNeedRLECompress = true;
_buf = NULL;
}
RMGfxSourceBuffer8RLE::~RMGfxSourceBuffer8RLE() {
if (_buf != NULL) {
delete[] _buf;
_buf = NULL;
}
}
int RMGfxSourceBuffer8RLE::init(const byte *buf, int dimx, int dimy, bool bLoadPalette) {
return RMGfxSourceBufferPal::init(buf, dimx, dimy, bLoadPalette);
}
void RMGfxSourceBuffer8RLE::init(RMDataStream &ds, int dimx, int dimy, bool bLoadPalette) {
if (bNeedRLECompress) {
RMGfxSourceBufferPal::init(ds, dimx, dimy, bLoadPalette);
} else {
int size;
ds >> size;
_buf = new byte[size];
ds.Read(_buf, size);
_dimx = dimx;
_dimy = dimy;
}
}
void RMGfxSourceBuffer8RLE::preparePalette(void) {
// Invoke the parent method
RMGfxSourceBuffer8::preparePalette();
// Handle RGB alpha blending
if (alphaBlendColor != -1) {
alphaR = (_palFinal[alphaBlendColor] >> 10) & 0x1F;
alphaG = (_palFinal[alphaBlendColor] >> 5) & 0x1F;
alphaB = (_palFinal[alphaBlendColor]) & 0x1F;
}
}
void RMGfxSourceBuffer8RLE::prepareImage(void) {
// Invoke the parent method
RMGfxSourceBuffer::prepareImage();
// Compress
compressRLE();
}
void RMGfxSourceBuffer8RLE::setAlreadyCompressed(void) {
bNeedRLECompress = false;
}
void RMGfxSourceBuffer8RLE::compressRLE(void) {
int x, y;
byte *startline;
byte *cur;
byte curdata;
byte *src;
byte *startsrc;
int rep;
// Perform RLE compression for lines
cur = MegaRLEBuf;
src = _buf;
for (y = 0; y < _dimy; y++) {
// Save the beginning of the line
startline = cur;
// Leave space for the length of the line
cur += 2;
// It starts from the empty space
curdata = 0;
rep = 0;
startsrc = src;
for (x = 0; x < _dimx;) {
if ((curdata == 0 && *src == 0) || (curdata == 1 && *src == alphaBlendColor)
|| (curdata == 2 && (*src != alphaBlendColor && *src != 0))) {
src++;
rep++;
x++;
} else {
if (curdata == 0) {
RLEWriteTrasp(cur, rep);
curdata++;
} else if (curdata == 1) {
RLEWriteAlphaBlend(cur, rep);
curdata++;
} else {
RLEWriteData(cur, rep, startsrc);
curdata = 0;
}
rep = 0;
startsrc = src;
}
}
// Pending data?
if (curdata == 1) {
RLEWriteAlphaBlend(cur, rep);
RLEWriteData(cur, 0, NULL);
}
if (curdata == 2) {
RLEWriteData(cur, rep, startsrc);
}
// End of line
RLEWriteEOL(cur);
// Write the length of the line
WRITE_LE_UINT16(startline, (uint16)(cur - startline));
}
// Delete the original image
delete[] _buf;
// Copy the compressed image
x = cur - MegaRLEBuf;
_buf = new byte[x];
Common::copy(MegaRLEBuf, MegaRLEBuf + x, _buf);
}
void RMGfxSourceBuffer8RLE::draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
int y;
byte *src;
uint16 *buf = bigBuf;
int x1, y1, u, v, width, height;
// Clipping
x1 = prim->Dst().x1;
y1 = prim->Dst().y1;
if (!clip2D(x1, y1, u, v, width, height, false, &bigBuf))
return;
// Go forward through the RLE lines
src = _buf;
for (y = 0; y < v; y++)
src += READ_LE_UINT16(src);
// Calculate the position in the destination buffer
buf += y1 * bigBuf.getDimx();
// Loop
if (prim->isFlipped()) {
// Eliminate horizontal clipping
// width = m_dimx;
// x1=prim->Dst().x1;
// Clipping
u = _dimx - (width + u);
x1 = (prim->Dst().x1 + _dimx - 1) - u;
for (y = 0; y < height; y++) {
// Decompressione
RLEDecompressLineFlipped(buf + x1, src + 2, u, width);
// Next line
src += READ_LE_UINT16(src);
// Skip to the next line
buf += bigBuf.getDimx();
}
} else {
for (y = 0; y < height; y++) {
// Decompression
RLEDecompressLine(buf + x1, src + 2, u, width);
// Next line
src += READ_LE_UINT16(src);
// Skip to the next line
buf += bigBuf.getDimx();
}
}
}
/****************************************************************************\
* RMGfxSourceBuffer8RLEByte Methods
\****************************************************************************/
RMGfxSourceBuffer8RLEByte::~RMGfxSourceBuffer8RLEByte() {
}
void RMGfxSourceBuffer8RLEByte::RLEWriteTrasp(byte *&cur, int rep) {
assert(rep < 255);
*cur ++ = rep;
}
void RMGfxSourceBuffer8RLEByte::RLEWriteAlphaBlend(byte *&cur, int rep) {
assert(rep < 255);
*cur ++ = rep;
}
void RMGfxSourceBuffer8RLEByte::RLEWriteData(byte *&cur, int rep, byte *src) {
assert(rep < 256);
*cur ++ = rep;
if (rep > 0) {
CopyMemory(cur, src, rep);
cur += rep;
src += rep;
}
return;
}
void RMGfxSourceBuffer8RLEByte::RLEWriteEOL(byte *&cur) {
*cur ++ = 0xFF;
}
void RMGfxSourceBuffer8RLEByte::RLEDecompressLine(uint16 *dst, byte *src, int nStartSkip, int nLength) {
int i, n;
int r, g, b;
if (nStartSkip == 0)
goto RLEByteDoTrasp;
while (1) {
assert(nStartSkip > 0);
// TRASP
n = *src++;
if (n == 0xFF)
return;
if (n >= nStartSkip) {
dst += n - nStartSkip;
nLength -= n - nStartSkip;
if (nLength > 0)
goto RLEByteDoAlpha;
else
return;
}
nStartSkip -= n;
assert(nStartSkip > 0);
// ALPHA
n = *src++;
if (n >= nStartSkip) {
n -= nStartSkip;
goto RLEByteDoAlpha2;
}
nStartSkip -= n;
assert(nStartSkip > 0);
// DATA
n = *src++;
if (n >= nStartSkip) {
src += nStartSkip;
n -= nStartSkip;
goto RLEByteDoCopy2;
}
nStartSkip -= n;
src += n;
}
while (1) {
RLEByteDoTrasp:
// Get the trasp of s**t
n = *src++;
// EOL?
if (n == 0xFF)
return;
dst += n;
nLength -= n;
if (nLength <= 0)
return;
RLEByteDoAlpha:
// Alpha
n = *src++;
RLEByteDoAlpha2:
if (n > nLength)
n = nLength;
for (i = 0; i < n; i++) {
r = (*dst >> 10) & 0x1F;
g = (*dst >> 5) & 0x1F;
b = *dst & 0x1F;
r = (r >> 2) + (alphaR >> 1);
g = (g >> 2) + (alphaG >> 1);
b = (b >> 2) + (alphaB >> 1);
*dst ++ = (r << 10) | (g << 5) | b;
}
nLength -= n;
if (!nLength)
return;
assert(nLength > 0);
//RLEByteDoCopy:
// Copy the stuff
n = *src++;
RLEByteDoCopy2:
if (n > nLength)
n = nLength;
for (i = 0; i < n; i++)
*dst ++ = _palFinal[*src++];
nLength -= n;
if (!nLength)
return;