Hello all, I have been working with terrain/mesh generation, and it has been a success so far but I have been attempting to create a loading screen as my code does take quite a while to load. I have tried to use coroutines but I cannot seem to get them to work, here is the code that I have tried:
-- test-
-- Use this function to perform your initial setup
function setup()
--Initialize tables
points={}
points2={}
--Set up an image for the static background scenery, as to not have to calculate everything 60 times a second.
img=image(WIDTH,HEIGHT)
--Make sure the noise is random
control=math.random(1,100)
--Set up physics bodies
cp=physics.body(CHAIN,false,unpack(points))
mp=physics.body(CIRCLE,10)
mp.position=vec2(WIDTH/2,HEIGHT/2)
generate()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 40)
fill(0,255,0)
rect(0,HEIGHT/2-10,progress*WIDTH/2,20)
coroutine.resume(cos)
--All we need to do in draw():
--[[sprite(img,WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
noStroke()
ellipse(mp.x,mp.y,mp.radius*2)]]--
end
function generate()
cos=coroutine.create(doLevelGen)
coroutine.resume(cos)
end
function doLevelGen()
progress=0
coroutine.yeild()
--Generate the initial random terrain with noise
for i=0, WIDTH do
table.insert(points, vec2(i,noise(i/100,control)*50+100))
end
progress=progress+1
print(progress)
coroutine.yeild()
table.insert(points, vec2(WIDTH,0))
table.insert(points, vec2(0,0))
--Set up our mesh for the ground
m=mesh()
m.vertices=triangulate(points)
m:setColors(0,255,0)
--Generate the background image
setContext(img)
for a=1,#points-1 do
m:draw()
end
setContext()
progress=progress+1
cos=nil
end
Any help is appreciated, and the coroutine code is based off of someone else’s on the forums, but I do not remember who. The rest of the code is mine, but you are welcome to use the generation and/or mesh code.