Combine images into single image - coronasdk

How can I combine two PNGs into one image?
If one image is displays "1", and another image displays "9" - I'd like make an image file showing "19".
There is a object "group" which group the images into an array, but it doesn't seem that I can merge the members of a group.

number1 = display.newImage( "number1.png" );
number9 = display.newImage( "number9.png" );
number19 = display.newGroup();
number19:insert(number1)
number19:insert(number9)
--Put 9 next to 1
number9.left =number1.width

I use a snapshot when I need to combine images. Very similar to a group, the snapshot can be rendered out as a single PNG or JPEG.
local Function MakeSnapshot()
local snapshot = display.newSnapshot(digitWidth*2, digitWidth)
local digit1 = display.newImage("number1.png")
local digit2 = display.newImage("number9.png")
digit1:translate(-digitWidth/2, 0)
digit2:translate(digitWidth/2, 0)
snapshot.group:insert(digit1)
snapshot.group:insert(digit2)
snapshot:invalidate()
--Save file as a single image:
display.save(snapshot, "19.png", system.DocumentsDirectory)
end

Related

dynamically add spritesheets to sequenceData array in corona

Problem: I have a long duration frame-based animation for which i have created many spritesheets (e.g. 50 spritesheets with 10 images in each - 2 MB per spritesheet). It takes noticeable time (and memory) to pre-load all 50 spritesheets before the animation can be played using code based on the example code snippet below (this is suggested in the corona docs):
local function spriteListener( event )
thisSprite = event.target -- "event.target" references the sprite
if ( event.phase == "ended" ) then
sequenceNumer = sequenceNumer + 1;
if (sequenceNumer <=10) then
thisSprite:setSequence(filenameArray[sequenceNumer])
thisSprite:play()
end
end
end
local spriteWidth = 551
local spriteHeight = 401
local numFrames1 = 15
local startFrame = 1
frameInfoSet1 = {width = spriteWidth , height = spriteHeight, numFrames = numFrames1}
filenameArray = {"001-015","016-030","031-045","046-060","061-075","076-090","091-105","106-120","121-135","136-150"}
fullSequence =
{
{name=filenameArray[1], sheet=graphics.newImageSheet("spritesheets/" .. filenameArray[1] .. ".png", frameInfoSet1), start = startFrame, count=numFrames1, time=2250, loopCount=1},
{name=filenameArray[2], sheet=graphics.newImageSheet("spritesheets/" .. filenameArray[2] .. ".png", frameInfoSet1), start = startFrame, count=numFrames1, time=2250, loopCount=1},
{name=filenameArray[3], sheet=graphics.newImageSheet("spritesheets/" .. filenameArray[3] .. ".png", frameInfoSet1), start = startFrame, count=numFrames1, time=2250, loopCount=1},
-- more such spritesheets are loaded further...
}
firstSpriteSheet = fullSequence[1]["sheet"]
sequenceNumer = 1
tt = display.newSprite (firstSpriteSheet, fullSequence)
tt.x = display.contentWidth/2 ; tt.y = display.contentHeight/2
tt:addEventListener( "sprite", spriteListener )
tt:play()
However, what i would like to achieve is a "streaming" version of this technique. i.e. I will load only 3 spritesheets out of the total of 50, therefore requiring say 6 MB to start playing the animation and keep loading additional spritesheets as the already loaded ones keep playing. So, to illustrate further, i load sheets 1,2,3 and start playing the animation and load sheet 4 by the time sheet 1 is finished, load sheet 5 by the time sheet 2 is finished and so on.
Any advice is appreciated ! Thanks in advance.
I see this is old question, but I will add thoughts from my expirience:
This is not an easy task, but it's possible
Preload sheets before you go
I suggest to add simple loading scene, where you will show progress bar while loading.
local filenameArray = ... -- this is from your code
local preloadedSheets = {}
local function enterFrameLoading()
local nextSheetToLoad = #preloadedSheets + 1
if (nextSheetToLoad > #filenameArray) then
Runtime:removeEventListener("enterFrame", enterFrameLoading)
-- go to game scene if you are using scenes
return
end
preloadedSheets[nextSheetToLoad] =
graphics.newImageSheet("spritesheets/" .. filenameArray[nextSheetToLoad] .. ".png", ??)
updateProgress(nextSheetToLoad) -- this function will update UI, i.e. progress bar, text, etc
end
Runtime:addEventListener("enterFrame", enterFrameLoading)
After preloading all sheets will be in memory, so you shouldn't have performance issues
Use multiple sprite objects, created on demand
You can create new objects inside spriteListener - again, store all sprites inside a table and use .isVisible = false to hide old sprites
Combine solutions 1 and 2
You can create sprite objects with just 3 sequences. Then preload other sheets in enterFrame listener like I showed above.
When preloading is finished you want to destroy old sprite and create new with the same sequence and frame
Let me know if you need more information on this

Display screenshot after it is taken

I am trying to display a screenCap after it is taken, it is saving the screenCap but how can i get the latest screenCap's url ?
local screenCap = display.captureScreen( true )
local alert = native.showAlert( "Success", "Screen Capture Saved to Library", { "OK" } )
NewsScreenShot = display.newImage( " path to the PNG file " )
http://jp.anscamobile.com/dev/reference/index/displaycapturescreen/index.html
The picture will be saved in the device gallery with a name Picture X.png. After this you need to custom selection to the latest indexed picture.
Btw you can try it whit display.save("name",path) and this will be alway the last saved picture.
display.captureBounds is good for saving the whole screen to the directory. But it usually saves the file with increase in last index. So it may be difficult to read them correctly. So I prefer display.save. But it is not a straight way.
For doing this, you have to:
First create a displayGroup.
Then add the screen objects to that group.
Return the display group.
Use display.save to save the entire group displayed.
Display the desired image from system.DocumentsDirectory.
I am giving a sample here:
-- creating the display group --
local localGroup = display.newGroup()
-- creating display objects and adding it to the group --
local bg = display.newRect(0,0,_w,_h)
bg.x = 160
bg.y = 240
bg:setFillColor(150)
localGroup:insert(bg)
local rect = display.newRect(0,0,50,50)
rect.x = 30+math.random(260)
rect.y = 30+math.random(420)
localGroup:insert(rect)
-- Take Screenshot --
local function saveGroupImages()
-- take screen shot to baseDirectory --
local baseDir = system.DocumentsDirectory
display.save( localGroup, "myScreenshot.jpg", baseDir )
end
rect:addEventListener("tap",saveGroupImages)
After this, you can read the file and display it as follows:
local readImage = display.newImage( "myScreenshot.jpg" ,system.DocumentsDirectory , 50, 100 )
readImage.x = 160
readImage.y = 240
readImage:scale(0.5,0.5)
keep Coding........... :)

