Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Loading the same image in diferent positions - Gideros Forum

Loading the same image in diferent positions

MendesMendes Member
edited March 2012 in General questions
I'm building a board game and i need to load the same image several times:
I have my images in a table like

--images on a table
Lateral = {Bitmap.new(Texture.new("images/ima1.jpg")),Bitmap.new(Texture.new("images/iman.jpg"))}

--wanna add images to a position where that image is needed
Lateral[1]:setPosition(40,40)
stage:addChild(Lateral[1])
--at this point no problem

The problem is that i need the same image to be in another position at the same time. The same image is needed in different places on my board game. I created a table because I need to keep track of all the images.
There is a way to load my images

Comments

  • ar2rsawseenar2rsawseen Maintainer
    I guess most optimal solution would be to create on texture an reuse it for every bitmap
    --create texture once
    local texture = Texture.new("images/someimg.jpg")
    --reuse on multiple images
    local image = Bitmap.new(texture)
    local sameimage = Bitmap.new(texture)
     
    --position each item accordingly
    image:setPosition(100,100)
    sameimage:setPosition(200,200)
  • There's an (important but often misunderstood) difference between Textures and Bitmaps.

    You use a Texture to make a reference to an instance of an image in memory and a Bitmap to make a reference to an object that represents that image (or part of it) when it's drawn on the screen.

    As ar2rsawseen's example shows you can have many different bitmaps all being created using the same texture - it just means they all "reference" the same image, so a Texture can be used by many different Bitmaps but each Bitmap can only reference one texture (at a time).

    Hope this helps

    Jon...
    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
  • Here some code of board game (checkers):

    http://www.giderosmobile.com/forum/uploads/FileUpload/16/59b5b1bf1f96f28d40f6fd66c79f56.rar

    For checkers I use table of sprites. Every sprite in table have self Bitmap. Usage of Sprites is more flexible than Bitmap. You may create your own functions for sprites. In this example is function Chckr:rotate(cent_x, cent_y, my_angle) which rotate checker around point (cent_x, cent_y) by an angle my_angle. It's like OOP.

    Sprite may contain some Bitmaps, make animation or show defined image depending of sprite state, etc.

    In this code I use only checkers type A. It may be another table for chekers type B with another bitmaps.

    Maybe it helps you.
    +1 -1 (+2 / -0 ) Share on Facebook
  • Thank you all. ar2rsawseen for the example that help me a lot and also to techdojo for the explanation, it was really helpful.

    AlexRu pretty good example, thanks.
    My board game is a version of Mankala. My hole animation is based on loading images on the screen.

    Likes: gorkem

    +1 -1 (+1 / -0 ) Share on Facebook
  • function showboard()

    Label2[1]:setText("".. Comp_Seeds) ---label to show computer seeds
    Label2[1]:setPosition(40, 48)
    stage:addChild(Label2[1])
    if (pedras_Comp>22) then
    Lateral22 = Bitmap.new(Lateral[23]) --load the texture with the image 22 seeds
    Lateral22:setPosition(10,50)
    stage:addChild(Lateral22)
    else
    Lateral22 = Bitmap.new(Lateral[pedras_Comp + 1]) --find the number of seed to load
    Lateral22:setPosition(10,50)
    stage:addChild(Lateral22)
    end

    Label2[2]:setText("".. Player_Seeds) --label to show player seeds
    Label2[2]:setPosition(570,210)
    stage:addChild(Label2[2])
    if (pedras_Jog>22) then
    Lateral22j = Bitmap.new(Lateral[23])
    Lateral22j:setPosition(535,50)
    stage:addChild(Lateral22j)
    else
    Lateral22j = Bitmap.new(Lateral[pedras_Jog + 1])
    Lateral22j:setPosition(535,50)
    stage:addChild(Lateral22j)
    end

    Label[1]:setText("".. boardInicial[1]) --first hole for the player side. I have a table
    and a image to load acording to the number of seeds
    Label[1]:setPosition(120,210)
    stage:addChild(Label[1])
    if (boardInicial[1]>12) then
    Hole1 = Bitmap.new(hole[12])
    Hole1:setPosition(85,125)
    stage:addChild(Hole1)
    else
    Hole1 = Bitmap.new(Hole[boardInicial[1]+1])
    Hole1:setPosition(85,125)
    stage:addChild(Hole1)
    end
    .
    .
    .
    I guess I have to make it sprites instead of just just Bitmap. What you guys think.
  • AlexRuAlexRu Member
    edited March 2012
    I would create:

    1. Hole_Sprite and Seed_Sprite.
    2. Two tables 1..6 of Player_1 and Player_2 Hole_Sprites. Maybe one table 1..12 if information about whom this hole will be in Hole_Sprites.
    3. Table 1..36 of Seed_Sprites. Seed_Sprites may have different images.
    4. Animation is Seed_Sprites moving.

    It's for Kalah. Good game :)
  • petecpetec Member
    I guess most optimal solution would be to create on texture an reuse it for every bitmap
    --create texture once
    local texture = Texture.new("images/someimg.jpg")
    --reuse on multiple images
    local image = Bitmap.new(texture)
    local sameimage = Bitmap.new(texture)
     
    --position each item accordingly
    image:setPosition(100,100)
    sameimage:setPosition(200,200)
    Hi,
    Just wondered if specifying the texture once is better than doing it twice as in the following where someimg.jpg is the same image:
    local image = Bitmap.new(Texture.new("images/someimg.jpg"))
    local sameimage = Bitmap.new(Texture.new("images/someimg.jpg"))
    Is your way more efficient? It looks as if it could be!
    Thanks
    Pete
  • ar2rsawseenar2rsawseen Maintainer
    Well I think it is. Both from memory management side and not reading from file system twice. :)
  • atilimatilim Maintainer
    edited March 2012
    Although loading the texture once and reusing is the fastest, this one:
    local image = Bitmap.new(Texture.new("images/someimg.jpg"))
    local sameimage = Bitmap.new(Texture.new("images/someimg.jpg"))
    is also very fast (and memory efficient). We internally reuse the texture if it's already on memory.

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 ) Share on Facebook
  • petecpetec Member
    OK - thanks for that. Good to know the other way is also fast.
Sign In or Register to comment.