public void SetPaletteColor(int Index, byte Red, byte Green, byte Blue)
 {
     if ((m_PaletteData == null) ||
         (Index < 0) ||
         (Index >= m_PaletteData.Length / 3))
     {
         return;
     }
     m_PaletteData.SetU8At(Index * 3, Red);
     m_PaletteData.SetU8At(Index * 3 + 1, Green);
     m_PaletteData.SetU8At(Index * 3 + 2, Blue);
 }
Esempio n. 2
0
        private GR.Memory.ByteBuffer DataFromHex()
        {
            Be.Windows.Forms.DynamicByteProvider dynProvider = (Be.Windows.Forms.DynamicByteProvider)hexView.ByteProvider;

            List <byte> dataBytes = dynProvider.Bytes;

            if (dataBytes.Count == 0)
            {
                return(new GR.Memory.ByteBuffer());
            }

            long dataStart  = hexView.SelectionStart;
            long dataLength = hexView.SelectionLength;

            if (hexView.SelectionLength == 0)
            {
                dataStart  = 0;
                dataLength = hexView.ByteProvider.Length;
            }


            GR.Memory.ByteBuffer data = new GR.Memory.ByteBuffer((uint)dataLength);
            for (int i = 0; i < dataLength; ++i)
            {
                data.SetU8At(i, dataBytes[(int)dataStart + i]);
            }
            return(data);
        }
Esempio n. 3
0
 private bool ImportFile(string file)
 {
     GR.Memory.ByteBuffer fileData = GR.IO.File.ReadAllBytes(file);
     if ((fileData != null) &&
         (fileData.Length >= 0))
     {
         GR.Memory.ByteBuffer fileName = new GR.Memory.ByteBuffer(16);
         string shortName  = System.IO.Path.GetFileNameWithoutExtension(file).ToUpper();
         int    nameLength = shortName.Length;
         if (nameLength > 16)
         {
             nameLength = 16;
         }
         for (int i = 0; i < 16; ++i)
         {
             if (i < nameLength)
             {
                 fileName.SetU8At(i, (byte)shortName[i]);
             }
             else
             {
                 fileName.SetU8At(i, 0xA0);
             }
         }
         if (m_Media != null)
         {
             if (m_Media.WriteFile(fileName, fileData, C64Studio.Types.FileType.PRG))
             {
                 RefreshFileView();
                 UpdateStatusInfo();
                 return(true);
             }
         }
     }
     return(false);
 }
