Created
December 23, 2012 16:27
-
-
Save anonymous/4364143 to your computer and use it in GitHub Desktop.
To report a bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
-- 0 chemin 00 | |
displayMode(FULLSCREEN) | |
setInstructionLimit(0) | |
-- Use this function to perform your initial setup | |
function setup() | |
path0 = Path() | |
fps = FPS() -- LINE TO COMMENT | |
map = image(WIDTH,HEIGHT) | |
end | |
-- This function gets called once every frame | |
function draw() | |
resetMatrix() resetStyle() | |
setContext(map) | |
-- This sets a dark background color | |
background(40, 40, 50) | |
-- This sets the line thickness | |
strokeWidth(5) | |
-- Do your drawing here | |
path0:draw() | |
setContext() | |
background(40, 40, 50) | |
sprite(map,WIDTH/2,HEIGHT/2) | |
fps:draw() -- LINE TO COMMENT | |
end | |
function touched(touch) | |
path0:touched(touch) | |
end | |
--# Path | |
Path = class() | |
function Path:init(x) | |
self.color = color(200,200,0) | |
self.pos = {} | |
self.ms = mesh() | |
local v = vec2( WIDTH/2,HEIGHT/2 ) | |
self:add(v) | |
self.selected = false | |
end | |
function Path:add(v) | |
local r = 0 | |
if (self.i or 0) > 0 then | |
local v0 = self.pos[self.i] | |
r = vec2(0,1):angleBetween(v-v0) | |
end | |
local i = self.ms:addRect(v.x,v.y,20,10,r) | |
self.ms:setRectColor(i, self.color ) | |
self.pos[i] = v | |
self.i = i | |
end | |
function Path:draw() | |
self.ms:draw() | |
if CurrentTouch and self.selected then | |
local touch = CurrentTouch | |
local v1 = vec2(touch.x, touch.y) | |
local v0 = self.pos[self.i] | |
if v0:dist(v1) > 5 then | |
v1 = v0 + (v1-v0):normalize()*5 | |
self:add(v1) | |
end | |
end | |
end | |
function Path:touched(touch) | |
if touch.state == BEGAN or touch.state == MOVING then | |
self.selected = true | |
else | |
self.selected = false | |
end | |
end | |
--# FPS | |
FPS = class() | |
-- this manages FPS and a progress bar | |
function FPS:init() | |
-- average fps | |
self.val = 60 | |
self.t0 = ElapsedTime | |
-- min fps | |
self.min = 60 | |
self.t1 = ElapsedTime | |
-- progress bar | |
self.frac = 0 | |
self:progressBarInit() | |
end | |
function FPS:draw() | |
local vShift = 0 | |
if self.progressBarActive then vShift = 30 end | |
-- update FPS value with some smoothing | |
local old = self.val | |
local frac = self.frac | |
-- local t1 = os.clock() | |
local t1 = ElapsedTime | |
local delta = t1 - self.t0 | |
self.t0 = t1 | |
local new = 1/delta or old | |
if new<self.min then self.min=new; self.t1=ElapsedTime+1 end | |
if self.t1<ElapsedTime then self.min=60 end | |
if new>65 then new=65 end | |
local ratio = new/old | |
if 0.5<ratio and ratio<2 then new = old*(1-frac)+ new*frac end | |
self.val = new | |
-- write the FPS on the screen | |
fill(208, 208, 208, 255) | |
fontSize(20) | |
font("AmericanTypewriter-Bold") | |
rectMode(CENTER) | |
text(math.floor(new).." fps (> "..math.floor(self.min)..")",70,HEIGHT-15-vShift) | |
-- draw progress bar | |
self:progressBarDraw() | |
end | |
function FPS:progressBarInit(txt) | |
self.frac = 0 | |
self.txt = txt or "running" | |
self.img = self:progressBarCalcInfoImg(self.txt,WIDTH*0.19,30,"top") | |
self.progressBarActive = false | |
end | |
function FPS:progressBarUpdate(frac) | |
self.frac = frac | |
if frac>0 and frac<1 then self.progressBarActive = true | |
else self.progressBarActive = false end | |
end | |
-- image to show job progress | |
function FPS:progressBarCalcInfoImg(txt,w,h,rot) | |
local w0,h0 | |
pushStyle() pushMatrix() | |
if rot=="left" or rot=="right" | |
then w0,h0 = h,w | |
else w0,h0 = w,h | |
end | |
local img0 = image(w0,h0) | |
setContext(img0) | |
font("AmericanTypewriter-Bold") | |
rectMode(CENTER) | |
textMode(CENTER) | |
strokeWidth(1) | |
background(255, 255, 255, 255) | |
fill(0, 0, 0, 255) | |
stroke(0, 0, 0, 255) | |
fontSize(20) | |
text(txt,w0/2,h0/2) | |
setContext() | |
local img = image(w,h) | |
setContext(img) | |
background(0, 0, 0, 255) | |
spriteMode(CENTER) | |
translate(w/2,h/2) | |
if rot=="left" then rotate(-90) end | |
if rot=="right" then rotate(90) end | |
sprite(img0,0,0) | |
setContext() | |
popStyle() popMatrix() | |
return img | |
end | |
function FPS:progressBarDraw() | |
if self.progressBarActive then | |
local img = self.img | |
pushStyle() | |
spriteMode(CORNER) | |
tint(128, 128, 128, 255) | |
sprite(img, 0, HEIGHT - img.height) | |
tint() | |
tint(255, 255, 255, 255) | |
clip(0,HEIGHT - img.height,self.frac*img.width,img.height) | |
sprite(img, 0, HEIGHT - img.height) | |
clip() | |
popStyle() | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment