File: models.lua

package info (click to toggle)
enigma 1.20-dfsg.1-2.2
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 64,696 kB
  • sloc: xml: 153,614; cpp: 63,581; ansic: 31,088; sh: 4,825; makefile: 1,858; yacc: 288; perl: 84; sed: 16
file content (421 lines) | stat: -rw-r--r-- 13,940 bytes parent folder | download | duplicates (4)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
------------------------------------------------------------------------
-- Copyright (C) 2002,2003 Daniel Heck
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
--
------------------------------------------------------------------------

-- This file contains common routines for defining new Enigma models.
-- It is used by all other model*.lua files.

-- Display the loading progress
function Progress(percent, text)
    local fontname   = "levelmenu"
    local scr        = video.GetScreen()
    local d          = scr:get_surface()
    local background = enigma.GetImage ("menu_bg", ".jpg")
    local logo       = enigma.GetImage("enigma_logo3")
    local x          = (d:width()  - logo:width())/2
    local y          = (d:height() - logo:height())/2
    local gs         = ecl.GS:new(d:size())
    local font2      = enigma.GetFont("menufontsel")

    d:blit(gs, 0, 0, background) -- offset missing!
    d:blit(gs, x , y-logo:height(), logo)

    if text then
        font2:render(d, (d:width() - font2:get_width(text))/2, y, text)
    end
--     font:render(d, x, y-10, strrep(".", 50))
--     font2:render(d, x, y-10, strrep(".", percent/2))
    scr:update_all();
    scr:flush_updates();
    gs:delete()
end

-- Return an unique modelname.
modeltag = 0
function UniqueModelname()
    modeltag = modeltag + 1
    return "xmodel" .. modeltag
end

--------------------------------------
--   Basic Single Image Functions   --
--------------------------------------

-- Define a new image model.
-- * 'name' is the name of the model being created
-- * 'opt' contains optional information about the image file.
--   The following fields can be set in 'opt':
--    * filename
--    * xoff,yoff (hotspot coordinates inside the image)
-- Suitable default values are used for all options
function DefImage(name, opt)
    opt = opt or {}
    name = name or UniqueModelname()
    local fname = opt.filename or name
    local err = display.DefineImage(name, fname, opt.xoff or 0, opt.yoff or 0, opt.padding or 0)
    if err ~= 0 then
        error ("Could not define model "..name..": error loading "..fname)
    end
    return name
end

-- Define multiple image models at once.  Use the same options for all
-- of them. The namelist contains the modelnames to be created.
function DefImages(namelist, opt)
    opt = opt or {}
    for i,name in pairs(namelist) do
	    DefImage(name,opt)
    end
end

-------------------------------------
--   Basic Subdivision Functions   --
-------------------------------------

-- Define many image models from one single big image file.
-- Inputarguments are the imagefilename and the options.
-- The defined models are named 'modelname1', 'modelname2' ...
function DefSubimages(name, options)
    local opts = options or { }
    local w = opts.w or 1
    local h = opts.h or 1
    local imgw = opts.imgw or TileSize
    local imgh = opts.imgh or TileSize
    local xoff = opts.xoff or 0
    local yoff = opts.yoff or 0
    local modelname = opts.modelname or name
    local padding = options.padding or 0
    local cnt = options.startindex or 1
    local imagelist = {}
    local r = ecl.Rect:new(0,0,0,0)
    for x=1,w do
        for y=1,h do
            r.x,r.y,r.w,r.h = imgw*(x-1),imgh*(y-1),imgw,imgh
            r.x = r.x + padding
            r.y = r.y + padding
            r.w = r.w - 2*padding
            r.h = r.h - 2*padding
            table.insert(imagelist, modelname..cnt)
            err = display.DefineSubImage(modelname..cnt, name, xoff+padding,yoff+padding, r)
            if err ~= 0 then
                error ("Could not define "..modelname..cnt..": error loading "..name)
            end
            cnt = cnt+1
        end
    end
    r:delete()
    return imagelist
end

-- Generates an image model by cutting it out of a larger image.
-- Arguments are the imagefilename, the modelname and the position
-- where to cut a square with length TileSize. The position is
-- measured in multiples of TileSize.
function DefTile(imagename, modelname, x, y)
    local image = GetSurface (imagename)
    local r=ecl.Rect:new(x * TileSize, y*TileSize, TileSize, TileSize)
    local subsurface = CropSurface (image, r)
    DefineImageModel (modelname, subsurface)
    r:delete()
end

-- Generate multiple image models by tiling a big image into many
-- smaller subimages.  The parameters are currently hardcoded.
-- Arguments are the filename of the image and a list of
-- modelnames to be generated out of this image.
function DefTiles(imagename, modelnames)
    local xoff = 0
    local yoff = 0
    local tilew = TileSize
    local tileh = TileSize

    local image = GetSurface (imagename)
    local imgw = image:width()
    local r=ecl.Rect:new(0,0,TileSize,TileSize)
    for i,mname in pairs(modelnames) do
        r.x,r.y,r.w,r.h = xoff,yoff,tilew,tileh
        DefTile(imagename, mname, xoff/TileSize, yoff/TileSize)
        xoff = xoff + tilew
        if xoff >= imgw then
            xoff = 0
            yoff = yoff + tileh
        end
    end
    r:delete()
end

-----------------------------------
--   Basic Animation Functions   --
-----------------------------------

-- Generate a list of frame names by appending increasing numerical
-- prefixes to a base name.  For example, 'FrameNames("hello", 1,2)'
-- yields {"hello_0001", "hello_0002"}.
-- [Filenames like this are created by Gimp's "Video/Split Image.."
-- tool.]

-- DEPRECEATED

-- function FrameNames(prefix, first, last)
--     local fn = {}
--     for i=first,last do
-- 	    table.insert(fn, prefix .. format("_%04d", i))
--     end
--     return fn
-- end

-- function AddFrameNames(fn, prefix, first, last)
--     for i=first,last do
-- 	    table.insert(fn, i, prefix .. format("_%04d", i))
--     end
-- end

-- Build a list of frames from a list of model names and a constant
-- duration.
function BuildFrames(names, msec)
    local a={}
    for i,n in pairs(names) do a[i] = {n, msec} end
    return a
end

-- Build a list of frames from (1) a list of names and (2) a list of
-- frame durations.  These two list are assumed to have the same
-- number of entries, or, more generally, to have the same index set.
function ComposeFrames(namelist, mseclist)
    local a={}
    for i=1,getn(namelist) do a[i] = {namelist[i], mseclist[i]} end
    return a
end

-- Given a list of frames, this function generates a new framelist
-- with for a ping-pong style animation.  (For example, an "abcd"
-- style animation would result in an "abcddcba"-style one.)
function PingPong(framelist)
    local a=framelist
    local n=getn(framelist)
    for i = 0,n-1 do
	    a[n+i+1] = framelist[n-i]
    end
    return a
end

-- Given a list of frames, this function generates a new framelist
-- with repeated framesequence.  (For example, an "abcd"
-- style animation with a cnt of 3 would result in an "abcdabcdabcd"-style one.)
function RepeatFrames(framelist, blocksize, cnt)
    local a={}
    for i=1,getn(framelist),blocksize do
	    for j=1,cnt do
	        for k=i,i+blocksize-1 do
		        table.insert(a,framelist[k])
	        end
	    end
    end
    return a
end

-- Given a list of frames, this function generates a new framelist
-- with repeated framesequence.  (For example, an "abcd"
-- style animation with a cnt of 3 would result in an "abcdabcdabcd"-style one.)
function RepeatAnim(framelist, cnt)
    return RepeatFrames(framelist, getn(framelist), cnt or 2)
end

-- Given a list of frames, this function generates a new framelist
-- with reversed frame order.  (For example, an "abcd"
-- style animation would result in an "dcba"-style one.)
function ReverseFrames(framelist)
    local a={}
    for i=getn(framelist),1,-1 do
	    table.insert(a, framelist[i])
    end
    return a
end

-- Define an animation from a list of images.  `frames' is a
-- framelist, as built with `FrameNames()', but the model names in
-- this list are interpreted as image filenames.
function DefAnimImages(name, frames, opt)
    opt = opt or {}
    local loopbool = opt.loop and true or false
    display.DefineAnim(name, loopbool)
    for i=1,getn(frames) do
	    local frame=frames[i]
	    opt.filename = frame[1]
	    local immodel = DefImage(nil, opt)
	    display.AddFrame(name, immodel, frame[2])
    end
end

-- Define an animation from a list of models.
-- * 'name' is the name of the animation being created
-- * 'frames' is the list of modelnames used
function DefAnim(name, frames, loop)
    local loopbool = loop and true or false
    display.DefineAnim(name,loopbool)
    for i=1,getn(frames) do
	    local frame = frames[i]
	    display.AddFrame(name, frame[1], frame[2])
    end
end

-- Define a new Animation.
-- * 'name' is the name of the animation being created
-- * 'opt' contains optional information about the animation.
function NewAnim(name,opts)
    local imagefile = opts.img or name
    local h = opts.h or 1
    local w = opts.w or 1
    local speed = opts.speed or 100
    local loop = opts.loop or 0

    local frames = DefSubimages(imagefile, {["h"]=h, ["w"]=w})
    if opts.pingpong then frames=PingPong(frames) end
    DefAnim(name, BuildFrames(frames, speed), loop)
end

--------------------------
--   Sprite functions   --
--------------------------

-- padding is calculated as:
-- padding = (1.25 - 2*Actorradius)/2
function SpriteImages(spriteimg, n, offsetfactor, padding)
    local factor = offsetfactor or 0.5
    local offset = -SpriteSize * factor
    local pad = math.floor( (padding or 0) * TileSize )
    return DefSubimages(spriteimg,
                         {h = n, imgw=SpriteSize, imgh=SpriteSize,
                             xoff = offset, yoff = offset, padding=pad})
end

function SpriteImage(spriteimg, offsetfactor, padding)
    local factor = offsetfactor or 0.5
    local offset = -SpriteSize * factor
    local pad = math.floor( (padding or 0) * TileSize )
    return DefImage(spriteimg, {xoff = offset, yoff = offset, padding=pad})
end

function SpriteWithShadow()

end

function SpriteAnim(name, images, shadows, framelen, loop)
    local frames={}
    local nframes = getn(images)
    for i=1,getn(images) do
        DefShModel(name..i, images[i], shadows[mod (i, getn(shadows))] )
        table.insert(frames, name..i)
    end
    DefAnim (name, BuildFrames(frames, framelen), loop)
end

function Sprite(descr)
    local imgfile = descr.imgfile or descr.name
    local loop    = descr.loop or false
    local img     = SpriteImages(imgfile, descr.nimages, 0.5, descr.padding)
    DefAnim(descr.name, BuildFrames(img, descr.framelen), loop)
end

---------------------------------
--   Floor related Functions   --
---------------------------------

-- Defines a random floor tile out of a given list.
function DefRandFloor(name, imagelist)
    display.DefineRandModel(name, getn(imagelist), imagelist)
end

---------------------------------
--   Stone related Functions   --
---------------------------------

-- Define a stone together with its shadow model:
function DefStone(name, shmodel, opt)
    opt = opt or {}
    shmodel = shmodel or "sh_solid"
    opt.filename = opt.filename or name
    display.DefineShadedModel(name, DefImage(nil, opt), shmodel)
end

-- Define a shaded model named `name' with texture model `fg' and
-- shadow model `bg'.  Both are real models (not names of image files), so
-- you can combine animated images with shadows etc.
function DefShModel(name, fg, bg)
    display.DefineShadedModel(name, fg, bg)
end

function DefSolidStone(name, front)
    DefShModel(name, front, "sh_solid")
end

function DefRoundStone(name, front)
    DefShModel(name, front, "sh_round")
end

function DefFloatingStone(name, front)
    DefShModel(name, front, "sh_floating")
end

-- Animated Stones --
function DefSolidStoneWithAnim(name, npictures, frametime)
    local n=DefSubimages(name, {h=npictures})
    DefAnim(name.."-animfg", BuildFrames(n,frametime))
    DefSolidStone(name.."-anim", name.."-animfg")
    DefSolidStone(name, n[1])
end

function DefRoundStoneWithAnim(name, npictures, frametime)
    local n=DefSubimages(name, {h=npictures})
    DefAnim(name.."-animfg", BuildFrames(n,frametime))
    DefRoundStone(name.."-anim", name.."-animfg")
    DefRoundStone(name, n[1])
end

-- Aliases --
function DefAlias(name, othername)
    display.DefineAlias(name,othername)
end

-- Define an overlay --
function DefOverlay(name, imglist)
    display.DefineOverlayImage(name, getn(imglist), imglist)
end

-- Define a multiple composite --
-- This function will compose a list of images
-- and store the result in "name".
-- As this function needs lots of memory, it's use is deprecated.
function DefMultipleComposite(name, imglist)
    -- Assert imglist >= 1!
    local listsize = table.getn(imglist)
    if listsize == 0 then
        DefAlias(name, "invisible")
    elseif listsize == 1 then
        DefAlias(name, imglist[1])
    elseif listsize == 2 then
        display.DefineComposite(name, imglist[1], imglist[2])
    elseif listsize > 2 then
        display.DefineComposite(name.."-2", imglist[1], imglist[2])
        for i = 3, table.getn(imglist)-1 do
            display.DefineComposite(name.."-"..i, name.."-"..(i-1), imglist[i])
        end
        display.DefineComposite(name, name.."-"..(table.getn(imglist)-1), imglist[table.getn(imglist)])
    end
end