-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicTile.cs
More file actions
365 lines (305 loc) · 11.2 KB
/
GraphicTile.cs
File metadata and controls
365 lines (305 loc) · 11.2 KB
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
using GR.Memory;
using System;
using System.Collections.Generic;
using System.Text;
namespace RetroDevStudio.Types
{
public class GraphicTile
{
public ColorSettings Colors = new ColorSettings();
public GraphicTileMode Mode = GraphicTileMode.COMMODORE_HIRES;
public int TransparentColorIndex = -1;
public int Width = 8;
public int Height = 8;
public int CustomColor = 1;
public ByteBuffer Data = new ByteBuffer( 8 );
public GR.Image.MemoryImage Image = new GR.Image.MemoryImage( 8, 8, System.Drawing.Imaging.PixelFormat.Format32bppRgb );
public GraphicTile()
{
}
public GraphicTile( GraphicTile OtherTile )
{
Width = OtherTile.Width;
Height = OtherTile.Height;
Mode = OtherTile.Mode;
Colors = OtherTile.Colors;
CustomColor = OtherTile.CustomColor;
TransparentColorIndex = OtherTile.TransparentColorIndex;
Data = new ByteBuffer( OtherTile.Data );
Image = new GR.Image.MemoryImage( OtherTile.Image );
}
public GraphicTile( int Width, int Height, GraphicTileMode Mode, ColorSettings Color )
{
this.Width = Width;
this.Height = Height;
this.Mode = Mode;
Colors = Color;
Data = new ByteBuffer( (uint)Lookup.NumBytes( Width, Height, Mode ) );
Image = new GR.Image.MemoryImage( Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb );
}
public bool SetPixel( int X, int Y, ColorType Color )
{
return SetPixel( X, Y, (int)Color );
}
public bool SetPixel( int X, int Y, int Color )
{
switch ( Mode )
{
case GraphicTileMode.COMMODORE_ECM:
case GraphicTileMode.COMMODORE_HIRES:
{
int bytePos = Y * ( ( Width + 7 ) / 8 ) + X / 8;
int byteValue = Data.ByteAt( bytePos );
if ( ( byteValue & ( 1 << ( 7 - ( X % 8 ) ) ) ) == 0 )
{
if ( Color != 0 )
{
Data.SetU8At( bytePos, (byte)( byteValue | ( 1 << ( 7 - ( X % 8 ) ) ) ) );
return true;
}
}
else if ( Color == 0 )
{
Data.SetU8At( bytePos, (byte)( byteValue & ~( 1 << ( 7 - ( X % 8 ) ) ) ) );
return true;
}
}
break;
case GraphicTileMode.COMMODORE_MULTICOLOR:
{
int bytePos = Y * ( ( Width + 7 ) / 8 ) + X / 8;
// mc mode
X = ( X % 8 ) / 2;
X = 3 - X;
byte newByte = (byte)( Data.ByteAt( bytePos ) & ~( 3 << ( 2 * X ) ) );
int replacementBytes = 0;
switch ( (ColorType)Color )
{
case ColorType.CUSTOM_COLOR:
replacementBytes = 3;
break;
case ColorType.MULTICOLOR_1:
replacementBytes = 1;
break;
case ColorType.MULTICOLOR_2:
replacementBytes = 2;
break;
}
newByte |= (byte)( replacementBytes << ( 2 * X ) );
if ( Data.ByteAt( bytePos ) != newByte )
{
Data.SetU8At( bytePos, newByte );
return true;
}
}
break;
case GraphicTileMode.MEGA65_FCM_16_COLORS:
{
int bytePos = X / 2 + Y * ( ( Width + 1 ) / 2 );
byte pixelValue = Data.ByteAt( bytePos );
if ( ( X % 2 ) == 0 )
{
if ( ( pixelValue >> 4 ) != Color )
{
pixelValue &= 0x0f;
pixelValue |= (byte)( Color << 4 );
Data.SetU8At( bytePos, pixelValue );
//Image.SetPixel( X, Y, (uint)Color );
return true;
}
}
else
{
if ( ( pixelValue & 0x0f ) != Color )
{
pixelValue &= 0xf0;
pixelValue |= (byte)Color;
Data.SetU8At( bytePos, pixelValue );
//Image.SetPixel( X, Y, (uint)Color );
return true;
}
}
return false;
}
case GraphicTileMode.MEGA65_FCM_256_COLORS:
if ( Data.ByteAt( X + Y * Width ) != Color )
{
Data.SetU8At( X + Y * Width, (byte)Color );
//Image.SetPixel( X, Y, (uint)Color );
return true;
}
break;
default:
Debug.Log( "GraphicTile.SetPixel, unsupported mode " + Mode );
break;
}
return false;
}
public int GetPixel( int X, int Y )
{
switch ( Mode )
{
case GraphicTileMode.COMMODORE_ECM:
case GraphicTileMode.COMMODORE_HIRES:
{
if ( ( Data.ByteAt( Y * ( ( Width + 7 ) / 8 ) + X / 8 ) & ( 1 << ( 7 - ( X % 8 ) ) ) ) != 0 )
{
return (int)ColorType.CUSTOM_COLOR;
}
return (int)ColorType.BACKGROUND;
}
case GraphicTileMode.COMMODORE_MULTICOLOR:
//if ( CustomColor >= 8 )
{
// multi color
int innerX = ( X % 8 ) / 2;
innerX = 3 - innerX;
int bitPattern = Data.ByteAt( Y * ( ( Width + 7 ) / 8 ) + X / 8 ) & ( 3 << ( 2 * innerX ) );
bitPattern >>= innerX * 2;
switch ( bitPattern )
{
case 0x00:
return (int)ColorType.BACKGROUND;
case 0x01:
return (int)ColorType.MULTICOLOR_1;
case 0x02:
return (int)ColorType.MULTICOLOR_2;
case 0x03:
default:
return (int)ColorType.CUSTOM_COLOR;
}
}
//goto case GraphicTileMode.COMMODORE_HIRES;
case GraphicTileMode.MEGA65_FCM_16_COLORS:
if ( ( X % 2 ) == 1 )
{
return Data.ByteAt( X / 2 + Y * ( ( Width + 1 ) / 2 ) ) & 0x0f;
}
return Data.ByteAt( X / 2 + Y * ( ( Width + 1 ) / 2 ) ) >> 4;
case GraphicTileMode.MEGA65_FCM_256_COLORS:
return Data.ByteAt( X + Y * Width );
default:
Debug.Log( "GraphicTile.GetPixel, unsupported mode " + Mode );
return 0;
}
}
public int MapColor( uint Color )
{
return 0;
}
public int MapPixelColor( int X, int Y, GraphicTile TargetTile )
{
int pixelValue = GetPixel( X, Y );
if ( Mode == TargetTile.Mode )
{
return pixelValue;
}
// now things are getting funny
uint pixelColor = GetColorFromValue( pixelValue );
var potentialColors = new List<uint>();
switch ( TargetTile.Mode )
{
case GraphicTileMode.COMMODORE_HIRES:
potentialColors.Add( Colors.Palette.ColorValues[Colors.BackgroundColor] );
// TODO - variable color!
potentialColors.Add( Colors.Palette.ColorValues[CustomColor] );
break;
case GraphicTileMode.COMMODORE_MULTICOLOR:
potentialColors.Add( Colors.Palette.ColorValues[Colors.BackgroundColor] );
potentialColors.Add( Colors.Palette.ColorValues[Colors.MultiColor1] );
potentialColors.Add( Colors.Palette.ColorValues[Colors.MultiColor2] );
// TODO - variable color!
potentialColors.Add( Colors.Palette.ColorValues[CustomColor] );
break;
case GraphicTileMode.COMMODORE_ECM:
potentialColors.Add( Colors.Palette.ColorValues[Colors.BackgroundColor] );
potentialColors.Add( Colors.Palette.ColorValues[Colors.BGColor4] );
// TODO - variable colors!
potentialColors.Add( Colors.Palette.ColorValues[CustomColor] );
break;
case GraphicTileMode.MEGA65_FCM_16_COLORS:
case GraphicTileMode.MEGA65_FCM_256_COLORS:
potentialColors.AddRange( Colors.Palette.ColorValues );
break;
}
int bestMatch = FindClosestEntryInPalette( pixelColor, potentialColors );
return bestMatch;
}
private int FindClosestEntryInPalette( uint PixelColor, IEnumerable<uint> PotentialColors )
{
int index = 0;
foreach ( var color in PotentialColors )
{
if ( PixelColor == color )
{
return index;
}
++index;
}
// TODO - find best match
return (int)ColorType.BACKGROUND;
}
private uint GetColorFromValue( int PixelValue )
{
if ( Lookup.HasCustomPalette( Mode ) )
{
return Colors.Palette.ColorValues[PixelValue];
}
switch ( (ColorType)PixelValue )
{
case ColorType.BACKGROUND:
return Colors.Palette.ColorValues[Colors.BackgroundColor];
case ColorType.MULTICOLOR_1:
return Colors.Palette.ColorValues[Colors.MultiColor1];
case ColorType.MULTICOLOR_2:
return Colors.Palette.ColorValues[Colors.MultiColor2];
case ColorType.CUSTOM_COLOR:
return Colors.Palette.ColorValues[CustomColor];
case ColorType.BGCOLOR4:
return Colors.Palette.ColorValues[Colors.BGColor4];
}
return 0;
}
public bool Fill( int X, int Y, int ColorIndex )
{
int origColor = GetPixel( X, Y );
if ( origColor == ColorIndex )
{
return false;
}
List<System.Drawing.Point> pointsToCheck = new List<System.Drawing.Point>();
pointsToCheck.Add( new System.Drawing.Point( X, Y ) );
int pixelWidth = Lookup.PixelWidth( Mode );
while ( pointsToCheck.Count != 0 )
{
System.Drawing.Point point = pointsToCheck[pointsToCheck.Count - 1];
pointsToCheck.RemoveAt( pointsToCheck.Count - 1 );
if ( GetPixel( point.X, point.Y ) != ColorIndex )
{
SetPixel( point.X, point.Y, ColorIndex );
if ( ( point.X - pixelWidth >= 0 )
&& ( GetPixel( point.X - pixelWidth, point.Y ) == origColor ) )
{
pointsToCheck.Add( new System.Drawing.Point( point.X - pixelWidth, point.Y ) );
}
if ( ( point.X + pixelWidth < Width )
&& ( GetPixel( point.X + pixelWidth, point.Y ) == origColor ) )
{
pointsToCheck.Add( new System.Drawing.Point( point.X + pixelWidth, point.Y ) );
}
if ( ( point.Y > 0 )
&& ( GetPixel( point.X, point.Y - 1 ) == origColor ) )
{
pointsToCheck.Add( new System.Drawing.Point( point.X, point.Y - 1 ) );
}
if ( ( point.Y + 1 < Height )
&& ( GetPixel( point.X, point.Y + 1 ) == origColor ) )
{
pointsToCheck.Add( new System.Drawing.Point( point.X, point.Y + 1 ) );
}
}
}
return true;
}
}
}