home *** CD-ROM | disk | FTP | other *** search
/ GameStar Special 2004 August / GSSH0804.iso / Geschicklichkeit / Enigma / Enigma-081.exe / data / models.lua < prev    next >
Text File  |  2003-09-28  |  8KB  |  299 lines

  1. ------------------------------------------------------------------------
  2. -- Copyright (C) 2002,2003 Daniel Heck
  3. --
  4. -- This program is free software; you can redistribute it and/or
  5. -- modify it under the terms of the GNU General Public License
  6. -- as published by the Free Software Foundation; either version 2
  7. -- of the License, or (at your option) any later version.
  8. --
  9. -- This program is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. -- GNU General Public License for more details.
  13. --
  14. -- You should have received a copy of the GNU General Public License along
  15. -- with this program; if not, write to the Free Software Foundation, Inc.,
  16. -- 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  17. --
  18. -- $Id: models.lua,v 1.6.2.1 2003/09/28 20:53:50 dheck Exp $
  19. ------------------------------------------------------------------------
  20.  
  21. -- This file contains common routines for defining new Enigma models.
  22. -- It is used by all other model*.lua files.
  23.  
  24. ---------------
  25. -- FUNCTIONS --
  26. ---------------
  27. function Progress(percent)
  28.     local fontname = "levelmenu"
  29.     local scr      = video.GetScreen();
  30.     local d = scr:get_surface()
  31.  
  32.     local logo = enigma.GetImage("enigma_logo2")
  33.     local x = (d:width()  - logo:width())/2
  34.     local y = (d:height() - logo:height())/2
  35.     local gs = GS:new(d:size())
  36.     local font = enigma.GetFont("menufont")
  37.     local font2 = enigma.GetFont("menufontsel")
  38.  
  39.     d:blit(gs, x , y-logo:height(), logo)
  40.  
  41.     font:render(d, x, y-10, strrep(".", 50))
  42.     font2:render(d, x, y-10, strrep(".", percent/2))
  43.     scr:update_all();
  44.     scr:flush_updates();
  45.     gs:delete()
  46. end
  47.  
  48. modeltag = 0
  49. function unique_modelname()
  50.     modeltag = modeltag + 1
  51.     return "xmodel" .. modeltag
  52. end
  53.  
  54. -- Define a new image model.  'name' is the name of the model being created
  55. -- and 'opt' contains optional information about the image file.  The following
  56. -- fields can be set in 'opt':
  57. --
  58. --     * filename
  59. --     * xoff,yoff (hotspot coordinates inside the image)
  60. --
  61. -- Suitable default values are used for all options
  62. function def_image(name, opt)
  63.     opt = opt or {}
  64.     name = name or unique_modelname()
  65.     fname = (opt.filename or name)
  66.     opt = opt or {}
  67.     display.DefineImage(name, fname, opt.xoff or 0, opt.yoff or 0)
  68.     return name
  69. end
  70.  
  71. -- Define multiple image models at once.  Use the same options for all
  72. -- of them.
  73. function def_images(names, opt)
  74.     opt = opt or {}
  75.     for i,name in names do
  76.     def_image(name,opt)
  77.     end
  78. end
  79.  
  80. -- Define many image models from one single big image file.
  81.  
  82. function def_subimages(name, options)
  83.     local opts = options or { }
  84.     local w = opts.w or 1
  85.     local h = opts.h or 1
  86.     local imgw = opts.imgw or 32
  87.     local imgh = opts.imgh or 32
  88.     local xoff = opts.xoff or 0
  89.     local yoff = opts.yoff or 0
  90.     local modelname = opts.modelname or name
  91.  
  92.     local imagelist={}
  93.     local cnt = 1
  94.     local r=Rect:new(0,0,0,0)
  95.     for x=1,w do
  96.         for y=1,h do
  97.             r.x,r.y,r.w,r.h = imgw*(x-1),imgh*(y-1),imgw,imgh
  98.             tinsert(imagelist, modelname..cnt)
  99.             display.DefineSubImage(modelname..cnt, name, xoff,yoff, r)
  100.             cnt = cnt+1
  101.         end
  102.     end
  103.     r:delete()
  104.     return imagelist
  105. end
  106.  
  107. function map_tiles (imginfo, func)
  108.     local w = imginfo.w or 1
  109.     local h = imginfo.h or 1
  110.     local tilew = imginfo.tilew or 32
  111.     local tileh = imginfo.tileh or 32
  112.     local r=Rect:new(0,0,0,0)
  113.     local n=1
  114.     for y=0,h-1 do
  115.         for x=0,w-1 do
  116.             r.x,r.y = x*tilew,y*tileh
  117.             r.w,r.h = tilew, tileh
  118.             func(n, r)
  119.             n=n+1
  120.         end
  121.     end
  122.     r:delete()
  123. end
  124.  
  125.  
  126. -- Generate multiple image models by tiling a big image into many
  127. -- smaller subimages.  The parameters are currently hardcoded, see
  128. -- "items.png" for an example image.
  129. function def_tiles(big_image, modelnames)
  130.     local xoff = 0
  131.     local yoff = 0
  132.     local imgw = 640
  133.     local tilew = 32
  134.     local tileh = 32
  135.     local r=Rect:new(0,0,0,0)
  136.     for i,mname in modelnames do
  137.         r.x,r.y,r.w,r.h = xoff,yoff,tilew,tileh
  138.         display.DefineSubImage(mname, big_image, 0, 0, r)
  139.         xoff = xoff + tilew
  140.         if xoff >= imgw then
  141.             xoff = 0
  142.             yoff = yoff + tileh
  143.         end
  144.     end
  145.     r:delete()
  146. end
  147.  
  148. function def_stone(name, shmodel, opt)
  149.     opt = opt or {}
  150.     shmodel = shmodel or "sh-solid"
  151.     opt.filename = opt.filename or name
  152.     display.DefineShadedModel(name, def_image(nil, opt), shmodel)
  153. end
  154.  
  155. function def_stone2(model, shmodel)
  156.     shmodel = shmodel or "sh-solid"
  157.     display.DefineShadedModel (model, model.."#", shmodel)
  158. end
  159.  
  160. function def_stones(names)
  161.     for i,name in names do def_stone(name) end
  162. end
  163. function def_roundstones(names)
  164.     for i,name in names do def_stone(name, "sh-round") end
  165. end
  166.  
  167. -- Define a shaded model named `name' with texture model `fg' and
  168. -- shadow model `bg'.  Both are real models (not names of image files), so
  169. -- you can combine animated images with shadows etc.
  170. function def_shmodel(name, fg, bg)
  171.     display.DefineShadedModel(name, fg, bg)
  172. end
  173.  
  174. function def_overlay(name, imglist)
  175.     display.DefineOverlayImage(name, getn(imglist), imglist)
  176. end
  177.  
  178. function def_solidstone(name, front)
  179.     def_shmodel(name, front, "sh-solid")
  180. end
  181. function def_roundstone(name, front)
  182.     def_shmodel(name, front, "sh-round")
  183. end
  184.  
  185. function def_alias(name, othername)
  186.     display.DefineAlias(name,othername)
  187. end
  188.  
  189. ----------------
  190. -- ANIMATIONS --
  191. ----------------
  192.  
  193. -- Generate a list of frame names by appending increasing numerical
  194. -- prefixes to a base name.  For example, 'framenames("hello", 1,2)'
  195. -- yields {"hello_0001", "hello_0002"}.
  196. -- [Filenames like this are created by Gimp's "Video/Split Image.."
  197. -- tool.]
  198.  
  199. function framenames(prefix, first, last)
  200.     local fn = {}
  201.     for i=first,last do
  202.     tinsert(fn, prefix .. format("_%04d", i))
  203.     end
  204.     return fn
  205. end
  206.  
  207. -- Build a list of frames from a list of model names and a constant
  208. -- duration.
  209. function buildframes(names, msec)
  210.     local a={}
  211.     for i,n in names do a[i] = {n, msec} end
  212.     return a
  213. end
  214.  
  215. -- Build a list of frames from (1) a list of names and (2) a list of
  216. -- frame durations.  These two list are assumed to have the same
  217. -- number of entries, or, more generally, to have the same index set.
  218. function composeframes(namelist, mseclist)
  219.     local a={}
  220.     for i=1,getn(namelist) do a[i] = {namelist[i], mseclist[i]} end
  221.     return a
  222. end
  223.  
  224.  
  225. -- Given a list of frames, this function generates a new framelist
  226. -- with for a ping-pong style animation.  (For example, an "abcd"
  227. -- style animation would result in an "abcddcba"-style one.)
  228.  
  229. function pingpong(framelist)
  230.     local a=framelist
  231.     local n=getn(framelist)
  232.     for i = 0,n-1 do
  233.     a[n+i+1] = framelist[n-i]
  234.     end
  235.     return a
  236. end
  237.  
  238. function repeat_frames(framelist, blocksize, cnt)
  239.     local a={}
  240.     for i=1,getn(framelist),blocksize do
  241.     for j=1,cnt do
  242.         for k=i,i+blocksize-1 do
  243.         tinsert(a,framelist[k])
  244.         end
  245.     end
  246.     end
  247.     return a
  248. end
  249.  
  250. function repeatanim(framelist, cnt)
  251.     return repeat_frames(framelist, getn(framelist), cnt or 2)
  252. end
  253.  
  254. function reverseframes(framelist)
  255.     local a={}
  256.     for i=getn(framelist),1,-1 do
  257.     tinsert(a, framelist[i])
  258.     end
  259.     return a
  260. end
  261.  
  262. -- Define an animation from a list of images.  `frames' is a
  263. -- framelist, as built with `framenames()', but the model names in
  264. -- this list are interpreted as image filenames.
  265.  
  266. function def_anim_images(name, frames, opt)
  267.     opt = opt or {}
  268.     display.DefineAnim(name, opt.loop)
  269.     for i=1,getn(frames) do
  270.     local frame=frames[i]
  271.     opt.filename = frame[1]
  272.     local immodel = def_image(nil, opt)
  273.     display.AddFrame(name, immodel, frame[2])
  274.     end
  275. end
  276.  
  277.  
  278. -- Define an animation from a list of models.
  279.  
  280. function def_anim(name, frames, loop)
  281.     display.DefineAnim(name, loop)
  282.     for i=1,getn(frames) do
  283.     local frame = frames[i]
  284.     display.AddFrame(name, frame[1], frame[2])
  285.     end
  286. end
  287.  
  288. function NewAnim(name,opts) -- name, img, h,w,speed,pingpong, loop)
  289.     local imagefile = opts.img or name
  290.     local h=opts.h or 1
  291.     local w=opts.w or 1
  292.     local speed = opts.speed or 100
  293.     local loop=opts.loop or 0
  294.  
  295.     local frames = def_subimages(imagefile, {["h"]=h, ["w"]=w})
  296.     if opts.pingpong then frames=pingpong(frames) end
  297.     def_anim(name, buildframes(frames, speed), loop)
  298. end
  299.