Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Creating lots of sprites is very slow

Author
Message
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 18th Sep 2012 01:35 Edited at: 18th Sep 2012 01:39
Hi.

I'm working on a program that creates a random world map. I create a sprite for each tile on the map, and that works fine for smaller maps but for larger maps the process of creating sprites gets exponentially slower and slower. I assumed if it took 1 ms to create 1 sprite it would take 4 ms to create 4, 16 ms to create 16 and so on, but that's not the case.

Here's some results from a test program I made.
Creating 768 (map size of 32x24) sprites takes 4 ms.
Creating 3072 (map size of 64x48) sprites takes 54 ms.
Creating 12288 (map size of 128x96) sprites takes 780 ms.
Creating 49152 (map size of 256x192) sprites takes 51193 ms.

Here's the code I used for testing. Change the tileSize variable to 2, 4, 8, 16 etc. for different map sizes.



I would like to create maps of a size up to 1024x768 (or even larger) but that seems impossible since creating sprites for much smaller maps (256x192) takes almost a minute. And since there's no basic 2d commands like draw dot, I don't know what other options there is.

So, does anyone have a suggestion on how I can improve the process of creating sprites or some other methods I could use? (I only need to create the sprites once then grab a image of the screen for later use, but still all those sprites must be created... )
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 18th Sep 2012 04:38
That is a problem, especially if you plan to take it to a mobile platform. I'm running your tests on a netbook (very slow computer) but I was able to chop off a few seconds (tile size of 8) with a little bit of optimization.



This create's a 'base' sprite and sets its size. The map sprites are then clones of this sprite. This means you don't have to manually set each sprite's size. However, this doesn't make huge difference for larger maps.

What you could try is Van B's technique of using text objects instead of sprites. Here is the idea in more detail.

Quote: "And since there's no basic 2d commands like draw dot, I don't know what other options there is."

I believe those are on the way, possibly next update.

Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 18th Sep 2012 08:34
For larger maps it is better to only create enough sprites to cover the screen and then move them around, changing their image/frame when needed.


Demo 2 is out! Click the image for more.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 18th Sep 2012 11:54
Digital hit the nail on the head. This is not a problem with AppGameKit, just in knowing the best way to use it. I'm finding there are a lot of little tricks like this to achieve what we want.

For example, one of the biggest problems on android is using transparent sprites. It's best to turn transparency off for background images or images that just don't need to be transparent, this hugely increases fps on android devices


this.mess = abs(sin(times#))
MarcoBruti
12
Years of Service
User Offline
Joined: 20th Nov 2011
Location: Caput Mundi
Posted: 18th Sep 2012 12:13
bad idea is to generate the entire map.
The good idea is to draw all sprites needed to cover the visibile screen and border areas, it is what I used in my DBPro tile-based games. Only for games that use few sprites, it is possible to draw all sprites in the world coordinates without affection performance. It is what I do for my pinball and platform game.
Moreover, I wonder if we can create a mega-map of, let's say, 1024x1024 tiles. That means about 1M tiles. If a tile is 64 pixel wide, it means a mega bitmap of 64Kx64K pixels. How an Android device, or even a PC, could manage such a big map?
On PC, with DB, I cannot create bitmaps more than 4096x4096. I learn that it is due to the limits of the graphics adapter, too.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 18th Sep 2012 12:40
Unless I've misinterpreted Ranietz's post (and example code) he is only creating enough sprites to fill the screen. He then wants to grab the image and use that. It's creating and setting up the sprites for the image grab which is taking a long time for smaller tile sizes.

Feel free to tell me I'm wrong though.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 18th Sep 2012 12:45
Oh, yes you could well be right there Hodgey, sorry Ranietz.

In that case perhaps try creating just one sprite, changing the image / position and using "drawSprite" then finally "getImage" at the end? It might be faster... making the image none transparent "setSpriteTransparency" might actually increase the render speed a little too.


this.mess = abs(sin(times#))
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 18th Sep 2012 18:34 Edited at: 18th Sep 2012 18:35
Thanks for the replys everyone.

When I said world map I didn't mean a map for the player to walk on but something like this:
http://images.wikia.com/elderscrolls/images/5/5d/Tamriel_map_arena.jpg

So I need a lot of small sprites with a size as small as 1x1 or 2x2.
I'm just experimenting with different algorithms at the moment so I haven't decided on the final map size yet or if this should work on mobile platforms.

I'll try the things you suggested and see if some of it improves the sprite creation speed.
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 18th Sep 2012 21:45 Edited at: 18th Sep 2012 21:53
Finally I solved it!
Thanks to baxslash who reminded me about the drawSprite command. I had not tried that one before. I can now draw a 1024x768 map (786432 tiles!!!) in about 2 seconds. I haven't tried it with images instead of a dummy sprite but it would probably be pointless to use an image for a 1x1 tile.

Here's the code I used for testing. It just creates tiles with a random grey color for now.


Thanks again everybody.


EDIT: forgot to use the clearScreen() command
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 18th Sep 2012 21:52
I have a question. Why don't you just load the fully created image, since you have one? Or, will the image be changing a lot over time?

Cheers,
Ancient Lady
AGK Community Tester
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 18th Sep 2012 21:58
@Ancient Lady: There is no image to load. I'm making a program to randomly create a world map.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 18th Sep 2012 22:01
Okay, good luck. I look forward to seeing the results.

Cheers,
Ancient Lady
AGK Community Tester
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 18th Sep 2012 22:05
Thanks. I'll post an example in a while.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 18th Sep 2012 22:16
Awesome, glad to help


this.mess = abs(sin(times#))
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 19th Sep 2012 01:29
Hi again.

I've attached an image of a random generated world map for thoose who are interested.

I used a combination of the Cellular Automata Method and the Diamond-square algorithm to create the map.

http://roguebasin.roguelikedevelopment.org/index.php/Cellular_Automata_Method_for_Generating_Random_Cave-Like_Levels
http://en.wikipedia.org/wiki/Diamond-square_algorithm

I'm not happy with the result yet but at least it's a starting point...

Attachments

Login to view attachments
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 19th Sep 2012 01:35
I'd say it's a pretty damn good starting point to say the least! Nice work


this.mess = abs(sin(times#))
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 19th Sep 2012 01:47
Thanks baxslash. I'm just trying out different algorithms at the moment. Hopefully I'll find one that works the way I want.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 19th Sep 2012 03:19
Have you tried the Perlin Noise algorithm? It might not be ideal for what you want but it's worth a shot. I think someone even wrote a function for it in the Useful Functions Project (see stickies).

Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 19th Sep 2012 04:06
I haven't tried the Perlin Noise algorithm yet but it's on my to-do list. There are some others I'm trying at the moment.
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 19th Sep 2012 05:52
On the subject of Perlin... and maps...

Have you seen what Ken Perlin went on to after his famous noise.

http://mrl.nyu.edu/~perlin/demox/Planet.html

I think you might find it interesting.
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 19th Sep 2012 14:36
How do you betatest a game with random maps? I imagine that some maps would be unplayable, if something was generated wrong... Should at least have a "Try again" button, to make another map, if the user finds it unusable.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 19th Sep 2012 15:03 Edited at: 19th Sep 2012 15:04
You can beta test by passing a random seed to the play testers, so they can generate the same random map by using the same seed value.

But AGK's random is very bad, so would probably be the same every time anyway.

I live for video games! (And beers, and football, and cars!)
See what I live for here: [url]http:\\www.TeamDefiant.co.uk[/url]
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 19th Sep 2012 17:02
Ranietz that is pretty cool! Thanks for sharing.

Cheers,
Ancient Lady
AGK Community Tester
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 19th Sep 2012 17:04
Quote: "
How do you betatest a game with random maps? I imagine that some maps would be unplayable, if something was generated wrong... Should at least have a "Try again" button, to make another map, if the user finds it unusable.
"


I plan to let the user generate maps until he generate one he is happy with. I will also try to make it so no maps are unplayable. It all depends on how good the random generating code is. I can also make a program that generates map after map and leave it running over night to se if there are any errors or if it chrashes.
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 19th Sep 2012 17:07
Quote: "Ranietz that is pretty cool! Thanks for sharing."


Thanks.

Login to post a reply

Server time is: 2024-05-04 13:43:03
Your offset time is: 2024-05-04 13:43:03