Advertisement
Drablos

Codea Sounds

Jan 10th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.74 KB | None | 0 0
  1. buttons = {}
  2. soundtypes = {
  3. {"SOUND_BLIT", SOUND_BLIT},
  4. {"SOUND_EXPLODE", SOUND_EXPLODE},
  5. {"SOUND_HIT", SOUND_HIT},
  6. {"SOUND_JUMP", SOUND_JUMP},
  7. {"SOUND_PICKUP", SOUND_PICKUP},
  8. {"SOUND_SHOOT", SOUND_SHOOT},
  9. {"SOUND_RANDOM", SOUND_RANDOM},
  10. }
  11.  
  12. function setup()
  13.     soundtype = readLocalData("soundtype", 1)
  14.     seed = readLocalData("seed", 1)
  15.    
  16.     font = Font()
  17.    
  18.     table.insert(buttons, Button("PLAY SOUND", WIDTH/2, HEIGHT/2 - 100, 210, 50))
  19.     table.insert(buttons, Button("UP", WIDTH/2 + 250, HEIGHT/2 + 40, 100, 50))
  20.     table.insert(buttons, Button("DOWN", WIDTH/2 + 250, HEIGHT/2 - 20, 100, 50))
  21.     table.insert(buttons, Button("PREV", WIDTH/2 - 260, HEIGHT/2 + 40, 100, 50))
  22.     table.insert(buttons, Button("NEXT", WIDTH/2 - 260, HEIGHT/2 - 20, 100, 50))
  23.     table.insert(buttons, Button("PRINT", WIDTH/2, HEIGHT/2 - 170, 100, 50))
  24.    
  25. end
  26.  
  27. function draw()
  28.     background(0, 0, 0)
  29.    
  30.     stroke(255, 255, 255, 255)
  31.     strokeWidth(1)
  32.     noSmooth()
  33.    
  34.     font:drawstring(soundtypes[soundtype][1], WIDTH/2 - 200, HEIGHT/2)
  35.     font:drawstring(""..seed, WIDTH/2 + 100, HEIGHT/2)
  36.    
  37.     for i,v in ipairs(buttons) do
  38.         v:draw()
  39.     end
  40. end
  41.  
  42. function touched(t)
  43.     if t.state == BEGAN then
  44.         for i,v in ipairs(buttons) do
  45.             if t.x > v.px - v.w/2 and t.x < v.px + v.w/2 and t.y > v.py - v.h/2 and t.y < v.py + v.h/2 then
  46.                 v:touched(t)
  47.             end
  48.         end
  49.     end
  50. end
  51.  
  52. function buttonpressed(l)
  53.     if l == "UP" then
  54.         seed = seed + 1
  55.     elseif l == "DOWN" then
  56.         seed = seed - 1
  57.     elseif l == "NEXT" then
  58.         soundtype = soundtype + 1
  59.         if soundtype > table.maxn(soundtypes) then
  60.             soundtype = 1
  61.         end
  62.     elseif l == "PREV" then
  63.         soundtype = soundtype - 1
  64.         if soundtype < 1 then
  65.             soundtype = table.maxn(soundtypes)
  66.         end
  67.     elseif l == "PLAY SOUND" then
  68.         sound(soundtypes[soundtype][2], seed)
  69.         saveLocalData("soundtype", soundtype)
  70.         saveLocalData("seed", seed)
  71.     elseif l == "PRINT" then
  72.         print("sound("..soundtypes[soundtype][1]..", "..seed..")")
  73.         saveLocalData("soundtype", soundtype)
  74.         saveLocalData("seed", seed)
  75.     end
  76. end
  77.  
  78. Button = class()
  79.  
  80. function Button:init(label, px, py, w, h)
  81.     self.label = label
  82.     self.px = px
  83.     self.py = py
  84.     self.w = w
  85.     self.h = h
  86. end
  87.  
  88. function Button:draw()
  89.     strokeWidth(2)
  90.     smooth()
  91.     noFill()
  92.     rectMode(CENTER)
  93.     rect(self.px, self.py, self.w, self.h)
  94.     noSmooth()
  95.     font:drawstring(self.label, self.px - self.w/2 + 5, self.py - 5)
  96. end
  97.  
  98. function Button:touched(t)
  99.     buttonpressed(self.label)
  100. end
  101.  
  102. Font = class()
  103.  
  104. -- - The Hershey Fonts were originally created by Dr.
  105. -- A. V. Hershey while working at the
  106. -- U. S. National Bureau of Standards.
  107.  
  108. -- Useful Links:
  109. -- http://emergent.unpythonic.net/software/hershey
  110. -- http://paulbourke.net/dataformats/hershey/
  111.  
  112. -- Re-encoding of font information and other shenanigans
  113. -- by Tom Bortels bortels@gmail.com November 2011
  114. -- all rights reversed (Hail Eris!)
  115.  
  116. -- "If I have seen a little further it is by standing
  117. --  on the shoulders of Giants."
  118. -- Isaac Newton
  119.  
  120. function Font:init()
  121.    -- font data - 2 decimal character # of points,
  122.    -- followed by 2*points of point data
  123.    -- 9->-9, 8-<-8, ... 1->-1, 0->0, A->1, B->2, ... Z->26
  124.    self.code = "9876543210ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  125.    -- this is the Hershey Roman Simplex font for ascii 32-127
  126.    self.fontdata =
  127.       "00160810EUEG11EBDAE0FAEB0516DUDN11LULN1121KYD711QYJ711DLRL11"
  128.    .. "CFQF2620HYH411LYL411QROTLUHUETCRCPDNEMGLMJOIPHQFQCOAL0H0EACC"
  129.    .. "3124UUC011HUJSJQIOGNENCPCRDTFUHUJTMSPSSTUU11QGOFNDNBP0R0TAUC"
  130.    .. "UESGQG3426WLWMVNUNTMSKQFOCMAK0G0EADBCDCFDHEILMMNNPNRMTKUITHR"
  131.    .. "HPIMKJPCRAT0V0WAWB0710ESDTEUFTFREPDO1014KYIWGTEPDKDGEBG2I5K7"
  132.    .. "1014CYEWGTIPJKJGIBG2E5C70816HUHI11CRML11MRCL0526MRM011DIVI08"
  133.    .. "10FAE0DAEBFAF1E3D40226DIVI0510EBDAE0FAEB0222TYB71720IUFTDQCL"
  134.    .. "CIDDFAI0K0NAPDQIQLPQNTKUIU0420FQHRKUK01420DPDQESFTHULUNTOSPQ"
  135.    .. "POOMMJC0Q01520EUPUJMMMOLPKQHQFPCNAK0H0EADBCD0620MUCGRG11MUM0"
  136.    .. "1720OUEUDLEMHNKNNMPKQHQFPCNAK0H0EADBCD2320PROTLUJUGTEQDLDGEC"
  137.    .. "GAJ0K0NAPCQFQGPJNLKMJMGLEJDG0520QUG011CUQU2920HUETDRDPENGMKL"
  138.    .. "NKPIQGQDPBOAL0H0EADBCDCGDIFKILMMONPPPROTLUHU2320PNOKMIJHIHFI"
  139.    .. "DKCNCODRFTIUJUMTORPNPIODMAJ0H0EADC1110ENDMELFMEN11EBDAE0FAEB"
  140.    .. "1410ENDMELFMEN11FAE0DAEBFAF1E3D40324TRDIT00526DLVL11DFVF0324"
  141.    .. "DRTID02018CPCQDSETGUKUMTNSOQOONMMLIJIG11IBHAI0JAIB5527RMQOOP"
  142.    .. "LPJOINHKHHIFKENEPFQH11LPJNIKIHJFKE11RPQHQFSEUEWGXJXLWOVQTSRT"
  143.    .. "OULUITGSEQDOCLCIDFEDGBIAL0O0RATBUC11SPRHRFSE0818IUA011IUQ011"
  144.    .. "DGNG2321DUD011DUMUPTQSRQROQMPLMK11DKMKPJQIRGRDQBPAM0D01821RP"
  145.    .. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRE1521DUD011DUKUNTPRQPRMRHQE"
  146.    .. "PCNAK0D01119DUD011DUQU11DKLK11D0Q00818DUD011DUQU11DKLK2221RP"
  147.    .. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRERH11MHRH0822DUD011RUR011DK"
  148.    .. "RK0208DUD01016LULEKBJAH0F0DACBBEBG0821DUD011RUDG11ILR00517DU"
  149.    .. "D011D0P01124DUD011DUL011TUL011TUT00822DUD011DUR011RUR02122IU"
  150.    .. "GTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU1321DUD011DUMUPTQSRQ"
  151.    .. "RNQLPKMJDJ2422IUGTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU11LD"
  152.    .. "R21621DUD011DUMUPTQSRQROQMPLMKDK11KKR02020QROTLUHUETCRCPDNEM"
  153.    .. "GLMJOIPHQFQCOAL0H0EACC0516HUH011AUOU1022DUDFECGAJ0L0OAQCRFRU"
  154.    .. "0518AUI011QUI01124BUG011LUG011LUQ011VUQ00520CUQ011QUC00618AU"
  155.    .. "IKI011QUIK0820QUC011CUQU11C0Q01114DYD711EYE711DYKY11D7K70214"
  156.    .. "0UN31114IYI711JYJ711CYJY11C7J71016FOHRJO11CLHQML11HQH0021602"
  157.    .. "P20710FUETDRDPEOFPEQ1719ONO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC17"
  158.    .. "19DUD011DKFMHNKNMMOKPHPFOCMAK0H0FADC1418OKMMKNHNFMDKCHCFDCFA"
  159.    .. "H0K0MAOC1719OUO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC1718CHOHOJNLMM"
  160.    .. "KNHNFMDKCHCFDCFAH0K0MAOC0812JUHUFTEQE011BNIN2219ONO2N5M6K7H7"
  161.    .. "F611OKMMKNHNFMDKCHCFDCFAH0K0MAOC1019DUD011DJGMINLNNMOJO00808"
  162.    .. "CUDTEUDVCU11DND01110EUFTGUFVEU11FNF3E6C7A70817DUD011NNDD11HH"
  163.    .. "O00208DUD01830DND011DJGMINLNNMOJO011OJRMTNWNYMZJZ01019DND011"
  164.    .. "DJGMINLNNMOJO01719HNFMDKCHCFDCFAH0K0MAOCPFPHOKMMKNHN1719DND7"
  165.    .. "11DKFMHNKNMMOKPHPFOCMAK0H0FADC1719ONO711OKMMKNHNFMDKCHCFDCFA"
  166.    .. "H0K0MAOC0813DND011DHEKGMINLN1717NKMMJNGNDMCKDIFHKGMFNDNCMAJ0"
  167.    .. "G0DACC0812EUEDFAH0J011BNIN1019DNDDEAG0J0LAOD11ONO00516BNH011"
  168.    .. "NNH01122CNG011KNG011KNO011SNO00517CNN011NNC00916BNH011NNH0F4"
  169.    .. "D6B7A70817NNC011CNNN11C0N03914IYGXFWEUESFQGPHNHLFJ11GXFVFTGR"
  170.    .. "HQIOIMHKDIHGIEICHAG0F2F4G611FHHFHDGBFAE1E3F5G6I70208DYD73914"
  171.    .. "EYGXHWIUISHQGPFNFLHJ11GXHVHTGRFQEOEMFKJIFGEEECFAG0H2H4G611HH"
  172.    .. "FFFDGBHAI1I3H5G6E72324CFCHDKFLHLJKNHPGRGTHUJ11CHDJFKHKJJNGPF"
  173.    .. "RFTGUJUL"
  174.  
  175.    local i=1
  176.    local c=32
  177.    self.font = {}
  178.    while (i < string.len(self.fontdata)) do
  179.       local cs = string.char(c)
  180.       self.font[cs] = {}
  181.       local points = string.sub(self.fontdata, i, i+1)
  182.       self.font[cs].points = points
  183.       self.font[cs].char = cs
  184.       self.font[cs].ascii = c
  185.       self.font[cs].width = string.sub(self.fontdata, i+2, i+3)
  186.       --print(cs,points,self.font[cs].width)
  187.       i = i + 4
  188.       self.font[cs].data = string.sub(self.fontdata, i, i+points*2)
  189.     --print(self.font[cs].data)
  190.       i = i + points*2
  191.       c = c + 1
  192.    end
  193.    i=-9
  194.    self.decode = {}
  195.    for c in self.code:gmatch"." do
  196.       self.decode[c]=i
  197.       i=i+1
  198.    end
  199. end
  200.  
  201. -- returns width in pixels of unscaled, strokeWidth(1) string
  202. function Font:stringwidth(s)
  203.    local x, l, i = 0, string.len(s)
  204.    for i = 1, l do
  205.       x = x + self.font[s:sub(i, i)].width
  206.    end
  207. end
  208.  
  209. -- draw a string at x,y (skipping offscreen draws)
  210. function Font:drawstring(s, x, y)
  211.    local l, i
  212.    l = string.len(s)
  213.    for i = 1, l do
  214.       local c = s:sub(i, i)
  215.       local w = self.font[c].width
  216.       if ((x + w) >= 0) then
  217.          x = x + (self:drawchar(c, x, y))
  218.       else
  219.          x = x + w -- skip offscreen left (but track position)
  220.       end
  221.       if (x > WIDTH) then break end -- skip offscreen right
  222.    end
  223. end
  224.  
  225. -- optimized draw string at x,y (old version for reference)
  226. function Font:olddrawstring(s, x, y)
  227.    local l, i
  228.    l = string.len(s)
  229.    for i = 1, l do
  230.       x = x + (self:drawchar(string.sub(s, i, i), x, y))
  231.    end
  232. end
  233.  
  234. function Font:drawchar(c, x, y)
  235.    local ax, ay, bx, by, minx, maxx = -1, -1, -1, -1, -1, -1
  236.    local p, plot
  237.    local ch = self.font[c]
  238.    for p=1, ch.points do
  239.       ax=bx
  240.       ay=by
  241.       bx=self.decode[ch.data:sub(p*2-1, p*2-1)]
  242.       by=self.decode[ch.data:sub(p*2, p*2)]
  243.       plot=true
  244.       if ((ax==-1) and (ay==-1)) then plot=false end
  245.       if ((bx==-1) and (by==-1)) then plot=false end
  246.       if (plot) then
  247.          line(x+ax, y+ay, x+bx, y+by)
  248.       end
  249.    end
  250.    return ch.width -- for drawstring
  251. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement