Author Topic: lua!!!!  (Read 24915 times)

0 Members and 3 Guests are viewing this topic.

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
lua!!!!
« on: May 28, 2007 »
I'm relatively new to programming, and for the past couple of months I've been scripting with lua (mostly with the PSP Lua Player) Can anyone tell me how to use lua with FB? Basically, I'd like to know if it would be possible to use pure lua code for the main part of a program, and revert to FB code only for things like graphics.
« Last Edit: June 24, 2007 by Merick »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: lua?
« Reply #1 on: May 28, 2007 »
Very good question Merick, Freebasic is very good at being made to work with other libs etc.

I've never come across Lua before, however there are several people here who are interested in Console Homebrew.

Your question seems to be the other way around though, it would seem that you are interested in developing for the PC with the Lua scripting language as opposed to using FB stuff on your Psp.

In any case, I think you've come to the right place, my guess is that Jim or Relsoft or Nino will be able to point you in the right direction. Console homebrew is not my own strength, I know nothing of it, Graphics in FB is something I know about and if you have any specific questions on that I will be happy to help.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #2 on: May 28, 2007 »
It's been a long time, and I didn't really have the patience to really sit down and work out how everything worked but at one time I did mess around with QB a little bit. It might take a bit to refresh my memory, but the stuff in FB that's based on the original QB codes should come pretty easy. However, back then I only used the standard QB code and never got into using libraries or anything like that.

The main reason I want to use lua with FB is because of the way lua uses variables and tables, and possibly the io functions. As I said it's been a while, but from what I can remember of QB, the lua functions for reading from a file (or text files at least) are a lot easier to use.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #3 on: May 28, 2007 »
i never sat down and learned lua on the psp but i did have a wee play.

do you have a small lua example you could post?

im sure we could show you how to get it up and running in fb, it is possible im sure.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #4 on: May 28, 2007 »
Here's something I was working on for the Lua Player. Since the lua player currently only has 3 functions for 2d graphics - points, lines, and rectangles (you can do more with the 3d gu, but that takes a bit more work to figure out), I decided to make a small graphics library for drawing polygons. These functions take some arguments and use them to calculate the coordinates for the various points on a polygon, then use the lua player's drawline function to draw them out. polylib.lua is the library itself, polytest.lua is a small test program I made to show off each of the functions in the lib. I know I'll have to change the graphics and input functions over to something compatible with FB, but that shouldn't be a problem as long as I can somehow get FB to use lua's tables and metatables.

function list:

draw_poly(x, y, radius, rotation, sides, sides_to_draw, closed, color)

draws a regular polygon (i.e. all sides are the same length)
x,y - the center point of the poly
radius -  distance from the center point to each corner of the poly
rotation - angle in degrees in which to rotate the poly around it's center
sides - number of sides the poly has
sides_to_draw - number of sides that will actually be drawn
closed  - if true and sides_to_draw is less than sides then it will draw a line connecting the end points
color - color to draw the poly

function draw_tri(x, y, side1, side2, angle, rotation, color)
draws a triangle rotated around the start point with two sides of the specified length at the given angle

draw_para(x,y, side1, side2, angle, rotation, color)
draws a parallelogram rotated around the start point, side1 and side2 are the lengths of each pair of sides, angle is the angle of the two sides that meet at the start point


draw_free(x, y, ptable, scale, color)

this one lets you draw free-form polys

ptable is basically a table with a list of coordinates relative to x,y. This example table has the coords to draw an outline of an x:
Code: [Select]
polytable = {
    {x = 1, y = 0},
    {x = 0, y = 1},
    {x = 1, y = 2},
    {x = 0, y = 3},
    {x = 1, y = 4},
    {x = 2, y = 3},
    {x = 3, y = 4},
    {x = 4, y = 3},
    {x = 3, y = 2},
    {x = 4, y = 1},
    {x = 3, y = 0},
    {x = 2, y = 1},
    {x = 1, y = 0},
    }


"scale" will let you change the size the poly is drawn at, set it to 1 for no change

-- advanced functions

polymorph.new(poly_table, frames, delay)

creates a new polymorph object

poly_table is a table that contains 2 of the same type of tables used for draw_free. Although the 2 poly tables don't have to be the exact same size, the smaller table must have at least half the number of points as the larger table.

frames is the number of animation frames you want it to take to morph one poly into the other,

delay is the delay between frames.

polymorph:start()   
starts the animation timer

polymorph:set_auto(auto_type, wait)
sets up the automatic switching, auto_type is a string value:

"reverse" -sets it to auto-reverse the animation
"reset" -sets it to auto-reset to the first frame of the animation
"stop"  -stops the animation timer

wait is a number value, it sets how long to wait before starting the next animation loop. If left out it will continue to use the previous wait value (default is 0)

polymorph:draw(x, y, scale, color)
draws the polymorph animation

