Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support TMX relative path for Source texture #1303

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class TilesetLoader(private val map: TiledMap, private val mapURL: URL) {
val image = try {
val ext = mapURL.toExternalForm().substringBeforeLast("/") + "/"

val stream = URL(ext + imageName).openStream()
val stream = URL(ext + tilesetImageName).openStream()

var img = if (transparentcolor.isEmpty())
Image(stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.almasb.fxgl.entity.components.IDComponent
import com.almasb.fxgl.entity.level.LevelLoadingException
import com.almasb.fxgl.test.RunWithFX
import javafx.geometry.Point2D
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.paint.Color
import javafx.scene.shape.Polygon
Expand Down Expand Up @@ -361,6 +362,38 @@ class TMXLevelLoaderTest {
assertThat(tileset.transparentcolor, `is`(""))
}

@Test
fun `Load tmx level with relative image source path`() {
// Relative Path check
val level = TMXLevelLoader().load(javaClass.getResource("relative_image_source.tmx"), GameWorld())
val levelImageChecksum = imageChecksum((level.entities[0].viewComponent.children[0] as ImageView).image)

// Validate that the Checksum strategy is legitimate
val levelIdem = TMXLevelLoader().load(javaClass.getResource("relative_image_source.tmx"), GameWorld())
val levelIdemChecksum = imageChecksum((levelIdem.entities[0].viewComponent.children[0] as ImageView).image)
assertEquals(levelImageChecksum, levelIdemChecksum)

// Standard none-relative Path check (uses the "same" image - different files, but it's the same image)
val levelValidator = TMXLevelLoader().load(javaClass.getResource("relative_image_source_validator.tmx"), GameWorld())
val levelValidatorChecksum = imageChecksum((levelValidator.entities[0].viewComponent.children[0] as ImageView).image)
assertEquals(levelImageChecksum, levelValidatorChecksum)

// Relative Path file doesn't exist - The checksum need to fail
val levelDummyImage = TMXLevelLoader().load(javaClass.getResource("relative_image_source_missing.tmx"), GameWorld())
val levelDummyImageChecksum = imageChecksum((levelDummyImage.entities[0].viewComponent.children[0] as ImageView).image)
assertNotEquals(levelImageChecksum, levelDummyImageChecksum)
}

fun imageChecksum(image: Image): Int {
var sum = 0
for (x in 0 until image.getWidth().toInt()) {
for (y in 0 until image.getHeight().toInt()) {
sum += image.getPixelReader().getArgb(x, y)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how big argb values are, so perhaps Long is more suitable. Though I suppose identical values will overflow in the same way, so Int should work too.

}
}
return sum
}

class MyEntityFactory : EntityFactory {

@Spawns("no_type,type1")
Expand Down Expand Up @@ -415,4 +448,5 @@ class TMXLevelLoaderTest {
return Entity().also { it.type = data.get("type") }
}
}

}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.3" orientation="orthogonal" renderorder="right-down" width="40" height="30" tilewidth="20" tileheight="20" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="relative_image_source" tilewidth="20" tileheight="20" spacing="1" margin="1" tilecount="238" columns="17">
<image source="relative/image_source.png" trans="ffffff" width="358" height="295"/>
</tileset>
<layer id="1" name="Tile Layer 1" width="40" height="30">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,177,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,179,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,177,229,229,229,229,229,179,0,177,229,229,229,229,229,179,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,211,229,213,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,211,229,229,229,229,229,229,229,229,229,229,229,229,229,213,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,211,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,213,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
</map>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.3" orientation="orthogonal" renderorder="right-down" width="40" height="30" tilewidth="20" tileheight="20" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="relative_image_source_missing" tilewidth="20" tileheight="20" spacing="1" margin="1" tilecount="238" columns="17">
<image source="relative/missing.png" trans="ffffff" width="358" height="295"/>
</tileset>
<layer id="1" name="Tile Layer 1" width="40" height="30">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,177,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,179,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,177,229,229,229,229,229,179,0,177,229,229,229,229,229,179,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,211,229,213,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,211,229,229,229,229,229,229,229,229,229,229,229,229,229,213,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,211,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,213,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
</map>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.3" orientation="orthogonal" renderorder="right-down" width="40" height="30" tilewidth="20" tileheight="20" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="relative_image_source_missing" tilewidth="20" tileheight="20" spacing="1" margin="1" tilecount="238" columns="17">
<image source="image_source_validator.png" trans="ffffff" width="358" height="295"/>
</tileset>
<layer id="1" name="Tile Layer 1" width="40" height="30">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,177,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,179,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,177,229,229,229,229,229,179,0,177,229,229,229,229,229,179,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,211,229,213,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,211,229,229,229,229,229,229,229,229,229,229,229,229,229,213,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,228,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,211,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,229,213,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
</map>