aboutsummaryrefslogtreecommitdiff
path: root/engines/tony/gfxcore.h
blob: 88f2d6ed22858fef30b8c0883482ac2797ae8ac8 (plain)
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
/* 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.
 *
 */
/**************************************************************************
 *                                     ���������������������������������� *
 *                                             Nayma Software srl         *
 *                    e                -= We create much MORE than ALL =- *
 *        u-        z$$$c        '.    ���������������������������������� *
 *      .d"        d$$$$$b        "b.                                     *
 *   .z$*         d$$$$$$$L        ^*$c.                                  *
 *  #$$$.         $$$$$$$$$         .$$$" Project: Roasted Moths........  *
 *    ^*$b       4$$$$$$$$$F      .d$*"                                   *
 *      ^$$.     4$$$$$$$$$F     .$P"     Module:  GfxCore.CPP..........  *
 *        *$.    '$$$$$$$$$     4$P 4                                     *
 *     J   *$     "$$$$$$$"     $P   r    Author:  Giovanni Bajo........  *
 *    z$   '$$$P*4c.*$$$*.z@*R$$$    $.                                   *
 *   z$"    ""       #$F^      ""    '$c                                  *
 *  z$$beu     .ue="  $  "=e..    .zed$$c                                 *
 *      "#$e z$*"   .  `.   ^*Nc e$""                                     *
 *         "$$".  .r"   ^4.  .^$$"                                        *
 *          ^.@*"6L=\ebu^+C$"*b."                                         *
 *        "**$.  "c 4$$$  J"  J$P*"    OS:  [ ] DOS  [X] WIN95  [ ] PORT  *
 *            ^"--.^ 9$"  .--""      COMP:  [ ] WATCOM  [X] VISUAL C++    *
 *                    "                     [ ] EIFFEL  [ ] GCC/GXX/DJGPP *
 *                                                                        *
 * This source code is Copyright (C) Nayma Software.  ALL RIGHTS RESERVED *
 *                                                                        *
 **************************************************************************/

#ifndef TONY_GFXCORE_H
#define TONY_GFXCORE_H

#include "common/system.h"
#include "tony/utils.h"

namespace Tony {

/****************************************************************************\
*       Prototipi di classi
\****************************************************************************/

//    Nome della classe           Albero genealogico     Astratto?
class RMGfxTask;             //                             Si
class RMGfxTaskSetPrior;     //     Task                    Si
class RMGfxBuffer;           //
class RMGfxSourceBuffer;     //     TaskP+[Buffer]          Si
class RMGfxTargetBuffer;     //     [Buffer]
class RMGfxSourceBufferPal;  //     Source                  Si
class RMGfxSourceBuffer4;    //     SourcePal
class RMGfxSourceBuffer8;    //     SourcePal
class RMGfxSourceBuffer16;   //     Source
class RMGfxWoodyBuffer;      //     Source16+Target
class RMGfxClearTask;        //     Task


/**
 * Graphics buffer
 */
class RMGfxBuffer {
protected:
    int m_dimx, m_dimy;
    byte *m_buf;
	byte *m_origBuf;
    bool m_bUseDDraw;

public:
    RMGfxBuffer();
    RMGfxBuffer(int dimx, int dimy, int nBpp, bool bUseDDraw = false);
    virtual ~RMGfxBuffer();

    // Attributes
    int Dimx() { return m_dimx; }
    int Dimy() { return m_dimy; }

    // Creation
    virtual void Create(int dimx, int dimy, int nBpp, bool bUseDDraw = false);
    virtual void Destroy(void);

    // Buffer access
    void Lock(void);
    void Unlock(void);

    // These are valid only if the buffer is locked
    operator byte *();
    operator void *();

	// Getting the offset for a given Y position
	void OffsetY(int nLines, int nBpp);
};

/**
 * Graphics primitive 
 */
class RMGfxPrimitive {
public:
    RMGfxTask *m_task;

protected:
    RMRect m_src;
    RMRect m_dst;

bool m_bStretch;
	byte m_bFlag;

public:
    RMGfxPrimitive() { m_bFlag = 0; m_task = NULL; m_src.SetEmpty(); m_dst.SetEmpty(); }

    RMGfxPrimitive(RMGfxTask *task) {
		m_task = task; m_bFlag = 0;
    }

	RMGfxPrimitive(RMGfxTask *task, const RMRect &src, RMRect &dst) {
		m_task = task; m_src = src; m_dst = dst; m_bFlag = 0;
		m_bStretch = (src.Width() != dst.Width() || src.Height() != dst.Height());
    }