draw_adv(x, y, ptable, scale, color)

this is an advanced version of the draw_free function, by adding a few things to the poly table you can change the way it gets drawn:

Code: [Select]
poly1 = {
{color = green, x = 1, y = 0},
{x = 0, y = 1},
{x = 1, y = 2},
{x = 2, y = 1},
{x = 1, y = 0},
{command = "break"},
{color = red, x = 1, y = 3},
{x = 0, y = 4},
{x = 1, y = 5},
{x = 2, y = 4},
{x = 1, y = 3},
{command = "break"},
{color = blue, x = 4, y = 0},
{x = 3, y = 1},
{x = 4, y = 2},
{x = 5, y = 1},
{x = 4, y = 0},
{command = "break"},
{color = white, x = 4, y = 3},
{x = 3, y = 4},
{x = 4, y = 5},
{x = 5, y = 4},
{x = 4, y = 3},
}

putting color = newcolor in any index will change the current drawing color. Although you can still put in a color when calling the function, it will be over-ridden by any color codes in the poly table

putting in command = "break" will stop the function from drawing a line between the points before and after the command

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #5 on: May 28, 2007 »
ill have a look into running lua stuff in fb for you but im sure someone else here will come with an answer for you as i dont really know lua that well.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #6 on: May 28, 2007 »
I think I got part of it. If I put this in:

Code: [Select]
option explicit
 
#include once "Lua/lua.bi"
#include once "Lua/luauxlib.bi"
#include once "Lua/lualib.bi"


dim L as lua_State ptr


L = lua_open( )             'create a lua state

luaopen_base(L)             'opens the basic library
luaopen_table(L)            'opens the table library
luaopen_io(L)               'opens the I/O library
luaopen_string(L)           'opens the string lib.
luaopen_math(L)             'opens the math lib.


then I can use lua_dofile(L, "script.lua") to run lua code from a file. Of course, in order to use graphics I'll have to make some wrapper functions in the main .bas file to let me call the FB graphics functions from the lua script

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: lua?
« Reply #7 on: May 28, 2007 »
It will be sorted when the libs guys get on here later if you don't fix it first :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #8 on: May 28, 2007 »
Heh, with help from the lua example that comes with FB, I think I've got it figured out how to make the function wrappers. It's only a start, but this example will let you use the screen function to set the screen mode from inside a lua dofile:

Code: [Select]
#include once "Lua/lua.bi"
#include once "Lua/luauxlib.bi"
#include once "Lua/lualib.bi"

function scr cdecl (byval L as lua_State ptr) as integer

    ' get the number of parameters passed on the virtual stack
  ' by the calling lua code
    dim pars(1 to 5) as integer
  numpar = lua_gettop( L )

  for i = 1 to numpar
    ' peek the specified vs element
    pars(i) = lua_tonumber( L, i )
  next i
   
    if numpar = 1 then
        screen pars(1)
    elseif numpar = 2 then
        screen pars(1),pars(2)
    elseif numpar = 3 then
        screen pars(1),pars(2),pars(3)
    elseif numpar = 4 then
        screen pars(1),pars(2),pars(3),pars(4)
    elseif numpar = 5 then
        screen pars(1),pars(2),pars(3),pars(4),pars(5)
    end if


end function

dim L as lua_State ptr


L = lua_open( )             'create a lua state

luaopen_base(L)             'opens the basic library
luaopen_table(L)            'opens the table library
luaopen_io(L)               'opens the I/O library
luaopen_string(L)           'opens the string lib.
luaopen_math(L)             'opens the math lib.

lua_register( L, "screen", @scr )

' execute the lua script from a file
lua_dofile( L, "test.lua" )

lua_close( L )
print "finished! press any key"
Sleep
End

one question though, in FB, which is more efficient - the if/then/elseif statments I'm useing now, or the select case statements?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: lua?
« Reply #9 on: May 28, 2007 »
Select case is generally better programming practice and allows you to use case else to have a default value too.

I use both, generally I use select case when there are several choices that would be difficult to read if I used a load of if thens..

I don't think it makes much difference in terms of overhead.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: lua?
« Reply #10 on: May 28, 2007 »
Good work in getting it going.  Lua's a really powerful script language which is used in lots of applications and games to control things like movie playback or AI.  I've always thought we should be using it at work to replace some of the C code we write now.  Lots of advantages - like you don't have to compile it, it's easy to understand, you can extend it in lots of ways...

It'll be nice to see some examples of this in action, since I've never had time to research it properly.  K+ Merick!

Jim
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #11 on: May 29, 2007 »
Well, with what I've done so far you can run a script in pure lua, but pure lua doesn't really have anything for getting user input, and (except for the file io functions) the only output is through the print statement, which I've found will only print to the console window and not a graphics window. Although I've made a start on some wrappers for some of the more simple functions, I don't really know enough yet to do some of the more complex stuff, like freetype or ogl.

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #12 on: May 29, 2007 »
Here's what I have so far:

Code: [Select]
#include once "Lua/lua.bi"
#include once "Lua/luauxlib.bi"
#include once "Lua/lualib.bi"

function scr (byval L as lua_State ptr) as integer

  numpar = lua_gettop( L )
   
    select case as const numpar
   
    case 1
        screen lua_tonumber(L, 1)
    case 2
        screen lua_tonumber(L, 1),lua_tonumber(L, 2),
    case 3
        screen lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),
    case 4
        screen lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),lua_tonumber(L, 4),
    case 5
        screen lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),lua_tonumber(L, 4),lua_tonumber(L, 5)
    end select


end function

function sres (byval L as lua_State ptr) as integer
   
    numpar = lua_gettop(L)

    select case as const numpar
    case 2
        screenres lua_tonumber(L, 1),lua_tonumber(L, 2),
    case 3
        screenres lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),
    case 4 
        screenres lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),lua_tonumber(L, 4),
    case 5 
        screenres lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),lua_tonumber(L, 4),lua_tonumber(L, 5)
    case 6 
        screenres lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),lua_tonumber(L, 4),lua_tonumber(L, 5),lua_tonumber(L, 6)
    end select

end function

function scopy (byval L as lua_State ptr) as integer
   
    numpar = lua_gettop( L )
   
    select case as const numpar
   
    case 0
        screencopy
    case 1
        screencopy lua_tonumber(L, 1)
    case 2
        screencopy lua_tonumber(L, 1),lua_tonumber(L, 2)
    end select
   
end function

function sset (byval L as lua_State ptr) as integer
   
    numpar = lua_gettop(L)
   
    select case as const numpar
    case 0
        screenset
    case 1
        screenset lua_tonumber(L, 1)
    case 2 
        screenset lua_tonumber(L, 1),lua_tonumber(L, 2)
    end select
end function

function vsync
    screensync
end function

function ikey (byval L as lua_State ptr) as integer
    lua_pushstring( L, inkey$)
    ikey = 1
end function

function newcolor (byval L as lua_State ptr) as integer
   
    numpar = lua_gettop(L)
   
    select case as const numpar
    case 3
        lua_pushvalue(L, rgb(lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3)))
    case 4
        lua_pushvalue(L, rgba(lua_tonumber(L, 1),lua_tonumber(L, 2),lua_tonumber(L, 3),lua_tonumber(L, 4)))
    end select
   
    newcolor = 1
   
end function

function sl (byval L as lua_State ptr) as integer
   
    numpar = lua_gettop(L)
   
    select case as const numpar
    case 0
        sleep
    case 1
        sleep lua_tonumber(L,1)
    end select
   
end function

dim L as lua_State ptr


L = lua_open( )             'create a lua state

luaopen_base(L)             'opens the basic library
luaopen_table(L)            'opens the table library
luaopen_io(L)               'opens the I/O library
luaopen_string(L)           'opens the string lib.
luaopen_math(L)             'opens the math lib.


' register wrapper functions for access from a lua script
lua_register( L, "Screen", @scr)
lua_register( L, "SRes", @sres)
lua_register( L, "SCopy", @scopy)
lua_register( L, "Sset", @sset)
lua_register( L, "Vsync", @vsync)
lua_register( L, "InKey", @ikey)
lua_register( L, "Sleep", @sl)
lua_register( L, "NewColor", @newcolor)

' execute the lua script from a file
lua_dofile( L, "test.lua" )

lua_close( L )
print "finished! press any key"
Sleep
End

most of these functions work ok, but when I try to use the newcolor function from the lua script the program crashes, can anyone tell me why?

here's how I'm calling it from the test.lua script:

Screen(13,32)

a= NewColor(100,100,100,100)
 
print(a)

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #13 on: May 30, 2007 »
Thanks to Tigra on the official FB forums I've been able to fix the problems I was having with the NewColor and sPrint functions.

Here's a new version of the interpreter, it includes a small lua script that uses the NewColor function and the dLine function, which is an incomplete but working wrapper for the standard line drawing function

*edit* link removed because I just uploaded a new version
« Last Edit: May 30, 2007 by Merick »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #14 on: May 30, 2007 »
thanks Merick, i for one am keeping a close eye on this as im very intrested. i think its really great what your doing as not a lot of people know this stuff!

so keep it going and ill be happy for anything else you can show us, great stuff mate k+

heh just had a play around with it there and i see how it works! can i maybe add a triangle function and a rectangle to it just to see if i can ill post up whatever i do.
« Last Edit: May 30, 2007 by ninogenio »
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #15 on: May 30, 2007 »
Actually, I've already added the rectangle function. Just put "B" or "BF" as the last argument for dLine:

