Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (789)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to load a lot of images or spritesheets efficiently?  (Read 6015 times)
0 Members and 1 Guest are viewing this topic.
Offline vspin

Junior Newbie

« Posted 2012-07-02 03:21:47 »

I am recreating an old game that was created in the late 90's (I've received permission to use the game images). The game has about 6000 100x100 images, which are mostly character frames for animation. The game (C++) was able to load the images and keep memory usage for the entire game around 50,000K. I know that the original game stored all the bitmaps in a special MultiBitMap format.

I am willing to change the images' format to any format. I just want to get memory usage down significantly. I tried creating 15 (2048x2048, with transparency)  PNG spritesheets, and then loading them using the Slick 2D game library, however, memory usage went through the roof. I thought about making smaller spritesheets, and loading just the sheets I need for the scene, however, it's possible the a single scene would need all the spritesheets loaded to render the scene. I am even considering the DDS file format..

Any suggestions?

Thank you
Offline Riven

« JGO Overlord »

Medals: 1357
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #1 - Posted 2012-07-02 03:38:04 »

My understanding is that Slick2D creates multiple copies of the image data. If you're only using Slick2D for the texture loading, you might want to write your own texture loading code, that doesn't make any copies and doesn't keep it all in RAM, after it has been sent to the GPU.

BTW: 6000 x 100x100 PNG = 6000 x 100x100x4 = 240000000 bytes (228MB)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline vspin

Junior Newbie

« Reply #2 - Posted 2012-07-02 05:20:15 »

I'll give that a try. Thank you for your help.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline vspin

Junior Newbie

« Reply #3 - Posted 2012-07-02 13:21:19 »

I found a great solution for myself!

I just used java.awt.Image ( and created a class to store every tile information (6000 of them) with a cache expiration time property, Image property, etc. When a file needs to be drawn, it checks if it's loaded, if not, it loads it. Then it updates the cache expire time plus one second, and finally draws. At the end of every scene, I expire unused tiles. My memory use is at 40,000K and my CPU is VERY low too. This works great for me, but I bet it wouldn't work in a lot of other situations.
Offline delt0r

JGO Wizard

Medals: 145
Exp: 18 years

Computers can do that?

« Reply #4 - Posted 2012-07-02 14:20:08 »

There are some very efficient loaders in TWL and exist as an independent package if you need it. They load png and jpg directly into a byte buffer.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline vspin

Junior Newbie

« Reply #5 - Posted 2012-07-02 21:00:36 »

Thank you. I'm going to look into it right now.
Offline davedes
« Reply #6 - Posted 2012-07-03 03:06:31 »

ImageIO is an OK solution if you just want something quick and easy. For better efficiency, here is what I would recommend:

  • Determine a way to only store what sprites are necessary in memory. This might involve extending Image with custom timers and an "expiration time." Or this may be "zone-based" (i.e. if the player exits the snow level, you can destroy the snow tiles..).
  • Allocate some empty textures -- how many depends on how much memory you want to allot to texture data, and how many sprites will be needed in memory at once. (Sounds effects also tend to take up a lot of memory.)
  • When the textures are needed (e.g. user enters snow level) then decode the sprite using PNGDecoder (or another of Matthias' decoders) and upload the byte buffer to one of your textures using glTexSubImage2D or a utility like this (note: if you use my PixelData utility, you'll want to add your own methods to deal more directly with the byte buffer).
  • Ideally threading would be used to load data in the background, as to not hog the CPU
This technique has the downside of being rather complicated. Truth is, your game probably doesn't require a very complex system like this. Most games that require thousands of sprites do not require them all to be rendered at the same time, and so it would be better to design a simple loading/unloading system based on levels or zones. You can use Image() to load the image, and destroy() to unload the image. Or you can just stick to ImageIO, which seems to be working for your needs. Smiley

Offline vspin

Junior Newbie

« Reply #7 - Posted 2012-07-03 04:56:28 »

Wonderful suggestions. Smiley

My game has no levels. Nearly all my sprites are apart of character animations. I removed a signification amount of sprites, which I didn't really need. I went from about 6000 to 3600. Everything is rendering smoothly.

Pages: [1]
  ignore  |  Print  

hadezbladez (2595 views)
2018-11-16 13:46:03

hadezbladez (928 views)
2018-11-16 13:41:33

hadezbladez (2549 views)
2018-11-16 13:35:35

hadezbladez (499 views)
2018-11-16 13:32:03

EgonOlsen (3675 views)
2018-06-10 19:43:48

EgonOlsen (4028 views)
2018-06-10 19:43:44

EgonOlsen (2439 views)
2018-06-10 19:43:20

DesertCoockie (3246 views)
2018-05-13 18:23:11

nelsongames (3292 views)
2018-04-24 18:15:36

nelsongames (4262 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!