Interesting to read a thread like this...
@Ñuño Martínez, sorry for not replying, I don't remember ever seeing your mail.
Anyway, I've tried get Tile Studio to compile in Lazarus many times over the years and always failed, until this time.
Now I got the tile editor working (almost) completely. The map editor however doesn't do much. There are some really strange things going on with the Canvas.Draw function with transparent bitmaps.
It took me quite a while to figure out that the behavior of TabControl.TabIndex and Form.Resize are different from Delphi in whether events are fired or not. I don't know if such things are bugs or maybe intentional choices.
Anyway, the newest source code is available on github: https://github.com/Wiering/Tile-Studio/tree/lazarus, maybe some of you lazarus experts immediately see what the issues are, any help would be appreciated!
Use subdirectories! Please. "src" for sources and "bin" for the final binary. And "docs" for documentation. Really, it will help a lot.
The main problem I had when I worked with your code is that most of it is in the main form. That mean thousand controls and it's hard to keep track of they all. You should split the stuff, using TFrame for different control groups (i.e. I did a TFrame for the "brush" selector, another one for the tiles selector, etc). This will also group your code and increase the order.
Also separate "logic" and "GUI" should help too. For example, split the "tile selector" in two units, one for the logic (i.e. add, remove, replace, load, save...) and the other will be the GUI that just renders the tiles and allows the user to select one of them.
No signature provided yet.
I know the code isn't very well structured. The whole project started as a little tool for myself which I kept expanding.
Can you guys provide some more information about this so together we might try to figure this out.
Bitmap transparency doesn't seem to work properly in Lazarus (or very different from Delphi at least). At first I thought it didn't work at all (posted an issue here http://bugs.freepascal.org/view.php?id=31556 ), but now I see that it does work if you set up the transparent color first. However, once you draw the bitmap onto something else, it forgets its transparent color, but somehow does keep a mask of the transparency you had before. In Delphi, you can just set the transparent color at any time and it will draw the image accordingly. Here is a simple program that behaves differently in Lazarus and Delphi, see the result image below.
Code:procedure TForm1.BitBtn1Click(Sender: TObject); var bmp1: TBitmap; begin bmp1 := TBitmap.Create(); bmp1.Transparent := TRUE; bmp1.TransparentColor := clRed; bmp1.Width := 100; bmp1.Height := 100; with bmp1.Canvas do begin Brush.Color := clRed; FillRect(Rect(0, 0, 100, 100)); Brush.Color := clYellow; FillRect(Rect(20, 20, 80, 80)); end; Image1.Picture.Bitmap := TBitmap.Create(); Image1.Picture.Bitmap.Width := 100; Image1.Picture.Bitmap.Height := 100; with Image1.Picture.Bitmap.Canvas do begin Brush.Color := clGreen; FillRect (Rect(0, 0, 100, 100)); Draw(0, 0, bmp1); end; with bmp1.Canvas do begin Brush.Color := clBlue; FillRect(Rect(0, 0, 50, 50)); Brush.Color := clRed; FillRect (Rect(50, 50, 100, 100)); end; with Image1.Picture.Bitmap.Canvas do begin Brush.Color := clGreen; FillRect (Rect(0, 0, 100, 100)); Draw(0, 0, bmp1); end; end;
[Edit] I found a workaround by adding Bitmap.Mask() before every Draw, which is probably very inefficient, but now the maps are rendered correctly.
Last edited by Wiering; 18-03-2017 at 09:07 AM.
Two big red flags I noticed in the source: the "WriteBitmapToPNGFile" and "ReadBitmapFromPNGFile" procedures in Tiles.pas. They simply won't work as written, in Lazarus. No matter what extension you provide the write procedure, it will always be saved as an actual bitmap (open something you've saved like this in IrfanView if you want to confirm, it will show a message alerting you about the wrong extension.) The read procedure will just straight-up raise an exception that says "Wrong image format." Here's refactored versions of both that work as intended for me:
Also, in the main form, the end of the SaveCurrentTile1Click procedure needs to be changed from thisCode:procedure WriteBitmapToPNGFile (OutputFilename: string; Bitmap: TBitmap; TransparentColor: Integer); var APNG: TPortableNetworkGraphic; begin Bitmap.Transparent := True; Bitmap.TransparentColor := TransparentColor; APNG := TPortableNetworkGraphic.Create; APNG.Assign(Bitmap); Bitmap.Free; APNG.SaveToFile(OutputFilename); APNG.Free; end; procedure ReadBitmapFromPNGFile (InputFilename: string; Bitmap: TBitmap); var APNG: TPortableNetworkGraphic; begin APNG := TPortableNetworkGraphic.Create; APNG.LoadFromFile(InputFileName); Bitmap.Assign(APNG); APNG.Free; end;
to this:Code:if UpperCase(ExtractFileExt (SavePictureDialog.Filename)) = '.PNG' then begin bmpTemp.TransparentColor := TRANS_COLOR; WriteBitmapToPngFile (SavePictureDialog.Filename, bmpTemp, TRANS_COLOR); end else bmpTemp.SaveToFile (SavePictureDialog.FileName); bmpTemp.Free;
Otherwise it will raise an access violation whenever you attempt to save to PNG (since the WriteBitmapToPNGFile procedure also frees the Bitmap.)Code:if UpperCase(ExtractFileExt (SavePictureDialog.Filename)) = '.PNG' then begin bmpTemp.TransparentColor := TRANS_COLOR; WriteBitmapToPngFile (SavePictureDialog.Filename, bmpTemp, TRANS_COLOR); end else begin bmpTemp.SaveToFile (SavePictureDialog.FileName); bmpTemp.Free; end;
EDIT: Just noticed the WriteTileBitmap function called by TMainForm.Generate1Click has the same problem. The ending needs to be changed from this:
to this:Code:if Ext = '.PNG' then WriteBitmapToPngFile(Filename, TempBitmap, TransColor) else TempBitmap.SaveToFile(FileName); end; TempBitmap.Free; WriteTileBitmap := True;
Code:if Ext = '.PNG' then WriteBitmapToPngFile(Filename, TempBitmap, TransColor) else begin TempBitmap.SaveToFile(FileName); TempBitmap.Free; end; end; WriteTileBitmap := True;
Last edited by Akira13; 20-03-2017 at 12:32 AM.
Thanks, I hadn't noticed that it was actually saving BMP files instead of PNG. I removed Bitmap.Free to make it compatible with the rest of the code.
I finally got the code generation to work now, at least for a simple project. It does still need to be tested a lot more to make sure all commands work properly.
Nice to read this.
My game engine has its own simple and limited map editor but once TileStudio is up and running I'll work on one or two tsd files if possible (I'm using IFF file format and need to calculate sizes in bytes).
Last edited by Ñuño Martínez; 23-03-2017 at 09:40 AM.
No signature provided yet.