forked from snes9xgit/snes9x
-
Notifications
You must be signed in to change notification settings - Fork 15
/
clip.cpp
234 lines (196 loc) · 5.33 KB
/
clip.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
/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/
#include "snes9x.h"
#include "memmap.h"
static uint8 region_map[6][6] =
{
{ 0, 0x01, 0x03, 0x07, 0x0f, 0x1f },
{ 0, 0, 0x02, 0x06, 0x0e, 0x1e },
{ 0, 0, 0, 0x04, 0x0c, 0x1c },
{ 0, 0, 0, 0, 0x08, 0x18 },
{ 0, 0, 0, 0, 0, 0x10 }
};
static inline uint8 CalcWindowMask (int, uint8, uint8);
static inline void StoreWindowRegions (uint8, struct ClipData *, int, int16 *, uint8 *, bool8, bool8 s = FALSE);
static inline uint8 CalcWindowMask (int i, uint8 W1, uint8 W2)
{
if (!PPU.ClipWindow1Enable[i])
{
if (!PPU.ClipWindow2Enable[i])
return (0);
else
{
if (!PPU.ClipWindow2Inside[i])
return (~W2);
return (W2);
}
}
else
{
if (!PPU.ClipWindow2Enable[i])
{
if (!PPU.ClipWindow1Inside[i])
return (~W1);
return (W1);
}
else
{
if (!PPU.ClipWindow1Inside[i])
W1 = ~W1;
if (!PPU.ClipWindow2Inside[i])
W2 = ~W2;
switch (PPU.ClipWindowOverlapLogic[i])
{
case 0: // OR
return (W1 | W2);
case 1: // AND
return (W1 & W2);
case 2: // XOR
return (W1 ^ W2);
case 3: // XNOR
return (~(W1 ^ W2));
}
}
}
// Never get here
return (0);
}
static inline void StoreWindowRegions (uint8 Mask, struct ClipData *Clip, int n_regions, int16 *windows, uint8 *drawing_modes, bool8 sub, bool8 StoreMode0)
{
int ct = 0;
for (int j = 0; j < n_regions; j++)
{
int DrawMode = drawing_modes[j];
if (sub)
DrawMode |= 1;
if (Mask & (1 << j))
DrawMode = 0;
if (!StoreMode0 && !DrawMode)
continue;
if (ct > 0 && Clip->Right[ct - 1] == windows[j] && Clip->DrawMode[ct - 1] == DrawMode)
Clip->Right[ct - 1] = windows[j + 1]; // This region borders with and has the same drawing mode as the previous region: merge them.
else
{
// Add a new region to the BG
Clip->Left[ct] = windows[j];
Clip->Right[ct] = windows[j + 1];
Clip->DrawMode[ct] = DrawMode;
ct++;
}
}
Clip->Count = ct;
}
void S9xComputeClipWindows (void)
{
int16 windows[6] = { 0, 256, 256, 256, 256, 256 };
uint8 drawing_modes[5] = { 0, 0, 0, 0, 0 };
int n_regions = 1;
int i, j;
// Calculate window regions. We have at most 5 regions, because we have 6 control points
// (screen edges, window 1 left & right, and window 2 left & right).
if (PPU.Window1Left <= PPU.Window1Right)
{
if (PPU.Window1Left > 0)
{
windows[2] = 256;
windows[1] = PPU.Window1Left;
n_regions = 2;
}
if (PPU.Window1Right < 255)
{
windows[n_regions + 1] = 256;
windows[n_regions] = PPU.Window1Right + 1;
n_regions++;
}
}
if (PPU.Window2Left <= PPU.Window2Right)
{
for (i = 0; i <= n_regions; i++)
{
if (PPU.Window2Left == windows[i])
break;
if (PPU.Window2Left < windows[i])
{
for (j = n_regions; j >= i; j--)
windows[j + 1] = windows[j];
windows[i] = PPU.Window2Left;
n_regions++;
break;
}
}
for (; i <= n_regions; i++)
{
if (PPU.Window2Right + 1 == windows[i])
break;
if (PPU.Window2Right + 1 < windows[i])
{
for (j = n_regions; j >= i; j--)
windows[j + 1] = windows[j];
windows[i] = PPU.Window2Right + 1;
n_regions++;
break;
}
}
}
// Get a bitmap of which regions correspond to each window.
uint8 W1, W2;
if (PPU.Window1Left <= PPU.Window1Right)
{
for (i = 0; windows[i] != PPU.Window1Left; i++) ;
for (j = i; windows[j] != PPU.Window1Right + 1; j++) ;
W1 = region_map[i][j];
}
else
W1 = 0;
if (PPU.Window2Left <= PPU.Window2Right)
{
for (i = 0; windows[i] != PPU.Window2Left; i++) ;
for (j = i; windows[j] != PPU.Window2Right + 1; j++) ;
W2 = region_map[i][j];
}
else
W2 = 0;
// Color Window affects the drawing mode for each region.
// Modes are: 3=Draw as normal, 2=clip color (math only), 1=no math (draw only), 0=nothing.
uint8 CW_color = 0, CW_math = 0;
uint8 CW = CalcWindowMask(5, W1, W2);
switch (Memory.FillRAM[0x2130] & 0xc0)
{
case 0x00: CW_color = 0; break;
case 0x40: CW_color = ~CW; break;
case 0x80: CW_color = CW; break;
case 0xc0: CW_color = 0xff; break;
}
switch (Memory.FillRAM[0x2130] & 0x30)
{
case 0x00: CW_math = 0; break;
case 0x10: CW_math = ~CW; break;
case 0x20: CW_math = CW; break;
case 0x30: CW_math = 0xff; break;
}
for (i = 0; i < n_regions; i++)
{
if (!(CW_color & (1 << i)))
drawing_modes[i] |= 1;
if (!(CW_math & (1 << i)))
drawing_modes[i] |= 2;
}
// Store backdrop clip window (draw everywhere color window allows)
StoreWindowRegions(0, &IPPU.Clip[0][5], n_regions, windows, drawing_modes, FALSE, TRUE);
StoreWindowRegions(0, &IPPU.Clip[1][5], n_regions, windows, drawing_modes, TRUE, TRUE);
// Store per-BG and OBJ clip windows
for (j = 0; j < 5; j++)
{
uint8 W = Settings.DisableGraphicWindows ? 0 : CalcWindowMask(j, W1, W2);
for (int sub = 0; sub < 2; sub++)
{
if (Memory.FillRAM[sub + 0x212e] & (1 << j))
StoreWindowRegions(W, &IPPU.Clip[sub][j], n_regions, windows, drawing_modes, sub);
else
StoreWindowRegions(0, &IPPU.Clip[sub][j], n_regions, windows, drawing_modes, sub);
}
}
}