This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Game Development and the fly likes Using a spritesheet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Game Development
Bookmark "Using a spritesheet" Watch "Using a spritesheet" New topic
Author

Using a spritesheet

Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

Hello,

I've gotten to know Games Programming relatively well (thanks to the guys here most of all!) and I have a regular BufferStrategy that I use and it's really cool. But now I want to get into some really hard stuff (well, I say hard, it is for me at least).

I've learnt that very old games (for like, Sega Mega Drive etc etc) used like a Sprite Sheet where it would consist of a 32x32 square grid. The sprites would be loaded from the appropriate tile. How would this be achieved in Java?

I'm guessing the theory is something like:

SpriteSheet has tiles.
SpriteSheet width = tile * 32.
SpriteSheet height = tile * 32.

(I'm now derping at figuring out how loading each individual tile would work).

I'm confident there is someone here who would know how to do this. You guys have certainly been awesome enough in teaching me and getting me this far so I'll say thanks now.

Thanks.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

Dan, I'm afraid that I don't have an answer for you, but I'd like to add to your question.

What's the advantage of using a sprite sheet over a bunch of individual sprite images?


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

Riaan Nel wrote:Dan, I'm afraid that I don't have an answer for you, but I'd like to add to your question.

What's the advantage of using a sprite sheet over a bunch of individual sprite images?


Well for one, using just one image would dramatically mean that I would have to use less memory, if you think about it, the amount of KB (or MB if we're going High Quality Images) stacks up. While a runnable jar is normally no more than about 5KB for me, using just one image would use less amount of memory.

Also, the sprites would be consitently the same size, I could also use a custom made font that I could draw inside the sprite sheet.

And, I've found that the main way this is done uses Bitwise Operation when defining the tiles etc, which means the amount of memory it uses there is tiny.

I don't want to use loads of images, I'd rather just load one image and then load like, sprites, off of it.

But thank you for the reply
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

There should be absolutely no advantage to this approach, other than lower access times on disk, which is a silly optimization anyway, because you probably aren't even loading *that* many files at a time. As a matter of fact, using sprite sheets may raise the amount of memory used, because you have to load bigger parts of the image at once to be able to extract data for a particular sprite, or you have to write awkward program logic to construct the sprites without loading the entire image into memory, as you're finding out right now.

Have you actually performed tests to determine that A) using the sheet approach improves performance and B) this improvement is justified?
Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

Stephan van Hulst wrote:There should be absolutely no advantage to this approach, other than lower access times on disk, which is a silly optimization anyway, because you probably aren't even loading *that* many files at a time. As a matter of fact, using sprite sheets may raise the amount of memory used, because you have to load bigger parts of the image at once to be able to extract data for a particular sprite, or you have to write awkward program logic to construct the sprites without loading the entire image into memory, as you're finding out right now.

Have you actually performed tests to determine that A) using the sheet approach improves performance and B) this improvement is justified?


I only ever read that doing this sort of thing can use more memory if you're not careful...that was what I found

and I have not been able to get a running example (yes, you're right, the logic needed to actually do this is ridiculous) but I have seen it working before.

It would just make things easier in my head, I would only ever have to load one image as a singular resource, I could just draw on another tile and then write some code to implement that part. Plus, I could possibly make my own fonts etc (and given a lot of the games I try to make are kinda low res anyway, this would be really cool).

I understand its a very odd, outdated and logically bad way to do this, but I'm just looking to expand knowledge with loading images.

Here's the BufferStrategy I use (this sets each pixel).



From there, I can add a frame limiter and stuff, but the main objective is to learn how to load images this way (I have actually already practiced with separate images but to no luck unfortunately).
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

What's difficult about maintaining different BufferedImages for each sprite? You already have to load at least one file, extending the logic with a loop to load multiple for different sprites seems like a trivial job.

If anything, using a sheet is going to make things harder.

If you want to go on with this, I recommend creating different BufferedImages, and cutting the individual sprites from the sheet and storing them in memory separately. So the hardest part would be to locate the individual sprites in the sheet. You need to devise some sort of scheme in what locations you're going to store the sprites. You already mentioned using 32*32 pixel tiles, so this would be pretty easy. Just load the image and then copy individual tiles to their new BufferedImage. Take a look at what methods are provided in BufferedImage and Graphics.
Bobby John
Greenhorn

Joined: Feb 10, 2012
Posts: 2
I tend to use spritesheets, purely for the fact that all my images are in one place. Im interested to know how it affects performance though.

I do it by creating a 256x256 image in Paint.NET and then use the GridMaker plugin to create the cells, usually 16x16, but you could also do 32x32.
Then I create a class called SpriteSheet and use it to load the image into a BufferedImage. I create a class called Sprite which also holds a BufferedImage. Using .getSubImage(), you can define the x and y of the individual sprite and the width and height.



As the cells are all the same size, you can create an array of sprites for a row on the sprite sheet using a for loop, multiplying the value by the sprite width each time it loops. The array can be useful for holding the frames for an animation.

I don't think it would improve performance if you only had a small number of images to load. But if you had say 1000 images that needed to be loaded, I think it would be faster to load a few spritesheets than lots of smaller images.
 
GeeCON Prague 2014
 
subject: Using a spritesheet