dLine(10,10, 100, 100, color) -- will draw a line
dLine(10,10, 100, 100, color, "B") --will draw a rectangle
dLine(10,10, 100, 100, color, "BF")  --will draw a filled rectangle

Also, with what I've done so far, I've been able to convert most of my psp Lua Player polylib (which already has a function for triangles), the only thing that doesn't work yet is the polymorph function

http://www.mediafire.com/?2yq0jz5lmbh

oh, in case you don't already know, in a lua script you use -- instead of ' for comments
« Last Edit: May 30, 2007 by Merick »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #16 on: May 30, 2007 »
cool cheers mate!

are types and arrays supported in lua?

-edit i see from the new file you posted that arrays are supported and also embeded functions this is really powerfull a lot more so than i first thought
« Last Edit: May 30, 2007 by ninogenio »
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #17 on: May 30, 2007 »
In lua, you have tables. Tables are basically types and arrays rolled into one construct. With regular variables, you don't need to declare them before you use them, and you don't need to assign them as a specific type (integer, string, etc..) because lua has automatic typing. Tables are a little different, before you can use one you do need to declare it, like this:

a = {}

this creates an empty table, but you can also put in some values when you declare it:

a = {1, 10}

this puts the number 1 at position 1, and the number 10 at position 2. To access each element, you would use it like this: a[1] or a[2] etc.. There is no limit to the number of elements you can put in a table. If you want each position to have more than one value, then you can do it like this:

Code: [Select]
a = {
    {1, 10},
    {3,"hello"},
    }

this is the same as

a = {}
a[1] = {1, 10}
a[2] = {3,"hello"}

in this example, a[1][2] = 10, and a[2][2] = "hello"

That's right, you can mix types in the same table.

one more example before I have to leave for work

Code: [Select]
a = {
    {x = 10, y = 20},
    {x = 13, y = 17}
    }

In this example, if you were using this as a list of screen coordinates you would access the elements like this:

dLine(a[1].x, a[1].y, a[2].x, a[2].y, color)

The ability to use tables like this is one of the reasons I love working with lua

Here's a link to a page with some tutorials on lua:

http://lua-users.org/wiki/TutorialDirectory

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #18 on: May 30, 2007 »
cheers mate, here is a mess around by me its now using tinyptc_ext for drawing its very buggy and ive managed to break draw_poly somehow but its very smooth for animation and it has vsync built in it also lets you switch between full screen and a window.

this stuff really is cool its kept me ocupied for a few hours so far and i fully intent to keep going.

<edit oops i had forgot boundary checking in the draw line function>

if the genioline function is replaced with this.
Code: [Select]
Sub GenioLine( byval x1 as double ,byval y1 as double ,byval x2 as double ,byval y2 as double , byval Col As Integer )

         dim i, deltax, deltay, numpixels as integer
         dim d, dinc1, dinc2 as integer
         dim x, xinc1, xinc2 as integer
         dim y, yinc1, yinc2 as integer

         'calculate deltaX and deltaY
         deltax = abs(int(x2) - int(x1))
         deltay = abs(int(y2) - int(y1))

         'initialize
         if(deltax >= deltay) then
                  'If x is independent variable
                  numpixels = deltax + 1
                  d = (2 * deltay) - deltax
                  dinc1 = deltay shl 1
                  dinc2 = (deltay - deltax) shl 1
                  xinc1 = 1
                  xinc2 = 1
                  yinc1 = 0
                  yinc2 = 1
         else
                  'if y is independent variable
                  numpixels = deltay + 1
                  d = ( 2 * deltax ) - deltay
                  dinc1 = deltax shl 1
                  dinc2 = ( deltax - deltay ) shl 1
                  xinc1 = 0
                  xinc2 = 1
                  yinc1 = 1
                  yinc2 = 1
         endif

         'move the right direction
         if ( int(x1) > int(x2) ) then
                  xinc1 = -xinc1
                  xinc2 = -xinc2
         endif

         if ( int(y1) > int(y2) ) then
                  yinc1 = -yinc1
                  yinc2 = -yinc2
         endif

         x = int(x1)
         y = int(y1)

         'draw the pixels
         for i = 1 to numpixels
                  if y<640 and y>0 then
                       if x<480 and x>0 then
                           
                                Buffer( y*ScrWidth+x ) = Col
                       endif
                  endif
                  if ( d < 0 ) then
                          d = d + dinc1
                          x = x + xinc1
                          y = y + yinc1
                  else
                          d = d + dinc2
                          x = x + xinc2
                          y = y + yinc2
                  endif
         next

End Sub

then draw_poly works fine
« Last Edit: May 30, 2007 by ninogenio »
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #19 on: May 30, 2007 »
Is tinyptc better than using the standard FB drawing routines? And since I don't know anything about it, Would you be willing be willing to make a header file with wrappers for all of it's functions?