Skip to content

Commit

Permalink
Adding palette fading to MHZ.
Browse files Browse the repository at this point in the history
  • Loading branch information
MainMemory committed May 18, 2015
1 parent 3fc2258 commit af8af82
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
15 changes: 15 additions & 0 deletions MHZ/All Seasons/setup.ini
@@ -0,0 +1,15 @@
file=../MHZ.dll
type=MHZ.MHZ
name=Mushroom Hill Zone (All Seasons)
music=MushroomHill1
fadelen=30
paltime=1 minute
version=S3K
[Level]
tiles=../tiles.bin
blocks=../blocks.bin
chunks=../chunks.bin
layout=../layout.bin
palette=../SonicAndTails.bin:0:0:16|../palette.bin:0:16:48
palette2=Early Fall|../SonicAndTails.bin:0:0:16|../palette2.bin:0:16:48
palette3=Late Fall|../SonicAndTails.bin:0:0:16|../palette2.bin:0:16:16|../palette3.bin:0:32:32
55 changes: 52 additions & 3 deletions MHZ/MHZ.cs
@@ -1,4 +1,5 @@
using SonicRetro.SonLVL.API;
using System;
using System.Drawing;

namespace MHZ
Expand All @@ -9,11 +10,16 @@ public class MHZ : SonicBGScrollSaver.Level
BitmapBits levelimg, layer1img, layer2img;
Bitmap bgimg = new Bitmap(1, 1);
int Width, Height;
LevelInfo levelinfo;
System.Timers.Timer paltimer;
int curpal;
int fadeframe = -1;

public override void Init(int width, int height)
{
Width = width;
Height = height;
levelinfo = IniSerializer.Deserialize<LevelInfo>("setup.ini");
LevelData.LoadGame("./setup.ini");
LevelData.LoadLevel("Level", true);
levelimg = LevelData.DrawBackground(null, true, true, false, false);
Expand All @@ -26,6 +32,17 @@ public override void Init(int width, int height)
Camera_Y_pos += (short)((levelimg.Height / 2) - (height / 2));
Camera_X_pos = 0;
UpdateScrolling(0, 0);
if (LevelData.Palette.Count > 1)
{
paltimer = new System.Timers.Timer((levelinfo.PaletteTime ?? TimeSpan.FromMinutes(1)).TotalMilliseconds);
paltimer.Elapsed += paltimer_Elapsed;
paltimer.Start();
}
}

void paltimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
fadeframe = 0;
}

public override System.Drawing.Bitmap GetBG()
Expand All @@ -35,6 +52,32 @@ public override System.Drawing.Bitmap GetBG()

public override void UpdateScrolling(short Camera_X_pos_diff, short Camera_Y_pos_diff)
{
if (fadeframe >= 0)
{
if (fadeframe == levelinfo.FadeLength)
{
curpal = (curpal + 1) % LevelData.Palette.Count;
int i = 0;
for (int y = 0; y < 4; y++)
for (int x = 0; x < 16; x++)
LevelData.BmpPal.Entries[i++] = LevelData.Palette[curpal][y, x].RGBColor;
fadeframe = -1;
paltimer.Start();
}
else
{
int i = 0;
int blendpal = (curpal + 1) % LevelData.Palette.Count;
double A = fadeframe++ / (double)levelinfo.FadeLength;
for (int y = 0; y < 4; y++)
for (int x = 0; x < 16; x++)
{
Color oldcolor = LevelData.Palette[curpal][y, x].RGBColor;
Color newcolor = LevelData.Palette[blendpal][y, x].RGBColor;
LevelData.BmpPal.Entries[i++] = Color.FromArgb((int)(((1 - A) * oldcolor.R) + (A * newcolor.R)), (int)(((1 - A) * oldcolor.G) + (A * newcolor.G)), (int)(((1 - A) * oldcolor.B) + (A * newcolor.B)));
}
}
}
Camera_X_pos += Camera_X_pos_diff;
Camera_Y_pos += Camera_Y_pos_diff;
BitmapBits bmp = new BitmapBits(levelimg.Width, levelimg.Height);
Expand All @@ -55,14 +98,20 @@ public override void UpdateScrolling(short Camera_X_pos_diff, short Camera_Y_pos

public override void PlayMusic()
{
SonicBGScrollSaver.Music.PlaySong(IniSerializer.Deserialize<MusicInfo>("setup.ini").Music);
SonicBGScrollSaver.Music.PlaySong(levelinfo.Music);
}
}

internal class MusicInfo
internal class LevelInfo
{
[System.ComponentModel.DefaultValue("MysticCave2P")]
[System.ComponentModel.DefaultValue("MushroomHill1")]
[IniName("music")]
public string Music { get; set; }
[System.ComponentModel.DefaultValue(30)]
[IniName("fadelen")]
public int FadeLength { get; set; }
[System.ComponentModel.TypeConverter(typeof(SonicBGScrollSaver.CustomTimeSpanConverter))]
[IniName("paltime")]
public TimeSpan? PaletteTime { get; set; }
}
}
3 changes: 3 additions & 0 deletions MHZ/MHZ.csproj
Expand Up @@ -51,6 +51,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="All Seasons\setup.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Early Fall\setup.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
2 changes: 1 addition & 1 deletion SonicBGScrollSaver/MainForm.cs
Expand Up @@ -50,7 +50,7 @@ public MainForm(IntPtr previewWndHandle)
}

private static readonly System.Timers.Timer FrameTimer = new System.Timers.Timer() { AutoReset = true };
private static readonly System.Timers.Timer SwitchTimer = new System.Timers.Timer() { AutoReset = true };
private static readonly System.Timers.Timer SwitchTimer = new System.Timers.Timer();
Graphics gfx;
Level level;
short hscrollspeed = 8, vscrollspeed;
Expand Down

0 comments on commit af8af82

Please sign in to comment.