Skip to main content
GameDev.net gamedev.net
🔒 Locked

MIDP dynamic loading

Started by DaemonSpawn Dec 22, 2004 at 3:27 AM 14 replies 2.1k views
Original Post
DaemonSpawn
DaemonSpawn
I'm currently making an isometric engine and want to make it so images are dynamically loaded as they become visible and discarded when they arent. My engine uses a single thread at the moment, so if I call createImage(Image source), the method blocks, and the frame rate suffers. Should I use two threads, one for loading (pictures, maps etc) and one for the main drawing loop? Im assuming this means that the createImage(Image source) method will be spread over several frames in its own thread, rather than interfering with the drawing code thread. OR Would it be better to use an inputstream in my main thread, and slowly read the image data in, then use createImage(byte[] imageData, int imageOffset,int imageLength) to create the image when it finishes? I'm thinking this would be better as it gives more control, although it would probably be slower. Just wondering which you experienced midp programmers have found to be better, or if you have come up with a better method for dynamic loading, feel free to share it! Thanks
shmoove
shmoove
What happens in the frames while you're waiting for the image to be loaded? Don't you need to display that image?

shmoove
DaemonSpawn
DaemonSpawn
The images im trying to dynamically load are tiles which arent yet in the screen, say 3 tiles or so offscreen, by the time they get into the visible tiles area they need to be loaded, as are tile images dereferenced as they go out of the 3 tile range.

What is happening at the moment, is the game stops as it loads the new tile, as its attempting to load them completely within one frame. I have a typical game loop at the moment, which executes each frame:

Get user input
Load new images <-- bottle neck
Update game world
Draw screen

What i need is a way to allow the images to load over a number of frames, so they dont get loaded in a single frame, and stop screen and game updates. Dont get confused, even one small image is too much to load in a single frame, so i cant just load one image per frame.

Hope that makes my problem more clear.
cb007sax
cb007sax
I don't think I've seen an isometric game that uses dynamic tiling like that. Usually, it puts many many tiles in ram, and when you go really far over on the map, it loads a completely new tileset or something.

This 3 tile away system won't work at all. You'll never be successful having to constantly load images.
DaemonSpawn
DaemonSpawn
Why cant it work? Is that because it is too slow to achieve on phones?

The reason I want to use this method is because the 'ram' in phones is very little, and to have all the tile and object images in memory at one time, even for a small map, is too much.

I dont think this is method is too hard to implement, i can ALMOST load a whole tile in one frame, im just wondering if theres already a good way to load images dynamically.
DaemonSpawn
DaemonSpawn
Yeh, i figured I would use the the normal 'load everything at the start' method if i couldnt get this work.

Oh well, Ill try anyway and post up a jar or sumthing to see how it runs on other phones, if it runs well on my phone ( LG U8110 )

Thanks for your opinions
shmoove
shmoove
Well, if you do go with the dynamic loading, then your second approach is the only viable option. If you use two threads you will have no control over the loading times, and chances are that the tiles might not be ready by the time you need them. With one thread you can retain a little bit of control, and possibly slow down if you see the tile isn't going to be ready in time. But I don't think dynamic loading is going to be practical. Apart from all the reasons people have already given (mainly, it's too slow to work properly), on many phones this constant loading and nulling of images will result in the memory being so fragmented that in very little time you won't be able to load anything at all.
So I guess loading everything at the beginning of a level (or on preset waypoints if memory is not enough), is the best way to go.

shmoove
kooktroop
kooktroop
Are you sure you really need dynamic loading?

I had a fair few tiles (in one image) for one of my games. I didnt need dynamic loading. Maybe you just arent managing your tiles properly.
Do not remove a fly from your friend's forehead with a hatchet.Chinese Proverb
DaemonSpawn
DaemonSpawn
Oops. I forgot about memory fragmentation, does it really happen? I guess dynamic loading may not be such a good idea then, its really not looking to be worth the effort, and is less compatible i guess.

kooktroop, how big an image were you able to load, and on what phones? In truth I'm not really sure how much heap memory most phones have, in regard to how many images this tranlates to. Im sorta making a iso chat game, so i gotta have quite a few characters, objects and tiles loaded, but now looking at games on the market, i should be able to have enough images.
shmoove
shmoove
The absolute minimum amount of heap for a phone that wants to be MIDP compliant is 128K. Most phones have 200K+. Of course, this memory has to contain all your classes and data as well as the images. How many images that translates to... well, that's hard to say. How the images are loaded into memory is implementation dependent, and often also depends on the color depth of the screen (ie, images will take more space on phones that have screens with more colors).

Quote:
Original post by DaemonSpawn
Oops. I forgot about memory fragmentation, does it really happen?

You bet.

shmoove
kooktroop
kooktroop
DaemonSpawn, just noticed you're from Australia!

Anyway, i was loading all of these:
128x32
192x48
192x28
102x24

Thats not that much, but that was coming near the memory limits of the series 40 (with game data as well).

Remember don't pre-optimise. Try out loading everything into memory the simple way, then if it doesnt work start to optimise.
Do not remove a fly from your friend's forehead with a hatchet.Chinese Proverb
DaemonSpawn
DaemonSpawn
Thats not too bad acually, roughly 41 32x16 tiles.

I think I'll take your advice kooktroop and just concentrate on making my game, its my first project in midp anyway and I havent gotten as far as I'd have hoped. The flash games i used to make were so much more carefree.....
Zahlman
Zahlman
Quote:
Original post by kooktroop
DaemonSpawn, just noticed you're from Australia!

Anyway, i was loading all of these:
128x32
192x48
192x28
102x24

Thats not that much, but that was coming near the memory limits of the series 40 (with game data as well).

Remember don't pre-optimise. Try out loading everything into memory the simple way, then if it doesnt work start to optimise.


That's only 41K or so out of the 200, I'd look elsewhere for space problems [smile] The code itself isn't going to take more than 64K to load into memory, so that sounds like you're talking about close to 100K of random heap allocations for data [wow] Remember arrays are objects too, so avoid multi-dimensional arrays if you can - and if you can represent something simply with a primitive, then do so (and get a preprocessor so you can do 'typedefs' :\ )
mixa
mixa
2DaemonSpawn:

Have you seen isoj2me project at www.sourceforge.net?

As for stopping threads for the time of reading resources data (jar or rms) - many KVM implementations call native layer for it, often synchronously, so VM task (not VM thread but OS native task running whole VM) is blocked until i/o operation finishes, so everything in java hangs. So all you can do is to show an logo/progress image and wait until the resource loads.

Regards from RU,
Mike

DaemonSpawn
DaemonSpawn
mixa

Yes i have seen the isoj2me project. The only problem (unless they've somehow solved it) is there isn't smooth scrolling. However I myself can't think of a way to do it, as clipping an object which is half on the screen looks funny.

Atm I just clip the background tiles, and the objects just appear from the blackness (would be so much better with a fadein) and aren't clipped but it doesnt look too bad...

http://www.freewebs.com/welive2die/iso.PNG

I was hoping the i/o commands were not like that, but along with the memory fragmentation problem, it makes dynamic loading this way impossible. Im now just loading the required images before each map loads (hopefully i wont run into the memory fragmentation problem with this method)

Thanks for your help

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.