@@ -26,10 +26,11 @@ public partial class MainWindow : Window
2626 private string _filename ;
2727
2828 private ushort [ ] [ ] _tilesetBytes ;
29- private byte [ ] _rows ;
29+ private byte [ ] _map ;
3030
31- private int _selectedTile ;
31+ private int _selectedTile = - 1 ;
3232 private GeometryDrawing _selectedTileDrawing = new GeometryDrawing ( ) ;
33+ private WriteableBitmap [ ] _rowBitmaps ;
3334
3435 public MainWindow ( )
3536 {
@@ -62,21 +63,20 @@ private void OpenButton_Click(object sender, RoutedEventArgs e)
6263
6364 private void Tileset_MouseLeftButtonUp ( object sender , MouseButtonEventArgs e )
6465 {
65- var position = e . GetPosition ( ( IInputElement ) sender ) ;
66- int x = ( int ) position . X ;
67- int y = ( int ) position . Y ;
68- if ( x < 0 || x > 256 || y < 0 || y > 128 )
66+ int x , y ;
67+ GetClickedTile ( sender , e , out x , out y ) ;
68+ if ( x < 0 || x >= 16 || y < 0 || y >= 8 )
6969 {
7070 return ;
7171 }
7272
73- _selectedTile = x / 16 + 16 * ( y / 16 ) ;
73+ _selectedTile = 16 * y + x ;
7474
7575 var tileGroup = ( DrawingGroup ) ( ( DrawingImage ) Tileset . Source ) . Drawing ;
7676 tileGroup . Children . Remove ( _selectedTileDrawing ) ;
7777
7878 var geometry = new GeometryGroup ( ) ;
79- geometry . Children . Add ( new RectangleGeometry ( new Rect ( new Point ( 16 * ( x / 16 ) , 16 * ( y / 16 ) ) , new Size ( 16 , 16 ) ) ) ) ;
79+ geometry . Children . Add ( new RectangleGeometry ( new Rect ( new Point ( 16 * x , 16 * y ) , new Size ( 16 , 16 ) ) ) ) ;
8080 _selectedTileDrawing = new GeometryDrawing
8181 {
8282 Geometry = geometry ,
@@ -87,6 +87,36 @@ private void Tileset_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
8787 tileGroup . Children . Add ( _selectedTileDrawing ) ;
8888 }
8989
90+ private void Map_MouseLeftButtonUp ( object sender , MouseButtonEventArgs e )
91+ {
92+ if ( _selectedTile == - 1 )
93+ {
94+ return ;
95+ }
96+
97+ int x , y ;
98+ GetClickedTile ( sender , e , out x , out y ) ;
99+ if ( x < 0 || x >= 256 || y < 0 || y >= 256 )
100+ {
101+ return ;
102+ }
103+
104+ _map [ 256 * y + x ] = ( byte ) _selectedTile ;
105+
106+ _rowBitmaps [ y ] . Lock ( ) ;
107+ _rowBitmaps [ y ] . WritePixels ( new Int32Rect ( 16 * x , 0 , 16 , 16 ) , _tilesetBytes [ _selectedTile ] , 16 * 2 , 0 ) ;
108+ _rowBitmaps [ y ] . Unlock ( ) ;
109+ }
110+
111+ private void GetClickedTile ( object sender , MouseButtonEventArgs e , out int x , out int y )
112+ {
113+ var position = e . GetPosition ( ( IInputElement ) sender ) ;
114+ x = ( int ) position . X ;
115+ y = ( int ) position . Y ;
116+ x /= 16 ;
117+ y /= 16 ;
118+ }
119+
90120 private void LoadOverworld ( )
91121 {
92122 LoadOverworldTileset ( ) ;
@@ -159,34 +189,27 @@ private void LoadOverworldTiles()
159189 var rowGroup = new DrawingGroup ( ) ;
160190 rowGroup . Open ( ) ;
161191
162- _rows = _rom . GetOverworldRows ( ) ;
192+ _map = _rom . GetOverworldMap ( ) ;
163193 var rowLength = FF4Rom . OverworldRowLength ;
194+ _rowBitmaps = new WriteableBitmap [ FF4Rom . OverworldRowCount ] ;
164195 for ( int y = 0 ; y < FF4Rom . OverworldRowCount ; y ++ )
165196 {
166- var rowBytes = new ushort [ 16 * 16 * rowLength ] ;
197+ _rowBitmaps [ y ] = new WriteableBitmap ( 16 * 256 , 16 , 72 , 72 , PixelFormats . Bgr555 , null ) ;
198+ _rowBitmaps [ y ] . Lock ( ) ;
167199 for ( int x = 0 ; x < rowLength ; x ++ )
168200 {
169- CopyTileToRow ( _tilesetBytes [ _rows [ y * rowLength + x ] ] , rowBytes , 16 * x ) ;
201+ var tile = _map [ y * rowLength + x ] ;
202+ _rowBitmaps [ y ] . WritePixels ( new Int32Rect ( 16 * x , 0 , 16 , 16 ) , _tilesetBytes [ tile ] , 16 * 2 , 0 ) ;
170203 }
171204
172- rowGroup . Children . Add ( new ImageDrawing (
173- BitmapSource . Create ( 16 * rowLength , 16 , 72 , 72 , PixelFormats . Bgr555 , null , rowBytes , 16 * rowLength * 2 ) ,
205+ _rowBitmaps [ y ] . Unlock ( ) ;
206+
207+ rowGroup . Children . Add ( new ImageDrawing ( _rowBitmaps [ y ] ,
174208 new Rect ( new Point ( 0 , 16 * y ) , new Size ( 16 * rowLength , 16 ) ) ) ) ;
175209 }
176210
177211 Map . Source = new DrawingImage ( rowGroup ) ;
178212 Map . Stretch = Stretch . None ;
179213 }
180-
181- private void CopyTileToRow ( ushort [ ] tile , ushort [ ] row , int rowOffset )
182- {
183- for ( int y = 0 ; y < 16 ; y ++ )
184- {
185- for ( int x = 0 ; x < 16 ; x ++ )
186- {
187- row [ rowOffset + 16 * y * FF4Rom . OverworldRowLength + x ] = tile [ 16 * y + x ] ;
188- }
189- }
190- }
191214 }
192215}
0 commit comments