• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using a spritesheet

 
Greenhorn
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Dan Moody
Greenhorn
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic