Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Is it possible to change a Bitmap's image "on the fly" — Gideros Forum

Is it possible to change a Bitmap's image "on the fly"

techdojotechdojo Guru
edited September 2012 in General questions
EDIT : YES IT IS! (see Bitmap:setTexture() and Bitmap:setTextureRegion() in 2012.8.2+) :)

I'm working on a super fast text renderer for which I want to use a single texture atlas containing all the font images that I want to use.

The idea is to have a number of "rows" that can contain text strings of different lengths, over time the contents of these row's will be updated and the text will change - currently whenever a row is updated, I create a new Sprite for the row and then add in a series of new Bitmaps (1 per character), however when there are multiple updates you get nasty spikes in the framerate - which I've tracked down to the amount of garbage being generated for all the above memory allocations.

A much better solution would be to "pre-allocate" a bank of bitmaps for each row (for the max number of possible characters per row contents), set them all to be invisible and then instead of re-creating each Bitmap, just simply change the U,V,Width/Height info for each bitmap effectively making it "point" to a different character in the font texture and then making it visible.

But...

The $64,000 question, is.. is it possible to update the texture region that a bitmap points to without having to create a new one. I'm sure it can be with a Shape using the beginPath, moveTo and lineTo functions but that seems overkill and might even be slower than the Bitmap.new()

I initially considered the TileMap:setTile() function but that would only work with a fixed with font.

Anyone (yeah I'm looking you @Atilim - master of the undocumented functions :) ) know of a way it can be done on Bitmaps?

As a side note - it'll make bitmap animations an absolute breeze!


PS. If it's NOT possible then prepare to expect a horde of very loud mice *squeaking* this way soon! :)

Likes: Mells

WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
+1 -1 (+1 / -0 )Share on Facebook

Comments

  • Bump?
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • atilimatilim Maintainer
    Accepted Answer
    It's great (and easy to implement) idea and therefore I've already added these functions:
    Bitmap:setTexture(Texture or TextureRegion)
    TextureRegion:setRegion(x, y, width, height)
    TextureRegion:getRegion() -- returns x, y, width, height
    Also I've started to think about Meshes.

    Likes: OZApps, GregBUG, Mells

    +1 -1 (+3 / -0 )Share on Facebook
  • techdojotechdojo Guru
    Accepted Answer
    @Atilm you ARE a star (and I don't mean in the flaming big ball of gas, prone to violent eruptions sense) :)

    Thank you very much ^:)^

    Likes: rodri

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • one *squeak* down

    Thank you @atilim :)
    have fun with our games~
    http://www.nightspade.com
  • OZAppsOZApps Guru
    edited September 2012
    It's great (and easy to implement) idea and therefore I've already added these functions:
    Bitmap:setTexture(Texture or TextureRegion)
    TextureRegion:setRegion(x, y, width, height)
    TextureRegion:getRegion() -- returns x, y, width, height
    Also I've started to think about Meshes.
    So I guess that can also be used for Animations from spritesheets, right?

    So, do you mean we could use something like
    local tex = Texture.new("someimage.png")
    local bmp = Bitmap.new(TextureRegion.new(x,y,wd,ht))
    and then somewhere in the code
    bmp:getTexture():setTextureRegion(x1,y1,wd1,ht1)
    or maybe
    bmp:setTextureRegion(x1,y1,wd1,ht1)
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • That's exactly what it could be used for.

    If developers embrace this and put all their anims on a single frame it'll make a really big difference to the app running speed - ESPECIALLY if you reuse objects and don't create any new objects in your EnterFrame function

    It'll also be a LOT more efficient than using Movieclips for big anims as you won't have to have a single sprite for each frame - just ONE bitmap that you just change the texture on.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • atilimatilim Maintainer
    @OZApps exactly. But with one difference, there won't be Bitmap:getTexture() function. You should hold a reference to your TextureRegion somewhere else like:
    local texture = Texture.new("someimage.png")
    local textureRegion = TextureRegion.new(texture, x, y, width, height)
    local bitmap = Bitmap.new(textureRegion)
     
    -- and then somewhere in the code
     
    textureRegion:setRegion(x1, y1, width1, height1)
    bitmap:setTexture(textureRegion) -- you should also set texture region so that bitmap gets the parameters
  • wow... i'm just coding Animator Studio 1.5 :D
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • OZAppsOZApps Guru
    edited September 2012
    @Atilim, and this will be in the coming release, right? I was so excited that I wrote some code to create and handle some animation based on that and when I ran it to test it... :(

    Waiting for 2012.8.2
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @techdojo: What is the difference in text output between your solution and using BMFont? It uses only one texture map too.
  • The problem with BMFont is that if you want to change the text then a new set of bitmaps will be required (hence memory allocation, garbage collection etc), this solution will allow you to update text every frame without any of that overhead.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • @techdojo: unless you use my modification to BMFont which allows the font to be a texture region of a larger texture so you could have multiple fonts on the same texture page. I already used this feature in HappyCatz
  • The issue isn't with packing all the images into a single atlas - this is already supported, the issue is being able to change a bitmap's image without having to create a new one and incur all the penalties associated with dynamic memory allocation.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • MellsMells Guru
    edited September 2012
    @atilim Great that you added this :)
    Also I've started to think about Meshes.
    From that discussion right?
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • atilimatilim Maintainer
    edited September 2012
    From that discussion right?
    yes.

    btw, previously I was planning to add only Bitmap:setTexture() function that accepts both Texture and TextureRegion objects but now there are two functions Bitmap:setTexture() and Bitmap:setTextureRegion() for setting Texture and TextureRegion objects separately.

  • Woo hoo :) +1 for the squeakers :)

    Likes: atilim

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • For reference and people that will find this discussion :
    Bitmap:setTexture() and Bitmap:setTextureRegion() are now documented [ link ].

    Bitmap:setTexture(texture)

    Sets the texture.
    Parameters:
    texture: (TextureBase)

    Bitmap:setTextureRegion(textureRegion)

    Sets the texture region.
    Parameters:
    textureRegion: (TextureRegion)
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • So This modification will make the Button Class a little more efficient?

    Button = Core.class(Bitmap)

    function Button:updateVisualState(state)
    if state then
    self:setTexture(self.downState) ---here self.downState is now a Texture
    else
    self:setTexture(self.upState)
    end
    end

  • @alexzheng yes it should, basically as efficient as it can get :)
Sign In or Register to comment.