### Author Topic: EGSL and coroutines  (Read 1442 times)

#### Tomaaz

• Guest
##### EGSL and coroutines
« on: 26. April 2013, 23:38:10 »
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() endend)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 endend 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)endfunction 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 endendopenwindow(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, 0pocz = 1licz = 8kol = 10stan = 5stan2 = 0.125/2stan3 = 0.125/4przes = 500przes2 = 250for k = 1, string.len(tekst2) do tekst[k] = string.sub(tekst2, k, k)endfor k = 1, 200, 2 do stars1[k] = math.random(1, 799) stars1[k + 1] = math.random(20, 470)endfor k = 1, 40, 2 do stars2[k] = math.random(1, 799) stars2[k + 1] = math.random(20, 470)endmuz = 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

• Hero Member
• Posts: 611
##### Re: EGSL and coroutines
« Reply #1 on: 27. April 2013, 09:04:23 »
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.
Best regards,
Cybermonkey

#### Tomaaz

• Guest
##### Re: EGSL and coroutines
« Reply #2 on: 27. April 2013, 09:40:29 »
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

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

#### Tomaaz

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