Skip to content

Instantly share code, notes, and snippets.

@spiiin
Created January 31, 2014 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spiiin/8738491 to your computer and use it in GitHub Desktop.
Save spiiin/8738491 to your computer and use it in GitHub Desktop.
//код расжатия:
private void reloadMap()
{
int romAddr = 0x83FC;
while (Globals.romdata[romAddr] != 0xFF)
{
int videoAddr = Utils.readWord(Globals.romdata, romAddr) - 0x2000;
romAddr += 2;
int count = Globals.romdata[romAddr++];
for (int i = 0; i < count; i++)
mapData[videoAddr++] = Globals.romdata[romAddr++];
}
}
//код изменения карты:
private void mapScreen_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X / 16;
int y = e.Y / 16;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
mapData[y * 32 + x] = (byte)curActiveBlock;
}
else
{
//bit magic!!!
int colorByte = mapData[0x3C0 + x / 4 + 8 * (y / 4)];
int startBitIndex = x % 4 / 2 * 2 + y % 4 / 2 * 4; //get start bit index
int subPal = (colorByte >> startBitIndex) & 0x03; //get 2 bits for subpal
subPal = (subPal + 1) & 0x3; //round increment it
colorByte &= ~(3 << startBitIndex); //clear 2 bits in color byte
colorByte |= (subPal << startBitIndex); //set 2 bits according subpal
mapData[0x3C0 + x / 4 + 8 * (y / 4)] = (byte)colorByte;
}
}
//код запаковки
void foundLongestZeros(byte[] data, int startIndex, int endIndex, out int firstZeroIndex, out int lastZeroIndex)
{
int longestZeroLen = 0;
int curZeroLen = 0;
int curFirstZeroIndex = -1;
for (int i = startIndex; i < endIndex; i++)
{
if (data[i] == 0)
{
if (++curZeroLen > longestZeroLen)
{
longestZeroLen = curZeroLen;
curFirstZeroIndex = i - longestZeroLen + 1;
}
}
else
{
curZeroLen = 0;
}
}
firstZeroIndex = curFirstZeroIndex;
lastZeroIndex = firstZeroIndex + longestZeroLen - 1;
}
void recursiveS(byte[] d, int first, int last, MemoryStream outBuf)
{
int f, e;
foundLongestZeros(d, first, last, out f, out e);
if (e - f >= 5)
{
recursiveS(d, first, f, outBuf);
recursiveS(d, e + 1, last, outBuf);
}
else
{
while (last > first)
{
int addr = first + 0x2000;
outBuf.WriteByte((byte)(addr >> 8));
outBuf.WriteByte((byte)(addr & 0xFF));
outBuf.WriteByte((byte)Math.Min(last - first, 255));
for (int ind = 0; ind < 255 && (first+ind) < last; ind++)
outBuf.WriteByte(d[first+ind]);
first += 255;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment