Latest Uploads
Extraterre ... .0.1 (zip)

dantheman363

Monty Teas ... Screenie 1

steve_ancell

Santa Clau ... ed his bag

waroffice

manic_platdude.png

spinal

Tetris Clone

steve_ancell

Super blues bros.

spinal

Forum Home

poor performing tile map render

UserMessage
Posted : Tuesday, 12 July 2011, 13:12 | Permalink | Mark Here
waroffice


I thought I would tidy up and optimise my tile map render code a little but i got some weird effect.

The newer code is about 20 fps slower than the old, and deathly slow when debugmode=1.

can someone explain why because i certainly cant?

this is the original code

-->

and this is the newer, as i thought better code

-->

Posted : Tuesday, 12 July 2011, 13:24 | Permalink | Mark Here
waroffice


one problem is it is drawing the debug text every cell.

removed that and it was closer but still slower by about 9-10 milliseconds
Posted : Tuesday, 12 July 2011, 13:43 | Permalink | Mark Here
shroom_monk


WW Entries : 8
I don't know of any intricacies Cobra has in this kind of thing, as I don't use it myself, but I don't see anything glaringly obviously wrong here. The only thing I can thing is all those nested for loops, but those seem rather necessary.

Have you tried putting a timer on every stage of the code to see which bits are slowest, rather than just timing the whole thing?

-----
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Posted : Tuesday, 12 July 2011, 13:47 | Permalink | Mark Here
Jayenkai


WW Entries : 103
Obvious shortcut..

Find your 4 screen limits..
Left, Right, Top, Bottom..
Then instead of doing 0-1024x0-1024, do only (left of screen to right of screen)x(top of screen to bottom of screen)


|edit| do an extra 1 block either side, too, to account for scrolling! |edit|

-----
Posted : Wednesday, 13 July 2011, 08:55 | Permalink | Mark Here
waroffice


oh i get you, no need to render unseen tiles.

and it will be 8 screen limits as you can walk diagonally, im sure I can come up with a formula to make it do a radius of 1.5 screens

then work it for the most optimal.

strangely the nested loops one is quicker?!?!???!!!??!?!
Posted : Wednesday, 13 July 2011, 10:19 | Permalink | Mark Here
JL235


WW Entries : 7
The simple problem is that you are performing too much drawing. You are iterating over the whole screen, multiple times, performing lots of calls.

So my first suggestion is to use a hardware accelerated graphics library, either with Cobra or something else. A 10 year old graphics card can trivially draw 1000 2D images to the screen, so there is no reason why all these calls should be slowing you down.

But if that is not attractive, here is an alternative 'dirty rectangle' approach. You split the screen into a series of off-screen buffers or images. This means the screen is made up of 4, 16, or more 'big tiles'.

You render your world map to these big tiles, and then render the big tiles to the screen. The advantage is that if the contents of a big tile has not changed, then you don't draw to redraw it. If it's contents does change, then your only redrawing a quarter (or less) of the screen.

Now lets say you scroll the world to the left, how is that handled? First move all big tiles to the left, and now you've correctly scrolled about 80% of the map. You then add new big tiles on to the right hand side, and redraw the new section of the world to those new big tiles.

Any big tile that are no longer visible can be removed. You could also cache these, and re-use them later for your new tiles that scroll on to the screen.

If a tile is animated then you will have to clear a section of the big-tiles, and redraw that whole section. What size should be based on if the tile overlaps other tiles, and if it's surrounding tiles overlap more tiles. All of the tiles affected will need to be cleared, and then redrawn.

If a tile does not overlap a surrounding tile, then it only needs to clear and redraw it's self.

Other game elements, like characters and background, can just be rendered before and after the big tiles are drawn to the screen. They don't need to be rendered to the big-tiles.

That technique should reduce the number of drawing calls from around 1000 to just 4 or 16 (depending on how many big tiles you have). That should make it _much_ faster, but incorrect code could reduce in sections not updating correctly.

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Posted : Thursday, 14 July 2011, 06:18 | Permalink | Mark Here
waroffice


That sounds like a perfect solution. I guess I had thought of a reasonably efficient way to do it and stuck with it.

if I understand you correctly you should:

  • render the 4 layers of the map to image buffers
  • render parts of these to the screen so it fits
  • update them on map changes, like tile changes etc

    If I'm to redesign my map engine, for probably the three hundredth time, I would like to get the theory down first, is there any recommended reading somewhere?

    Stuff I have found on the internet points to the same way I do it now
  • Posted : Thursday, 14 July 2011, 06:31 | Permalink | Mark Here
    waroffice


    I just went over my code this is how it works

    the game engine tracks where the player is.

    it then calls the map drawing function with parameters, x,y where to draw it, width and height, it will then draw the part of the map that's visible.

    Posted : Thursday, 14 July 2011, 09:03 | Permalink | Mark Here
    waroffice


    I'm struggling to find any decent theory on this subject. What I can find is code for other languages that do it the same way I do.

    im looking through amazon right now for some physical works but still bobbins
    Posted : Thursday, 14 July 2011, 09:04 | Permalink | Mark Here
    HoboBen


    WW Entries : 9
    It would take some changes, but using Sprites instead of Images would solve most of this for you.

    Just create an initial (w / sprite_width) * (h / sprite_height) set of sprites, plus one extra row and column for scrolling the screen, and just simply use PositionSprite and SpriteFrame.

    -----
    github
    Posted : Thursday, 14 July 2011, 09:15 | Permalink | Mark Here
    waroffice


    I tried that before and it was slower. I have learnt a lot since then though

    I think I will leave the renderer as it is for now. Its not slow but if its not done correctly it will annoy me.

    Hoboben, do you know a way to disable vsync for cobra2d?
    Posted : Thursday, 14 July 2011, 09:25 | Permalink | Mark Here
    HoboBen


    WW Entries : 9
    Fairly certain you can just use VSync(0)

    It says it's Cobra3D in the docs, but I've used it with Cobra2D before.

    -----
    github
    Latest Posts
    Coder's Block
    Jayenkai Thu 09:01
    Consumer Lockout
    9572AD Thu 06:09
    Professor Oak, Honest!!
    spinal Wed 23:24
    Position in Mind
    steve_ancell Wed 20:11
    Sonic Adventure v0.x
    Jayenkai Wed 07:08
    FIRST!!!
    steve_ancell Wed 03:55
    Noel's Graduation
    waroffice Wed 02:55
    Monkey vs iOS tweaks
    spinal Tue 22:51
    Fucking Pound Sign Unicode Bullshit Bollocks
    Dabz Tue 13:00
    Progress / Location Bars
    dna Tue 08:55
    More

    Latest Items
    News : Newsletter #176
    Jayenkai Sat 04:49
    News : Newsletter #175
    Dabz Tue 09:38
    Blog : Snow: More Material Junk
    Cower Sat 23:17
    Dev-Diary : Mutant Monty: Amstrad CPC to Windows conversion
    rockford Fri 13:14
    Techy : AppleTV
    Jayenkai Thu 09:40
    Blog : Graphviz
    steve_ancell Sat 14:17
    Pets : Top-Down Shadow Hack
    Jayenkai Tue 05:52
    Snippet : JNKrunch v1.0
    Jayenkai Sat 07:20
    News : Newsletter #173
    waroffice Fri 04:47
    Blog : Material Loading
    Cower Fri 02:08
    Pets : I Done Won A Thing
    shroom_monk Sun 11:31
    Pets : Repurposing A Lexer
    Cower Mon 22:06
    Bah : Feeling a Little Angry
    spinal Mon 11:26
    News : Newsletter #170
    Dabz Sat 00:34
    Showcase : sbfgen
    Cower Sat 16:57
    More

    Who's Online
    Dabz
    Thu, at 22:36
    Jayenkai
    Thu, at 19:46
    HoboBen
    Thu, at 18:50
    CodersRule
    Thu, at 18:29
    Cower
    Thu, at 18:21
    Evil Roy Ferguson
    Thu, at 17:34
    dna
    Thu, at 17:30
    9572AD
    Thu, at 17:28
    steve_ancell
    Thu, at 17:19
    Hotshot
    Thu, at 16:46
    Link to this page
    Site : Jayenkai 2006-Infinity |
    MudChat's origins, BBCode's former life, Image Scaler.