This repository was archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcftSpriteAtlas.monkey
executable file
·213 lines (197 loc) · 7.19 KB
/
cftSpriteAtlas.monkey
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'#DOCON#
#rem
Title: fantomEngine
Description: A 2D game framework for the Monkey programming language
Author: Michael Hartlef
Contact: [email protected]
Website: http://www.fantomgl.com
License: MIT
#End
'nav:<blockquote><nav><b>fantomEngine documentation</b> | <a href="index.html">Home</a> | <a href="classes.html">Classes</a> | <a href="3rdpartytools.html">3rd party tools</a> | <a href="examples.html">Examples</a> | <a href="changes.html">Changes</a></nav></blockquote>
#Rem
'header:The class [b]ftSpriteAtlas[/b] manages spritesheets inside the fantomEngine. It reduces the call to the device storage by imprting all relevant data into memory.
#End
Import fantomEngine
'#DOCOFF#
'***************************************
Class ftAtlasSubImage
Field name:String
Field img:Image
Field tpXPos:Int = 0
Field tpYPos:Int = 0
Field tpWidth:Int = 0
Field tpHeight:Int = 0
Field isRotated:Bool = False
Field angleOffset:Float = 0.0
End
'#DOCON#
'***************************************
Class ftSpriteAtlas
'#DOCOFF#
Field atlas:Image = Null
Field name:String
Field tpStringFromFile:String
Field tpAllStrings:String[]
Field engine:ftEngine
Field imgMap:StringMap<ftAtlasSubImage> = Null
'------------------------------------------
Method _Print:Void()
For Local item:= Eachin Self.imgMap
Print(item.Key() + ";" + ftAtlasSubImage(item.Value()).name)
Next
End
'------------------------------------------
Method _Parse:Void()
Local s:String
Local tpasLen:Int
tpasLen = tpAllStrings.Length()
For Local count:Int = 0 To (tpasLen-1)
s = String(tpAllStrings[count]).ToLower().Trim()
If s.Contains("rotate:") Then
Local si := New ftAtlasSubImage
'** Get name
si.name = String(tpAllStrings[count-1]).ToLower()
'** Get rotation flag
Local strRot:String = tpAllStrings[count]
strRot = strRot.Replace("rotate:","").Trim()
If strRot.ToLower = "true" Then
si.isRotated = True
si.angleOffset = -90.0
Endif
'** Get X, Y
Local strXY:String = tpAllStrings[count+1]
strXY = strXY.Replace("xy:","").Trim()
Local strXYsplit:String[] = strXY.Split(",")
si.tpXPos = Int(strXYsplit[0])
si.tpYPos = Int(strXYsplit[1])
'** Get Width, Height
Local strWH:String = tpAllStrings[count+2]
strWH = strWH.Replace("size:","").Trim()
Local strWHsplit:String[] = strWH.Split(",")
si.tpWidth = Int(strWHsplit[0])
si.tpHeight = Int(strWHsplit[1])
'** Grab the sub image
If strRot.ToLower = "true" Then
si.img = Self.atlas.GrabImage( si.tpXPos,si.tpYPos,si.tpHeight,si.tpWidth,1,Image.MidHandle )
Else
si.img = Self.atlas.GrabImage( si.tpXPos,si.tpYPos,si.tpWidth,si.tpHeight,1,Image.MidHandle )
Endif
si.img.SetHandle(si.img.Width()/2, si.img.Height()/2)
'** Add subimage to the image map
Self.imgMap.Add(si.name, si)
Endif
Next
End
'#DOCON#
'------------------------------------------
'summery:Returns the angle offset of an image.
Method GetAngleOffset:Float(imageName:String)
Local si := Self.imgMap.Get(imageName.ToLower())
If si <> Null Then
Return si.angleOffset
Endif
Return 0.0
End
'------------------------------------------
'summery:Returns the height of an image.
Method GetHeight:Int(imageName:String)
Local si := Self.imgMap.Get(imageName.ToLower())
If si <> Null Then
Return si.tpHeight
Endif
Return 0
End
'------------------------------------------
'summery:Returns the image with the given name.
Method GetImage:Image(imageName:String)
Local img:Image = Null
Local si := Self.imgMap.Get(imageName.ToLower())
If si <> Null Then
img = si.img
Endif
Return img
End
'------------------------------------------
'summery:Returns the height of the subimage with the given name.
Method GetImageHeight:Float(imageName:String)
Local retVal:Float = 0.0
Local si := Self.imgMap.Get(imageName.ToLower())
If si <> Null Then
retVal = si.tpHeight
Endif
Return retVal
End
'------------------------------------------
'summery:Returns the width of the subimage with the given name.
Method GetImageWidth:Float(imageName:String)
Local retVal:Float = 0.0
Local si := Self.imgMap.Get(imageName.ToLower())
If si <> Null Then
retVal = si.tpWidth
Endif
Return retVal
End
'------------------------------------------
'summery:Returns the number of images in the sprite atlas.
Method GetImageCount:Int()
Return Self.imgMap.Count()
End
'------------------------------------------
'summery:Returns TRUE if the image with the given name is rotated inside the sprite atlas.
Method GetRotated:Bool(imageName:String)
Local si := Self.imgMap.Get(imageName.ToLower())
If si <> Null Then
Return si.isRotated
Endif
Return False
End
'------------------------------------------
'summery:Returns the width of an image.
Method GetWidth:Int(imageName:String)
Local si := Self.imgMap.Get(imageName.ToLower())
If si <> Null Then
Return si.tpWidth
Endif
Return 0
End
'------------------------------------------
'summery:Loads a sprite atlas from the given image and data file name.
Method Load:Void(imgName:String, dataName:String)
tpStringFromFile = LoadString(dataName)
If tpStringFromFile.Length() > 0 Then
'tpAllStrings = tpStringFromFile.Split(String.FromChar(10))
tpAllStrings = tpStringFromFile.Split(String.FromChar(13)+String.FromChar(10))
If tpAllStrings.Length() < 2 then
tpAllStrings = tpStringFromFile.Split(String.FromChar(10))
Endif
atlas = LoadImage(imgName)
name = dataName
imgMap = New StringMap<ftAtlasSubImage>
Self._Parse()
Endif
End
'------------------------------------------
'summery:Remove a sprite atlas. Set discard to TRUE if you want the corresponding images to be discarded.
Method Remove:Void(discard:Bool = False)
If discard = True Then Self.atlas.Discard()
Self.atlas = Null
Self.engine = Null
Self.atlas = Null
For Local item:= Eachin Self.imgMap
If discard = True Then
ftAtlasSubImage(item.Value()).img.Discard()
Else
ftAtlasSubImage(item.Value()).img = Null
Endif
Next
Self.imgMap.Clear()
End
End
#rem
footer:This fantomEngine framework is released under the MIT license:
[quote]Copyright (c) 2011-2016 Michael Hartlef
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software, and to permit persons to whom the software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[/quote]
#end