Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
[2016.8] RenderTarget:save() — Gideros Forum

[2016.8] RenderTarget:save()

hgy29hgy29 Maintainer
edited August 2016 in Code snippets
Gideros now comes with the ability to save the content of a render target to a file, either in .png or .jpg format.

First thing that comes to my mind with this new feature (beside the specific case for which I actually needed it), is that it allows to save a screen shot of your app, just a part of it, or even a not displayed compisition of sprites, to a temporary file for later upload to some website or facebook.

It can also be used for dynamic texture creation, for exemple to generate different versions of a base texture at run time, possibly through shaders. The app package will then be smaller and textures can be generated on first launch.

Exemple: to make a screenshot, you could do:
local rt=RenderTarget.new(application:getContentWidth(),application:getContentHeight())
rt:draw(stage)
rt:save("|T|screenshot.png")
I hope you'll find it useful!
+1 -1 (+5 / -0 )Share on Facebook

Comments

  • n1cken1cke Maintainer
    Paint app:
    rnd @ |math.random(0, 0xFFFFFF)|
    app @ application
    size @ 0.01
     
    app:setLogicalDimensions(app:getDeviceWidth(), app:getDeviceHeight())
     
    local w, h = app:getContentWidth(), app:getContentHeight()
    local pixel = Pixel.new(0, 1, size * w, size * h)
    local texture = RenderTarget.new(w, h)
    local canvas = Bitmap.new(texture)
    stage:addChild(canvas)
     
    local function draw(e)
    	pixel:setColor(rnd)
    	pixel:setPosition(e.x or e.touch.x, e.y or e.touch.y)
    	texture:draw(pixel)
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, draw)
    stage:addEventListener(Event.MOUSE_MOVE, draw)
    stage:addEventListener(Event.TOUCHES_BEGIN, draw)
    stage:addEventListener(Event.TOUCHES_MOVE, draw)
     
    stage:addEventListener(Event.KEY_DOWN, function()
    	texture:save("|T|screenshot"..os.time()..".png")
    	print "YOUR AWESOME PIC IS SAVED!"
    	texture:clear(0, 0)
    end)
    You draw with "rainbow" brush and you can save your pics if you press any key.

    Likes: hgy29, keszegh, pie, MoKaLux

    +1 -1 (+4 / -0 )Share on Facebook
  • This is a very nice feature to have, thank you.
  • XmanXman Member
    pretty good feature.

    I notice a tiny drawback of the draw api of RenderTarget.
    when draw a sprite, it always starts from the position(0,0),it's not possible to draw the whole device screen if the Logical Translate is not 0 on some devices.

    It would be useful to add extra parameters x, y to the draw api.

    Likes: totebo

    +1 -1 (+1 / -0 )Share on Facebook
  • antixantix Member
    edited August 2016
    Err nevermind :)
  • hgy29hgy29 Maintainer
    @Xman, yes I see what you mean, good idea!
  • totebototebo Member
    edited August 2016
    I agree @Xman. I'd also like to use RenderTarget on negative coordinates. If it's a sprite, it first has to be moved, then drawn, then moved back to its original position. I use this method quite a bit, and I suspect it could cause an overhead if the target sprite has hundreds of nested sprites inside of it (like mine does).
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    I'll add extra x,y parameters then, and a second form with a transform matrix as parameter for more complex transformations (scale/rotate/shear/...)

    Likes: totebo, pie

    +1 -1 (+2 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    Although not yet in the docs yet, 2016.8.1 includes a few more RenderTarget improvements.

    Now you can create a repeatable rendertarget by setting the fourth argument to true:
     -- Create a 128x128, filtered and repeatable render target
     rt=RenderTarget.new(128,128,true,true)
    And you can supply translation or transform when drawing on sprite on it:
     -- Draw sprite at -10,30 instead of 0,0
     rt:draw(sprite,-10,30)
    -- Draw rotated sprite
    m=Matrix.new()
    m:setRotationZ(15)
    rt:draw(sprite,m)
    +1 -1 (+4 / -0 )Share on Facebook
  • AWESOME.

    Likes: MoKaLux

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.