1/*****************************************************************************\
2 Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3 This file is licensed under the Snes9x License.
4 For further information, consult the LICENSE file in the root directory.
5\*****************************************************************************/
6
7#ifndef _GFX_H_
8#define _GFX_H_
9
10#include "port.h"
11
12struct SGFX
13{
14 uint16 *Screen;
15 uint16 *SubScreen;
16 uint8 *ZBuffer;
17 uint8 *SubZBuffer;
18 uint32 Pitch;
19 uint32 ScreenSize;
20 uint16 *S;
21 uint8 *DB;
22 uint16 *ZERO;
23 uint32 RealPPL; // true PPL of Screen buffer
24 uint32 PPL; // number of pixels on each of Screen buffer
25 uint32 LinesPerTile; // number of lines in 1 tile (4 or 8 due to interlace)
26 uint16 *ScreenColors; // screen colors for rendering main
27 uint16 *RealScreenColors; // screen colors, ignoring color window clipping
28 uint8 Z1; // depth for comparison
29 uint8 Z2; // depth to save
30 uint32 FixedColour;
31 uint8 DoInterlace;
32 uint8 InterlaceFrame;
33 uint32 StartY;
34 uint32 EndY;
35 bool8 ClipColors;
36 uint8 OBJWidths[128];
37 uint8 OBJVisibleTiles[128];
38
39 struct ClipData *Clip;
40
41 struct
42 {
43 uint8 RTOFlags;
44 int16 Tiles;
45
46 struct
47 {
48 int8 Sprite;
49 uint8 Line;
50 } OBJ[32];
51 } OBJLines[SNES_HEIGHT_EXTENDED];
52
53 void (*DrawBackdropMath) (uint32, uint32, uint32);
54 void (*DrawBackdropNomath) (uint32, uint32, uint32);
55 void (*DrawTileMath) (uint32, uint32, uint32, uint32);
56 void (*DrawTileNomath) (uint32, uint32, uint32, uint32);
57 void (*DrawClippedTileMath) (uint32, uint32, uint32, uint32, uint32, uint32);
58 void (*DrawClippedTileNomath) (uint32, uint32, uint32, uint32, uint32, uint32);
59 void (*DrawMosaicPixelMath) (uint32, uint32, uint32, uint32, uint32, uint32);
60 void (*DrawMosaicPixelNomath) (uint32, uint32, uint32, uint32, uint32, uint32);
61 void (*DrawMode7BG1Math) (uint32, uint32, int);
62 void (*DrawMode7BG1Nomath) (uint32, uint32, int);
63 void (*DrawMode7BG2Math) (uint32, uint32, int);
64 void (*DrawMode7BG2Nomath) (uint32, uint32, int);
65
66 const char *InfoString;
67 uint32 InfoStringTimeout;
68 char FrameDisplayString[256];
69};
70
71struct SBG
72{
73 uint8 (*ConvertTile) (uint8 *, uint32, uint32);
74 uint8 (*ConvertTileFlip) (uint8 *, uint32, uint32);
75
76 uint32 TileSizeH;
77 uint32 TileSizeV;
78 uint32 OffsetSizeH;
79 uint32 OffsetSizeV;
80 uint32 TileShift;
81 uint32 TileAddress;
82 uint32 NameSelect;
83 uint32 SCBase;
84
85 uint32 StartPalette;
86 uint32 PaletteShift;
87 uint32 PaletteMask;
88 uint8 EnableMath;
89 uint8 InterlaceLine;
90
91 uint8 *Buffer;
92 uint8 *BufferFlip;
93 uint8 *Buffered;
94 uint8 *BufferedFlip;
95 bool8 DirectColourMode;
96};
97
98struct SLineData
99{
100 struct
101 {
102 uint16 VOffset;
103 uint16 HOffset;
104 } BG[4];
105};
106
107struct SLineMatrixData
108{
109 short MatrixA;
110 short MatrixB;
111 short MatrixC;
112 short MatrixD;
113 short CentreX;
114 short CentreY;
115 short M7HOFS;
116 short M7VOFS;
117};
118
119extern uint16 BlackColourMap[256];
120extern uint16 DirectColourMaps[8][256];
121extern uint8 mul_brightness[16][32];
122extern uint8 brightness_cap[64];
123extern struct SBG BG;
124extern struct SGFX GFX;
125
126#define H_FLIP 0x4000
127#define V_FLIP 0x8000
128#define BLANK_TILE 2
129
130struct COLOR_ADD
131{
132 static alwaysinline uint16 fn(uint16 C1, uint16 C2)
133 {
134 const int RED_MASK = 0x1F << RED_SHIFT_BITS;
135 const int GREEN_MASK = 0x1F << GREEN_SHIFT_BITS;
136 const int BLUE_MASK = 0x1F;
137
138 int rb = C1 & (RED_MASK | BLUE_MASK);
139 rb += C2 & (RED_MASK | BLUE_MASK);
140 int rbcarry = rb & ((0x20 << RED_SHIFT_BITS) | (0x20 << 0));
141 int g = (C1 & (GREEN_MASK)) + (C2 & (GREEN_MASK));
142 int rgbsaturate = (((g & (0x20 << GREEN_SHIFT_BITS)) | rbcarry) >> 5) * 0x1f;
143 uint16 retval = (rb & (RED_MASK | BLUE_MASK)) | (g & GREEN_MASK) | rgbsaturate;
144#if GREEN_SHIFT_BITS == 6
145 retval |= (retval & 0x0400) >> 5;
146#endif
147 return retval;
148 }
149
150 static alwaysinline uint16 fn1_2(uint16 C1, uint16 C2)
151 {
152 return ((((C1 & RGB_REMOVE_LOW_BITS_MASK) +
153 (C2 & RGB_REMOVE_LOW_BITS_MASK)) >> 1) +
154 (C1 & C2 & RGB_LOW_BITS_MASK)) | ALPHA_BITS_MASK;
155 }
156};
157
158struct COLOR_ADD_BRIGHTNESS
159{
160 static alwaysinline uint16 fn(uint16 C1, uint16 C2)
161 {
162 return ((brightness_cap[ (C1 >> RED_SHIFT_BITS) + (C2 >> RED_SHIFT_BITS) ] << RED_SHIFT_BITS) |
163 (brightness_cap[((C1 >> GREEN_SHIFT_BITS) & 0x1f) + ((C2 >> GREEN_SHIFT_BITS) & 0x1f)] << GREEN_SHIFT_BITS) |
164 // Proper 15->16bit color conversion moves the high bit of green into the low bit.
165 #if GREEN_SHIFT_BITS == 6
166 ((brightness_cap[((C1 >> 6) & 0x1f) + ((C2 >> 6) & 0x1f)] & 0x10) << 1) |
167 #endif
168 (brightness_cap[ (C1 & 0x1f) + (C2 & 0x1f)] ));
169 }
170
171 static alwaysinline uint16 fn1_2(uint16 C1, uint16 C2)
172 {
173 return COLOR_ADD::fn1_2(C1, C2);
174 }
175};
176
177
178struct COLOR_SUB
179{
180 static alwaysinline uint16 fn(uint16 C1, uint16 C2)
181 {
182 int rb1 = (C1 & (THIRD_COLOR_MASK | FIRST_COLOR_MASK)) | ((0x20 << 0) | (0x20 << RED_SHIFT_BITS));
183 int rb2 = C2 & (THIRD_COLOR_MASK | FIRST_COLOR_MASK);
184 int rb = rb1 - rb2;
185 int rbcarry = rb & ((0x20 << RED_SHIFT_BITS) | (0x20 << 0));
186 int g = ((C1 & (SECOND_COLOR_MASK)) | (0x20 << GREEN_SHIFT_BITS)) - (C2 & (SECOND_COLOR_MASK));
187 int rgbsaturate = (((g & (0x20 << GREEN_SHIFT_BITS)) | rbcarry) >> 5) * 0x1f;
188 uint16 retval = ((rb & (THIRD_COLOR_MASK | FIRST_COLOR_MASK)) | (g & SECOND_COLOR_MASK)) & rgbsaturate;
189#if GREEN_SHIFT_BITS == 6
190 retval |= (retval & 0x0400) >> 5;
191#endif
192 return retval;
193 }
194
195 static alwaysinline uint16 fn1_2(uint16 C1, uint16 C2)
196 {
197 return GFX.ZERO[((C1 | RGB_HI_BITS_MASKx2) -
198 (C2 & RGB_REMOVE_LOW_BITS_MASK)) >> 1];
199 }
200};
201
202void S9xStartScreenRefresh (void);
203void S9xEndScreenRefresh (void);
204void S9xBuildDirectColourMaps (void);
205void RenderLine (uint8);
206void S9xComputeClipWindows (void);
207void S9xDisplayChar (uint16 *, uint8);
208void S9xGraphicsScreenResize (void);
209// called automatically unless Settings.AutoDisplayMessages is false
210void S9xDisplayMessages (uint16 *, int, int, int, int);
211
212// external port interface which must be implemented or initialised for each port
213bool8 S9xGraphicsInit (void);
214void S9xGraphicsDeinit (void);
215bool8 S9xInitUpdate (void);
216bool8 S9xDeinitUpdate (int, int);
217bool8 S9xContinueUpdate (int, int);
218void S9xReRefresh (void);
219void S9xSetPalette (void);
220void S9xSyncSpeed (void);
221
222// called instead of S9xDisplayString if set to non-NULL
223extern void (*S9xCustomDisplayString) (const char *, int, int, bool, int type);
224
225#endif
226