Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (711)
Games in Android Showcase (213)
games submitted by our members
Games in WIP (785)
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 4165 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: 1259
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: 139
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  
You cannot reply to this message, because it is very, very old.

numerical (165 views)
2017-02-21 07:32:16

numerical (165 views)
2017-02-21 07:31:46

theagentd (272 views)
2017-02-18 13:42:33

theagentd (274 views)
2017-02-18 13:35:16

h.pernpeintner (1441 views)
2017-01-24 22:39:11

h.pernpeintner (1426 views)
2017-01-24 22:38:32

Galdo (1989 views)
2017-01-12 13:44:09

Archive (2043 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2580 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2331 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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!