Author Topic: EGSL and coroutines  (Read 2448 times)

Tomaaz

  • Guest
EGSL and coroutines
« on: April 26, 2013, 09:38:10 PM »
Lua's coroutines make easier to do different tasks simultaneously. Unfortunetaly, EGSL functions doesn't work properly when placed inside a coroutine. Look at this example (all files are attached, on Linux run from the terminal):

Code: [Select]
primes = coroutine.create(function()
for prime = 3, 1000000, 2 do
isprime = 1
for dziel = 3, math.ceil(math.sqrt(prime)), 2 do
if prime % dziel == 0 then
isprime = 0
break
end
end
if isprime == 1 then
print(prime)
end
coroutine.yield()
end
end)

function stars()
for k = 1, 200, 2 do
dot(stars1[k], stars1[k + 1])
end
for k = 1, 40, 2 do
fillbox(stars2[k], stars2[k + 1], stars2[k] + 1, stars2[k + 1] + 1)
end
for k = 1, 200, 2 do
if stars1[k] < 798 then
stars1[k] = stars1[k] + 1
else
stars1[k] = 1
end
end
for k = 1, 40, 2 do
if stars2[k] < 798 then
stars2[k] = stars2[k] + 3
else
stars2[k] = 1
end
end
end

function animation()
zx = zx + 3
zy = zy + 3
a = zx / 99
b = zy / 99
alphachannel(164)
for x = -1,1 do
for y = -1,1 do
for z = -1,1 do
y2 = y
x2 = x * math.cos(a) - y2 * math.sin(a)
y3 = x * math.sin(a) + y2 * math.cos(a)
y2 = y3
y3 = y2 * math.cos(b) - z * math.sin(b)
z2 = y2 * math.sin(b) + z * math.cos(b)
sx = x2 * (z2 + 2)
sy = y3 * (z2 + 2)
picture2 = zoomimage(picture, (z2 + 2) / 4, (z2 + 2) / 4)
putimage(sx * 40 + przes, sy * 40 + przes2, picture2)
freeimage(picture2)
przes = przes + stan2
przes2 = przes2 + stan3
if przes == 150 or przes == 550 then
stan2 = -stan2
end      
if przes2 == 160 or przes2 == 260 then
stan3 = -stan3
end
end
end
end
alphachannel(255)
end

function scroll()
colour(kol, kol, kol)
kol = kol + stan
if kol >= 255 or kol < 10 then
stan = -stan
end
for k = 1, 98 do
drawtext((k * 8) + licz, 500, tekst[pocz + k])
end
licz = licz - 1
if licz < 0 then
pocz = pocz + 1
licz = 8
end
if pocz > table.maxn(tekst) then
pocz = 1
end
end

openwindow(800, 519, 0, "Simple demo")
setframetimer(50)
backcolour(0, 0, 0)
picture = loadimage("1.png")
colourkey(0, 0, 0)

stars1 = {}
stars2 = {}
tekst = {}
tekst2 = "                                                                                                   Hi everybody! This is a simple demo made with EGSL - a very nice interpreter for making retro games. EGSL is based on Lua, an easy to learn scripting language that is often used in game industry. If you are interested in making simple 2d games or graphic demos, EGSL is something for you. It is free, open-source and multiplatform. Versions for Windows, Linux, Mac and Haiku are available. For more information, please visit http://egsl.retrogamecoding.org/. To exit, press 'Esc'. Code: Tomaaz."

a, b, zx, zy, x, y, z, y2, x2, y3, x3, z2, sx, sy = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
pocz = 1
licz = 8
kol = 10
stan = 5
stan2 = 0.125/2
stan3 = 0.125/4
przes = 500
przes2 = 250

for k = 1, string.len(tekst2) do
tekst[k] = string.sub(tekst2, k, k)
end

for k = 1, 200, 2 do
stars1[k] = math.random(1, 799)
stars1[k + 1] = math.random(20, 470)
end

for k = 1, 40, 2 do
stars2[k] = math.random(1, 799)
stars2[k + 1] = math.random(20, 470)
end

muz = loadmusic("1.ogg")
playmusic(muz, 99, 1000)
repeat
coroutine.resume(primes)
klawisz = getkey()
coroutine.resume(primes)
clearscreen()
coroutine.resume(primes)
colour(255, 255, 255)
coroutine.resume(primes)
stars()
coroutine.resume(primes)
animation()
coroutine.resume(primes)
scroll()
coroutine.resume(primes)
colour((255 - kol) / 4 + 64, (255 - kol) / 4 + 64, 0)
coroutine.resume(primes)
fillbox(0, 0, 799, 19)
coroutine.resume(primes)
fillbox(0, 515, 799, 518)
coroutine.resume(primes)
fillbox(0, 471, 799, 490)
coroutine.resume(primes)
colour(0, 0, 0)
coroutine.resume(primes)
fillbox (5, 499, 20, 514)
coroutine.resume(primes)
fillbox (779, 499, 799, 514)
coroutine.resume(primes)
sync()
until klawisz == 27

It runs simultaneously a demo (with graphics and sound) and a console program that counts all prime numbers less than one milion. It works only because the part that uses EGSL functions is outside a coroutine, but if any EGSL function would be placed inside a coroutine it wouldn't work properly.

Coroutines seem to be a perfect solution for games (different animations that take place at the same time, for example). Is it any way to use them with EGSL?

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: EGSL and coroutines
« Reply #1 on: April 27, 2013, 07:04:23 AM »
Hm, is think this is also known as threads. Unfortunately the egslengine doesn't use threads which is maybe why it doesn't work with coroutines.

Tomaaz

  • Guest
Re: EGSL and coroutines
« Reply #2 on: April 27, 2013, 07:40:29 AM »
Possibly, but Lua's coroutines use a specific type of multi-tasking. Coroutines are like functions. The important difference is that execution of a coroutine can be paused at any time and then resumed when needed. Only one coroutine may be run at the time and programmer needs to decide how to organize several coroutines to work together. In theory, it should work with EGSL functions. In practice, all you get is a message about wrong usage of called EGSL functions. But it's still possible to use in EGSL programs coroutines with pure Lua code.

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: EGSL and coroutines
« Reply #3 on: April 27, 2013, 08:14:02 AM »
I think because EGSL works as one thread it can't be splitted.
Does the coroutines work with Löve2d?
« Last Edit: April 27, 2013, 08:36:11 AM by Cybermonkey »

Tomaaz

  • Guest
Re: EGSL and coroutines
« Reply #4 on: April 27, 2013, 08:22:56 AM »
That must be it. It's not a big deal. Good programmer will easily manage without coroutines. ;)