Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Docs:
- -- http://www.opengl.org/sdk/docs/man4/xhtml/
- -- http://christopheremoore.net/opengl-lua-shell/
- -- http://christopheremoore.net/opengl-lua-shell-reference/
- local Font,vsx,vsy,Circuits,u,v,DisplayList,tick,LogoDL,OldMouse,Lifetime
- local Kernel_Panic_Logo={x=225,y=88,c={128,128,255},{
- x=0,y=50,
- --[[K]]{{1,1,10,1,10,14,23,1,35,1,19,17,32,30,26,36,10,20,10,37,1,37},c={0,255,255}},
- --[[E]]{{40,1,71,1,64,8,48,8,48,15,63,15,66,18,62,22,48,22,48,29,62,29,70,37,40,37}},
- --[[R]]{{77,1,84,1,84,16,88,16,103,1,111,1,96,16,107,16,107,30,100,37,77,37},{84,22,100,22,100,26,95,31,84,31}},
- --[[N]]{{116,1,125,1,125,30,130,30,138,22,138,1,147,1,147,27,137,37,116,37}},
- --[[E]]{{156,1,187,1,179,9,163,9,163,16,175,16,175,22,163,22,163,29,179,29,187,37,156,37}},
- --[[L]]{{193,1,224,1,217,8,202,8,202,37,193,28}},
- },{
- x=30,y=0,
- --[[P]]{{1,1,8,1,8,16,20,16,30,26,19,37,1,37},{8,23,17,23,17,30,8,30},c={0,255,255}},
- --[[A]]{{34,1,40,1,40,16,61,16,61,1,67,1,67,24,54,37,46,37,34,25},{41,22,60,22,52,30,49,30}},
- --[[N]]{{75,1,83,1,83,29,93,29,98,24,98,1,106,1,106,28,97,37,75,37}},
- --[[I]]{{112,1,120,9,120,29,112,37}},
- --[[C]]{{126,1,155,1,162,8,134,8,134,30,159,30,152,37,126,37}},
- }}
- local function CreateList(f,...)
- local name=gl.glGenList()
- assert(type(name)=="number","name is a "..type(name))
- gl.glNewList(name,gl.GL_COMPILE)
- f(...)
- gl.glEndList()
- return name
- end
- local function BeginEnd(m,f,...)
- gl.glBegin(m)
- f(...)
- gl.glEnd()
- end
- local function Scribble(sentence,x,y,s)
- local s=math.min((s or 0.8*sx)/(sentence.x or 1),(s or 0.8*sy)/(sentence.y or 1))
- local x=(x or vsx/2)-s*(sentence.x or 0)/2
- local y=(y or vsy/2)-s*(sentence.y or 0)/2
- for _,word in ipairs(sentence) do
- local x,y=x+s*(word.x or 0),y+s*(word.y or 0)
- for _,letter in ipairs(word) do
- local c=letter.c or word.c or sentence.c
- if c then
- local c=function(k)
- local c=(c[k] or 1)
- return math.abs(c)<=1 and c or c/255
- end
- gl.glColor(0.03*c(1),0.03*c(2),0.03*c(3),1)
- else
- gl.glColor(0,0.03,0.03,1)
- end
- for _,loop in ipairs(letter) do
- BeginEnd(gl.GL_LINE_LOOP,function()
- for p=1,#loop-1,2 do
- gl.glVertex(x+s*loop[p],y+s*loop[p+1])
- end
- end)
- end
- end
- end
- end
- local function ScribbleFancy(sentence,x,y,s)
- -- Save Logo
- if not LogoDL then
- LogoDL=CreateList(Scribble,sentence,x,y,s)
- end
- -- Draw the wobbling logo
- --gl.glBlendFunc("add")
- gl.glEnable(gl.GL_BLEND)
- gl.glBlendFunc(gl.GL_ONE,gl.GL_ONE)
- gl.glLineWidth(1)
- gl.glColor(0,0.03,0.03,1)-- Faint Cyan
- gl.glPushMatrix()
- local cos,sin=math.cos,math.sin
- local theta=((os.clock()/5)%1)*2*math.pi-- Where 5 is the period in second
- for alpha=0,6.28,2*math.pi/64 do
- local a=6*cos(alpha+theta);
- local b=1*sin(alpha+theta);
- local tx=a*cos(theta)-b*sin(theta);
- local ty=a*sin(theta)+b*cos(theta);
- gl.glTranslate(tx,ty,0)
- gl.glCallList(LogoDL)
- gl.glTranslate(-tx,-ty,0)
- end
- gl.glPopMatrix()
- gl.glLineWidth(1)
- -- Draw the logo tail
- local mx,my=glush.mouse()
- mx,my=vsx*mx,vsy*my
- if OldMouse then
- if mx==OldMouse.omx and my==OldMouse.omy then
- OldMouse.t=1-math.min(10,os.clock()-OldMouse.omc)/10
- else
- OldMouse.t=1
- OldMouse.omx=mx
- OldMouse.omy=my
- OldMouse.omc=os.clock()
- end
- else
- OldMouse={omx=mx,omy=my,omc=os.clock()-60,t=0}
- end
- --gl.glBlendFunc("add")
- gl.glEnable(gl.GL_BLEND)
- gl.glBlendFunc(gl.GL_ONE,gl.GL_ONE)
- local s=math.min((s or 0.8*sx)/(sentence.x or 1),(s or 0.8*sy)/(sentence.y or 1))
- local x=(x or vsx/2)-s*(sentence.x or 0)/2
- local y=(y or vsy/2)-s*(sentence.y or 0)/2
- local rx=x+s*sentence.x/2*(1+cos(((os.clock()/13)%1)*2*math.pi))-- Where 13 is the period in second
- local ry=y+s*sentence.y/2*(1+sin(((os.clock()/17)%1)*2*math.pi))-- Where 17 is the period in second
- for _,word in ipairs(sentence) do
- local x,y=x+s*(word.x or 0),y+s*(word.y or 0)
- for _,letter in ipairs(word) do
- for _,loop in ipairs(letter) do
- BeginEnd(gl.GL_QUAD_STRIP,function()
- for p=0,#loop,2 do
- local x,y=x+s*loop[1+p%#loop],y+s*loop[1+(p+1)%#loop]
- local c=letter.c or word.c or sentence.c
- if c then
- local c=function(k)
- local c=(c[k] or 1)
- return math.abs(c)<=1 and c or c/255
- end
- gl.glColor(0.1*c(1),0.1*c(2),0.1*c(3),1)-- Faint Green
- else
- gl.glColor(0,0.1,0,1)-- Faint Green
- end
- gl.glVertex(x,y)
- --local dx,dy=x-mx,y-my-- mouse
- --local dx,dy=x-rx,y-ry-- clock
- local t=OldMouse.t
- local dx,dy=x-mx*t-rx*(1-t),y-my*t-ry*(1-t)--both mouse and clock
- local d=math.sqrt(dx^2+dy^2)
- dx,dy=vsy/3*dx/d,vsy/3*dy/d
- gl.glColor(0,0,0,1)-- Black
- gl.glVertex(x+dx,y+dy)
- end
- end)
- end
- end
- end
- gl.glColor(1,1,1,1)
- gl.glDisable(gl.GL_BLEND)
- end
- local function DrawTitle()
- ScribbleFancy(Kernel_Panic_Logo,vsx*0.5,vsy*0.5,vsx*0.7)
- end
- local function DrawInfo()
- Font=Font or gl.glLoadFont("FreeSansBold.otf", 50, 10, 5)
- Font:Print("Hello World!", vsx*0.5, vsy*0.2, vsx/40, "cvo")
- end
- local function Toss()
- return math.random()>0.5 and 1 or 0
- end
- local function InitializeCells()
- if not(vsx and vsy) then
- return
- end
- local nct=3*math.sqrt(vsy)
- -- nct is the number of circuit tracks vertically.
- -- I made it neither constant nor proportionnal to screen size, but inbetween
- u=2+math.ceil(nct*vsx/math.min(vsx,vsy))
- v=2+math.ceil(nct*vsy/math.min(vsx,vsy))
- if #Circuits<=2 then
- Lifetime=math.ceil(math.min(u,v)*3/2)
- -- Betterize Random
- for k=1,80+os.time()%200 do
- math.random()
- end
- local a={}
- for x=1,u do
- a[x]={}
- for y=1,v+1 do
- a[x][y]=Toss()
- end
- end
- return a
- else
- -- Initialize cells at 0
- local a={}
- for x=1,u do
- a[x]={}
- for y=1,v+1 do
- a[x][y]=0
- end
- end
- for b=0,(#Circuits==3 and 2 or 0) do-- with b=0,2 it's squarer
- -- Initialize border cells at random
- for x=1,u do
- a[x][1+b]=Toss()
- a[x][v-b]=Toss()
- end
- for y=1,v do
- a[1+b][y]=Toss()
- a[u-b][y]=Toss()
- end
- end
- return a
- end
- end
- local function IterateCells(a)
- local b={}
- -- Recopy half of the border cells (frozen)
- for x=1,u do
- b[x]={}
- b[x][1]=a[x][1]
- end
- for y=1,v do
- b[1][y]=a[1][y]
- end
- -- Make non border cells evolve
- for x=2,u-1 do
- for y=2,v-1 do
- local n=a[x+1][y+1]+
- a[x+1][y+0]+
- a[x+1][y-1]+
- a[x-1][y+1]+
- a[x-1][y+0]+
- a[x-1][y-1]+
- a[x+0][y+1]+
- a[x+0][y-1]
- if n==3 then
- b[x][y]=1--Birth
- elseif n<=4 then
- b[x][y]=a[x][y]--Survival
- else
- b[x][y]=0--Death
- end
- end
- end
- -- Recopy other half of the border cells (frozen)
- for x=1,u do
- b[x][v]=a[x][v]
- end
- for y=1,v do
- b[u][y]=a[u][y]
- end
- return b
- end
- local function KillFourCells(a)
- -- Copy a to b
- local b={}
- for x=1,u do
- b[x]={}
- for y=1,v do
- b[x][y]=a[x][y]
- end
- end
- -- Remove squares
- for x=3,u-1 do
- for y=3,v-1 do
- if a[x][y]+a[x-1][y]+a[x][y-1]+a[x-1][y-1]==4 then
- local hasKilled=false
- for i=x-1,x do
- for j=y-1,y do
- if a[i-1][j]+a[i+1][j]+a[i][j-1]+a[i][j+1]==3 then
- b[i][j]=0
- hasKilled=true
- end
- end
- end
- if not hasKilled then
- b[x][y]=0
- end
- end
- end
- end
- -- Remove diamonds
- for x=2,u-1 do
- for y=2,v-1 do
- if a[x-1][y]+a[x+1][y]+a[x][y-1]+a[x][y+1]==4 and a[x][y]==0 then
- b[x][y]=1
- end
- end
- end
- return b
- end
- local function DrawCircuit(A,C,T,dx,dy)
- local dx,dy=-1-dx,-1-dy
- --gl.glTexture(false)
- gl.glColor(T*C[1],T*C[2],T*C[3],1)
- gl.glLineWidth(1)
- local s=(vsx/(u-3)+vsy/(v-3))/2--Scale: distance between tracks
- local vias={}
- BeginEnd(gl.GL_LINES,function()
- -- Draw tracks
- for i=2,u-1 do
- for j=2,v-1 do
- if A[i][j]~=0 then
- local line=function(i2,j2)
- gl.glVertex((i+dx)*s,(j+dy)*s)
- gl.glVertex((i2+dx)*s,(j2+dy)*s)
- end
- -- Draw horizontal & vertical tracks
- if A[i-1][j]~=0 then
- line(i-1,j)
- end
- if A[i+1][j]~=0 then
- line(i+1,j)
- end
- if A[i][j-1]~=0 then
- line(i,j-1)
- end
- if A[i][j+1]~=0 then
- line(i,j+1)
- end
- -- Draw diagonal tracks
- if A[i-1][j-1]~=0 and not (A[i-1][j]~=0 or A[i][j-1]~=0) then
- line(i-1,j-1)
- end
- if A[i-1][j+1]~=0 and not (A[i-1][j]~=0 or A[i][j+1]~=0) then
- line(i-1,j+1)
- end
- if A[i+1][j-1]~=0 and not (A[i+1][j]~=0 or A[i][j-1]~=0) then
- line(i+1,j-1)
- end
- if A[i+1][j+1]~=0 and not (A[i+1][j]~=0 or A[i][j+1]~=0) then
- line(i+1,j+1)
- end
- end
- end
- end
- -- Draw vias
- local circle=function(i,j)
- local x,y,r=(i+dx)*s,(j+dy)*s,s/3
- gl.glVertex(x+r*math.cos(0),y+r*math.sin(0))
- for a=0,6.2,math.pi/8 do
- gl.glVertex(x+r*math.cos(a),y+r*math.sin(a))
- gl.glVertex(x+r*math.cos(a),y+r*math.sin(a))
- end
- gl.glVertex(x+r*math.cos(0),y+r*math.sin(0))
- end
- for i=2,u-1 do
- for j=2,v-1 do
- if A[i][j]~=0 then
- if A[i-1][j]+A[i-1][j-1]+A[i-1][j+1]+A[i][j-1]+A[i][j+1]==0
- or A[i+1][j]+A[i+1][j-1]+A[i+1][j+1]+A[i][j-1]+A[i][j+1]==0
- or A[i][j-1]+A[i-1][j-1]+A[i+1][j-1]+A[i-1][j]+A[i+1][j]==0
- or A[i][j+1]+A[i-1][j+1]+A[i+1][j+1]+A[i-1][j]+A[i+1][j]==0 then
- circle(i,j)
- vias[1+#vias]={i,j}
- end
- end
- end
- end
- end)
- -- Drill vias
- gl.glColor(0,0,0,1)
- for _,via in ipairs(vias) do
- BeginEnd(gl.GL_TRIANGLE_FAN,function()
- local x,y,r=(via[1]+dx)*s,(via[2]+dy)*s,s/3-1
- gl.glVertex(x,y)
- for a=0,6.3,math.pi/8 do
- gl.glVertex(x+r*math.cos(a),y+r*math.sin(a))
- end
- end)
- end
- gl.glLineWidth(1)
- gl.glColor(1,1,1,1)
- end
- local function GetNewCircuit()
- local r=math.random()
- local dice=math.random(1,3)
- local c
- if dice==1 then
- c={r,1,0,1}-- Apple green
- elseif dice==2 then
- c={1,r,0,1}-- Orange
- else
- c={1,0,1,1}-- Pink
- end
- return {Matrix=InitializeCells(),Color=c,Age=0,dx=math.random(),dy=math.random()}
- end
- local function GrowCircuit()
- if not Circuits then
- local N=4
- tick=0
- Circuits={}
- for k=1,N do
- Circuits[k]=GetNewCircuit()
- Circuits[k].Age=Lifetime*2*(1+N-k)/N
- end
- end
- -- Next lattice iteration
- if 10*os.clock()>tick then
- tick=tick+1
- local circuit=Circuits[1+tick%#Circuits]
- circuit.Matrix=IterateCells(circuit.Matrix)
- circuit.Age=circuit.Age+1
- if circuit.DisplayList then
- gl.glDeleteLists(circuit.DisplayList,1)
- end
- circuit.DisplayList=CreateList(DrawCircuit,KillFourCells(KillFourCells(circuit.Matrix)),
- circuit.Color,(1-circuit.Age/(2*Lifetime))/2,circuit.dx,circuit.dy)
- end
- -- Draw circuits
- for _,circuit in ipairs(Circuits) do
- if circuit.DisplayList then
- gl.glCallList(circuit.DisplayList)
- end
- end
- -- Death of old circuit
- if Circuits[1].Age>2*Lifetime then
- gl.glDeleteLists(Circuits[1].DisplayList,1)
- table.insert(Circuits,1+#Circuits,GetNewCircuit())
- table.remove(Circuits,1)
- end
- end
- local function PrintContent()
- local a={}
- for k,v in pairs(gl) do
- local t=type(v)
- a[t]=a[t] or {}
- table.insert(a[t],k)
- end
- for t,b in pairs(a) do
- print("")
- print("["..t:upper().."]")
- table.sort(b)
- for _,n in ipairs(b) do
- print(n)
- end
- end
- end
- function glush.display(a,b)
- --PrintContent()
- --glush.exit()
- if vsx and vsy then
- gl.glMatrixMode(gl.GL_PROJECTION)
- gl.glLoadIdentity()
- gl.glOrtho(0,vsx,0,vsy,0,1)
- gl.glMatrixMode(gl.GL_MODELVIEW)
- gl.glLoadIdentity()
- -- Now 0,0 is bottom left and vsx,vsy is top right
- --gl.glPushAttrib(gl.glALL_ATTRIB_BITS)
- --gl.glDepthTest(false)
- --gl.glDepthMask(false)
- --gl.glBlending(false)
- --gl.glTexture(false)
- gl.glDisable(gl.GL_DEPTH_TEST)
- gl.glDisable(gl.GL_BLEND)
- gl.glClear(GL_COLOR_BUFFER_BIT)
- gl.glColor(0,0,0,1)
- gl.glRect(0,0,vsx,vsy)
- GrowCircuit()
- DrawTitle()
- --DrawInfo()
- --gl.glPopAttrib()
- else
- vsx, vsy = glush.dispSize()
- end
- end
- function Shutdown()-- Unused
- if DisplayList then
- gl.glDeleteLists(DisplayList,1)
- DisplayList=nil
- end
- if Font then
- --gl.glDeleteFont(Font)
- --Font=nil
- end
- if LogoDL then
- gl.glDeleteLists(LogoDL,1)
- LogoDL=nil
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement