Commit 7916b566 authored by Jesse Freeman's avatar Jesse Freeman

Fixes to Music, Sound, Color, Font, Tilemap and Controller chips. Also added...

Fixes to Music, Sound, Color, Font, Tilemap and Controller chips. Also added new APIs to the APIBridge and optimized SongData, TextureData and rendering.
parent 0c544a8a
......@@ -22,7 +22,7 @@ namespace PixelVisionSDK
{
/// <summary>
/// The <see cref="APIBridge" /> is the communication layer between the games
/// This is the communication layer between the games
/// and the engine's chips. It's designed to provide a clean and safe API
/// for games to use without exposing the rest of the underpinnings of the
/// engine.<br />
......@@ -51,13 +51,6 @@ namespace PixelVisionSDK
/// <param name="enginechips"></param>
public IEngineChips chips { get; set; }
/// <summary>
/// </summary>
public float timeDelta
{
get { return chips.chipManager.timeDelta; }
}
public int spriteWidth
{
get { return chips.spriteChip.width; }
......@@ -141,6 +134,11 @@ namespace PixelVisionSDK
get { return chips.controllerChip.mousePosition; }
}
public int backgroundColor
{
get { return chips.screenBufferChip.backgroundColor; }
}
public void DrawSprite(int id, int x, int y, bool flipH = false, bool flipV = false, bool aboveBG = true,
int colorOffset = 0)
{
......@@ -166,14 +164,20 @@ namespace PixelVisionSDK
var total = ids.Length;
if (flipH || flipV)
{
var height = MathUtil.CeilToInt(total / width);
SpriteChipUtil.FlipSpriteData(ref ids, width, height, flipH, flipV);
}
for (var i = 0; i < total; i++)
{
var id = Convert.ToInt32(ids[i]);
var id = ids[i];
if (id > -1)
{
var newX = MathUtil.FloorToInt(i % width) * spriteWidth + x;
var newY = MathUtil.FloorToInt(i / width) * spriteWidth + y;
DrawSprite(id, newX, newY, flipH, flipV, aboveBG, colorOffset);
}
}
......@@ -306,11 +310,16 @@ namespace PixelVisionSDK
public void DrawTextBox(string text, int witdh, int x, int y, string fontName = "Default", int letterSpacing = 0,
bool wholeWords = false)
{
text = wholeWords ? FontChip.WordWrap(text, witdh) : FontChip.Split(text, witdh);
text = FormatWordWrap(text, witdh, wholeWords);
DrawFont(text, x, y, fontName, letterSpacing);
}
public string FormatWordWrap(string text, int witdh, bool wholeWords = false)
{
return wholeWords ? FontChip.WordWrap(text, witdh) : FontChip.Split(text, witdh);
}
public void DrawTextBoxToBuffer(string text, int witdh, int column, int row, string fontName = "Default",
int letterSpacing = 0, bool wholeWords = false)
{
......
......@@ -16,6 +16,7 @@
using System;
using PixelVisionSDK.Utils;
using UnityEngine;
namespace PixelVisionSDK.Chips
{
......@@ -69,23 +70,65 @@ namespace PixelVisionSDK.Chips
var total = songDataCollection.Length;
for (var i = 0; i < total; i++)
{
if (songDataCollection[i] == null)
var sondData = songDataCollection[i];
if (sondData == null)
{
songDataCollection[i] = CreateNewSongData("Untitled" + i);
songDataCollection[i] = CreateNewSongData("Untitled" + i, maxTracks);
}
else
{
sondData.totalTracks = totalTracks;
}
}
}
}
}
public virtual SongData CreateNewSongData(string name)
public int totalNotes
{
get
{
return maxNoteNum;;
}
set
{
if (maxNoteNum == value)
return;
var total = totalLoops;
for (int i = 0; i < total; i++)
{
songDataCollection[i].totalNotes = value;
}
}
}
public virtual SongData CreateNewSongData(string name, int tracks = 4)
{
return new SongData(name);
return new SongData(name, tracks);
}
protected int _totalTracks = 0;
public int totalTracks
{
get { return soundChip.totalChannels; }
get
{
return _totalTracks;
}
set
{
value = Mathf.Clamp(value, 1, maxTracks);
var total = songDataCollection.Length;
for (var i = 0; i < total; i++)
{
songDataCollection[i].totalTracks = value;
}
_totalTracks = value;
}
}
/// <summary>
......@@ -136,7 +179,8 @@ namespace PixelVisionSDK.Chips
//engine.chipManager.AddToUpdateList(this);
totalLoops = 16;
maxTracks = 4;
totalTracks = maxTracks;
// Setup the sequencer values
var a = 440.0f; // a is 440 hz...
......@@ -174,6 +218,7 @@ namespace PixelVisionSDK.Chips
UpdateNoteTickLengths();
tracksPerLoop = activeSongData.tracks.Length;
UpdateMusicNotes();
LoadInstruments(activeSongData);
}
/// <summary>
......@@ -248,8 +293,9 @@ namespace PixelVisionSDK.Chips
LoadInstruments(activeSongData);
}
var total = activeSongData.tracks.Length;
// loop through each oldInstruments track
for (var trackNum = 0; trackNum < tracksPerLoop; trackNum++)
for (var trackNum = 0; trackNum < total; trackNum++)
{
// what note is it?
var gotANote = activeSongData.tracks[trackNum].notes[sequencerBeatNumber % notesPerTrack];
......
......@@ -70,7 +70,7 @@ namespace PixelVisionSDK.Chips
/// <param name="param">
/// A string representing the synth properties.
/// </param>
public void UpdateSound(int index, string param)
public virtual void UpdateSound(int index, string param)
{
var synth = sounds[index];
synth.CacheSound();
......@@ -99,7 +99,7 @@ namespace PixelVisionSDK.Chips
/// <summary>
/// Configures the <see cref="SoundChip" /> by registering itself with
/// the engine and setting up the deafult values for total
/// the engine and setting up the default values for total
/// <see cref="sounds" /> and total channels.
/// </summary>
public override void Configure()
......
......@@ -60,10 +60,16 @@ namespace PixelVisionSDK.Chips
/// </summary>
public void Init()
{
foreach (var chip in chips)
var chipNames = chips.Keys.ToList();
foreach (var chipName in chipNames)
{
chip.Value.Init();
chips[chipName].Init();
}
// foreach (var chip in chips)
// {
// chip.Value.Init();
// }
}
/// <summary>
......@@ -223,7 +229,7 @@ namespace PixelVisionSDK.Chips
/// <param name="chip">
/// Instance to the chip that needs to be activated.
/// </param>
public void ActivateChip(string id, AbstractChip chip)
public void ActivateChip(string id, AbstractChip chip, bool autoActivate = true)
{
if (HasChip(id))
{
......@@ -242,7 +248,8 @@ namespace PixelVisionSDK.Chips
drawChips.Add(chip as IDraw);
}
chip.Activate(engine);
if(autoActivate)
chip.Activate(engine);
}
/// <summary>
......
......@@ -130,27 +130,27 @@ namespace PixelVisionSDK.Chips
return float.Parse(GetData(key, defaultValue.ToString()));
}
public Dictionary<string, object> GenerateMetaData()
{
var metaData = new Dictionary<string, object>();
metaData.Add("name", name);
metaData.Add("description", description);
return metaData;
}
public void LoadMetaData(Dictionary<string, object> metaData)
{
if (metaData == null)
return;
if (metaData.ContainsKey("name"))
name = metaData["name"] as string;
if (metaData.ContainsKey("description"))
description = metaData["description"] as string;
}
// public Dictionary<string, object> GenerateMetaData()
// {
// var metaData = new Dictionary<string, object>();
//
// metaData.Add("name", name);
// metaData.Add("description", description);
//
// return metaData;
// }
//
// public void LoadMetaData(Dictionary<string, object> metaData)
// {
// if (metaData == null)
// return;
//
// if (metaData.ContainsKey("name"))
// name = metaData["name"] as string;
//
// if (metaData.ContainsKey("description"))
// description = metaData["description"] as string;
// }
/// <summary>
/// Used for updating the game's logic.
......
......@@ -127,23 +127,6 @@ namespace PixelVisionSDK.Chips
/// </returns>
float GetData(string key, float defaultValue);
/// <summary>
/// Used to generate meta data about the game. This includes the name,
/// <see cref="description" /> and more.
/// </summary>
/// <returns>
/// Returns a Dictionary with a string for the key and value.
/// </returns>
Dictionary<string, object> GenerateMetaData();
/// <summary>
/// Reads meta data and overrides the game's own values
/// </summary>
/// <param name="metaData">
/// Accepts a Dictionary with a string for the key and value.
/// </param>
void LoadMetaData(Dictionary<string, object> metaData);
}
}
\ No newline at end of file
......@@ -123,20 +123,19 @@ namespace PixelVisionSDK.Chips
/// palette when it resizes.
/// </summary>
/// <value>Int</value>
public int supportedColors
public int supportedColors { get; protected set; }
public void RecalculateSupportedColors()
{
get
var count = 0;
var total = _colors.Length;
for (var i = 0; i < total; i++)
{
var count = 0;
var total = _colors.Length;
for (var i = 0; i < total; i++)
{
if (_colors[i] != transparent)
count ++;
}
return count;
if (_colors[i] != transparent)
count ++;
}
supportedColors = count;
}
/// <summary>
......@@ -202,6 +201,9 @@ namespace PixelVisionSDK.Chips
if (index > _colors.Length || index < 0)
return;
// Make sure that all colors are uppercase
color = color.ToUpper();
if (ColorData.ValidateColor(color))
{
_colors[index] = color;
......@@ -240,8 +242,6 @@ namespace PixelVisionSDK.Chips
pages = MathUtil.CeilToInt(total / colorsPerPage);
}
}
}
\ No newline at end of file
......@@ -291,24 +291,30 @@ namespace PixelVisionSDK.Chips
return i + 1;
}
public static string Split(string str, int chunkSize)
public static string Split(string text, int maxLineLength)
{
var total = (double) str.Length;
var split = Enumerable.Range(0, (int) Math.Floor(total / chunkSize))
.Select(i => str.Substring(i * chunkSize, chunkSize)).ToArray();
var text = string.Join("\n", split);
return string.Join("\n", CalcualteSplit(text, maxLineLength));
}
var newTotal = text.Length;
public static string[] CalcualteSplit(string text, int maxLineLength)
{
var list = new List<string>();
if (total > newTotal)
int currentIndex;
var lastWrap = 0;
var whitespace = new[] { ' ', '\r', '\n', '\t' };
do
{
text += "\n" + str.Substring(newTotal);
}
return text;
currentIndex = lastWrap + maxLineLength > text.Length ? text.Length : (text.LastIndexOfAny(new[] { ' ', ',', '.', '?', '!', ':', ';', '-', '\n', '\r', '\t' }, Math.Min(text.Length - 1, lastWrap + maxLineLength)) + 1);
if (currentIndex <= lastWrap)
currentIndex = Math.Min(lastWrap + maxLineLength, text.Length);
list.Add(text.Substring(lastWrap, currentIndex - lastWrap).Trim(whitespace));
lastWrap = currentIndex;
} while (currentIndex < text.Length);
return list.ToArray();
}
}
}
\ No newline at end of file
......@@ -15,9 +15,6 @@
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PixelVisionSDK.Utils;
namespace PixelVisionSDK.Chips
......@@ -36,12 +33,6 @@ namespace PixelVisionSDK.Chips
protected int[] flags = new int[0];
protected bool[] invalid = new bool[0];
protected int[] paletteIDs = new int[0];
/// <summary>
/// A flag to toggle whether tile data should be serialized.
/// </summary>
public bool serializeTileData;
protected int[] spriteIDs = new int[0];
protected int tmpIndex;
protected int[] tmpPixelData = new int[8 * 8];
......@@ -175,7 +166,7 @@ namespace PixelVisionSDK.Chips
/// The row position of the tile. 0 is the top of the tile map.
/// </param>
/// <returns>
/// Returns the colorint offset.
/// Returns the color int offset.
/// </returns>
public int ReadPaletteAt(int column, int row)
{
......@@ -195,7 +186,7 @@ namespace PixelVisionSDK.Chips
/// The row position of the tile. 0 is the top of the tile map.
/// </param>
/// <param name="paletteID">
/// A colorint offset.
/// A color int offset.
/// </param>
public void UpdatePaletteAt(int column, int row, int paletteID)
{
......@@ -268,6 +259,8 @@ namespace PixelVisionSDK.Chips
var size = total;
//TODO this would break the existing tilemap structure
//Debug.Log("Resize Tile Map "+ size);
if (spriteIDs.Length != size)
Array.Resize(ref spriteIDs, size);
......
......@@ -15,6 +15,7 @@
//
using System;
using UnityEngine;
namespace PixelVisionSDK.Chips
{
......@@ -203,6 +204,24 @@ namespace PixelVisionSDK.Chips
return controllers[controllerID].ReadKeyMap(button);
}
public int CaptureKey(int[] values)
{
if (keyInput != null)
{
var total = values.Length;
for (int i = 0; i < total; i++)
{
var keyCode = (int) values.GetValue(i);
if (keyInput.GetKey(keyCode))
return keyCode;
}
}
return 0;
}
}
}
\ No newline at end of file
......@@ -40,9 +40,22 @@ namespace PixelVisionSDK
/// </summary>
public TrackData[] tracks = new TrackData[0];
public SongData(string name = "Untitled")
public int totalNotes
{
set
{
var total = tracks.Length;
for (var i = 0; i < total; i++)
{
tracks[i].totalNotes = value;
}
}
}
public SongData(string name = "Untitled", int tracks = 4)
{
Reset(name);
totalTracks = tracks;
}
/// <summary>
......@@ -71,6 +84,7 @@ namespace PixelVisionSDK
if (tracks[i] == null)
{
tracks[i] = CreateNewTrack();
tracks[i].sfxID = i;
}
}
}
......@@ -81,6 +95,7 @@ namespace PixelVisionSDK
{
return new TrackData();
}
/// <summary>
/// Reset the default values of the SongData instance
/// </summary>
......
......@@ -55,10 +55,10 @@ namespace PixelVisionSDK
/// dimensions and an optional value for changing the wrap mode.
/// </summary>
/// <param name="width">
/// Anint for the width of the TextureData.
/// An int for the width of the TextureData.
/// </param>
/// <param name="height">
/// Anint for the height of the TextureData.
/// An int for the height of the TextureData.
/// </param>
/// <param name="wrapMode">
/// An optional value to support texture wrapping. It's set to
......@@ -166,7 +166,7 @@ namespace PixelVisionSDK
/// copied.
/// </param>
/// <param name="data">
/// Anint array where pixel data will be copied to.
/// An int array where pixel data will be copied to.
/// </param>
public void GetPixels(int x, int y, int blockWidth, int blockHeight, int[] data)
{
......
......@@ -32,6 +32,7 @@ namespace PixelVisionSDK
/// </summary>
IEngineChips chips { get; set; }
int backgroundColor { get; }
}
}
\ No newline at end of file
......@@ -25,11 +25,6 @@ namespace PixelVisionSDK
public interface IPixelVisionAPI : IKeyInput, IMouseInput
{
/// <summary>
/// The time difference between the last frame.
/// </summary>
float timeDelta { get; }
/// <summary>
/// A flag for whether the engine is
/// <see cref="IPixelVisionAPI.paused" /> or not.
......@@ -250,6 +245,15 @@ namespace PixelVisionSDK
void DrawTextBox(string text, int witdh, int x, int y, string fontName = "Default", int letterSpacing = 0,
bool wholeWords = false);
/// <summary>
/// Reformats text with word wrap.
/// </summary>
/// <param name="text"></param>
/// <param name="witdh"></param>
/// <param name="wholeWords"></param>
/// <returns></returns>
string FormatWordWrap(string text, int witdh, bool wholeWords = false);
/// <summary>
/// Draws text to the buffer to a predefined width and word wraps.
/// </summary>
......@@ -422,7 +426,7 @@ namespace PixelVisionSDK
void TogglePause(bool value);
/// <summary>
/// This enables or dissabled the display wrap which allows sprites
/// This enables or disabled the display wrap which allows sprites
/// that go off-screen to be rendered on the opposite side.
/// </summary>
/// <param name="value"></param>
......@@ -443,7 +447,7 @@ namespace PixelVisionSDK
void SaveData(string key, string value);
/// <summary>
/// Reads saved data based on the spupplied key.
/// Reads saved data based on the supplied key.
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
......
......@@ -16,6 +16,7 @@
using System;
using System.Linq;
using System.Text;
using PixelVisionSDK.Chips;
namespace PixelVisionSDK.Utils
......@@ -157,6 +158,7 @@ namespace PixelVisionSDK.Utils
//Debug.Log("Old Size "+ oldSize + " new size "+newSize +" new size "+ newSpriteWidth+"x"+ newSpriteHeight);
}
private static StringBuilder tmpSB = new StringBuilder();
/// <summary>
/// </summary>
......@@ -165,7 +167,16 @@ namespace PixelVisionSDK.Utils
/// </returns>
public static string SpriteDataToString(int[] data)
{
return string.Join(",", data.Select(x => x.ToString()).ToArray());
tmpSB.Length = 0;
var total = data.Length;
for (int i = 0; i < total; i++)
{
tmpSB.Append(data[i]);
}
return tmpSB.ToString();//string.Join(",", data.Select(x => x.ToString()).ToArray()));
}
public static void ShiftPixelData(ref int[] pixelData, int offset, int emptyColorID = -1)
......@@ -182,6 +193,28 @@ namespace PixelVisionSDK.Utils
}
}
/// <summary>
/// Tests to see if sprite <paramref name="data" /> is empty. This method
/// iterates over all the ints in the supplied <paramref name="data" />
/// array and looks for a value of -1. If all values are -1 then it
/// returns true.
/// </summary>
/// <param name="data">An array of ints</param>
/// <returns>
/// </returns>
public static bool IsEmpty(int[] data)
{
var total = data.Length;
for (var i = 0; i < total; i++)
{
if (data[i] > -1)
return false;
}
return false;
}
public static void CovertSpritesToRawData(ref int[] pixelData, int[] spriteIDs, int width, IEngineChips chips)
{
var spriteChip = chips.spriteChip;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment