News: 11 March 2016 - Forum Rules

Author Topic: GBA raw graphics function  (Read 2660 times)

Madsiur

  • RHDN Patreon Supporter!
  • Full Member
  • *****
  • Posts: 180
  • FF6 hacker
    • View Profile
GBA raw graphics function
« on: August 31, 2014, 01:55:59 pm »
I've been working on a graphic editor for FF6A for some times now. The graphics are appearing fine in the editor with the correct palettes and colors but when I save them they appears as this in the game:



This happens even when I make no changes to the portraits. I figured it had something to do with the function that change the WriteableBitmap in a byte array. I took the function from an open source GBA graphic editor that already exists and modified it a bit. Here's the function:

Code: [Select]
        static public byte[] toGbaRaw(WriteableBitmap wBmp, int emptyGraphBlocks, List<Color> pal)
        {
            byte[] result = new byte[wBmp.PixelWidth * wBmp.PixelHeight * (int)bpp / 8 - emptyGraphBlocks * 8 * (int)bpp];

            for (int i = 0; i < result.Length; i++)
            {
                // bpp value is 4
                Point coord = tiledCoord(i * 8 / (int)bpp, wBmp.PixelWidth, 8);
                Color color1 = wBmp.GetPixel((int)coord.X, (int)coord.Y);
                Color color2 = wBmp.GetPixel((int)coord.X + 1, (int)coord.Y);

                byte value = 0x00;

                // if not transparency
                if(!color1.Equals(Color.FromArgb(0x00, 0x00, 0x00, 0x00)))
                {
                    value = (byte)(pal.IndexOf(color1) & 0xF);
                }

                // if not transparency
                if (!color2.Equals(Color.FromArgb(0x00, 0x00, 0x00, 0x00)))
                {
                    value += (byte)(pal.IndexOf(color2) & 0xF);
                }

                result[i] = value;
            }

            return result;
        }

I've been trying different things to solve this problem in the last few day but nothing seems to work. :banghead: Does anyone has an idea what would be wrong? Thanks in advance

For the record here's the function that takes a byte array and transform it into a bitmap. This one works correctly:

Code: [Select]
        static public unsafe WriteableBitmap toBmp(byte* graph, int length, int width, Color[] pal, out int emptyGraphPlocks)
        {
            int height, add;
            add = 0;

            if (length % (32 * width) != 0)
                add = 1;

            height = (((length / 32) - (length / 32) % width) / width + add);
            emptyGraphPlocks = (width * height) - (length / 32);

            BitmapPalette bPal = new BitmapPalette(pal);
            WriteableBitmap wBmp = new WriteableBitmap(width * 8, height * 8, 96, 96, PixelFormats.Pbgra32, null);

            for (int i = 0; i < length; i++)
            {
                Point pt = tiledCoord(i * 2, width * 8, 8);
                wBmp.SetPixel((int)pt.X, (int)pt.Y, bPal.Colors[*(graph + i) & 0xF]);
                wBmp.SetPixel((int)(pt.X + 1), (int)pt.Y, bPal.Colors[(*(graph + i) >> 4) & 0xF]);
            }

            return wBmp;
        }

KC

  • Full Member
  • ***
  • Posts: 210
    • View Profile
Re: GBA raw graphics function
« Reply #1 on: August 31, 2014, 02:14:57 pm »
You need to shift the second index left by four bits.

Code: [Select]
value |= (byte)((pal.IndexOf(color2) & 0xF) << 4);

Madsiur

  • RHDN Patreon Supporter!
  • Full Member
  • *****
  • Posts: 180
  • FF6 hacker
    • View Profile
Re: GBA raw graphics function
« Reply #2 on: August 31, 2014, 03:24:15 pm »
Yes! That did it! Thanks a lot KC!