Share Your Code

Using Sprite Sheets for Stationary Images

Posted by jflowers45, Posted on September 2, 2011

If you want to actually run this code you'll want to Download the full demo package

Essentially, it shows a few tips for reducing memory usage using sprite sheets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require "sprite"
require "rackSpriteSmall"
 
function cleanAll() 
        if (rows ~= nil) then 
                for i=1,6,1 do
                        if (rows[i] ~= nil) then 
                                rows[i]:removeSelf()
                                rows[i]=nil
                        end
                        if (spritesets ~= nil and spritesets[i] ~= nil) then
                                spritesets[i]=nil
                        end
                end
                rows=nil
        end
        if (racksheet ~= nil) then
                racksheet:dispose()     --similar to display object :removeSelf(), but for spriteSheets
                rackSheetData=nil
        end
        
        if(blackRectangleBG ~= nil) then
                blackRectangleBG:removeSelf()
                blackRectangleBG=nil
        end
end
 
function updateText(stateNum,stateDesc)
        oldX=stateNumTxt.x
        stateNumTxt.text="State " .. stateNum
        stateNumTxt:setReferencePoint(display.CenterLeftReferencePoint);
        stateNumTxt.x=oldX
        
        oldX=stateDescTxt.x
        stateDescTxt.text= stateDesc 
        stateDescTxt:setReferencePoint(display.CenterLeftReferencePoint);
        stateDescTxt.x=oldX
        
end
 
function drawRowsBadWay()
        rows={}
        for by=1,6,1 do 
                local rowFilename="rack/row" .. by .. ".png"
                local aRowImg = display.newImageRect(rowFilename,480,320); 
                rows[by]=display.newGroup()
                rows[by].x, rows[by].y = 240,160
                rows[by]:insert(aRowImg)
        end
end
 
function drawRowsGoodWay()
        rackSheetData = rackSpriteSmall.getSpriteSheetData()
        racksheet = sprite.newSpriteSheetFromData( "rackSpriteSmall.png", rackSheetData )
        
        spritesets={}
        rows={}
                
        for by=1,6,1 do
                spritesets[by] =sprite.newSpriteSet(racksheet, by, 1)
                sprite.add( spritesets[by], "row", 1, 1, 5200, 0 )
                rows[by]=sprite.newSprite(spritesets[by])
                rows[by].x, rows[by].y = 240,160
                
        end
end
 
function drawRectBadWay()
        blackRectangleBG=display.newImageRect("blackRectangle.jpg",480,320);  
        blackRectangleBG.alpha=0.7
        blackRectangleBG.x, blackRectangleBG.y=240,160
end
 
function drawRectGoodWay()
        blackRectangleBG = display.newRect(0,0,480,320)
        blackRectangleBG:setFillColor(0,0,0)    
        blackRectangleBG.alpha=0.7
        blackRectangleBG.x, blackRectangleBG.y=240,160
end
 
function renderState(stateNum)
        cleanAll()
        if (stateNum==1) then
                updateText(stateNum,"Start: Touch to proceed")
        elseif (stateNum==2) then
                updateText(stateNum,"Draw rack using 6 images - uses 6MB")
                drawRowsBadWay()
        elseif (stateNum==3) then
                updateText(stateNum,"Memory released")
        elseif (stateNum==4) then
                updateText(stateNum,"Sprite sheet uses 1MB instead of 6MB!")
                drawRowsGoodWay()
        elseif (stateNum==5) then
                updateText(stateNum,"Memory released")
        elseif (stateNum==6) then
                updateText(stateNum, "Black dither as image - takes 1.05MB")
                drawRectBadWay()
        elseif (stateNum==7) then
                updateText(stateNum,"Memory released")
        elseif (stateNum==8) then
                updateText(stateNum,"Black Dither as drawing object - adds <0.02MB memory!")
                drawRectGoodWay()
        end
        
        textGroup.parent:insert(textGroup)
        performance.group.parent:insert( performance.group ) 
        
        if (blackRectangleBG ~= nil) then
                blackRectangleBG.parent:insert(blackRectangleBG)
        end
        
end
 
 
 
 
function screenTouch( event )
        if "ended" == event.phase then
                currentState=currentState+1
                if (currentState==9) then
                        currentState=1
                end
                renderState(currentState)
        end
end
 
Runtime:addEventListener( "touch", screenTouch )
 
fps = require("fps")
performance = fps.PerformanceOutput.new();
performance.group.x, performance.group.y = 120,  15;
performance.alpha = 0.6; -- So it doesn't get in the way of the rest of the scene
 
 
stateNumTxt = display.newText( "State 1:Empty.", 55, 260, "Verdana-Bold", 14 )
stateNumTxt:setTextColor( 255, 255, 255) 
stateNumTxt:setReferencePoint(display.CenterLeftReferencePoint);
 
stateDescTxt = display.newText( "Empty - touch to proceed", 55, 280, "Verdana-Bold", 14 )
stateDescTxt:setTextColor( 255, 255, 255) 
stateDescTxt:setReferencePoint(display.CenterLeftReferencePoint);
 
textGroup=display.newGroup()
textGroup:insert(stateNumTxt)
textGroup:insert(stateDescTxt)
 
currentState=1
renderState(1)


Replies

akao
User offline. Last seen 16 hours 50 min ago. Offline
Joined: 25 Feb 2011

Thanks for this! I have always heard that sprite saves texture memory, I had no idea!!