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

Add utility for generic sprite sheet format. #350

Merged
merged 4 commits into from
Mar 20, 2022
Merged

Add utility for generic sprite sheet format. #350

merged 4 commits into from
Mar 20, 2022

Conversation

tsnee
Copy link
Contributor

@tsnee tsnee commented Mar 15, 2022

No description provided.

Copy link
Member

@davesmith00000 davesmith00000 left a comment

Choose a reason for hiding this comment

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

Great start. For a relatively small code block I'm afraid I have left many comments, but a good percentage of them are just style points. Thanks!

width: Int,
height: Int,
tileWidth: Int,
tileHeight: Int,
Copy link
Member

Choose a reason for hiding this comment

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

The convention is that the main constructor will use Size instead of separate width and height values. What we then do is has a companion with an apply method that gives additional (more loosely typed) construction options. So this would be (perhaps):

assetName: AssetName, imageSize: Size, tileSize: Size, margin: Int

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, move the current argument list to an apply method? OK.

y <- 0 until height by tileHeight + margin
x <- 0 until width by tileWidth + margin
rectangle = Rectangle(x, y, tileWidth, tileHeight)
} yield rectangle
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Of course, you may want the image height for validation, and the link above would then need to be multiplied out by the size.

The advantage of your approach is that you've pre-calculated the rectangles just one, at an instantiation cost. If someone made one of these on every frame to look up a single index, they're paying a lot for little benefit. If they make one once, build a bunch of graphics ahead of time and store them and throw this instance away, then the cost is totally worth it. However, the maths calculation is cheap, so unless I've missed something (I've yet to have my first coffee of the day so... likely!) then you might as well do the math?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess this was a case of premature optimization. In my own code, I instantiate the class once, but call its apply method ~240,000 times per second, so it seemed like a no-brainer.

* and increases as it goes right and down
* @return an optional Graphic, None if the given index was out of bounds.
*/
def apply(idx: Int): Option[Graphic[Material.Bitmap]] =
Copy link
Member

Choose a reason for hiding this comment

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

I think this apply method is a good idea - I wouldn't have thought of doing it this way - but it's good. Might be nice to offer another version more explicitly named like fromIndex (helps people find what can be done via their IDE), and have this apply method delegate to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

@davesmith00000 davesmith00000 requested a review from a team March 16, 2022 08:51
@davesmith00000
Copy link
Member

Follow up thought: The point of this is convenient use of tilesets so that people can focus on their game building. What other things would be helpful here that we can do for them?

val maxRows: Int = ???
val maxColumns: Int = ???
val maxIndex: Int = ???
val length: Int = maxIndex

def fromCoords/Point(pt: Point): Option[Graphic[Material.Bitmap]] = ???

Anything else you can think of?

On that last one, lots of excellent reasons to need to look up by index, but people are bad at indexes, and they might also like to just be able to look up by coordinates now and again.

tsnee added 4 commits March 18, 2022 02:57
NB: Changing the class name from GenericTileset to Tileset breaks the
build on my Mac laptop. It seems to be caused by a naming conflict between
Tileset and TiledMap's TileSet (note the capital S). Perhaps there is
something peculiar to my environment that makes the build case-insensitive?
Copy link
Member

@davesmith00000 davesmith00000 left a comment

Choose a reason for hiding this comment

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

LGTM

@davesmith00000 davesmith00000 merged commit e4a55b6 into PurpleKingdomGames:master Mar 20, 2022
@sylvansson sylvansson mentioned this pull request May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants