Author Topic: How to draw nested surfaces without any frame delay ( code example )  (Read 481 times)

Oomek

  • Sr. Member
  • ****
  • Posts: 239
    • View Profile
As some of you probably know drawing something on nested surfaces in AM introduces an unwanted artifact, a 1 or more frames of delay in updating the nested surface content.
Luckily I discovered a neat trick that circumvents that.

Here is a code example that draws a game title twice in white colour, and twice in black but on nested surfaces ( with a little offset to make you see the white text underneath )
Just scroll through your game list and observe that the left side is blinking in white, that's the delay I'm referring to as the black title on a nested surface is being drawn with 1 frame delay.
The text on the right is drawn without any delay.

Code: [Select]
// This is an example of how to draw nested surfaces and avoid 1 frame delay ( or more, depending on the nesting level )
// The text on the left is blinking as the white text drawn on fe is updated first,
// then the black text on a surface is drawn on top, but it's late by 1 frame
// On the right the white and black texts are drawn on the same frame


local flw = fe.layout.width
local flh = fe.layout.height

local background = fe.add_text( "", 0, 0, flw, flh )
background.set_bg_rgb( 50, 150, 150 )



// title drawn on fe. in white
local textOnFe1 = fe.add_text( "[Title]", 0, 0, flw/2, 50 )
textOnFe1.align = Align.Left

// title drawn on fe. in white
local textOnFe2 = fe.add_text( "[Title]", flw/2, 0, flw/2, 50 )
textOnFe2.align = Align.Left



// SURFACE WITHOUT DELAY ( on the right )
local nestedSurface2 = fe.add_surface( flw/2, 50 )

// title drawn on nested surface in black
local txtsu2 = nestedSurface2.add_text( "[Title]", 0, 0, flw/2, 50 )
txtsu2.align = Align.Left
txtsu2.set_rgb( 0, 0, 0 )



// main surface on which both nested surfaces are drawn
local mainSurface = fe.add_surface( flw, 50 )

// shift the main surface by 2 pixes so you can see the white text underneath
mainSurface.set_pos( 0, 1 )

/////////////////////////////////////
// THE LINES BELOW ARE THE TRICK
nestedSurface2.visible = false
nestedSurface2 = mainSurface.add_clone( nestedSurface2 )
nestedSurface2.visible = true
/////////////////////////////////////

// position nested surface to the right
nestedSurface2.set_pos( flw/2, 0 )



// SURFACE WITH DELAY ( on the left )
local nestedSurface1 = mainSurface.add_surface( flw/2, 50 )

// title drawn on nested surface in black
local txtsu1 = nestedSurface1.add_text( "[Title]", 0, 0, flw/2, 50 )
txtsu1.align = Align.Left
txtsu1.set_rgb( 0, 0, 0 )



// do something at tick() so the screen keeps updating
function tick(ttime) {
mainSurface.x = 0
}
fe.add_ticks_callback( "tick" )


progets

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 749
    • View Profile
Re: How to draw nested surfaces without any frame delay ( code example )
« Reply #1 on: June 10, 2018, 10:29:27 AM »
Very nice!

zpaolo11x

  • Sr. Member
  • ****
  • Posts: 224
    • View Profile
Re: How to draw nested surfaces without any frame delay ( code example )
« Reply #2 on: August 10, 2018, 11:00:01 AM »
This is great! I missed this post and now that I did some tests I'm going to rewrite a large part of my them to take this into account :D Thank you!

zpaolo11x

  • Sr. Member
  • ****
  • Posts: 224
    • View Profile
Re: How to draw nested surfaces without any frame delay ( code example )
« Reply #3 on: August 11, 2018, 03:35:56 AM »
Oomek maybe you can help me: my theme is based on your "carrier", usually I draw the tiles as surfaces on "fe" but recently I tried to create a carrier_surface, and draw the tiles surfaces on top of that.
This introduces the delay as per your post, but using your "trick" it now works and I see no more frame delay when, for example, going to a new list. Sadly I can't seem to make zorder work anymore: I used to change the zorder of the "selected" tile to make it pop over the other. Is there any strange interaction between nested surfaces, clones etc?

Oomek

  • Sr. Member
  • ****
  • Posts: 239
    • View Profile
Re: How to draw nested surfaces without any frame delay ( code example )
« Reply #4 on: August 11, 2018, 03:40:12 AM »
It’s a known limitation of AM that zorder does not work on surfaces. There is no easy fix unfortunately for the current moment. AM needs rewriting in that department.

zpaolo11x

  • Sr. Member
  • ****
  • Posts: 224
    • View Profile
Re: How to draw nested surfaces without any frame delay ( code example )
« Reply #5 on: August 11, 2018, 10:55:38 AM »
Thank you for confirming this, I thought I was doing something wrong :D Well I just have to rearrange the way items are placed in the theme, not a great issue (I hope)