home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Enigma / Enigma-1.01-w7.exe / data / models.lua < prev    next >
Encoding:
Text File  |  2009-12-13  |  9.5 KB  |  333 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.11 2004/05/27 20:30:44 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, text)
  28.     local fontname = "levelmenu"
  29.     local scr      = video.GetScreen();
  30.     local d = scr:get_surface()
  31.  
  32.     local background = enigma.GetImage ("menu_bg", ".jpg")
  33.     local logo = enigma.GetImage("enigma_logo3")
  34.     local x = (d:width()  - logo:width())/2
  35.     local y = (d:height() - logo:height())/2
  36.     local gs = ecl.GS:new(d:size())
  37.     local font2 = enigma.GetFont("menufontsel")
  38.  
  39.     d:blit(gs, 0, 0, background)
  40.     d:blit(gs, x , y-logo:height(), logo)
  41.  
  42.     if text then
  43.         font2:render(d, (d:width() - font2:get_width(text))/2, y, text)
  44.     end
  45. --     font:render(d, x, y-10, strrep(".", 50))
  46. --     font2:render(d, x, y-10, strrep(".", percent/2))
  47.     scr:update_all();
  48.     scr:flush_updates();
  49.     gs:delete()
  50. end
  51.  
  52. modeltag = 0
  53. function unique_modelname()
  54.     modeltag = modeltag + 1
  55.     return "xmodel" .. modeltag
  56. end
  57.  
  58. -- Define a new image model.  'name' is the name of the model being created
  59. -- and 'opt' contains optional information about the image file.  The following
  60. -- fields can be set in 'opt':
  61. --
  62. --     * filename
  63. --     * xoff,yoff (hotspot coordinates inside the image)
  64. --
  65. -- Suitable default values are used for all options
  66. function def_image(name, opt)
  67.     opt = opt or {}
  68.     name = name or unique_modelname()
  69.     local fname = opt.filename or name
  70.     local err = display.DefineImage(name, fname, opt.xoff or 0, opt.yoff or 0, opt.padding or 0)
  71.     if err ~= 0 then
  72.         error ("Could not define model "..name..": error loading "..fname)
  73.     end
  74.     return name
  75. end
  76.  
  77. -- Define multiple image models at once.  Use the same options for all
  78. -- of them.
  79. function def_images(names, opt)
  80.     opt = opt or {}
  81.     for i,name in pairs(names) do
  82.     def_image(name,opt)
  83.     end
  84. end
  85.  
  86. -- Define many image models from one single big image file.
  87.  
  88. function def_subimages(name, options)
  89.     local opts = options or { }
  90.     local w = opts.w or 1
  91.     local h = opts.h or 1
  92.     local imgw = opts.imgw or TileSize
  93.     local imgh = opts.imgh or TileSize
  94.     local xoff = opts.xoff or 0
  95.     local yoff = opts.yoff or 0
  96.     local modelname = opts.modelname or name
  97.     local padding = options.padding or 0
  98.  
  99.     local imagelist={}
  100.     local cnt = 1
  101.     local r=ecl.Rect:new(0,0,0,0)
  102.     for x=1,w do
  103.         for y=1,h do
  104.             r.x,r.y,r.w,r.h = imgw*(x-1),imgh*(y-1),imgw,imgh
  105.             r.x = r.x + padding
  106.             r.y = r.y + padding
  107.             r.w = r.w - 2*padding
  108.             r.h = r.h - 2*padding
  109.             tinsert(imagelist, modelname..cnt)
  110.             err = display.DefineSubImage(modelname..cnt, name, xoff+padding,yoff+padding, r)
  111.             if err ~= 0 then
  112.                 error ("Could not define "..modelname..cnt..": error loading "..name)
  113.             end
  114.             cnt = cnt+1
  115.         end
  116.     end
  117.     r:delete()
  118.     return imagelist
  119. end
  120.  
  121. function map_tiles (imginfo, func)
  122.     local w = imginfo.w or 1
  123.     local h = imginfo.h or 1
  124.     local tilew = imginfo.tilew or TileSize
  125.     local tileh = imginfo.tileh or TileSize
  126.     local r=ecl.Rect:new(0,0,0,0)
  127.     local n=1
  128.     for y=0,h-1 do
  129.         for x=0,w-1 do
  130.             r.x,r.y = x*tilew,y*tileh
  131.             r.w,r.h = tilew, tileh
  132.             func(n, r)
  133.             n=n+1
  134.         end
  135.     end
  136.     r:delete()
  137. end
  138.  
  139.  
  140. function DefineTile (imagename, modelname, x, y)
  141.     local image = GetSurface (imagename)
  142.     local r=ecl.Rect:new(x * TileSize, y*TileSize, TileSize, TileSize)
  143.     local subsurface = CropSurface (image, r)
  144.     DefineImageModel (modelname, subsurface)
  145.     r:delete()
  146. end
  147.  
  148.  
  149. -- Generate multiple image models by tiling a big image into many
  150. -- smaller subimages.  The parameters are currently hardcoded, see
  151. -- "items.png" for an example image.
  152. function DefineTiles(imagename, modelnames)
  153.     local xoff = 0
  154.     local yoff = 0
  155.     local tilew = TileSize
  156.     local tileh = TileSize
  157.  
  158.     local image = GetSurface (imagename)
  159.     local imgw = image:width()
  160.     local r=ecl.Rect:new(0,0,TileSize,TileSize)
  161.     for i,mname in pairs(modelnames) do
  162.         r.x,r.y,r.w,r.h = xoff,yoff,tilew,tileh
  163. --        local subsurface = CropSurface (image, r)
  164. --        DefineImageModel (mname, subsurface)
  165.         DefineTile (imagename, mname, xoff/TileSize, yoff/TileSize)
  166.         xoff = xoff + tilew
  167.         if xoff >= imgw then
  168.             xoff = 0
  169.             yoff = yoff + tileh
  170.         end
  171.     end
  172.     r:delete()
  173. end
  174.  
  175.  
  176. function def_stone(name, shmodel, opt)
  177.     opt = opt or {}
  178.     shmodel = shmodel or "sh-solid"
  179.     opt.filename = opt.filename or name
  180.     display.DefineShadedModel(name, def_image(nil, opt), shmodel)
  181. end
  182.  
  183. function def_stone2(model, shmodel)
  184.     shmodel = shmodel or "sh-solid"
  185.     display.DefineShadedModel (model, model.."#", shmodel)
  186. end
  187.  
  188. function def_stones(names)
  189.     for i,name in names do def_stone(name) end
  190. end
  191. function def_roundstones(names)
  192.     for i,name in names do def_stone(name, "sh-round") end
  193. end
  194.  
  195. -- Define a shaded model named `name' with texture model `fg' and
  196. -- shadow model `bg'.  Both are real models (not names of image files), so
  197. -- you can combine animated images with shadows etc.
  198. function def_shmodel(name, fg, bg)
  199.     display.DefineShadedModel(name, fg, bg)
  200. end
  201.  
  202. function def_overlay(name, imglist)
  203.     display.DefineOverlayImage(name, getn(imglist), imglist)
  204. end
  205.  
  206. function def_solidstone(name, front)
  207.     def_shmodel(name, front, "sh-solid")
  208. end
  209. function def_roundstone(name, front)
  210.     def_shmodel(name, front, "sh-round")
  211. end
  212.  
  213. function def_floatingstone(name, front)
  214.     def_shmodel(name, front, "sh-floating")
  215. end
  216.  
  217. function def_alias(name, othername)
  218.     display.DefineAlias(name,othername)
  219. end
  220.  
  221. ----------------
  222. -- ANIMATIONS --
  223. ----------------
  224.  
  225. -- Generate a list of frame names by appending increasing numerical
  226. -- prefixes to a base name.  For example, 'framenames("hello", 1,2)'
  227. -- yields {"hello_0001", "hello_0002"}.
  228. -- [Filenames like this are created by Gimp's "Video/Split Image.."
  229. -- tool.]
  230.  
  231. function framenames(prefix, first, last)
  232.     local fn = {}
  233.     for i=first,last do
  234.     tinsert(fn, prefix .. format("_%04d", i))
  235.     end
  236.     return fn
  237. end
  238.  
  239. -- Build a list of frames from a list of model names and a constant
  240. -- duration.
  241. function buildframes(names, msec)
  242.     local a={}
  243.     for i,n in pairs(names) do a[i] = {n, msec} end
  244.     return a
  245. end
  246.  
  247. -- Build a list of frames from (1) a list of names and (2) a list of
  248. -- frame durations.  These two list are assumed to have the same
  249. -- number of entries, or, more generally, to have the same index set.
  250. function composeframes(namelist, mseclist)
  251.     local a={}
  252.     for i=1,getn(namelist) do a[i] = {namelist[i], mseclist[i]} end
  253.     return a
  254. end
  255.  
  256.  
  257. -- Given a list of frames, this function generates a new framelist
  258. -- with for a ping-pong style animation.  (For example, an "abcd"
  259. -- style animation would result in an "abcddcba"-style one.)
  260.  
  261. function pingpong(framelist)
  262.     local a=framelist
  263.     local n=getn(framelist)
  264.     for i = 0,n-1 do
  265.     a[n+i+1] = framelist[n-i]
  266.     end
  267.     return a
  268. end
  269.  
  270. function repeat_frames(framelist, blocksize, cnt)
  271.     local a={}
  272.     for i=1,getn(framelist),blocksize do
  273.     for j=1,cnt do
  274.         for k=i,i+blocksize-1 do
  275.         tinsert(a,framelist[k])
  276.         end
  277.     end
  278.     end
  279.     return a
  280. end
  281.  
  282. function repeatanim(framelist, cnt)
  283.     return repeat_frames(framelist, getn(framelist), cnt or 2)
  284. end
  285.  
  286. function reverseframes(framelist)
  287.     local a={}
  288.     for i=getn(framelist),1,-1 do
  289.     tinsert(a, framelist[i])
  290.     end
  291.     return a
  292. end
  293.  
  294. -- Define an animation from a list of images.  `frames' is a
  295. -- framelist, as built with `framenames()', but the model names in
  296. -- this list are interpreted as image filenames.
  297.  
  298. function def_anim_images(name, frames, opt)
  299.     opt = opt or {}
  300.     local loopbool = opt.loop and (opt.loop == 1 or opt.loop == true) or false
  301.     display.DefineAnim(name, loopbool)
  302.     for i=1,getn(frames) do
  303.     local frame=frames[i]
  304.     opt.filename = frame[1]
  305.     local immodel = def_image(nil, opt)
  306.     display.AddFrame(name, immodel, frame[2])
  307.     end
  308. end
  309.  
  310.  
  311. -- Define an animation from a list of models.
  312.  
  313. function def_anim(name, frames, loop)
  314.     local loopbool = loop and (loop == 1 or loop == true) or false
  315.     display.DefineAnim(name,loopbool)
  316.     for i=1,getn(frames) do
  317.     local frame = frames[i]
  318.     display.AddFrame(name, frame[1], frame[2])
  319.     end
  320. end
  321.  
  322. function NewAnim(name,opts) -- name, img, h,w,speed,pingpong, loop)
  323.     local imagefile = opts.img or name
  324.     local h=opts.h or 1
  325.     local w=opts.w or 1
  326.     local speed = opts.speed or 100
  327.     local loop=opts.loop or 0
  328.  
  329.     local frames = def_subimages(imagefile, {["h"]=h, ["w"]=w})
  330.     if opts.pingpong then frames=pingpong(frames) end
  331.     def_anim(name, buildframes(frames, speed), loop)
  332. end
  333.