Forum Discussion

Komag's avatar
Komag
Roku Guru
11 years ago

Why can't my new region draw to another region?

I've had a hard time trying to understand really how "region"s work, they still confuse me a lot.

screen = CreateObject("roScreen", TRUE, 1280, 720)
screen.SetAlphaEnable(TRUE)
mainR = CreateObject("roRegion", screen, 32, 8, 1216, 704) ' leaves enough room for overscan
mainR.SetAlphaEnable(TRUE)

Everything is working fine. Now I want to add a HUD and have it be its own region for easier x/y management, and I want to base the regions x/y and size off of the mainR like so:

hudR = CreateObject("roRegion", mainR, 870, 532, 290, 116) ' Draws to mainR
hudR.SetAlphaEnable(TRUE)

Anything I try to draw into this doesn't show up, why?

hudR = CreateObject("roRegion", screen, 902, 540, 290, 116) ' Draws to screen

If I switch to this (and adjust the numbers based off screen instead of mainR) and everything works perfectly, no other changes, so I know it's not some other problem.

I can live with this I guess, but why can't I draw a region to a region, what am I misunderstanding about regions?

5 Replies

  • There's a statement in the Component Reference, under roBitmap:

    The same bitmap cannot be used as a source and a destination in a single DrawObject() call


    Both your source and destination regions map onto the same bitmap, your roScreen, so you're effectively trying to draw something that's on your screen bitmap onto another part of the same screen bitmap.
  • Hmm, I'm trying to digest that...

    Right now I'm just drawing some rectangles.
    	drawBox(TRUE, box2X, box2Y, 74, 74, 5, &h008100C0, drAA.colors.darkGreen, hudR)

    FUNCTION drawBox(bck AS BOOLEAN, x, y, w, h, thk, color, bckColor, drawTo)
    ' trig by drawHealthBar(1), drawScrollbar(1), drawSwapMenu(1), drawText(1), drawGenMenu(1), drawControlsPreset(1)
    ' bck: background, w: width, h: height, thk: border thickness, drawTo: region/bitmap/screen
    drawTo.DrawRect( x, y, w, thk, color ) ' border top
    drawTo.DrawRect( x, y+thk, thk, h-(2*thk), color ) ' border left
    drawTo.DrawRect( x+w-thk, y+thk, thk, h-(2*thk), color ) ' border right
    drawTo.DrawRect( x, y+h-thk, w, thk, color ) ' border bottom
    IF bck THEN drawTo.DrawRect( x+thk, y+thk, w-(2*thk), h-(2*thk), bckcolor) ' background (inside border)
    END FUNCTION

    So my actual draw calls are not objects but rectangles. I always thought that line in the reference meant you can do something like:
    hudR.DrawObject(100, 200, hudR)
  • Okay, I see what you're doing now. It doesn't look like you can map a region onto another region, at least if these statements under roRegion mean what I think they mean:

    The roRegion component is used to represent a subsection of a bitmap


    CreateObject("roRegion", Object bitmap, Integer x, Integer y,Integer width, Integer height)


    I would assume "bitmap" means the second parameter has to be a bitmap, i.e. a region can only map onto a bitmap (which would include roScreen since that is also a bitmap), and not onto another region.
  • You may be able to use the roCompositor and roSprite to do what you want. If your hud thing has its own bitmap, you can set up a compositor that writes to your main region via a sprite that reads from a hud region mapped on to your hud bitmap:

    Sub Main ()
    port = CreateObject ("roMessagePort")

    screen = CreateObject("roScreen", TRUE)
    screen.SetPort (port)
    screen.SetAlphaEnable(TRUE)

    mainR = CreateObject("roRegion", screen, 32, 8, 600, 400) ' leaves enough room for overscan [changed dimensions to fit in SD screen]
    mainR.SetAlphaEnable(TRUE)

    hudBitmap = CreateObject ("roBitmap", {Width: 290, Height: 116, AlphaEnable: False})
    hudR = CreateObject("roRegion", hudBitmap, 0, 0, hudBitmap.GetWidth (), hudBitmap.GetHeight ()) ' Draws to hudBitmap
    hudR.SetAlphaEnable(TRUE)

    compositor = CreateObject ("roCompositor")
    compositor.SetDrawTo (mainR, &h404040ff)
    sprite = compositor.NewSprite (200, 100, hudR) ' 200, 100 is the position relative to your mainR origin where the hud bits will go

    drawBox(TRUE, 0, 0, 74, 74, 5, &h008100C0, &h00FF00FF, hudR) ' writes box in the hud bitmap at position 0, 0, relative to hud bitmap

    compositor.DrawAll () ' The compositor will take the contents from the sprite's region (hud bitmap) and put them on the compositor's surface (mainR)
    screen.SwapBuffers ()

    Wait (0, port)
    End Sub

    FUNCTION drawBox(bck AS BOOLEAN, x, y, w, h, thk, color, bckColor, drawTo)
    ' trig by drawHealthBar(1), drawScrollbar(1), drawSwapMenu(1), drawText(1), drawGenMenu(1), drawControlsPreset(1)
    ' bck: background, w: width, h: height, thk: border thickness, drawTo: region/bitmap/screen
    drawTo.DrawRect( x, y, w, thk, color ) ' border top
    drawTo.DrawRect( x, y+thk, thk, h-(2*thk), color ) ' border left
    drawTo.DrawRect( x+w-thk, y+thk, thk, h-(2*thk), color ) ' border right
    drawTo.DrawRect( x, y+h-thk, w, thk, color ) ' border bottom
    IF bck THEN drawTo.DrawRect( x+thk, y+thk, w-(2*thk), h-(2*thk), bckcolor) ' background (inside border)
    END FUNCTION
  • That explanation, that a region can only draw to a bitmap, helps it make a little more sense for me.

    Heading off to bed now, but at quick glance that new idea looks really interesting.

    I'll see if I can get chance over the weekend to test it out, thanks! 🙂