Skip to content

Commit

Permalink
music player pattern update invoke check (extension)
Browse files Browse the repository at this point in the history
  • Loading branch information
movAX13h committed Jul 29, 2018
1 parent f067e59 commit c34146b
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 61 deletions.
15 changes: 15 additions & 0 deletions T2Tools/Formats/EIBFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

using System;

namespace T2Tools.Formats
{
class EIBFile
{
int a;

public EIBFile(byte[] data)
{
a = BitConverter.ToInt16(data, 0);
}
}
}
54 changes: 6 additions & 48 deletions T2Tools/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public partial class MainForm : Form
private int currentImgZoom = 3;

private MapMaker mapMaker;
private TilemapMaker tilemapMaker;

public MainForm()
{
Expand All @@ -43,7 +44,9 @@ private void MainForm_Load(object sender, EventArgs e)
{
MessageBox.Show("Failed to load game data: " + game.Error);
return;
}
}

tilemapMaker = new TilemapMaker(game.Assets);

// fill TOC list
foreach (var entry in game.Assets.Entries.Values)
Expand Down Expand Up @@ -136,7 +139,7 @@ private void fileSelected(TOCListItem item)
case TOCEntryType.Tileset:
case TOCEntryType.CollisionInfo:
tilesCollisionsCheckbox.Checked = item.Entry.Type == TOCEntryType.CollisionInfo;
Bitmap tilesetBitmap = makeTilesetBitmap(item.Entry);
Bitmap tilesetBitmap = tilemapMaker.MakeTilesetBitmap(item.Entry, tilesCollisionsCheckbox.Checked);
if (tilesetBitmap != null)
{
tilesPictureBox.Image = tilesetBitmap;
Expand Down Expand Up @@ -438,54 +441,9 @@ private void playSelectedTFM()
#endregion

#region tiles preview
private Bitmap makeTilesetBitmap(TOCEntry entry)
{
string picName, palName, colName;
TOCEntry picEntry, palEntry, colEntry;

// level number from BLOCK?.PIC or WORLD?.COL file
if (!int.TryParse(entry.Name.Substring(5, 1), out int levelNumber))
return null;
if (levelNumber == 6) levelNumber = 5;

if (entry.Type == TOCEntryType.CollisionInfo) // get matching block?.pic
{
colEntry = entry;
picName = $"BLOCK{levelNumber}.PIC";
if (!game.Assets.Entries.ContainsKey(picName))
return null;
picEntry = game.Assets.Entries[picName];
}
else // get matching world?.col
{
picEntry = entry;

// collision entry
colName = $"WORLD{levelNumber}.COL";
if (!game.Assets.Entries.ContainsKey(colName))
return null;
colEntry = game.Assets.Entries[colName];
}

// palette entry
palName = $"WORLD{levelNumber}.PAL";
if (!game.Assets.Entries.ContainsKey(palName))
return null;
palEntry = game.Assets.Entries[palName];

// read col data
COLFile colFile = tilesCollisionsCheckbox.Checked ? new COLFile(colEntry.Data) : null;

try
{
return TilemapMaker.FromBitmaps(PICConverter.PICToBitmaps(picEntry.Data, palEntry.Data), colFile);
}
catch { return null; }
}

private void tilesCollisionsCheckbox_CheckedChanged(object sender, EventArgs e)
{
tilesPictureBox.Image = makeTilesetBitmap(selectedItem.Entry);
tilesPictureBox.Image = tilemapMaker.MakeTilesetBitmap(selectedItem.Entry, tilesCollisionsCheckbox.Checked);
}
#endregion
}
Expand Down
1 change: 1 addition & 0 deletions T2Tools/T2Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Formats\BOBFile.cs" />
<Compile Include="Formats\COLFile.cs" />
<Compile Include="Formats\DrawProgramCpu.cs" />
<Compile Include="Formats\EIBFile.cs" />
<Compile Include="Formats\Palette.cs" />
<Compile Include="Formats\PCMFile.cs" />
<Compile Include="Formats\PCXFile.cs" />
Expand Down
3 changes: 2 additions & 1 deletion T2Tools/Turrican/TOCEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace T2Tools.Turrican
{
public enum TOCEntryType { Unknown, Gap, Text, Language, StaticSprite, AnimatedSprite,
Tileset, Bitmap, PixelFont, TextmodeFont, Palette, EntitiesList,
Music, Sound, Executable, Map, DAT, DIR, CollisionInfo }
Music, Sound, Executable, Map, DAT, DIR, CollisionInfo, BossSprite }

public class TOCEntry
{
Expand Down Expand Up @@ -41,6 +41,7 @@ public TOCEntryType Type
case ".gap": return TOCEntryType.Gap;
case ".eib": return TOCEntryType.EntitiesList;
case ".col": return TOCEntryType.CollisionInfo;
case ".mc": return TOCEntryType.BossSprite;
default: return TOCEntryType.Unknown;
}
}
Expand Down
72 changes: 63 additions & 9 deletions T2Tools/Turrican/TilemapMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,94 @@ public static Bitmap FromBitmaps(Bitmap[] bitmaps, COLFile collisionInfo)
{
if (i < collisionInfo.Entries.Length)
{
if (collisionInfo.Entries[i].A > 0)
var entry = collisionInfo.Entries[i];
if (entry.A > 0)
{
brush.Color = colors[collisionInfo.Entries[i].A];
brush.Color = colors[entry.A];
gfx.FillRectangle(brush, 16 * x, 16 * y, 8, 8);
}

if (collisionInfo.Entries[i].B > 0)
if (entry.B > 0)
{
brush.Color = colors[collisionInfo.Entries[i].B];
brush.Color = colors[entry.B];
gfx.FillRectangle(brush, 16 * x + 8, 16 * y, 8, 8);
}

if (collisionInfo.Entries[i].C > 0)
if (entry.C > 0)
{
brush.Color = colors[collisionInfo.Entries[i].C];
brush.Color = colors[entry.C];
gfx.FillRectangle(brush, 16 * x, 16 * y + 8, 8, 8);
}

if (collisionInfo.Entries[i].D > 0)
if (entry.D > 0)
{
brush.Color = colors[collisionInfo.Entries[i].D];
brush.Color = colors[entry.D];
gfx.FillRectangle(brush, 16 * x + 8, 16 * y + 8, 8, 8);
}
}
else
{
gfx.DrawLine(Pens.Red, 16 * x + 2, 16 * y + 2, 16 * x + 14, 16 * y + 14);
gfx.DrawLine(Pens.Red, 16 * x + 14, 16 * y + 2, 16 * x + 2, 16 * y + 14);
gfx.DrawLine(Pens.Red, 16 * x + 14, 16 * y + 2, 16 * x + 2, 16 * y + 14);
}
}
}
}

return bmp;
}

private TOC assets;

public TilemapMaker(TOC assets)
{
this.assets = assets;
}

public Bitmap MakeTilesetBitmap(TOCEntry entry, bool collisions) // entry can be COL or PIC
{
string picName, palName, colName;
TOCEntry picEntry, palEntry, colEntry;

// level number from BLOCK?.PIC or WORLD?.COL file
if (!int.TryParse(entry.Name.Substring(5, 1), out int levelNumber))
return null;
if (levelNumber == 6) levelNumber = 5;

if (entry.Type == TOCEntryType.CollisionInfo) // get matching block?.pic
{
colEntry = entry;
picName = $"BLOCK{levelNumber}.PIC";
if (!assets.Entries.ContainsKey(picName))
return null;
picEntry = assets.Entries[picName];
}
else // get matching world?.col
{
picEntry = entry;

// collision entry
colName = $"WORLD{levelNumber}.COL";
if (!assets.Entries.ContainsKey(colName))
return null;
colEntry = assets.Entries[colName];
}

// palette entry
palName = $"WORLD{levelNumber}.PAL";
if (!assets.Entries.ContainsKey(palName))
return null;
palEntry = assets.Entries[palName];

// read col data
COLFile colFile = collisions ? new COLFile(colEntry.Data) : null;

try
{
return FromBitmaps(PICConverter.PICToBitmaps(picEntry.Data, palEntry.Data), colFile);
}
catch { return null; }
}

}
}
12 changes: 12 additions & 0 deletions T2Tools/Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,17 @@ public static void FillCircle(this Graphics g, Brush brush,
radius + radius, radius + radius);
}

public static void InvokeIfRequired(this Control control, MethodInvoker action)
{
if (control.InvokeRequired)
{
control.Invoke(action);
}
else
{
action();
}
}

}
}
27 changes: 27 additions & 0 deletions TFXTool/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Reflection;
using System.Windows.Forms;

namespace TFXTool
{
public static class Extensions
{
public static void DoubleBuffered(this Control control, bool enable)
{
var doubleBufferPropertyInfo = control.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
doubleBufferPropertyInfo.SetValue(control, enable, null);
}

public static void InvokeIfRequired(this Control control, MethodInvoker action)
{
if (control.InvokeRequired)
{
control.Invoke(action);
}
else
{
action();
}
}

}
}
7 changes: 4 additions & 3 deletions TFXTool/PlayerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ private void Playroutine_MacroStart(object sender, MacroStartEventArgs e)

private void Playroutine_TrackstepPositionChanged(object sender, EventArgs e)
{
patternLabel.Text = "Pattern: " + currentPlayback.Playroutine.TrackstepPosition.ToString();


patternLabel.InvokeIfRequired(() =>
{
patternLabel.Text = "Pattern: " + currentPlayback.Playroutine.TrackstepPosition.ToString();
});
}

private void Playroutine_SongEnded(object sender, EventArgs e)
Expand Down
1 change: 1 addition & 0 deletions TFXTool/TFXTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
Expand Down

0 comments on commit c34146b

Please sign in to comment.