-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackgroundLayer.cs
113 lines (98 loc) · 3.84 KB
/
BackgroundLayer.cs
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
using PKHack;
using System.Drawing;
using System.Drawing.Imaging;
namespace Kraken
{
/// <summary>
/// The BackgroundLayer class collects together the various elements of a battle background:
/// - BG Graphics
/// - BG Palette
/// - A Distorter object to compute transformations
/// </summary>
public class BackgroundLayer
{
private int entry;
private BackgroundGraphics gfx;
private BackgroundPalette pal;
private Distorter distort;
private Bitmap bmp;
/// <summary>
/// The index of the layer entry that was loaded
/// </summary>
public int Entry
{
get { return entry; }
}
/// <summary>
/// Constructs a BackgroundLayer object by loading the specified entry from the specified ROM object
/// </summary>
public BackgroundLayer(Rom src, int entry)
{
distort = new Distorter();
bmp = new Bitmap(256, 256, PixelFormat.Format24bppRgb);
LoadEntry(src, entry);
}
/// <summary>
/// Renders a frame of the background animation into the specified Bitmap
/// </summary>
/// <param name="dst">Bitmap object into which to render</param>
/// <param name="letterbox">Size in pixels of black borders at top and bottom of image</param>
/// <param name="ticks">Time value of the frame to compute</param>
/// <param name="alpha">Blending opacity</param>
/// <param name="erase">Whether or not to clear the destination bitmap before rendering</param>
public void OverlayFrame(Bitmap dst, int letterbox, int ticks, float alpha, bool erase)
{
distort.OverlayFrame(dst, letterbox, ticks, alpha, erase);
}
private void LoadGraphics(Rom src, int n)
{
gfx = (BackgroundGraphics)src.GetObject("BackgroundGraphics", n);
}
private void LoadPalette(Rom src, int n)
{
pal = (BackgroundPalette)src.GetObject("BackgroundPalette", n);
}
private void LoadEffect(Rom src, int n)
{
BattleBGEffect effect = (BattleBGEffect)src.GetObject("BattleBGEffect", n);
distort.Effect.Amplitude = effect.Amplitude;
distort.Effect.AmplitudeAcceleration = (short)effect.AmplitudeAcceleration;
distort.Effect.Compression = effect.Compression;
distort.Effect.CompressionAcceleration = effect.CompressionAcceleration;
distort.Effect.Frequency = effect.Frequency;
distort.Effect.FrequencyAcceleration = (short)effect.FrequencyAcceleration;
distort.Effect.Speed = effect.Speed;
if (effect.Type == 1)
distort.Effect.Effect = DistortionEffect.Type.Horizontal;
else if (effect.Type == 3)
distort.Effect.Effect = DistortionEffect.Type.Vertical;
else
distort.Effect.Effect = DistortionEffect.Type.HorizontalInterlaced;
}
private void LoadEntry(Rom src, int n)
{
entry = n;
BattleBG bg = (BattleBG)src.GetObject("BattleBG", n);
// Set graphics / palette
LoadGraphics(src, bg.GraphicsIndex);
LoadPalette(src, bg.PaletteIndex);
int e = bg.Animation;
byte e1 = (byte)(e >> 24);
byte e2 = (byte)(e >> 16);
byte e3 = (byte)(e >> 8);
byte e4 = (byte)(e);
// This is probably crap
if (e2 != 0)
LoadEffect(src, e2);
else
LoadEffect(src, e1);
InitializeBitmap();
}
private void InitializeBitmap()
{
bmp = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
gfx.Draw(bmp, pal);
distort.Original = bmp;
}
}
}