Creating an image from a sprite sheet using the original images file name

Creating sprite sheets aka texture atlases rather than using many hundres of individual images is recommended everywhere. I have hundreds of images for a word learning game; but there are hundreds of words, no animation sequences. So having generated the data file and sprite sheet, i am looking for an example of how to create an image when needed from the original image file name (as stored in the sprite sheet data (lua code) file (both created with texture packer).
This much seems right:
local sprite = require("sprite")
local CN_70_tiles_corona = require("CN_70_tiles_corona")
local spriteDataCN = CN_70_tiles_corona.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData( "CN_70_tiles_corona.png", spriteDataCN )
before creating the sprite sheet, would create my image with something like this:
t1 = display.newImage(cnTiles[tileNO])
where cnTiles[1], for examples, is a value placed in an array from a sqlite table such as "sit_word100.png".
there is now an entry in my generate lua file below the 'getSpritSheetData' function something like this:
{
name = "sit_word100.png",
spriteColorRect = { x = 0, y = 0, width = 69, height = 69 },
textureRect = { x = 2, y = 2, width = 69, height = 69 },
spriteSourceSize = { width = 69, height = 69 },
spriteTrimmed = false,
textureRotated = false
},
i can see that ALL my image file names are now stored in the data to provide a way to refer to my image within the sprite sheet, but since i do NOT want to use "sprite sets", i can't find an example of just getting the one image when in eed it.
I want something that allows me to refer to my now spritesheet-ified image using the original image name. is this possible? e.g.
t1 = display.newImage(CN_70_tiles_corona.getSpriteSheetData(name = "sit_word100.png")
The easy way is to create sheets with TexturePacker and use SpriteGrabber to take the sprites your need.
It's an awesome add-on to Corona-SDK which can be found here:
http://developer.anscamobile.com/code/spritegrabber-spritesheets-two-lines

shuffling images in a table

I am trying to shuffle the table of image and I also want to store the co ordinates for those images how can i do that? Is there any other way to do that? The one that I have done is this
local alpha = {{"alpha_a"} , {"alpha_b"} , {"alpha_c"} , {"alpha_d"} ,
{"alpha_e"} , {"alpha_f"} , {"alpha_g"} , {"alpha_h"}}
local coordinates ={{x=092, y=470}, {x=197, y=470}, {x=302, y=470},
{x=407, y=470}, {x=512, y=470}, {x=617, y=470} }
for i=1, #alpha do
local selection = table.remove(coordinates, math.random(#coordinates))
print(selection.x,selection.y, #coordinates)
images = display.newImage(alpha[i][1]..".png")
images.x = selection.x
images.y = selection.y
images:addEventListener("touch",swapping)
end
It's not entirely clear what you are trying to achieve, but I guess it might be keeping the image together with its own coordinates.
I think the logical approach to accomplishing this is reconsidering your data structure, and putting the coordinates and names into the same table like
local alpha = {{"alpha_a",x=092, y=470} , {"alpha_b",x=197, y=470} , {"alpha_c",x=302, y=470} , {"alpha_d",x=407, y=470} , {"alpha_e",x=512, y=470}} --...

In Corona SDK how to add label(text) to images?

In corona SDK how to add label(text) to images
I have created my image as follow
local item = display.newImageRect('images/foo.png',70,70);
item.x = 50;
item.y = 50;
How to add a text to the image?
Many Thanks
To add text to the image, so it appears to "stick" to the image, you could create the object (as you have) and also the text object directly above it. Then, create a group and stick them both in it.
Here is an example of how that could be done:
local myGroup = display.newGroup()
local item = display.newImageRect('images/foo.png',70,70)
item.x = 50
item.y = 50
local text = display.newText( "My Text", 0, 0, "Helvetica", 18 )
text:setTextColor( 0, 0, 0, 255 )
-- insert items into group, in the order you want them displayed:
myGroup:insert( item )
myGroup:insert( text )
Then, to move it around, just modify the x/y of 'myGroup'.
Hope that helps!

Resources