@@ -25,6 +25,8 @@ public partial class MainWindow : Window
2525 private FF4Rom _rom ;
2626 private string _filename ;
2727
28+ private ushort [ ] [ ] _tilesetBytes ;
29+
2830 public MainWindow ( )
2931 {
3032 InitializeComponent ( ) ;
@@ -77,27 +79,28 @@ private void LoadOverworldTileset()
7779
7880 var tileCount = FF4Rom . MapTileCount ;
7981 var tiles = new ImageDrawing [ tileCount ] ;
82+ _tilesetBytes = new ushort [ tileCount ] [ ] ;
8083 for ( int i = 0 ; i < tileCount ; i ++ )
8184 {
82- var tileBytes = new ushort [ 16 * 16 ] ;
85+ _tilesetBytes [ i ] = new ushort [ 16 * 16 ] ;
8386 var subTileIndex = formations [ i ] ;
84- CopySubTile ( subTiles , 32 * subTileIndex , tileBytes , 0 , palette , offsets [ subTileIndex ] ) ;
87+ CopySubTileToTile ( subTiles , 32 * subTileIndex , _tilesetBytes [ i ] , 0 , palette , offsets [ subTileIndex ] ) ;
8588 subTileIndex = formations [ i + tileCount ] ;
86- CopySubTile ( subTiles , 32 * subTileIndex , tileBytes , 8 , palette , offsets [ subTileIndex ] ) ;
89+ CopySubTileToTile ( subTiles , 32 * subTileIndex , _tilesetBytes [ i ] , 8 , palette , offsets [ subTileIndex ] ) ;
8790 subTileIndex = formations [ i + 2 * tileCount ] ;
88- CopySubTile ( subTiles , 32 * subTileIndex , tileBytes , 8 * 16 , palette , offsets [ subTileIndex ] ) ;
91+ CopySubTileToTile ( subTiles , 32 * subTileIndex , _tilesetBytes [ i ] , 8 * 16 , palette , offsets [ subTileIndex ] ) ;
8992 subTileIndex = formations [ i + 3 * tileCount ] ;
90- CopySubTile ( subTiles , 32 * subTileIndex , tileBytes , 8 * 16 + 8 , palette , offsets [ subTileIndex ] ) ;
93+ CopySubTileToTile ( subTiles , 32 * subTileIndex , _tilesetBytes [ i ] , 8 * 16 + 8 , palette , offsets [ subTileIndex ] ) ;
9194
9295 tileGroup . Children . Add ( new ImageDrawing (
93- BitmapSource . Create ( 16 , 16 , 72 , 72 , PixelFormats . Bgr555 , null , tileBytes , 16 * 2 ) ,
96+ BitmapSource . Create ( 16 , 16 , 72 , 72 , PixelFormats . Bgr555 , null , _tilesetBytes [ i ] , 16 * 2 ) ,
9497 new Rect ( new Point ( 16 * ( i % 16 ) , 16 * ( i / 16 ) ) , new Size ( 16 , 16 ) ) ) ) ;
9598 }
9699
97100 Tileset . Source = new DrawingImage ( tileGroup ) ;
98101 }
99102
100- private void CopySubTile ( byte [ ] subTiles , int subTilesOffset , ushort [ ] tile , int tileOffset , ushort [ ] palette , int paletteOffset )
103+ private void CopySubTileToTile ( byte [ ] subTiles , int subTilesOffset , ushort [ ] tile , int tileOffset , ushort [ ] palette , int paletteOffset )
101104 {
102105 for ( int y = 0 ; y < 8 ; y ++ )
103106 {
@@ -127,28 +130,37 @@ private void FixPalette(ushort[] palette)
127130
128131 private void LoadOverworldTiles ( )
129132 {
130- var tiles = ( ( DrawingGroup ) ( ( DrawingImage ) Tileset . Source ) . Drawing ) . Children ;
131-
132133 var rowGroup = new DrawingGroup ( ) ;
133134 rowGroup . Open ( ) ;
134135
135136 var rows = _rom . GetOverworldRows ( ) ;
137+ var rowLength = FF4Rom . OverworldRowLength ;
136138 for ( int y = 0 ; y < FF4Rom . OverworldRowCount ; y ++ )
137139 {
138- var tileGroup = new DrawingGroup { Transform = new TranslateTransform ( 0 , 16 * y ) } ;
139- tileGroup . Open ( ) ;
140- for ( int x = 0 ; x < FF4Rom . OverworldRowLength ; x ++ )
140+ var rowBytes = new ushort [ 16 * 16 * rowLength ] ;
141+ for ( int x = 0 ; x < rowLength ; x ++ )
141142 {
142- tileGroup . Children . Add ( new ImageDrawing (
143- ( ( ImageDrawing ) tiles [ rows [ FF4Rom . OverworldRowLength * y + x ] ] ) . ImageSource ,
144- new Rect ( new Point ( 16 * x , 0 ) , new Size ( 16 , 16 ) ) ) ) ;
143+ CopyTileToRow ( _tilesetBytes [ rows [ y * rowLength + x ] ] , rowBytes , 16 * x ) ;
145144 }
146145
147- rowGroup . Children . Add ( tileGroup ) ;
146+ rowGroup . Children . Add ( new ImageDrawing (
147+ BitmapSource . Create ( 16 * rowLength , 16 , 72 , 72 , PixelFormats . Bgr555 , null , rowBytes , 16 * rowLength * 2 ) ,
148+ new Rect ( new Point ( 0 , 16 * y ) , new Size ( 16 * rowLength , 16 ) ) ) ) ;
148149 }
149150
150151 Map . Source = new DrawingImage ( rowGroup ) ;
151152 Map . Stretch = Stretch . None ;
152153 }
154+
155+ private void CopyTileToRow ( ushort [ ] tile , ushort [ ] row , int rowOffset )
156+ {
157+ for ( int y = 0 ; y < 16 ; y ++ )
158+ {
159+ for ( int x = 0 ; x < 16 ; x ++ )
160+ {
161+ row [ rowOffset + 16 * y * FF4Rom . OverworldRowLength + x ] = tile [ 16 * y + x ] ;
162+ }
163+ }
164+ }
153165 }
154166}
0 commit comments