public static void save_CHR(BinaryWriter _bw, CHR_data _chr_data)
        {
            int j;
            int x;
            int y;
            int val;

            ushort data;

            for (j = 0; j < 4; j++)
            {
                for (y = 0; y < utils.CONST_CHR_SIDE_PIXELS_CNT; y++)
                {
                    data = 0;

                    for (x = 0; x < utils.CONST_CHR_SIDE_PIXELS_CNT; x++)
                    {
                        val = _chr_data.get_data()[(y << utils.CONST_CHR_SIDE_PIXELS_CNT_POW_BITS) + ((utils.CONST_CHR_SIDE_PIXELS_CNT - 1) - x)];

                        data |= ( ushort )(((val >> j) & 0x01) << x);
                    }

                    _bw.Write(data);
                }
            }
        }
Exemplo n.º 2
0
        public static void update_bitmap8x16(CHR_data _chr_data1, CHR_data _chr_data2, Bitmap _bmp, CHR_data_attr _attr, bool _alpha, int _plt_ind, palette_small[] _plt_arr)
        {
            BitmapData bmp_data = _bmp.LockBits(new Rectangle(0, 0, _bmp.Width, _bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);

            if (bmp_data != null)
            {
                fill_CHR_bmp_data(bmp_data.Scan0, 0, _chr_data1, _attr, _alpha, _plt_ind, _plt_arr);

                if (_chr_data2 != null)
                {
                    fill_CHR_bmp_data(bmp_data.Scan0, (8 * bmp_data.Stride), _chr_data2, _attr, _alpha, _plt_ind, _plt_arr);

                    _bmp.UnlockBits(bmp_data);
                }
                else
                {
                    _bmp.UnlockBits(bmp_data);

                    Graphics gfx = Graphics.FromImage(_bmp);

                    utils.brush.Color = Color.Red;

                    gfx.FillRectangle(utils.brush, 0, 8, 8, 8);
                    gfx.Flush();
                }

                flip_bmp(_bmp, (_attr != null ? _attr.flip_flag:0));
            }
        }
Exemplo n.º 3
0
        private void update()
        {
            clear_background(CONST_BACKGROUND_COLOR);

            // sprite data drawing
            if (m_spr_data != null)
            {
                disable(false);

                draw_chars();

                if (m_selected_CHR >= 0 && m_mode == EMode.m_build)
                {
                    // draw a frame of the selected CHR
                    CHR_data_attr chr_attr = m_spr_data.get_CHR_attr()[m_selected_CHR];
                    CHR_data      chr_data = m_spr_data.get_CHR_data().get_data()[chr_attr.CHR_ind];

                    float chr_scr_pos_x = transform_to_scr_pos(chr_attr.x + m_offset_x + m_spr_data.offset_x, m_scr_half_width);
                    float chr_scr_pos_y = transform_to_scr_pos(chr_attr.y + m_offset_y + m_spr_data.offset_y, m_scr_half_height);

                    float CHR_height = get_CHR_height();

                    m_pen.Color = Color.Red;
                    m_gfx.DrawRectangle(m_pen, chr_scr_pos_x, chr_scr_pos_y, m_CHR_size, CHR_height);

                    m_pen.Color = Color.White;
                    m_gfx.DrawRectangle(m_pen, chr_scr_pos_x - 1, chr_scr_pos_y - 1, m_CHR_size + 2, CHR_height + 2);

#if DEF_NES
                    m_label.Text = "Pos: " + chr_attr.x + ";" + chr_attr.y + " / Palette: " + (chr_attr.palette_ind + 1) + " / Id: " + chr_attr.CHR_ind + " / Tiles: " + m_spr_data.get_CHR_attr().Count;
#elif DEF_SMS || DEF_PCE
                    m_label.Text = "Pos: " + chr_attr.x + ";" + chr_attr.y + " / Id: " + chr_attr.CHR_ind + " / Tiles: " + m_spr_data.get_CHR_attr().Count;
Exemplo n.º 4
0
        public static Bitmap create_bitmap8x16(CHR_data _chr_data1, CHR_data _chr_data2, CHR_data_attr _attr, bool _alpha, int _plt_ind, palette_small[] _plt_arr = null)
        {
            Bitmap bmp = new Bitmap(8, 16, PixelFormat.Format32bppPArgb);

            update_bitmap8x16(_chr_data1, _chr_data2, bmp, _attr, _alpha, _plt_ind, _plt_arr);

            return(bmp);
        }
Exemplo n.º 5
0
        public static Bitmap create_CHR_bitmap(CHR_data _chr_data, CHR_data_attr _attr, bool _alpha, int _plt_ind, palette_small[] _plt_arr = null)
        {
            Bitmap bmp = new Bitmap(CONST_CHR_SIDE_PIXELS_CNT, CONST_CHR_SIDE_PIXELS_CNT, PixelFormat.Format32bppPArgb);

            update_CHR_bitmap(_chr_data, bmp, _attr, _alpha, _plt_ind, _plt_arr);

            return(bmp);
        }
Exemplo n.º 6
0
        public CHR_data copy()
        {
            CHR_data copy = new CHR_data();

            Array.Copy(m_data, copy.m_data, utils.CONST_CHR_TOTAL_PIXELS_CNT);

            return(copy);
        }
Exemplo n.º 7
0
        private static void fill_CHR_bmp_data(IntPtr _data_ptr, int _data_offset, CHR_data _chr_data, CHR_data_attr _attr, bool _alpha, int _plt_ind, palette_small[] _plt_arr)
        {
            unsafe
            {
                int *img_buff = ( int * )(_data_ptr + _data_offset);

#if DEF_NES
                bool  apply_palette = (_plt_arr != null && _plt_ind >= 0);
                int[] clr_inds      = apply_palette ? _plt_arr[_plt_ind].get_color_inds():null;
                int   alpha;
#endif
                int clr = 0;
                int pix_ind;

                for (int p = 0; p < CONST_CHR_TOTAL_PIXELS_CNT; p++)
                {
                    pix_ind = _chr_data.get_data()[p];

#if DEF_NES
                    if (apply_palette)
#endif
                    {
#if DEF_NES
                        clr = palette_group.Instance.main_palette[clr_inds[pix_ind]];
#elif DEF_SMS
                        clr = palette_group.Instance.main_palette[palette_group.Instance.get_palettes_arr()[pix_ind / CONST_NUM_SMALL_PALETTES].get_color_inds()[pix_ind % CONST_NUM_SMALL_PALETTES]];
#endif

#if DEF_FIXED_LEN_PALETTE16_ARR
                        if (_attr != null)
                        {
                            clr = palette_group.Instance.main_palette[palettes_array.Instance.get_color((_attr.palette_ind < 0 ? 0:_attr.palette_ind), pix_ind)];
                        }
                        else
                        {
                            clr = palette_group.Instance.main_palette[palette_group.Instance.get_palettes_arr()[pix_ind / CONST_NUM_SMALL_PALETTES].get_color_inds()[pix_ind % CONST_NUM_SMALL_PALETTES]];
                        }
#endif
                        if ((pix_ind != 0 && _alpha == true) || _alpha == false)
                        {
                            clr |= 0xFF << 24;
                        }
                    }
#if DEF_NES
                    else
                    {
                        alpha = (pix_ind == 0) ? (_alpha ? 0x00:0xFF):0xFF;

                        pix_ind <<= 6;
                        clr       = alpha << 24 | pix_ind << 16 | pix_ind << 8 | pix_ind;
                    }
#endif
                    img_buff[p] = clr;
                }
            }
        }
Exemplo n.º 8
0
        public bool equals(CHR_data _obj)
        {
            for (int i = 0; i < utils.CONST_CHR_TOTAL_PIXELS_CNT; i++)
            {
                if (get_data()[i] != _obj.get_data()[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 9
0
        public bool swap_CHRs(int _ind1, int _ind2)
        {
            if (_ind1 >= 0 && _ind1 < get_data().Count&& _ind2 >= 0 && _ind2 < get_data().Count)
            {
                CHR_data data = get_data()[_ind1];
                get_data()[_ind1] = get_data()[_ind2];
                get_data()[_ind2] = data;

                return(true);
            }

            return(false);
        }
Exemplo n.º 10
0
        public static void update_CHR_bitmap(CHR_data _chr_data, Bitmap _bmp, CHR_data_attr _attr, bool _alpha, int _plt_ind, palette_small[] _plt_arr)
        {
            BitmapData bmp_data = _bmp.LockBits(new Rectangle(0, 0, _bmp.Width, _bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);

            if (bmp_data != null)
            {
                fill_CHR_bmp_data(bmp_data.Scan0, 0, _chr_data, _attr, _alpha, _plt_ind, _plt_arr);

                _bmp.UnlockBits(bmp_data);

                flip_bmp(_bmp, (_attr != null ? _attr.flip_flag:0));
            }
        }
Exemplo n.º 11
0
        private void remap_color_inds(CHR_data _chr_data, int _start_clr_ind, byte[] _remap_arr)
        {
            int  j;
            byte clr_ind;

            for (j = 0; j < utils.CONST_CHR_TOTAL_PIXELS_CNT; j++)
            {
                clr_ind = _chr_data.get_data()[j];

                clr_ind = _remap_arr[_start_clr_ind + clr_ind];

                _chr_data.get_data()[j] = clr_ind;
            }
        }
Exemplo n.º 12
0
        public void import(BinaryReader _br)
        {
            byte[] pix_arr = new byte[utils.CONST_CHR_SIDE_PIXELS_CNT];
            byte[] tmp_arr = new byte[utils.CONST_CHR_NATIVE_SIZE_IN_BYTES];

            int i;
            int j;

            byte[] data_arr = new byte[utils.CONST_CHR_NATIVE_SIZE_IN_BYTES >> 3];

#if DEF_SMS || DEF_PCE
            int ind_offset;
#endif
            int shift_dec1_cnt;

            CHR_data chr_data;

            if (_br.BaseStream.Length < utils.CONST_CHR_NATIVE_SIZE_IN_BYTES)
            {
                _br.BaseStream.Position = _br.BaseStream.Length;

                return;
            }

            do
            {
                chr_data = new CHR_data();

                tmp_arr = _br.ReadBytes(utils.CONST_CHR_NATIVE_SIZE_IN_BYTES);

                for (i = 0; i < utils.CONST_CHR_SIDE_PIXELS_CNT; i++)
                {
#if DEF_NES
                    data_arr[0] = tmp_arr[i];
                    data_arr[1] = tmp_arr[i + utils.CONST_CHR_SIDE_PIXELS_CNT];
#elif DEF_SMS
                    ind_offset = i << 2;

                    data_arr[0] = tmp_arr[ind_offset];
                    data_arr[1] = tmp_arr[ind_offset + 1];
                    data_arr[2] = tmp_arr[ind_offset + 2];
                    data_arr[3] = tmp_arr[ind_offset + 3];
#elif DEF_PCE
                    ind_offset = i << 1;

                    data_arr[0] = tmp_arr[ind_offset];
                    data_arr[1] = tmp_arr[ind_offset + 32];
                    data_arr[2] = tmp_arr[ind_offset + 64];
                    data_arr[3] = tmp_arr[ind_offset + 96];

                    ++ind_offset;

                    data_arr[4] = tmp_arr[ind_offset];
                    data_arr[5] = tmp_arr[ind_offset + 32];
                    data_arr[6] = tmp_arr[ind_offset + 64];
                    data_arr[7] = tmp_arr[ind_offset + 96];
#else
                    ...
#endif
                    for (j = 0; j < utils.CONST_CHR_SIDE_PIXELS_CNT; j++)
                    {
#if DEF_NES || DEF_SMS
                        shift_dec1_cnt = utils.CONST_CHR_SIDE_PIXELS_CNT - 1 - j;
#endif
#if DEF_NES
                        pix_arr[j] = ( byte )(((data_arr[0] & (1 << shift_dec1_cnt)) >> shift_dec1_cnt) | (((data_arr[1] << 1) & (1 << (utils.CONST_CHR_SIDE_PIXELS_CNT - j))) >> shift_dec1_cnt));
#elif DEF_SMS
                        pix_arr[j] = ( byte )(((data_arr[0] >> shift_dec1_cnt & 0x01)) | (((data_arr[1] >> shift_dec1_cnt & 0x01)) << 1) | (((data_arr[2] >> shift_dec1_cnt & 0x01)) << 2) | (((data_arr[3] >> shift_dec1_cnt & 0x01)) << 3));
#elif DEF_PCE
                        if (j < 8)
                        {
                            shift_dec1_cnt = 7 - j;
                            pix_arr[j]     = ( byte )(((data_arr[4] >> shift_dec1_cnt & 0x01)) | (((data_arr[5] >> shift_dec1_cnt & 0x01)) << 1) | (((data_arr[6] >> shift_dec1_cnt & 0x01)) << 2) | (((data_arr[7] >> shift_dec1_cnt & 0x01)) << 3));
                        }
                        else
                        {
                            shift_dec1_cnt = 7 - (j - 8);
                            pix_arr[j]     = ( byte )(((data_arr[0] >> shift_dec1_cnt & 0x01)) | (((data_arr[1] >> shift_dec1_cnt & 0x01)) << 1) | (((data_arr[2] >> shift_dec1_cnt & 0x01)) << 2) | (((data_arr[3] >> shift_dec1_cnt & 0x01)) << 3));
                        }
Exemplo n.º 13
0
        private sprite_params cut_CHRs(sprite_params _spr_params, int _min_x, int _max_x, int _min_y, int _max_y, List <byte[]> _lines_arr, bool _alpha, int _alpha_ind, int _num_colors, bool _crop_image, int _palette_slot)
        {
            // cut sprite by tiles 8x8
            {
                _spr_params.m_offset_x = _min_x;
                _spr_params.m_offset_y = _min_y;

                int dx_incr = _max_x - _min_x + 1;
                int dy_incr = _max_y - _min_y + 1;

                _spr_params.m_size_x = ((dx_incr % utils.CONST_CHR_SIDE_PIXELS_CNT != 0) ? (((dx_incr) + utils.CONST_CHR_SIDE_PIXELS_CNT) & -utils.CONST_CHR_SIDE_PIXELS_CNT):dx_incr) >> utils.CONST_CHR_SIDE_PIXELS_CNT_POW_BITS;
                _spr_params.m_size_y = ((dy_incr % utils.CONST_CHR_SIDE_PIXELS_CNT != 0) ? (((dy_incr) + utils.CONST_CHR_SIDE_PIXELS_CNT) & -utils.CONST_CHR_SIDE_PIXELS_CNT):dy_incr) >> utils.CONST_CHR_SIDE_PIXELS_CNT_POW_BITS;

                int x;
                int y;

                int i;

                byte[] pixels_line = null;

                byte[] pixels_row = new byte[utils.CONST_CHR_SIDE_PIXELS_CNT];

                int n_lines = _lines_arr.Count;

                CHR_data      chr_data = null;
                CHR_data_attr chr_attr = null;

                int chr_pos_x = 0;
                int chr_pos_y = 0;

                byte pix_ind      = 0;
                int  num_row_pixs = 0;

                int pix_acc = 0;

                int col_n;
                int row_n;
                int x_offset = 0;

                for (y = 0; y < _spr_params.m_size_y; y++)
                {
                    x_offset = 0;

                    for (x = 0; x < _spr_params.m_size_x; x++)
                    {
                        chr_pos_x = x * utils.CONST_CHR_SIDE_PIXELS_CNT;
                        chr_pos_y = y * utils.CONST_CHR_SIDE_PIXELS_CNT;

                        pix_acc = 0;

                        chr_data = new CHR_data();
                        chr_attr = new CHR_data_attr(chr_pos_x, chr_pos_y);
#if DEF_FIXED_LEN_PALETTE16_ARR
                        chr_attr.palette_ind = _palette_slot;
#endif
                        chr_pos_x += _min_x;
                        chr_pos_y += _min_y;

                        // calc first non zero column
                        if (_alpha)
                        {
                            pix_acc = 0;

                            chr_pos_x += x_offset;

                            for (col_n = 0; col_n < utils.CONST_CHR_SIDE_PIXELS_CNT; col_n++)
                            {
                                for (row_n = 0; row_n < utils.CONST_CHR_SIDE_PIXELS_CNT; row_n++)
                                {
                                    pixels_line = (chr_pos_y + row_n < _lines_arr.Count) ? _lines_arr[chr_pos_y + row_n]:null;

                                    pix_ind = (chr_pos_x + col_n <= _max_x) ? (pixels_line != null ? pixels_line[chr_pos_x + col_n]:(_alpha ? (byte)_alpha_ind:(byte)0)):(_alpha ? (byte)_alpha_ind:(byte)0);

                                    if (_alpha && (_alpha_ind != 0))
                                    {
                                        pix_ind += ( byte )(_num_colors - _alpha_ind);
                                        pix_ind %= ( byte )_num_colors;
                                    }

                                    pix_acc += pix_ind;
                                }

                                if (pix_acc != 0)
                                {
                                    x_offset  += col_n;
                                    chr_pos_x += col_n;

                                    chr_attr.x += x_offset;

                                    break;
                                }
                            }

                            if (col_n == utils.CONST_CHR_SIDE_PIXELS_CNT)
                            {
                                continue;
                            }
                        }

                        for (i = 0; i < utils.CONST_CHR_SIDE_PIXELS_CNT; i++)
                        {
                            pixels_line = (chr_pos_y + i < _lines_arr.Count) ? _lines_arr[chr_pos_y + i]:null;

                            // copy line
                            num_row_pixs = 0;

                            do
                            {
                                pix_ind = (chr_pos_x + num_row_pixs <= _max_x) ? (pixels_line != null ? pixels_line[chr_pos_x + num_row_pixs]:(_alpha ? (byte)_alpha_ind:(byte)0)):(_alpha ? (byte)_alpha_ind:(byte)0);

                                // fix the pixel index so that the first one will be transparency index
                                if (_alpha && (_alpha_ind != 0))
                                {
                                    pix_ind += ( byte )(_num_colors - _alpha_ind);
                                    pix_ind %= ( byte )_num_colors;
                                }

                                pix_acc += pix_ind;

                                pixels_row[num_row_pixs] = pix_ind;
                            }while(++num_row_pixs < utils.CONST_CHR_SIDE_PIXELS_CNT);

                            chr_data.push_line(pixels_row);
                        }

                        // save non-zero tile
                        if (!_crop_image || (pix_acc != 0 || !_alpha))
                        {
                            chr_attr.CHR_ind = m_CHR_arr.Count;
                            _spr_params.m_CHR_attr.Add(chr_attr);

                            m_CHR_arr.Add(chr_data);

                            if (m_CHR_arr.Count >= utils.CONST_CHR_BANK_MAX_SPRITES_CNT)
                            {
                                break;
                            }
                        }
                    }

                    if (m_CHR_arr.Count >= utils.CONST_CHR_BANK_MAX_SPRITES_CNT)
                    {
                        MainForm.message_box("The CHR bank is full!", "Data Import", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                        break;
                    }
                }
            }

            // convert tiles into pixels
            _spr_params.m_size_x <<= utils.CONST_CHR_SIDE_PIXELS_CNT_POW_BITS;
            _spr_params.m_size_y <<= utils.CONST_CHR_SIDE_PIXELS_CNT_POW_BITS;

            return(_spr_params);
        }