This repository has been 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 pathcftImageMng.monkey
executable file
·368 lines (326 loc) · 11.6 KB
/
cftImageMng.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
'#DOCON#
#rem
Title: fantomEngine
Description: A 2D game framework for the Monkey programming language
Author: Michael Hartlef
Contact: michaelhartlef@gmail.com
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>
'header:The module cftImageMng contains several classes to manage loaded images inside your game.
Import fantomEngine
'***************************************
'summery:The ftImage class contains a mojo image and some information about.
Class ftImage
'#DOCOFF#
Field img:Image= Null
Field atlas:ftImage = Null
Field flags:Int = Image.DefaultFlags
Field path:String = ""
Field imageNode:list.Node<ftImage> = Null
Field frameWidth:Int = -1
Field frameHeight:Int = -1
Field frameCount:Int = -1
Field frameStartX:Int = -1
Field frameStartY:Int = -1
Field isGrabed:Bool = False
Field isAtlas:Bool = False
Field engine:ftEngine
'#DOCON#
'------------------------------------------
'summery:Returns the frame count of a ftImage.
Method GetFrameCount:Int()
Return Self.frameCount
End
'------------------------------------------
'summery:Returns the frame height of a ftImage.
Method GetFrameHeight:Int()
Return Self.frameHeight
End
'------------------------------------------
'summery:Returns the frame width of a ftImage.
Method GetFrameWidth:Int()
Return Self.frameWidth
End
'------------------------------------------
Method GetImage:Image()
'summery:Returns the image of a ftImage.
Return Self.img
End
'------------------------------------------
'summery:Returns the path (filename) of a ftImage.
Method GetPath:String()
Return Self.path
End
'------------------------------------------
'summery:Reloads an image.
Method ReLoad:Void()
If path.Length() > 1 Or Self.isGrabed = True Then
If Self.isGrabed = True Or Self.path.Length()> 1 then
Self.img.Discard()
If Self.isGrabed = True Then
Self.img = atlas.img.GrabImage( frameStartX,frameStartY,frameWidth,frameHeight,frameCount,flags )
Else
If frameCount = 1 Then
Self.img = mojo.LoadImage(path, frameCount, flags)
Else
Self.img = mojo.LoadImage(path, frameWidth, frameHeight, frameCount, flags)
Endif
Endif
If img <> Null Then
If img.HandleX = 0.5 And img.HandleY = 0.5
img.SetHandle(img.Width()/2, img.Height()/2)
Endif
Endif
Endif
Endif
End
'------------------------------------------
'summery:Removes an ftImage.
Method Remove:Void(discard:Bool = False)
If discard = True Then Self.img.Discard()
Self.img = Null
Self.engine = Null
Self.imageNode.Remove()
End
'------------------------------------------
'summery:Sets the path (filename) of a ftImage.
Method SetPath:String(filePath:String)
Self.path = filePath
End
End
'***************************************
'summery:The ftImageManager class handles all images for fantomEngine.
Class ftImageManager
'#DOCOFF#
Field imageList := New List<ftImage>
Field engine:ftEngine
'#DOCON#
'-----------------------------------------------------------------------------
'summery:Creates an ftImage by grabbing it from a ftImage atlas.
Method GrabImage:ftImage(atlas:ftImage, frameStartX:Int,frameStartY:Int,frameWidth:Int,frameHeight:Int,frameCount:Int, flags:Int=Image.DefaultFlags)
Local tmpImg:Image = Null
Local newImage:ftImage = Null
If newImage = Null Then
newImage = New ftImage
newImage.engine = Self.engine
newImage.atlas = atlas
newImage.flags = flags
newImage.frameCount = frameCount
newImage.frameHeight = frameHeight
newImage.frameWidth = frameWidth
newImage.frameStartX = frameStartX
newImage.frameStartY = frameStartY
newImage.isGrabed = True
' Mark the atlas is being one
atlas.isAtlas = True
tmpImg = atlas.img.GrabImage( frameStartX,frameStartY,frameWidth,frameHeight,frameCount,flags )
If tmpImg <> Null Then
If tmpImg.HandleX = 0.5 And tmpImg.HandleY = 0.5
tmpImg.SetHandle(tmpImg.Width()/2, tmpImg.Height()/2)
Endif
Endif
#If CONFIG="debug"
If tmpImg = Null Then Error ("~n~nError in file cftImageMng.monkey, Method ftImageManager.GrabImage~n~nCould not grab image from atlas~n~n")
#End
newImage.img = tmpImg
newImage.imageNode = Self.imageList.AddLast(newImage)
Endif
Return newImage
End
'-----------------------------------------------------------------------------
'summery:Creates an ftImage by grabbing it from a Image atlas.
Method GrabImage:ftImage(atlas:Image, frameStartX:Int,frameStartY:Int,frameWidth:Int,frameHeight:Int,frameCount:Int, flags:Int=Image.DefaultFlags)
Local tmpImg:Image = Null
Local newImage:ftImage = Null
If newImage = Null Then
newImage = New ftImage
newImage.engine = Self.engine
'newImage.atlas = atlas
newImage.flags = flags
newImage.frameCount = frameCount
newImage.frameHeight = frameHeight
newImage.frameWidth = frameWidth
newImage.frameStartX = frameStartX
newImage.frameStartY = frameStartY
newImage.isGrabed = True
' Mark the atlas is being one
For Local tmpImgNode := Eachin Self.imageList
If tmpImgNode.img = atlas Then
tmpImgNode.isAtlas = True
newImage.atlas = tmpImgNode
Exit
Endif
Next
tmpImg = atlas.GrabImage( frameStartX,frameStartY,frameWidth,frameHeight,frameCount,flags )
If tmpImg <> Null Then
If tmpImg.HandleX = 0.5 And tmpImg.HandleY = 0.5
tmpImg.SetHandle(tmpImg.Width()/2, tmpImg.Height()/2)
Endif
Endif
#If CONFIG="debug"
If tmpImg = Null Then Error ("~n~nError in file cftImageMng.monkey, Method ftImageManager.GrabImage~n~nCould not grab image from atlas~n~n")
#End
newImage.img = tmpImg
newImage.imageNode = Self.imageList.AddLast(newImage)
Endif
Return newImage
End
'-----------------------------------------------------------------------------
#Rem
'summery:Creates an ftImage from an image.
#End
Method LoadImage:ftImage(image:Image)
Local tmpImg:Image = Null
Local newImage:ftImage = Null
For Local tmpImgNode := Eachin Self.imageList
If tmpImgNode.img = image Then
newImage = tmpImgNode
Exit
Endif
Next
If newImage = Null Then
newImage = New ftImage
newImage.engine = Self.engine
newImage.path = ""
newImage.flags = Image.DefaultFlags
newImage.frameCount = 1
newImage.img = image
newImage.imageNode = Self.imageList.AddLast(newImage)
Endif
If newImage.img <> Null Then
If newImage.img.HandleX = 0.5 And newImage.img.HandleY = 0.5
newImage.img.SetHandle(newImage.img.Width()/2, newImage.img.Height()/2)
Endif
Endif
Return newImage
End
'-----------------------------------------------------------------------------
#Rem
'summery:Loads an image like mogo.LoadImage.
#End
Method LoadImage:ftImage(path:String, frameCount:Int=1, flags:Int=Image.DefaultFlags)
Local tmpImg:Image = Null
Local newImage:ftImage = Null
For Local tmpImgNode := Eachin Self.imageList
If tmpImgNode.path = path Then
newImage = tmpImgNode
Exit
Endif
Next
If newImage = Null Then
newImage = New ftImage
newImage.engine = Self.engine
newImage.path = path
newImage.flags = flags
newImage.frameCount = frameCount
tmpImg = mojo.LoadImage(path, frameCount, flags)
#If CONFIG="debug"
If tmpImg = Null Then Error ("~n~nError in file cftImageMng.monkey, Method ftImageManager.LoadImage~n~nCould not load image~n~n"+path)
#End
newImage.img = tmpImg
newImage.imageNode = Self.imageList.AddLast(newImage)
Endif
If newImage.img <> Null Then
If newImage.img.HandleX = 0.5 And newImage.img.HandleY = 0.5
newImage.img.SetHandle(newImage.img.Width()/2, newImage.img.Height()/2)
Endif
Endif
Return newImage
End
'-----------------------------------------------------------------------------
#Rem
'summery:Loads an image like mogo.LoadImage.
#End
Method LoadImage:ftImage(path:String, frameWidth:Int, frameHeight:Int, frameCount:Int, flags:Int=Image.DefaultFlags)
Local tmpImg:Image = Null
Local newImage:ftImage = Null
For Local tmpImgNode := Eachin Self.imageList
If tmpImgNode.path = path Then
newImage = tmpImgNode
Exit
Endif
Next
If newImage = Null Then
newImage = New ftImage
newImage.engine = Self.engine
newImage.path = path
newImage.flags = flags
newImage.frameCount = frameCount
newImage.frameHeight = frameHeight
newImage.frameWidth = frameWidth
tmpImg = mojo.LoadImage(path, frameWidth, frameHeight, frameCount, flags)
#If CONFIG="debug"
If tmpImg = Null Then Error ("~n~nError in file cftImageMng.monkey, Method ftImageManager.LoadImage~n~nCould not load image~n~n"+path)
#End
newImage.img = tmpImg
newImage.imageNode = Self.imageList.AddLast(newImage)
Endif
If newImage.img <> Null Then
If newImage.img.HandleX = 0.5 And newImage.img.HandleY = 0.5
newImage.img.SetHandle(newImage.img.Width()/2, newImage.img.Height()/2)
Endif
Endif
Return newImage
End
'------------------------------------------
#Rem
'summery:Reload all images.
#End
Method ReLoadAllImages:Void()
' Reload sprite atlas
For Local item:= Eachin Self.imageList.Backwards()
If item.isAtlas = True Then item.ReLoad()
Next
' Reload grabbed images
For Local item:= Eachin Self.imageList.Backwards()
If item.isGrabed = True Then item.ReLoad()
Next
' Reload single images
For Local item:= Eachin Self.imageList.Backwards()
If item.isGrabed = False And item.isAtlas = False Then item.ReLoad()
Next
End
'------------------------------------------
#Rem
'summery:Removes all images from the image handler.
#End
Method RemoveAllImages:Void(discard:Bool = False)
For Local item:= Eachin Self.imageList.Backwards()
item.Remove(discard)
Next
End
'-----------------------------------------------------------------------------
#Rem
'summery:Removes an image from the image handler by the given Image handle.
#End
Method RemoveImage:Void(image:Image, discard:Bool = False)
For Local item := Eachin Self.imageList.Backwards()
If item.img = image Then
item.Remove(discard)
Exit
Endif
Next
End
'-----------------------------------------------------------------------------
#Rem
'summery:Removes an image from the image handler by the given filename.
#End
Method RemoveImage:Void(filepath:String, discard:Bool = False)
For Local item := Eachin Self.imageList.Backwards()
If item.path = filepath Then
item.Remove(discard)
Exit
Endif
Next
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