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

0 Members and 2 Guests are viewing this topic.

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #20 on: May 31, 2007 »
Here's a new version. In this one, I've split the graphic and input/system functions into two separate header files, and I've also renamed most (but not all) of them so that the lua versions are spelled the same as the original Fb version.  In addition, I've also created a system.lua file. This script will be run before the main.lua and it will contain some useful functions written mostly in lua code. I just started on it, so right now it only has an advanced timer function that uses the lua metatable system. To see how this works, check the comments in the file itself. I did this function first because with it I was able to get the polymorph function in polylib working.

 (of course, if you don't want to use the functions in the system.lua file you could always replace it with one that has your own functions, or even remove the lua_dofile("system.lua") from the main .bas before compiling)

http://www.mediafire.com/?4qxxtzyvfzh

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #21 on: May 31, 2007 »
its really taking shape!! i will maybe try a little something with it soon.

oh one thing mate that you probably already know but if you cdecl your fb wrapper functions it will get rid of a lot of warnings when registering them.

 
« Last Edit: May 31, 2007 by ninogenio »
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #22 on: May 31, 2007 »
actually, I didn't know about that

**

Using one of the examples included with FB as a base, I've started working on a header for using the FreeType libs. I was able to get the font loader working (or at least, there were no errors when I tried using it), but when I tried adding the font printing function from the example the compiler gave me an error about a pointer and I'm not sure exactly what's wrong as I've never tried using the freetype library before


put this in a file named "lua freetype.bi" and put an include for it in "interpreter.bas"(make sure you put it after the lua open commands)

Code: [Select]
'' Lua Freetype header, modified from
'' FreeType2 library test, by jofers (spam[at]betterwebber.com)
''

#include "freetype2/freetype.bi"

' Alpha blending
#define FT_MASK_RB_32         &h00FF00FF
#define FT_MASK_G_32         &h0000FF00

' DataStructure to make it easy
Type FT_Var
    ErrorMsg   As FT_Error
    Library    As FT_Library
    PixelSize  As Integer
End Type

Dim Shared FT_Var As FT_Var

Declare sub DrawGlyph(ByVal FontFT As FT_Face, ByVal x As Integer, ByVal y As Integer, ByVal Clr As UInteger)

' Initialize FreeType
' -------------------
FT_Var.ErrorMsg = FT_Init_FreeType(@FT_Var.Library)
If FT_Var.ErrorMsg Then
    Print "Could not load library"
    End
End If

' Load a font
' -----------
Function loadfont cdecl (byval L as lua_State ptr) As Integer
    args = lua_gettop(L)
    select case as const args
    case 0
        return luaL_error(L, "you must enter a font file")
    case 1
        if lua_isstring( L, 1) then
            Dim Face As FT_Face
            Dim ErrorMsg As FT_Error
           
            ErrorMsg = FT_New_Face(FT_Var.Library, *lua_tostring(L,1), 0, @Face )
            If ErrorMsg Then return luaL_error(L, "can't find font file")
            lua_pushnumber(L, CInt(Face))
        end if
    case else
        return luaL_error(L, "LoadFont takes only one argument")
    end select
    GetFont = 1
End Function

lua_register( L, "LoadFont", @loadfont)


' Print Text
' ----------
Function PrintFT cdecl (byval L as lua_State ptr) As Integer
    args = lua_gettop(L)
    select case as const args
    case 6
        x = lua_tonumber(L,1)
        y = lua_tonumber(L,2)
        Font = lua_tonumber(L,3)
        Size = lua_tonumber(L,4)
        Clr = lua_tonumber(L,5)
        Text = *lua_tostring(L,6)
       
        Dim ErrorMsg   As FT_Error
        Dim FontFT     As FT_Face
        Dim GlyphIndex As FT_UInt
        Dim Slot       As FT_GlyphSlot
        Dim PenX       As Integer
        Dim PenY       As Integer
        Dim i          As Integer
       
        ' Get rid of any alpha channel in AlphaClr
        Clr = Clr Shl 8 Shr 8
   
        ' Convert font handle
        FontFT = Cast(FT_Face, Font)
       
        ' Set font size
        ErrorMsg = FT_Set_Pixel_Sizes(FontFT, Size, Size)
        FT_Var.PixelSize = Size
       If ErrorMsg Then Return 0
       
        ' Draw each character
        Slot = FontFT->Glyph
        PenX = x
        PenY = y
           
        For i = 0 To Len(Text) - 1
            ' Load character index
            GlyphIndex = FT_Get_Char_Index(FontFT, Text[i])
           
            ' Load character glyph
            ErrorMsg = FT_Load_Glyph(FontFT, GlyphIndex, FT_LOAD_DEFAULT)
            If ErrorMsg Then Return 0
           
            ' Render glyph
            ErrorMsg = FT_Render_Glyph(FontFT->Glyph, FT_RENDER_MODE_NORMAL)
            If ErrorMsg Then Return 0
           
            ' Check clipping
            If (PenX + FontFT->Glyph->Bitmap_Left + FontFT->Glyph->Bitmap.Width) > 320 Then Exit For
            If (PenY - FontFT->Glyph->Bitmap_Top + FontFT->Glyph->Bitmap.Rows) > 240 Then Exit For
            If (PenX + FontFT->Glyph->Bitmap_Left) < 0 Then Exit For
            If (PenY - FontFT->Glyph->Bitmap_Top) < 0 Then Exit For
           
            ' Set pixels
            DrawGlyph FontFT, PenX + FontFT->Glyph->Bitmap_Left, PenY - FontFT->Glyph->Bitmap_Top, Clr
           
            PenX += Slot->Advance.x Shr 6
        Next i
    case else
        return luaL_error(L, "wrong number of arguments")
    end select
End Function

lua_register( L, "FontPrint", @PrintFT)

sub DrawGlyph(ByVal FontFT As FT_Face, ByVal x As Integer, ByVal y As Integer, ByVal Clr As UInteger)
    Dim BitmapFT As FT_Bitmap
    Dim BitmapPtr As UByte Ptr
    Dim DestPtr As UInteger Ptr
   
    Dim BitmapHgt As Integer
    Dim BitmapWid As Integer
    Dim BitmapPitch As Integer
   
    Dim Src_RB As UInteger
    Dim Src_G As UInteger
    Dim Dst_RB As UInteger
    Dim Dst_G As UInteger
    Dim Dst_Color As UInteger
    Dim Alpha As Integer

    BitmapFT = FontFT->Glyph->Bitmap
    BitmapPtr = BitmapFT.Buffer
    BitmapWid = BitmapFT.Width
    BitmapHgt = BitmapFT.Rows
    BitmapPitch = 320 - BitmapFT.Width
   
    DestPtr = Cast(UInteger Ptr, ScreenPtr) + (y * 320) + x
   
    Do While BitmapHgt
        Do While BitmapWid
            ' Thanks, GfxLib
            Src_RB = Clr And FT_MASK_RB_32
            Src_G  = Clr And FT_MASK_G_32

            Dst_Color = *DestPtr
            Alpha = *BitmapPtr
           
            Dst_RB = Dst_Color And FT_MASK_RB_32
            Dst_G  = Dst_Color And FT_MASK_G_32
           
            Src_RB = ((Src_RB - Dst_RB) * Alpha) Shr 8
            Src_G  = ((Src_G - Dst_G) * Alpha) Shr 8
           
            *DestPtr = ((Dst_RB + Src_RB) And FT_MASK_RB_32) Or ((Dst_G + Src_G) And FT_MASK_G_32)
           
            DestPtr += 1
            BitmapPtr += 1
            BitmapWid -= 1
        Loop
       
        BitmapWid = BitmapFT.Width
        BitmapHgt -= 1
        DestPtr += BitmapPitch
    Loop
   
End sub

to load a font in lua, use something like this:

name = LoadFont("path/filename.ttf")

name can be any variable name, the path/filename can be either a quoted string, or a string variable with the filename in it. If the font is in the same folder as the executable you can leave out the path part

to print with a font, use this:

FontPrint(x, y, font, size, color, text)

where font is the variable name that you used with the LoadFont function, and color is a color made with the NewColor function
« Last Edit: June 01, 2007 by Merick »

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #23 on: June 01, 2007 »
hmm, I think the error is because the font loader isn't working right and so the wrong data type is getting passed to the font print function

*edit*

Got it paritally working. The FontPrint function will now print the text, however if you set the screen resolution to an odd size it comes out as garbage. Also, I've found that using some fonts won't print the letters, just boxes.

Code: [Select]
'' Lua Freetype header, modified from
'' FreeType2 library test, by jofers (spam[at]betterwebber.com)
''

#include "freetype2/freetype.bi"

' Alpha blending
#define FT_MASK_RB_32         &h00FF00FF
#define FT_MASK_G_32         &h0000FF00

' DataStructure to make it easy
Type FT_Var
    ErrorMsg   As FT_Error
    Library    As FT_Library
    PixelSize  As Integer
End Type

Dim Shared FT_Var As FT_Var

Declare sub DrawGlyph(ByVal FontFT As FT_Face, ByVal x As Integer, ByVal y As Integer, ByVal Clr As UInteger)
Declare Function PrintFT(ByVal x As Integer, ByVal y As Integer, ByVal Text As String, ByVal Font As Integer, ByVal Size As Integer = 14, ByVal Clr As UInteger = Rgb(255, 255, 255))

' Initialize FreeType
' -------------------
FT_Var.ErrorMsg = FT_Init_FreeType(@FT_Var.Library)
If FT_Var.ErrorMsg Then
    Print "Could not load library"
    End
End If

' Load a font
' -----------
Function loadfont cdecl (byval L as lua_State ptr) As Integer
    args = lua_gettop(L)
    select case as const args
    case 0
        return luaL_error(L, "you must enter a font file")
    case 1
        if lua_isstring( L, 1) then
            Dim Face As FT_Face
            Dim ErrorMsg As FT_Error
            ErrorMsg = FT_New_Face(FT_Var.Library, *lua_tostring(L,1), 0, @Face )
            If ErrorMsg Then return luaL_error(L, "can't find font file")
            lua_pushnumber(L, CInt(Face))
        end if
    case else
        return luaL_error(L, "LoadFont takes only one argument")
    end select
    loadfont = 1
End Function

lua_register( L, "LoadFont", @loadfont)

function FontPrint cdecl (byval L as lua_State ptr) As Integer
    args = lua_gettop(L)
    select case as const args
    case 6
        if lua_isnumber(L,1) and lua_isnumber(L,2) and lua_isnumber(L,3) and lua_isnumber(L,4) and lua_isnumber(L,5) and lua_isstring(L,6) then
            PrintFT lua_tonumber(L,1), lua_tonumber(L,2), *lua_tostring(L,6), lua_tonumber(L,3), lua_tonumber(L,4), lua_tonumber(L,5)
        else
            return luaL_error(L, "an argument was incorrect")
        end if
    case else
        return luaL_error(L, "wrong number of arguments")
    end select
            'PrintFT Rnd * 200, Rnd * 180 + 20, "Hello World!", ArialFont, Rnd * 22 + 10, Rgb(Rnd * 255, Rnd * 255, Rnd * 255)
end function

lua_register( L, "FontPrint", @FontPrint)

Function PrintFT(ByVal x As Integer, ByVal y As Integer, ByVal Text As String, ByVal Font As Integer, ByVal Size As Integer = 14, ByVal Clr As UInteger = Rgb(255, 255, 255))
    Dim ErrorMsg   As FT_Error
    Dim FontFT     As FT_Face
    Dim GlyphIndex As FT_UInt
    Dim Slot       As FT_GlyphSlot
    Dim PenX       As Integer
    Dim PenY       As Integer
    Dim i          As Integer
   
    ' Get rid of any alpha channel in AlphaClr
    Clr = Clr Shl 8 Shr 8

    ' Convert font handle
    FontFT = Cast(FT_Face, Font)
   
    ' Set font size
    ErrorMsg = FT_Set_Pixel_Sizes(FontFT, Size, Size)
    FT_Var.PixelSize = Size
   If ErrorMsg Then Return 0
   
    ' Draw each character
    Slot = FontFT->Glyph
    PenX = x
    PenY = y
       
    For i = 0 To Len(Text) - 1
        ' Load character index
        GlyphIndex = FT_Get_Char_Index(FontFT, Text[i])
       
        ' Load character glyph
        ErrorMsg = FT_Load_Glyph(FontFT, GlyphIndex, FT_LOAD_DEFAULT)
        If ErrorMsg Then Return 0
       
        ' Render glyph
        ErrorMsg = FT_Render_Glyph(FontFT->Glyph, FT_RENDER_MODE_NORMAL)
        If ErrorMsg Then Return 0
       
        ' Check clipping
        If (PenX + FontFT->Glyph->Bitmap_Left + FontFT->Glyph->Bitmap.Width) > 320 Then Exit For
        If (PenY - FontFT->Glyph->Bitmap_Top + FontFT->Glyph->Bitmap.Rows) > 240 Then Exit For
        If (PenX + FontFT->Glyph->Bitmap_Left) < 0 Then Exit For
        If (PenY - FontFT->Glyph->Bitmap_Top) < 0 Then Exit For
       
        ' Set pixels
        DrawGlyph FontFT, PenX + FontFT->Glyph->Bitmap_Left, PenY - FontFT->Glyph->Bitmap_Top, Clr
       
        PenX += Slot->Advance.x Shr 6
    Next i
End Function

sub DrawGlyph(ByVal FontFT As FT_Face, ByVal x As Integer, ByVal y As Integer, ByVal Clr As UInteger)
    Dim BitmapFT As FT_Bitmap
    Dim BitmapPtr As UByte Ptr
    Dim DestPtr As UInteger Ptr
   
    Dim BitmapHgt As Integer
    Dim BitmapWid As Integer
    Dim BitmapPitch As Integer
   
    Dim Src_RB As UInteger
    Dim Src_G As UInteger
    Dim Dst_RB As UInteger
    Dim Dst_G As UInteger
    Dim Dst_Color As UInteger
    Dim Alpha As Integer

    BitmapFT = FontFT->Glyph->Bitmap
    BitmapPtr = BitmapFT.Buffer
    BitmapWid = BitmapFT.Width
    BitmapHgt = BitmapFT.Rows
    BitmapPitch = 320 - BitmapFT.Width
   
    DestPtr = Cast(UInteger Ptr, ScreenPtr) + (y * 320) + x
   
    Do While BitmapHgt
        Do While BitmapWid
            ' Thanks, GfxLib
            Src_RB = Clr And FT_MASK_RB_32
            Src_G  = Clr And FT_MASK_G_32

            Dst_Color = *DestPtr
            Alpha = *BitmapPtr
           
            Dst_RB = Dst_Color And FT_MASK_RB_32
            Dst_G  = Dst_Color And FT_MASK_G_32
           
            Src_RB = ((Src_RB - Dst_RB) * Alpha) Shr 8
            Src_G  = ((Src_G - Dst_G) * Alpha) Shr 8
           
            *DestPtr = ((Dst_RB + Src_RB) And FT_MASK_RB_32) Or ((Dst_G + Src_G) And FT_MASK_G_32)
           
            DestPtr += 1
            BitmapPtr += 1
            BitmapWid -= 1
        Loop
       
        BitmapWid = BitmapFT.Width
        BitmapHgt -= 1
        DestPtr += BitmapPitch
    Loop
   
End sub

the lua script I'm using with this:

Code: [Select]
screenres(320, 240, 32 )

math.randomseed(os.time())

font = LoadFont("fonts/arial.ttf")

for X = 1, 20 do
    color = NewColor(math.random() * 255, math.random() * 255, math.random() * 255)
    size = math.random() * 22 + 10
   
    FontPrint(math.random() * 200, math.random() * 180 + 20, font, size, color, "Hello World!")
end
« Last Edit: June 01, 2007 by Merick »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #24 on: June 01, 2007 »
one thing i can see that might scramble the letters when you change the screenres is this line

DestPtr = Cast(UInteger Ptr, ScreenPtr) + (y * 320) + x

where 320 is it needs to be the scr width of your window, to make that possible you might have to have a global scrwidth in the graphics.bas file then fill in its size in the screenres function.

thats just a quick guess though, could you upload what you have so far? so i can see if i can guess whats up.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #25 on: June 01, 2007 »
Here it is, I tried adding a screen_width global as you suggested but that didn't work

http://www.mediafire.com/?6yzfgy0lffm

*edit* i made an error in the graphics.bi that I just posted       

screen_width = lua_tonumber(L, 2)
should be         
screen_width = lua_tonumber(L, 1)

however, even correcting that doesn't fix it

**
just did a test,  the probelm is that the value set to screen_width isn't lasting outside the function
« Last Edit: June 01, 2007 by Merick »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #26 on: June 01, 2007 »
right im messing around with it now but heres a couple of bits spotted right away.

now in fb to make a variable global so functions can acssess it you have to use the shared keyword so.

dim screen_width as integer.

becomes

dim shared screen_width as integer.

what fb would have been doing was creating a local screen_width function with a value of 0.

also.

BitmapPitch = 320 - BitmapFT.Width

should be

BitmapPitch = screen_width - BitmapFT.Width

right im going to download the freetype dll so i can try this out.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #27 on: June 01, 2007 »
actually, I had already realized that about using shared but the value still remains at zero

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #28 on: June 01, 2007 »
where can i get my hands on the freetype dll you are yousing.

it should be fairly easy to get this going.

-edit got it
here is the code working i only had to modify a couple of bits.
i aslo cleaned out the warnings for you a couple of notes when you have a function in fb you have to return a value if there is no specific value to return just return(0) and it stops the compiler grumbling.

also with the wrapper functions even if you dont need to push values through still write them as.

function screen_clear cdecl (byval L as lua_State ptr)

and it will stop the compiler moaning.

now here is the working code all you have todo is add the fonts folder and the freetype dll also i think you will have to modify the freetype clipping planes to your window width/height.

« Last Edit: June 01, 2007 by ninogenio »
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #29 on: June 01, 2007 »
Hey thanks.

As for the freetype dll I didn't install anything extra, I'm just using FBIde with Freebasic 0.16b installed over the version that was packaged with the ide, a couple of the files in the examples folder give me errors when I try to compile them, but the ones for freetype work fine.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #30 on: June 01, 2007 »
No probs what errors do you get? i think we are yousing the same version of the compiler.

so you dont need the freetype dll? cause when i tryed to run your example it gave me an error saying freetype dll wasnt there unless you maybe have freetype installed in your system32 folder.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #31 on: June 01, 2007 »
well, since I replaced my files with the edited ones that you posted, the compiler give me no errors at all.

I've got several programs installed on my pc that use the GTK (including Gimp itself), the GTK installs freetype as part of it's package.

Of course, it could also be that it's using the libfreetype.dll.a file in c:\freebasic\lib\win32

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #32 on: June 01, 2007 »
ahh right im guessing one of the packages youve installed has put the freetype dll in your system folder.

so what are your plans for this now are you planning on any games?
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #33 on: June 01, 2007 »
Well, eventually I  might try doing a game, but for right now I want to get the interpreter to be able to use most of the standard FreeBasic graphics functions, after that I'd like to try to get it so you can load jpg, gif, and png images, and then maybe some basic 3d stuff with OGL

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #34 on: June 01, 2007 »
cool well keep us updated,

i might look into some stuff and whatever i find ill share.
the basic ogl stuff shouldnt be that hard.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #35 on: June 02, 2007 »
Well, it could take me a while. Until now I've been writing scripts for the PSP Lua Player which already has all of that compiled into it. The source for that is available but it's all in C++ which I know nothing about, and instead of OGL it uses psp-specific libraries for it's limited 3d stuff so that can't really help me here.


Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #36 on: June 02, 2007 »
Would you like me to take care of the ogl stuff? it could take me a little while as ive got other stuff im working on but i could chip away at it in my spare time for you.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #37 on: June 02, 2007 »
that would be great, the more help I can get with this the better

In fact, I've been wondering if maybe we shouldn't make this a community project, maybe ask the admins to set up a section of the board just for Lua stuff.

***

hmm....  I did a small test to add the circle function, using only the x,y, radius, and color arguments, it works but if you use it in a non-standard resolution the circle gets squashed into an ellipse.
« Last Edit: June 02, 2007 by Merick »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: lua?
« Reply #38 on: June 02, 2007 »
Maybe we can think about a separate section later when we see where this is going?  Primarily DBF is a demo coding site so it'll be interesting to see how it works out. :)

What is the benefit of being able to call the FB graphics functions from Lua?  Why not write in FB?

Jim
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #39 on: June 02, 2007 »
I only just started working with FB so I don't really know all it can do yet, but Lua is a very powerful scripting language. It's as easy, if not easier to learn than basic. I haven't really done much with it yet, just a few things for the psp lua player, but I've been able to pick it up a lot faster than any other programming language I've tried.