Esempio n. 4
0
        private bool ImportChar(GR.Image.FastImage Image, int CharIndex, bool ForceMulticolor)
        {
            if (Image.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                // invalid format
                return(false);
            }
            // Match image data
            GR.Memory.ByteBuffer Buffer = new GR.Memory.ByteBuffer(m_Charset.Characters[CharIndex].Data);

            int chosenCharColor = -1;

            bool isMultiColor = false;

            unsafe
            {
                // determine single/multi color
                bool[] usedColor           = new bool[16];
                int    numColors           = 0;
                bool   hasSinglePixel      = false;
                bool   usedBackgroundColor = false;

                for (int y = 0; y < Image.Height; ++y)
                {
                    for (int x = 0; x < Image.Width; ++x)
                    {
                        int colorIndex = (int)Image.GetPixelData(x, y) % 16;
                        if (colorIndex >= 16)
                        {
                            return(false);
                        }
                        if ((x % 2) == 0)
                        {
                            if (colorIndex != (int)Image.GetPixelData(x + 1, y) % 16)
                            {
                                // not a double pixel, must be single color then
                                hasSinglePixel = true;
                            }
                        }

                        if (!usedColor[colorIndex])
                        {
                            if (colorIndex == m_Charset.BackgroundColor)
                            {
                                usedBackgroundColor = true;
                            }
                            usedColor[colorIndex] = true;
                            numColors++;
                        }
                    }
                }
                //Debug.Log( "For Char " + CharIndex + ", hasSinglePixel = " + hasSinglePixel + ", numColors = " + numColors + ", usedBackgroundColor = " + usedBackgroundColor );
                if ((hasSinglePixel) &&
                    (numColors > 2))
                {
                    return(false);
                }
                if ((hasSinglePixel) &&
                    (numColors == 2) &&
                    (!usedBackgroundColor))
                {
                    return(false);
                }
                if ((!hasSinglePixel) &&
                    (numColors > 4))
                {
                    return(false);
                }
                if ((!hasSinglePixel) &&
                    (numColors == 4) &&
                    (!usedBackgroundColor))
                {
                    return(false);
                }
                int otherColorIndex = 16;
                if ((!hasSinglePixel) &&
                    (numColors == 2) &&
                    (usedBackgroundColor))
                {
                    for (int i = 0; i < 16; ++i)
                    {
                        if ((usedColor[i]) &&
                            (i != m_Charset.BackgroundColor))
                        {
                            otherColorIndex = i;
                            break;
                        }
                    }
                }
                if ((!ForceMulticolor) &&
                    ((hasSinglePixel) ||
                     ((numColors == 2) &&
                      (usedBackgroundColor) &&
                      (otherColorIndex < 8))))
                //||   ( numColors == 2 ) )
                {
                    // eligible for single color
                    int usedFreeColor = -1;
                    for (int i = 0; i < 16; ++i)
                    {
                        if (usedColor[i])
                        {
                            if (i != m_Charset.BackgroundColor)
                            {
                                if (usedFreeColor != -1)
                                {
                                    return(false);
                                }
                                usedFreeColor = i;
                            }
                        }
                    }

                    for (int y = 0; y < Image.Height; ++y)
                    {
                        for (int x = 0; x < Image.Width; ++x)
                        {
                            int ColorIndex = (int)Image.GetPixelData(x, y) % 16;

                            int BitPattern = 0;

                            if (ColorIndex != m_Charset.BackgroundColor)
                            {
                                BitPattern = 1;
                            }

                            // noch nicht verwendete Farbe
                            if (BitPattern == 1)
                            {
                                chosenCharColor = ColorIndex;
                            }
                            byte byteMask = (byte)(255 - (1 << ((7 - (x % 8)))));
                            Buffer.SetU8At(y + x / 8, (byte)((Buffer.ByteAt(y + x / 8) & byteMask) | (BitPattern << ((7 - (x % 8))))));
                        }
                    }
                }
                else
                {
                    // multi color
                    isMultiColor = true;
                    int usedMultiColors = 0;
                    int usedFreeColor   = -1;
                    for (int i = 0; i < 16; ++i)
                    {
                        if (usedColor[i])
                        {
                            if ((i == m_Charset.MultiColor1) ||
                                (i == m_Charset.MultiColor2) ||
                                (i == m_Charset.BackgroundColor))
                            {
                                ++usedMultiColors;
                            }
                            else
                            {
                                usedFreeColor = i;
                            }
                        }
                    }
                    if (numColors - usedMultiColors > 1)
                    {
                        // only one free color allowed
                        return(false);
                    }
                    for (int y = 0; y < Image.Height; ++y)
                    {
                        for (int x = 0; x < Image.Width / 2; ++x)
                        {
                            int ColorIndex = (int)Image.GetPixelData(2 * x, y) % 16;

                            byte BitPattern = 0;

                            if (ColorIndex == m_Charset.BackgroundColor)
                            {
                                BitPattern = 0x00;
                            }
                            else if (ColorIndex == m_Charset.MultiColor1)
                            {
                                BitPattern = 0x01;
                            }
                            else if (ColorIndex == m_Charset.MultiColor2)
                            {
                                BitPattern = 0x02;
                            }
                            else
                            {
                                // noch nicht verwendete Farbe
                                chosenCharColor = usedFreeColor;
                                BitPattern      = 0x03;
                            }
                            byte byteMask = (byte)(255 - (3 << ((3 - (x % 4)) * 2)));
                            Buffer.SetU8At(y + x / 4, (byte)((Buffer.ByteAt(y + x / 4) & byteMask) | (BitPattern << ((3 - (x % 4)) * 2))));
                        }
                    }
                }
            }
            for (int i = 0; i < 8; ++i)
            {
                m_Charset.Characters[CharIndex].Data.SetU8At(i, Buffer.ByteAt(i));
            }
            if (chosenCharColor == -1)
            {
                chosenCharColor = 0;
            }
            m_Charset.Characters[CharIndex].Color = chosenCharColor;
            if ((isMultiColor) &&
                (chosenCharColor < 8))
            {
                m_Charset.Characters[CharIndex].Color = chosenCharColor + 8;
            }
            m_Charset.Characters[CharIndex].Mode = isMultiColor ? Types.CharsetMode.MULTICOLOR : C64Studio.Types.CharsetMode.HIRES;
            return(true);
        }
        public override bool HandleImport(CharsetScreenProject CharScreen, CharsetScreenEditor Editor)
        {
            string filename;

            if (Editor.OpenFile("Open Charpad project, Marq's PETSCII editor files or binary data", Constants.FILEFILTER_CHARSET_CHARPAD + Constants.FILEFILTER_MARQS_PETSCII + Constants.FILEFILTER_ALL, out filename))
            {
                if (System.IO.Path.GetExtension(filename).ToUpper() == ".CTM")
                {
                    // a charpad project file
                    GR.Memory.ByteBuffer projectFile = GR.IO.File.ReadAllBytes(filename);

                    Formats.CharpadProject cpProject = new RetroDevStudio.Formats.CharpadProject();
                    if (!cpProject.LoadFromFile(projectFile))
                    {
                        return(false);
                    }

                    CharScreen.CharSet.Colors.BackgroundColor = cpProject.BackgroundColor;
                    CharScreen.CharSet.Colors.MultiColor1     = cpProject.MultiColor1;
                    CharScreen.CharSet.Colors.MultiColor2     = cpProject.MultiColor2;
                    CharScreen.CharSet.Colors.BGColor4        = cpProject.BackgroundColor4;

                    int maxChars = cpProject.NumChars;
                    if (maxChars > CharScreen.CharSet.TotalNumberOfCharacters)
                    {
                        maxChars = CharScreen.CharSet.TotalNumberOfCharacters;
                    }

                    CharScreen.CharSet.ExportNumCharacters = maxChars;
                    for (int charIndex = 0; charIndex < CharScreen.CharSet.ExportNumCharacters; ++charIndex)
                    {
                        CharScreen.CharSet.Characters[charIndex].Tile.Data        = cpProject.Characters[charIndex].Data;
                        CharScreen.CharSet.Characters[charIndex].Tile.CustomColor = cpProject.Characters[charIndex].Color;

                        Editor.RebuildCharImage(charIndex);
                    }

                    // import tiles
                    var mapProject = new MapProject();
                    mapProject.MultiColor1 = cpProject.MultiColor1;
                    mapProject.MultiColor2 = cpProject.MultiColor2;

                    for (int i = 0; i < cpProject.NumTiles; ++i)
                    {
                        Formats.MapProject.Tile tile = new Formats.MapProject.Tile();

                        tile.Name = "Tile " + (i + 1).ToString();
                        tile.Chars.Resize(cpProject.TileWidth, cpProject.TileHeight);
                        tile.Index = i;

                        for (int y = 0; y < tile.Chars.Height; ++y)
                        {
                            for (int x = 0; x < tile.Chars.Width; ++x)
                            {
                                tile.Chars[x, y].Character = (byte)cpProject.Tiles[i].CharData.UInt16At(2 * (x + y * tile.Chars.Width));
                                tile.Chars[x, y].Color     = cpProject.Tiles[i].ColorData.ByteAt(x + y * tile.Chars.Width);
                            }
                        }
                        mapProject.Tiles.Add(tile);
                    }

                    var map = new Formats.MapProject.Map();
                    map.Tiles.Resize(cpProject.MapWidth, cpProject.MapHeight);
                    for (int j = 0; j < cpProject.MapHeight; ++j)
                    {
                        for (int i = 0; i < cpProject.MapWidth; ++i)
                        {
                            map.Tiles[i, j] = cpProject.MapData.ByteAt(i + j * cpProject.MapWidth);
                        }
                    }
                    map.TileSpacingX = cpProject.TileWidth;
                    map.TileSpacingY = cpProject.TileHeight;
                    mapProject.Maps.Add(map);

                    Editor.comboBackground.SelectedIndex  = mapProject.Charset.Colors.BackgroundColor;
                    Editor.comboMulticolor1.SelectedIndex = mapProject.Charset.Colors.MultiColor1;
                    Editor.comboMulticolor2.SelectedIndex = mapProject.Charset.Colors.MultiColor2;
                    Editor.comboBGColor4.SelectedIndex    = mapProject.Charset.Colors.BGColor4;
                    Editor.comboCharsetMode.SelectedIndex = (int)cpProject.DisplayModeFile;

                    GR.Memory.ByteBuffer charData  = new GR.Memory.ByteBuffer((uint)(map.Tiles.Width * map.TileSpacingX * map.Tiles.Height * map.TileSpacingY));
                    GR.Memory.ByteBuffer colorData = new GR.Memory.ByteBuffer((uint)(map.Tiles.Width * map.TileSpacingX * map.Tiles.Height * map.TileSpacingY));

                    for (int y = 0; y < map.Tiles.Height; ++y)
                    {
                        for (int x = 0; x < map.Tiles.Width; ++x)
                        {
                            int tileIndex = map.Tiles[x, y];
                            if (tileIndex < mapProject.Tiles.Count)
                            {
                                // a real tile
                                var tile = mapProject.Tiles[tileIndex];

                                for (int j = 0; j < tile.Chars.Height; ++j)
                                {
                                    for (int i = 0; i < tile.Chars.Width; ++i)
                                    {
                                        charData.SetU8At(x * map.TileSpacingX + i + (y * map.TileSpacingY + j) * (map.Tiles.Width * map.TileSpacingX), tile.Chars[i, j].Character);
                                        colorData.SetU8At(x * map.TileSpacingX + i + (y * map.TileSpacingY + j) * (map.Tiles.Width * map.TileSpacingX), tile.Chars[i, j].Color);
                                    }
                                }
                            }
                        }
                    }

                    if (cpProject.MapColorData != null)
                    {
                        // this charpad project has alternative color data
                        for (int i = 0; i < cpProject.MapHeight; ++i)
                        {
                            for (int j = 0; j < cpProject.MapWidth; ++j)
                            {
                                colorData.SetU8At(j + i * cpProject.MapWidth, cpProject.MapColorData.ByteAt(j + i * cpProject.MapWidth));
                            }
                        }
                    }

                    Editor.ImportFromData(map.TileSpacingX * map.Tiles.Width,
                                          map.TileSpacingY * map.Tiles.Height,
                                          charData, colorData, CharScreen.CharSet);
                }
                else if (System.IO.Path.GetExtension(filename).ToUpper() == ".C")
                {
                    string cData = GR.IO.File.ReadAllText(filename);
                    if (!string.IsNullOrEmpty(cData))
                    {
                        int dataStart = cData.IndexOf('{');
                        if (dataStart == -1)
                        {
                            return(false);
                        }

                        /*
                         * int     dataEnd = cData.IndexOf( '}', dataStart );
                         * if ( dataEnd == -1 )
                         * {
                         * return false;
                         * }
                         * string  actualData = cData.Substring( dataStart + 1, dataEnd - dataStart - 2 );
                         */
                        string actualData = cData.Substring(dataStart + 1);

                        var screenData = new ByteBuffer();

                        var    dataLines = actualData.Split('\n');
                        string dataType  = "";
                        for (int i = 0; i < dataLines.Length; ++i)
                        {
                            var dataLine = dataLines[i].Trim();
                            if (dataLine.StartsWith("//"))
                            {
                                if (dataLine.Contains("META:"))
                                {
                                    int metaPos = dataLine.IndexOf("META:");

                                    // META: 16 25 DIRART upper
                                    string[] parms = dataLine.Substring(metaPos + 5).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                                    if (parms.Length >= 2)
                                    {
                                        int newWidth  = GR.Convert.ToI32(parms[0]);
                                        int newHeight = GR.Convert.ToI32(parms[1]);

                                        if ((newWidth > 0) &&
                                            (newHeight > 0))
                                        {
                                            Editor.SetScreenSize(newWidth, newHeight);
                                        }
                                    }
                                    if (parms.Length >= 3)
                                    {
                                        dataType = parms[2].ToUpper();
                                    }
                                }
                                continue;
                            }
                            int pos      = 0;
                            int commaPos = -1;

                            while (pos < dataLine.Length)
                            {
                                commaPos = dataLine.IndexOf(',', pos);
                                if (commaPos == -1)
                                {
                                    // end of line
                                    byte byteValue = GR.Convert.ToU8(dataLine.Substring(pos));

                                    screenData.AppendU8(byteValue);
                                    break;
                                }
                                else
                                {
                                    byte byteValue = GR.Convert.ToU8(dataLine.Substring(pos, commaPos - pos));

                                    screenData.AppendU8(byteValue);
                                    pos = commaPos + 1;
                                }
                            }
                        }
                        // border and BG first

                        if (dataType != "DIRART")
                        {
                            CharScreen.CharSet.Colors.BackgroundColor = screenData.ByteAt(1);
                            screenData = screenData.SubBuffer(2);
                        }

                        Editor.ImportFromData(screenData);
                    }
                }
                else
                {
                    GR.Memory.ByteBuffer data = GR.IO.File.ReadAllBytes(filename);

                    Editor.ImportFromData(data);
                }
            }

            return(true);
        }
        public void Box(int X, int Y, int Width, int Height, uint Value)
        {
            if ((X + Width <= 0) ||
                (X >= m_Width) ||
                (Y + Height <= 0) ||
                (Y >= m_Height))
            {
                return;
            }
            if (X < 0)
            {
                Width += X;
                X      = 0;
            }
            if (X + Width >= m_Width)
            {
                Width = m_Width - X;
            }
            if (Y < 0)
            {
                Height += Y;
                Y       = 0;
            }
            if (Y + Height >= m_Height)
            {
                Height = m_Height - Y;
            }
            switch (BitsPerPixel)
            {
            case 8:
                for (int i = 0; i < Height; ++i)
                {
                    for (int j = 0; j < Width; ++j)
                    {
                        m_ImageData.SetU8At(j + X + BytesPerLine * (Y + i), (byte)Value);
                    }
                }
                break;

            case 24:
                for (int i = 0; i < Height; ++i)
                {
                    for (int j = 0; j < Width; ++j)
                    {
                        m_ImageData.SetU8At((j + X) * 3 + BytesPerLine * (Y + i), (byte)(Value & 0xff));
                        m_ImageData.SetU8At((j + X) * 3 + BytesPerLine * (Y + i) + 1, (byte)((Value & 0xff00) >> 8));
                        m_ImageData.SetU8At((j + X) * 3 + BytesPerLine * (Y + i) + 2, (byte)((Value & 0xff0000) >> 16));
                    }
                }
                break;

            case 32:
                for (int i = 0; i < Height; ++i)
                {
                    for (int j = 0; j < Width; ++j)
                    {
                        m_ImageData.SetU32At((j + X) * 4 + BytesPerLine * (Y + i), Value);
                    }
                }
                break;

            default:
                throw new NotSupportedException("Bitdepth " + BitsPerPixel + " not supported yet");
            }
        }