    RMGfxPrimitive(RMGfxTask *task, const RMPoint &src, RMRect &dst) {
		m_task = task; m_src.TopLeft() = src; m_dst = dst; m_bFlag = 0;
    }

    RMGfxPrimitive(RMGfxTask *task, const RMPoint &src, RMPoint &dst) {
		m_task = task; m_src.TopLeft() = src; m_dst.TopLeft() = dst; m_bFlag = 0;
    }

    RMGfxPrimitive(RMGfxTask *task, const RMRect &src, RMPoint &dst) {
		m_task = task; m_src = src; m_dst.TopLeft() = dst; m_bFlag = 0;
    }

    RMGfxPrimitive(RMGfxTask *task, const RMRect &dst) {
		m_task = task; m_dst = dst; m_src.SetEmpty(); m_bFlag = 0;
    }

    RMGfxPrimitive(RMGfxTask *task, const RMPoint &dst) {
		m_task = task; m_dst.TopLeft() = dst; m_src.SetEmpty(); m_bFlag = 0;
    }

	virtual ~RMGfxPrimitive() { }

    void SetFlag(byte bFlag)		{ m_bFlag=bFlag; }
	void SetTask(RMGfxTask *task)   { m_task = task; }
    void SetSrc(const RMRect &src)        { m_src = src; }
    void SetSrc(const RMPoint &src)       { m_src.TopLeft() = src; }
    void SetDst(const RMRect &dst)        { m_dst = dst; }
    void SetDst(const RMPoint &dst)       { m_dst.TopLeft() = dst; }
    void SetStrecth(bool bStretch)	{ m_bStretch = bStretch; }

	bool HaveDst()					{ return !m_dst.IsEmpty(); }
	RMRect &Dst()					{ return m_dst; }

	bool HaveSrc()					{ return !m_src.IsEmpty(); }
	RMRect &Src()					{ return m_src; }

	// Flags
	bool IsFlipped()				{ return m_bFlag&1; }

	// Duplicate
	virtual RMGfxPrimitive* Duplicate() { return new RMGfxPrimitive(*this); }
};


/**
 * Graphic drawing task
 */
class RMGfxTask {
protected:
	int m_nPrior;
	int m_nInList;

public:
	// Costruttore standard
    RMGfxTask();
	virtual ~RMGfxTask() { }

	virtual int Priority();
    virtual void Draw(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) = 0;
    virtual bool RemoveThis();
		
	// Registration
	virtual void Register(void) { m_nInList++; }
	virtual void Unregister(void) { m_nInList--; assert(m_nInList >= 0); }
};


/**
 * Graphic drawing with priority
 */
class RMGfxTaskSetPrior : public RMGfxTask {
public:
	virtual ~RMGfxTaskSetPrior() { }
    void SetPriority(int nPrior);
};


/**
 * Task that cleans the destination buffer
 */
class RMGfxClearTask : public RMGfxTask {
public:
	virtual ~RMGfxClearTask() { }

	int Priority();
    void Draw(RMGfxTargetBuffer& bigBuf, RMGfxPrimitive* prim);
	bool RemoveThis();
};


/**
 * Task that draws a coloured box
 */
class RMGfxBox : public RMGfxTaskSetPrior { 
protected:
	uint16 wFillColor;

public:
	virtual ~RMGfxBox() { }

	void SetColor(byte r, byte g, byte b);
	void Draw(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim);
	bool RemoveThis();
};


/**
 * Buffer source for the design, which is a task. This is an abstract base.
 */
class RMGfxSourceBuffer : public virtual RMGfxBuffer, public RMGfxTaskSetPrior {
public:
    // Carica i dati della surface a basso livello
    virtual int Init(uint32 resID, int dimx, int dimy, bool bLoadPalette = false);
    virtual int Init(const byte *buf, int dimx, int dimy, bool bLoadPalette = false);
	virtual void Init(RMDataStream &ds, int dimx, int dimy, bool bLoadPalette = false);
	
	virtual ~RMGfxSourceBuffer();

protected:
	virtual void PrepareImage(void);
	bool Clip2D(int &x1, int &y1, int &u, int &v, int &width, int &height, bool bUseSrc, RMGfxTargetBuffer *buf);
	void OffsetY(int nLines) { RMGfxBuffer::OffsetY(nLines,Bpp()); }

public:
    virtual int Bpp() = 0;
};


/**
 * 16-bit colour source
 */
class RMGfxSourceBuffer16 : public RMGfxSourceBuffer {
protected:
	virtual void PrepareImage(void);
	bool m_bTrasp0;
	
public:
    RMGfxSourceBuffer16(bool bUseTrasp = false);
    RMGfxSourceBuffer16(int dimx, int dimy, bool bUseDDraw = false);
	virtual ~RMGfxSourceBuffer16();

	// Inizializzazione
	void Create(int dimx, int dimy, bool bUseDDraw = false);

    int Bpp();
    virtual void Draw(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim);
};


/**
 * Buffer source with palette
 */
class RMGfxSourceBufferPal : public RMGfxSourceBuffer {
protected:
	// The size of the palette is  (1<<Bpp())*4
	byte m_pal[256 * 3];
	uint16 m_palFinal[256];

	// Post process to prepare the palette for drawing
	virtual void PreparePalette(void);

public:
	virtual ~RMGfxSourceBufferPal();

	virtual int Init(const byte *buf, int dimx, int dimy, bool bLoadPalette = false);
	virtual void Init(RMDataStream& ds, int dimx, int dimy, bool bLoadPalette = false);
 
	int LoadPaletteWA(uint32 resID, bool bSwapped = false);
	int LoadPaletteWA(const byte *buf, bool bSwapped = false);
	int LoadPalette(uint32 resID);
	int LoadPalette(const byte *buf);
};


/**
 * Buffer source with a 256 colour palette
 */
class RMGfxSourceBuffer8 : public RMGfxSourceBufferPal {
protected:
	bool m_bTrasp0;
	
public:
	RMGfxSourceBuffer8(bool bTrasp0 = true);
	RMGfxSourceBuffer8(int dimx, int dimy, bool bUseDDraw = false);
	virtual ~RMGfxSourceBuffer8();

	// Inizializzazione
	void Create(int dimx, int dimy, bool bUseDDraw = false);

	int Bpp();
    virtual void Draw(RMGfxTargetBuffer& bigBuf, RMGfxPrimitive *prim);
};


/**
 * Buffer source with a 256 colour palette, and alpha blending
 */
class RMGfxSourceBuffer8AB : public RMGfxSourceBuffer8 {
protected:
	int CalcTrasp(int f, int b);

public:
	virtual ~RMGfxSourceBuffer8AB();
	virtual void Draw(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim);
};


/**
 * Buffer source with a 256 colour palette, RLE compressed
 */

class RMGfxSourceBuffer8RLE : public virtual RMGfxSourceBuffer8 {
protected:
	int alphaBlendColor;
	int alphaR, alphaB, alphaG;
	bool bNeedRLECompress;

protected:
	static byte MegaRLEBuf[];
		
	virtual void RLEWriteTrasp(byte *&cur, int rep) = 0;
	virtual void RLEWriteData(byte *&cur, int rep, byte *src) = 0;
	virtual void RLEWriteEOL(byte *&cur) = 0;
	virtual void RLEWriteAlphaBlend(byte *&cur, int rep) = 0;
	virtual void RLEDecompressLine(uint16 *dst, byte *src, int nStartSkip, int nLength) = 0;
	virtual void RLEDecompressLineFlipped(uint16 *dst, byte *src, int nStartSkip, int nLength) = 0;

	// Perform image compression in RLE
	void CompressRLE(void);
	
protected:
	// Overriding initialization methods
	virtual void PrepareImage(void);
	virtual	void PreparePalette(void);

public:
	RMGfxSourceBuffer8RLE();
	virtual ~RMGfxSourceBuffer8RLE();

	// Overload of the initialization method
	virtual void Init(RMDataStream& ds, int dimx, int dimy, bool bLoadPalette = false);
	virtual int Init(const byte *buf, int dimx, int dimy, bool bLoadPalette = false);

	// Draw image with RLE decompression
	void Draw(RMGfxTargetBuffer& bigBuf, RMGfxPrimitive* prim);

	// Sets the color that will be alpha blended
	void SetAlphaBlendColor(int color);

	// Warn if the data is already compressed
	void SetAlreadyCompressed(void);
};

class RMGfxSourceBuffer8RLEByte : public RMGfxSourceBuffer8RLE {
protected:
	void RLEWriteTrasp(byte *  &cur, int rep);
	void RLEWriteAlphaBlend(byte *  &cur, int rep);
	void RLEWriteData(byte *  &cur, int rep, byte *src);
	void RLEWriteEOL(byte *  &cur);
	void RLEDecompressLine(uint16 *dst, byte *src, int nStartSkip, int nLength);
	void RLEDecompressLineFlipped(uint16 *dst, byte *src, int nStartSkip, int nLength);

public:
	virtual ~RMGfxSourceBuffer8RLEByte();
};

class RMGfxSourceBuffer8RLEWord : public RMGfxSourceBuffer8RLE {
protected:
	void RLEWriteTrasp(byte *  &cur, int rep);
	void RLEWriteAlphaBlend(byte *  &cur, int rep);
	void RLEWriteData(byte *  &cur, int rep, byte *src);
	void RLEWriteEOL(byte *  &cur);
	virtual void RLEDecompressLine(uint16 *dst, byte *src, int nStartSkip, int nLength);
	virtual void RLEDecompressLineFlipped(uint16 *dst, byte *src, int nStartSkip, int nLength);

public:
	virtual ~RMGfxSourceBuffer8RLEWord();
};

class RMGfxSourceBuffer8RLEWordAB : public RMGfxSourceBuffer8RLEWord {
protected:
	virtual void RLEDecompressLine(uint16 *dst, byte *src, int nStartSkip, int nLength);

public:
	virtual ~RMGfxSourceBuffer8RLEWordAB();
};


/**
 * Buffer source with a 256 color palette, with anti-aliasing
 */
class RMGfxSourceBuffer8AA : public virtual RMGfxSourceBuffer8 {
protected:
	static byte MegaAABuf[];
	static byte MegaAABuf2[];
	byte *m_aabuf;

	// Calcola il buffer per l'antialiasing
	void CalculateAA(void);

	// Disegna l'AA
	void DrawAA(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim);

protected:
	void PrepareImage(void);

public:
	RMGfxSourceBuffer8AA();
	virtual ~RMGfxSourceBuffer8AA();

	// Draw con antialiasing
	void Draw(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim);
};


class RMGfxSourceBuffer8RLEByteAA : public RMGfxSourceBuffer8RLEByte, public RMGfxSourceBuffer8AA {
protected:
	void PrepareImage(void);

public:
	void Draw(RMGfxTargetBuffer& bigBuf, RMGfxPrimitive* prim);

	// Overloaded initialisation methods
	virtual void Init(RMDataStream &ds, int dimx, int dimy, bool bLoadPalette = false);
    virtual int Init(const byte *buf, int dimx, int dimy, bool bLoadPalette = false);

	virtual ~RMGfxSourceBuffer8RLEByteAA();
};

class RMGfxSourceBuffer8RLEWordAA : public RMGfxSourceBuffer8RLEWord, public RMGfxSourceBuffer8AA {
protected:
	void PrepareImage(void);

public:
	void Draw(RMGfxTargetBuffer& bigBuf, RMGfxPrimitive* prim);

	// Overloaded initialisation method
	virtual void Init(RMDataStream& ds, int dimx, int dimy, bool bLoadPalette = false);
    virtual int Init(byte *buf, int dimx, int dimy, bool bLoadPalette = false);

	virtual ~RMGfxSourceBuffer8RLEWordAA();
};


/**
 * Source buffer with 16 colours
 */
class RMGfxSourceBuffer4 : public RMGfxSourceBufferPal {
public:
    RMGfxSourceBuffer4();
    RMGfxSourceBuffer4(int dimx, int dimy, bool bUseDDraw = false);

	// Initialisation
	void Create(int dimx, int dimy, bool bUseDDraw = false);

    int Bpp();
    virtual void Draw(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim);
};


/**
 * Destination buffer which manages it's own internal list of tasks
 */
class RMGfxTargetBuffer : public virtual RMGfxBuffer {
private:
static RMGfxClearTask taskClear;

	struct OTList {
		RMGfxPrimitive* prim;
		OTList* next;

		OTList();
		OTList(RMGfxPrimitive* pr) { prim=pr; }
	};

private:
	OSystem::MutexRef csModifyingOT;

protected:
	OTList *otlist;
	int m_otSize;

public:
	RMGfxTargetBuffer();
	virtual ~RMGfxTargetBuffer();

	// management of the OT list
	void ClearOT(void);
	void DrawOT(void);
	void AddPrim(RMGfxPrimitive *prim); // The pointer must be delted

	// Adds a task to clear the screen
	void AddClearTask(void);

	operator byte *() { return m_buf; }
	operator void *() { return (void *)m_buf; }
	operator uint16 *() { 
		// FIXME: This may not be endian safe
		return (uint16 *)m_buf; 
	}

	// Offseting buffer
	void OffsetY(int nLines) { RMGfxBuffer::OffsetY(nLines, 16); }
};


/**
 * Ring buffer, which is both source and by destination
 */
class RMGfxWoodyBuffer: public RMGfxSourceBuffer16, public RMGfxTargetBuffer {
public:
    RMGfxWoodyBuffer();
    RMGfxWoodyBuffer(int dimx, int dimy, bool bUseDDraw = false);
	virtual ~RMGfxWoodyBuffer();

    virtual void Draw(RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim);
};

} // End of namespace Tony

#endif