-
-
Notifications
You must be signed in to change notification settings - Fork 998
/
gfxcore.cpp
1997 lines (1526 loc) · 40.5 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 {
/****************************************************************************\
* Metodi di RMGfxTask
\****************************************************************************/
RMGfxTask::RMGfxTask() {
m_nPrior = 0;
m_nInList = 0;
}
int RMGfxTask::Priority() {
return m_nPrior;
}
void RMGfxTask::RemoveThis(CORO_PARAM, bool &result) {
result = true;
}
/****************************************************************************\
* Metodi di RMGfxTaskSetPrior
\****************************************************************************/
void RMGfxTaskSetPrior::SetPriority(int nPrior) {
m_nPrior = nPrior;
}
/****************************************************************************\
* Metodi di RMGfxBuffer
\****************************************************************************/
RMGfxBuffer::RMGfxBuffer() {
m_dimx = m_dimy = 0;
m_bUseDDraw = false;
m_origBuf = m_buf = NULL;
}
RMGfxBuffer::~RMGfxBuffer() {
Destroy();
}
void RMGfxBuffer::Create(int dimx, int dimy, int nBpp, bool bUseDDraw) {
// Distruggi il buffer se esiste di gia'
if (m_buf != NULL)
Destroy();
// Copia i parametri nei membri privati
m_dimx = dimx;
m_dimy = dimy;
m_bUseDDraw = bUseDDraw;
if (!m_bUseDDraw) {
// Alloca il buffer
m_origBuf = m_buf = new byte[m_dimx * m_dimy * nBpp / 8];
assert(m_buf != NULL);
Common::fill(m_origBuf, m_origBuf + m_dimx * m_dimy * nBpp / 8, 0);
}
}
void RMGfxBuffer::Destroy(void) {
if (!m_bUseDDraw) {
if (m_origBuf != NULL && m_origBuf == m_buf) {
delete[] m_origBuf;
m_origBuf = m_buf = NULL;
}
}
}
void RMGfxBuffer::Lock(void) {
if (m_bUseDDraw) {
// Gestisce l'accelerazione
}
}
void RMGfxBuffer::Unlock(void) {
if (m_bUseDDraw) {
// Gestisce l'accelerazione
}
}
void RMGfxBuffer::OffsetY(int nLines, int nBpp) {
m_buf += nLines* Dimx() * nBpp / 8;
}
RMGfxBuffer::operator byte *() {
return m_buf;
}
RMGfxBuffer::operator void *() {
return (void *)m_buf;
}
RMGfxBuffer::RMGfxBuffer(int dimx, int dimy, int nBpp, bool bUseDDraw) {
Create(dimx, dimy, nBpp, bUseDDraw);
}
/****************************************************************************\
* Metodi di RMGfxSourceBuffer
\****************************************************************************/
/****************************************************************************\
*
* Function:
*
* Description:
*
* Input:
*
* Return:
*
\****************************************************************************/
int RMGfxSourceBuffer::Init(const byte *buf, int dimx, int dimy, bool bLoadPalette) {
Create(dimx, dimy, Bpp());
CopyMemory(m_buf, buf, dimx * dimy * Bpp() / 8);
// Richiama la funzione di preparazione della surface (ereditata)
PrepareImage();
return dimx * dimy * Bpp() / 8;
}
void RMGfxSourceBuffer::Init(RMDataStream& ds, int dimx, int dimy, bool bLoadPalette) {
Create(dimx, dimy,Bpp());
ds.Read(m_buf, dimx * dimy*Bpp() / 8);
// Richiama la funzione di preparazione della surface (ereditata)
PrepareImage();
}
RMGfxSourceBuffer::~RMGfxSourceBuffer() {
}
void RMGfxSourceBuffer::PrepareImage(void) {
// Non fa nulla: puo' essere overloadata se necessaria
}
bool RMGfxSourceBuffer::Clip2D(int &x1, int &y1, int &u, int &v, int &width, int &height, bool bUseSrc, RMGfxTargetBuffer* buf) {
int destw, desth;
/*
OBSOLETE CODE:
if (buf == NULL) {
destw=RM_SX;
desth=RM_SY;
} else {
destw = buf->Dimx();
desth = buf->Dimy();
}
*/
destw = buf->Dimx();
desth = buf->Dimy();
if (!bUseSrc) {
u = v = 0;
width = m_dimx;
height = m_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;
}
/****************************************************************************\
*
* Function: void RMGfxSourceBuffer::Init(uint32 resID, int dimx, int dimy);
*
* Description: Carica una surface partendo dall'ID della risorsa
*
* Input: uint32 resID ID della risorsa
* int dimx, dimy Dimensione del buffer
*
\****************************************************************************/
int RMGfxSourceBuffer::Init(uint32 resID, int dimx, int dimy, bool bLoadPalette) {
return Init(RMRes(resID), dimx, dimy, bLoadPalette);
}
/****************************************************************************\
* Metodi di RMGfxWoodyBuffer
\****************************************************************************/
RMGfxWoodyBuffer::~RMGfxWoodyBuffer() {
}
void RMGfxWoodyBuffer::Draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
CORO_BEGIN_CONTEXT;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
// Prima si fa disegnare tramite la propria OT list
CORO_INVOKE_0(DrawOT);
// Poi disegna se stesso nel 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) {
}
/****************************************************************************\
* Metodi di RMGfxTargetBuffer
\****************************************************************************/
RMGfxClearTask RMGfxTargetBuffer::taskClear;
RMGfxTargetBuffer::RMGfxTargetBuffer() {
otlist = NULL;
m_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->m_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 del buffer per accederci
Lock();
// g_system->lockMutex(csModifyingOT);
while (_ctx->cur != NULL) {
// Richiama la draw sul task, passandogli una copia della primitiva
_ctx->myprim=_ctx->cur->prim->Duplicate();
CORO_INVOKE_2(_ctx->cur->prim->m_task->Draw, *this, _ctx->myprim);
delete _ctx->myprim;
// Controlla se e' arrivato il momento di rimuovere il task dalla OTlist
CORO_INVOKE_1(_ctx->cur->prim->m_task->RemoveThis, _ctx->result);
if (_ctx->result) {
// Deregistra il task
_ctx->cur->prim->m_task->Unregister();
// Cancella il task liberando la memoria
delete _ctx->cur->prim;
_ctx->next = _ctx->cur->next;
delete _ctx->cur;
// Se era il primo elemento, aggiorna la testa della lista
if (_ctx->prev == NULL)
otlist = _ctx->next;
// Altrimenti aggiorna il puntatore al successivo dell'elemento precedente
else
_ctx->prev->next = _ctx->next;
_ctx->cur = _ctx->next;
} else {
// Aggiorna il puntatore al precedente e scorre la lista
_ctx->prev = _ctx->cur;
_ctx->cur = _ctx->cur->next;
}
}
// g_system->unlockMutex(csModifyingOT);
// Unlock dopo la scrittura
Unlock();
CORO_END_CODE;
}
void RMGfxTargetBuffer::AddPrim(RMGfxPrimitive *prim) {
int nPrior;
OTList *cur, *n;
// g_system->lockMutex(csModifyingOT);
// Avverte che e' in lista di OT
prim->m_task->Register();
// Controlla la sua priorita'
nPrior = prim->m_task->Priority();
n = new OTList(prim);
// Lista vuota
if (otlist == NULL) {
otlist = n;
otlist->next = NULL;
}
// Inserimento in testa
else if (nPrior < otlist->prim->m_task->Priority()) {
n->next = otlist;
otlist = n;
} else {
cur = otlist;
while (cur->next != NULL && nPrior > cur->next->prim->m_task->Priority())
cur=cur->next;
n->next = cur->next;
cur->next = n;
}
// g_system->unlockMutex(csModifyingOT);
}
void RMGfxTargetBuffer::AddClearTask(void) {
AddPrim(new RMGfxPrimitive(&taskClear));
}
/****************************************************************************\
* Metodi di RMGfxSourceBufferPal
\****************************************************************************/
RMGfxSourceBufferPal::~RMGfxSourceBufferPal() {
}
int RMGfxSourceBufferPal::LoadPaletteWA(const byte *buf, bool bSwapped) {
int i;
if (bSwapped)
for (i = 0; i < (1 << Bpp()); i++) {
m_pal[i * 3 + 0] = buf[i * 3 + 2];
m_pal[i * 3 + 1] = buf[i * 3 + 1];
m_pal[i * 3 + 2] = buf[i * 3 + 0];
}
else
CopyMemory(m_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(m_pal + i * 3, buf + i * 4, 3);
PreparePalette();
return (1 << Bpp()) * 4;
}
void RMGfxSourceBufferPal::PreparePalette(void) {
int i;
for (i = 0; i < 256; i++) {
m_palFinal[i] = (((int)m_pal[i * 3 + 0] >> 3) << 10) |
(((int)m_pal[i * 3 + 1] >> 3) << 5) |
(((int)m_pal[i * 3 + 2] >> 3) << 0);
}
}
int RMGfxSourceBufferPal::Init(const byte *buf, int dimx, int dimy, bool bLoadPalette) {
int read;
// Carica l'immagine RAW
read = RMGfxSourceBuffer::Init(buf, dimx, dimy);
// Carica la palette se necessario
if (bLoadPalette)
read += LoadPaletteWA(&buf[read]);
return read;
}
void RMGfxSourceBufferPal::Init(RMDataStream &ds, int dimx, int dimy, bool bLoadPalette) {
// Carica l'immagine RAW
RMGfxSourceBuffer::Init(ds, dimx, dimy);
// Carica la palette se necessario
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);
}
/****************************************************************************\
* Metodi di RMGfxSourceBuffer4
\****************************************************************************/
void RMGfxSourceBuffer4::Draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
}
RMGfxSourceBuffer4::RMGfxSourceBuffer4(int dimx, int dimy, bool bUseDDraw)
: RMGfxBuffer(dimx,dimy,4,bUseDDraw) {
SetPriority(0);
}
/****************************************************************************\
*
* Function: int RMGfxSourceBuffer4::Bpp();
*
* Description: Ritorna il numero di bit per pixel della surface
*
* Return: Bit per pixel
*
\****************************************************************************/
int RMGfxSourceBuffer4::Bpp() {
return 4;
}
void RMGfxSourceBuffer4::Create(int dimx, int dimy, bool bUseDDraw) {
RMGfxBuffer::Create(dimx,dimy,4,bUseDDraw);
}
/****************************************************************************\
* Metodi di RMGfxSourceBuffer8
\****************************************************************************/
RMGfxSourceBuffer8::~RMGfxSourceBuffer8() {
}
void RMGfxSourceBuffer8::Draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
int x, y, width, height, u, v;
int bufx = bigBuf.Dimx();
uint16 *buf = bigBuf;
byte *raw = m_buf;
// Destination nel buffer
RMRect dst;
if (prim->HaveDst())
dst=prim->Dst();
// Esegue il clip
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;
// Offset iniziale nel buffer
buf += dst.y1 * bufx + dst.x1;
// Passaggio normale
if (m_bTrasp0) {
for (y = 0; y < height; y++) {
raw = m_buf + (y + v) * m_dimx + u;
for (x = 0; x < width; x++) {
if (*raw) *buf = m_palFinal[*raw];
buf++;
raw++;
}
buf += bufx - width;
}
} else {
for (y = 0; y < height; y++) {
raw = m_buf + (y + v) * m_dimx + u;
for (x = 0; x < width; x += 2) {
buf[0] = m_palFinal[raw[0]];
buf[1] = m_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) {
m_bTrasp0 = bTrasp0;
}
/****************************************************************************\
*
* Function: int RMGfxSourceBuffer8::Bpp();
*
* Description: Ritorna il numero di bit per pixel della surface
*
* Return: 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)
/****************************************************************************\
* Metodi di RMGfxSourceBuffer8AB
\****************************************************************************/
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.Dimx();
uint16 *buf = bigBuf;
byte *raw = m_buf;
// Destination nel buffer
RMRect dst;
if (prim->HaveDst())
dst = prim->Dst();
// Esegue il clip
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;
// Offset iniziale nel buffer
buf += dst.y1 * bufx + dst.x1;
// Passaggio normale
if (m_bTrasp0) {
for (y = 0; y < height; y++) {
raw = m_buf + (y + v) * m_dimx + u;
for (x = 0; x < width; x++) {
if (*raw) *buf = CalcTrasp(m_palFinal[*raw], *buf);
buf++;
raw++;
}
buf += bufx - width;
}
} else {
for (y = 0; y < height; y++) {
raw = m_buf + (y+v) * m_dimx + u;
for (x = 0; x < width; x += 2) {
buf[0] = CalcTrasp(m_palFinal[raw[0]], buf[0]);
buf[1] = CalcTrasp(m_palFinal[raw[1]], buf[1]);
buf += 2;
raw += 2;
}
buf += bufx - width;
}
}
return;
}
/****************************************************************************\
* Metodi di RMGfxSourceBuffer8RLE
\****************************************************************************/
byte RMGfxSourceBuffer8RLE::MegaRLEBuf[512 * 1024];
void RMGfxSourceBuffer8RLE::SetAlphaBlendColor(int color) {
alphaBlendColor = color;
}
RMGfxSourceBuffer8RLE::RMGfxSourceBuffer8RLE() {
alphaBlendColor = -1;
bNeedRLECompress = true;
m_buf = NULL;
}
RMGfxSourceBuffer8RLE::~RMGfxSourceBuffer8RLE() {
if (m_buf != NULL) {
delete[] m_buf;
m_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;
m_buf = new byte[size];
ds.Read(m_buf, size);
m_dimx = dimx;
m_dimy = dimy;
}
}
void RMGfxSourceBuffer8RLE::PreparePalette(void) {
// Richiama il metodo padre
RMGfxSourceBuffer8::PreparePalette();
// Si salva gli RGB di alpha blending
if (alphaBlendColor != -1) {
alphaR = (m_palFinal[alphaBlendColor] >> 10) & 0x1F;
alphaG = (m_palFinal[alphaBlendColor] >> 5) & 0x1F;
alphaB = (m_palFinal[alphaBlendColor]) & 0x1F;
}
}
void RMGfxSourceBuffer8RLE::PrepareImage(void) {
// Richiama il metodo padre
RMGfxSourceBuffer::PrepareImage();
// Comprime
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;
// Esegue la compressione RLE, per linee
cur = MegaRLEBuf;
src = m_buf;
for (y = 0;y < m_dimy; y++) {
// Si salva l'inizio della linea
startline = cur;
// Lascia lo spazio per la lunghezza della linea
cur += 2;
// Si parte dallo spazio vuoto
curdata = 0;
rep = 0;
startsrc = src;
for (x = 0; x < m_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;
}
}
// Dati in sospeso?
if (curdata == 1) {
RLEWriteAlphaBlend(cur, rep);
RLEWriteData(cur, 0, NULL);
}
if (curdata == 2) {
RLEWriteData(cur, rep, startsrc);
}
// Fine linea
RLEWriteEOL(cur);
// Scrive la lunghezza della linea
WRITE_LE_UINT16(startline, (uint16)(cur - startline));
}
// Cancella l'immagine originale
delete[] m_buf;
// Ci copia l'immagine compressa
x = cur - MegaRLEBuf;
m_buf = new byte[x];
Common::copy(MegaRLEBuf, MegaRLEBuf + x, m_buf);
}
void RMGfxSourceBuffer8RLE::Draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
int y;
byte *src;
uint16 *buf = bigBuf;
int x1, y1, u, v, width, height;
// Clippiamo lo sprite
x1=prim->Dst().x1;
y1=prim->Dst().y1;
if (!Clip2D(x1,y1,u,v,width,height,false,&bigBuf))
return;
// Andiamo avanti di V linee nell'immagine RLE
src = m_buf;
for (y = 0;y<v;y++)
src += READ_LE_UINT16(src);
// Posizionamoci nel buffer di destinazione
buf += y1*bigBuf.Dimx();
// Looppone
if (prim->IsFlipped()) {
// Annulliamo il clipping orizzontale
// width = m_dimx;
// x1=prim->Dst().x1;
// Flippiamo il clipping
u = m_dimx - (width+u);
x1 = (prim->Dst().x1 + m_dimx - 1) - u;
for (y = 0; y < height; y++) {
// Decompressione
RLEDecompressLineFlipped(buf + x1, src + 2, u, width);
// Prossima linea
src += READ_LE_UINT16(src);
// Skippa alla linea successiva
buf += bigBuf.Dimx();
}
} else {
for (y = 0; y < height; y++) {
// Decompressione
RLEDecompressLine(buf + x1, src + 2, u, width);
// Prossima linea
src += READ_LE_UINT16(src);
// Skippa alla linea successiva
buf += bigBuf.Dimx();
}
}
}
/****************************************************************************\
* Metodi di RMGfxSourceBuffer8RLEByte
\****************************************************************************/
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:
// Via il trasp di merda
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;