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

0 Members and 3 Guests are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: lua?
« Reply #40 on: June 02, 2007 »
These days Merick, modern languages seem to be getting more and more similar and I'm sure that you could probably pick up fb's gfx commands easily enough.

Personally I work with FB, however I know bugger all about FB's graphics commands!! I just use tinyptc and it's all putting numbers into memory banks. Can be broken down to the pixel level.. I could help you with that I am sure..

For now though you seem to be having a lot of fun with what you're doing and it seems a gentle way of learning FB to me so if it makes you happy... :D
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #41 on: June 02, 2007 »
Is tinyptc better than using the standard graphics commands?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: lua?
« Reply #42 on: June 02, 2007 »
You have more control over everything as you have to write all the graphics commands yourself!

Not too daunting though, I actually made a library of commands that anyone is free to use for stuff like drawing simple text, triangles, circles, lines etc, most of it uses inline assembler so it's fast too.

The main reason I use it is for size reasons... Also an excellent and revised version of tinyptc has been developed on this forum called PTC_EXT

This was made by Rbraz and it's what I use. It's identical to Tinyptc, however it has perfectly synced  refresh and support for MMX.

These intros are all created with it;

http://www.defacto2.net/cracktros.cfm?mode=author&value=Shockwave&recordsAtOnce=10

You should really follow steps 1,2 and 3 of these tutorials;

http://dbfinteractive.com/index.php?PHPSESSID=d15c6d471fa5d137ee8a0558b2a05319&cat=127

Which will guide you through your first steps of using tinyptc including installation of ptc_ext if you are interested. :)



Shockwave ^ Codigos
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #43 on: June 02, 2007 »
Thanks, I'll look into it.

Here's another update, this time I've added a readme with a list of all the commands that I've made wrappers for:

http://www.mediafire.com/?7vbyw21x1fg

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #44 on: June 02, 2007 »
if your intrested in ptc merick, if you look further back i posted an example of setting ptc up wih lua for you.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #45 on: June 02, 2007 »
I did check it out actually, I just haven't done anything with it yet because I wanted to get the standard FB graphics functions working first. I want to have a number of different headers so that people can use whatever graphics library they want by including a different set of files when compiling LuaFB.

One thing I can use some help with. I tried adding the imagecreate function, but I don't think it's working because I'm not sure which data type to push into the lua stack, any ideas?

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #46 on: June 02, 2007 »
the correct type for image create is (any ptr) but im not quite shure what you mean.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #47 on: June 02, 2007 »
In order to send anything from FB to the lua state, or from the lua state to FB, you need to pass it through the lua stack, that's what this is for in the function: (byval L as lua_State ptr)

On the lua side, if you use a function like this:

sPrint(x,y, text ,color)

each argument is put into the stack, and can be accessed from the FB side by pointing to the position in the stack. For instance:

lua_tonumber(L, 2) -- gets the y position

On the FB side, to return some data to the lua side, you need to push it onto the stack:

function ikey cdecl (byval L as lua_State ptr)
    lua_pushstring( L, inkey$) -- pushes the result of inkey$ onto the stack
    ikey = 1  -- tells lua that one item was pushed into the stack
end function

In order to be able to use an image made with imagecreate, I need to figure out how to send the image pointer through the stack


** I just looked in lua.bi, ther is a function to get a pointer - lua_topointer, but not one  to push it
« Last Edit: June 02, 2007 by Merick »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #48 on: June 02, 2007 »
Yeah i know all that but i dont think that lua script can handle pointers can it? what you need todo is have your image loading function in fb and pass it the filename.bmp/jpg/png then in fb there will be a global array that holds the image info then from lua you could call a function like draw image and in fb it will blit the image array it has to the screen.

well thats how i would do it anywho.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #49 on: June 02, 2007 »
I'm not too familiar with FB arrays, in lua tables will automatically expand if you try to add new data to the end of it, do FB arrays work the same, or would I have to redim the array to make it bigger? Something like this would be needed to accomodate a variable number of images. I suppose I could make a function to set the size of the image array, but I'd prefer to be able to set it up to do that automatically.

As for pointers, well if I use lua_pushlightuserdata on the result of imagecreate, then print it with lua's print command this is what is printed out:

userdata:  0093F0A8

however, if I try to use lua_topointer or touserdata to reverse it, I get dozens of errors, none of which are from any of my files
« Last Edit: June 02, 2007 by Merick »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #50 on: June 02, 2007 »
fb arrays need to be redim`d but theres another way this can be done give me a few days as im not going to be at the pc very much for the next couple of days and ill bash something up for you is that cool?
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #51 on: June 02, 2007 »
no prob, thanks for your help, I want to get this sorted out so I can implement the "target" option for the graphics commands

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #52 on: June 02, 2007 »
I just figured out how to run a script without having to load an external file.

For instance, take my timer from the system.lua file:

Code: [Select]
Timer = {}
function Timer.new()
    T = setmetatable(
        {
            start_time = 0,
            running = false,
            stopped_time = 0,
        },
        {
            __index = Timer
        }
    )
    return T
end
   
function Timer:start()
    self.start_time = timer()
    self.running = true
end

function Timer:time()
    if self.running then
        return (timer()-self.start_time)
    else
        return self.stopped_time
    end
end

function Timer:reset()
    self.start_time = timer()
    self.stopped_time = 0
end

function Timer:stop()
    self.stopped_time = (timer()-self.start_time)
    self.running = false
end

If you remove the line breaks to create a single line and then load it into a string, you can run the it with lua_dostring from you're main .bas file:

Code: [Select]
dim code as string

code = "Timer = {} function Timer.new() T = setmetatable({start_time = 0, running = false, stopped_time = 0, }, {__index = Timer}) return T end function Timer:start()   self.start_time = timer() self.running = true end function Timer:time() if self.running then return (timer()-self.start_time) else return self.stopped_time end end function Timer:reset() self.start_time = timer() self.stopped_time = 0 end function Timer:stop() self.stopped_time = (timer()-self.start_time) self.running = false end"

lua_dostring(L, code)

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #53 on: June 03, 2007 »
Anyone know how to convert header files from C/C++ .h to Freebasic .bi? There's already a set of bindings for using OpenGL in lua, but it's all in C. 


Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #54 on: June 03, 2007 »
theres a tool for converting somewhere but you might have some difficulties running gdi based gl in freebasic based on the screen function also with gl you will have to use standard screen reses.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #55 on: June 03, 2007 »
I found that tool: swig

I posted about this on the FreeBasic board, the guys there are helping me out with it

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #56 on: June 05, 2007 »
Right i had a spare half hour there so here is a bmp image loader for you.

ImageLoader.Bi
Code: [Select]
const BITMAP_ID = &H4D42

Dim Shared NoImagesBmpStack As Integer

Type BITMAPINFOHEADER Field = 1
        BiSize          as integer
        BiWidth         as integer
        BiHeight        as integer
        BiPlanes        as short
        BiBitCount      as short
        BiCompression   as integer
        BiSizeImage     as integer
        BiXPelsPerMeter as integer
        BiYPelsPerMeter as integer
        BiClrUsed       as integer
        BiClrImportant  as integer
End Type



Type PALETTEENTRY Field = 1
        peRed   As Byte
        peGreen As Byte
        peBlue  As Byte
        peFlags As Byte
End Type



Type BITMAPFILEHEADER Field = 1
        BfType As Short
        BfSize As Integer
        BfReserved1 As Short
        BfReserved2 As Short
        BfOffBits As Integer
End Type



'This is a replacement for AUX_RGBImageRec because GLAUX lib is not part of
'the freebasic install (perhaps not worth having for only one useful function).
'------------------------------------------------------------------------------
'Added by nino changed this so now we have a Byte And An Integer Buffer
Type BITMAP_RGBImageRec Field = 1
        SizeX as integer
        SizeY as integer
        ByteBuffer as Ubyte Ptr
        IntegerBuffer as Integer Ptr
End Type

Type BmpsImageStack
     Image As BITMAP_RGBImageRec Ptr
End Type

Declare Function LoadBmp( Filename as string ) as BITMAP_RGBImageRec ptr

Dim Shared BmpImageStack As BmpsImageStack Ptr


Function LuaBmpSlots cdecl ( ByVal L As Lua_State Ptr ) As Integer

        NoImagesBmpStack = Lua_ToNumber(L,1)
        BmpImageStack = CAllocate( NoImagesBmpStack * SizeOf( BmpsImageStack ) )
       
        Return(0)
       
End Function
lua_register( L, "LuaBmpSlots", @LuaBmpSlots )

Declare Function DeAllocateBmpStack()
Function DeAllocateBmpStack()
   
        For X = 0 To NoImagesBmpStack
            If ( BmpImageStack ) Then
                If ( BmpImageStack[ X ]->Image ) Then
                    If ( BmpImageStack[ X ]->Image->IntegerBuffer ) Then
                        DeAllocate( BmpImageStack[ X ]->Image->IntegerBuffer )
                    End If
                    If ( BmpImageStack[ X ]->Image->ByteBuffer ) Then
                        DeAllocate( BmpImageStack[ X ]->Image->ByteBuffer )
                    End If
                EndIf
                DeAllocate( BmpImageStack[ X ]->Image )
            End If
        Next
       
        If BmpImageStack Then
            DeAllocate( BmpImageStack )
        End If
       
        Return(0)
       
End Function



Function LuaLoadBmp cdecl ( ByVal L As Lua_State Ptr ) As Integer
   
        Dim BmpSlot As Integer = Lua_ToNumber( L , 1 )
        Dim FileName As String = *Lua_ToString( L , 2 )
        If BmpSlot > NoImagesBmpStack Then Return
        BmpImageStack[ BmpSlot ]->Image = LoadBmp( FileName )
        Return(0)
       
End Function
lua_register( L, "LuaLoadBmp", @LuaLoadBmp )


'' Loads A Bitmap Image. Only uncompressed 8 or 24 bit BITMAP immages supported
Function LoadBmp( Filename as string ) as BITMAP_RGBImageRec ptr

        dim bitmapfileheader as BITMAPFILEHEADER '' this contains the bitmapfile header
        dim bitmapinfoheader as BITMAPINFOHEADER '' this is all the info including the palette
        dim bmpalette(256) as PALETTEENTRY       '' we will store the palette here

        dim index as integer
        dim noofpixels as integer
        dim p as ubyte ptr
        dim r as ubyte, g as ubyte, b as ubyte
        dim pbmpdata as BITMAP_RGBImageRec ptr
        dim f as integer
        f = freefile

        If (open (Filename, for binary, as #f) = 0) then             '' Does The File Exist?

        Get #f, , bitmapfileheader
        If bitmapfileheader.bfType <> BITMAP_ID then
                Close #f
                Return 0
        End If

        Get #f, , bitmapinfoheader

        If bitmapinfoheader.biBitCount=8 or bitmapinfoheader.biBitCount=24 then

        If bitmapinfoheader.biBitCount = 8 then
            for index = 0 to 255
                get #f, , bmpalette(index)
            next
        End If

        noofpixels = bitmapinfoheader.biWidth*bitmapinfoheader.biHeight

        '' allocate the memory for the image (24 bit memory image)
        pbmpdata = allocate(len(BITMAP_RGBImageRec))
       
        if pbmpdata = 0 then
            '' close the file
            close #f
            return 0
        end if
       
        pbmpdata->sizeX  = bitmapinfoheader.biWidth
        pbmpdata->sizeY  = bitmapinfoheader.biHeight
       
        pbmpdata->ByteBuffer = Allocate( noofpixels * 3 )
        pbmpdata->IntegerBuffer = Allocate( noofpixels * 4 )
       
        If pbmpdata->ByteBuffer = 0 Then
            '' close the file
            close #f
            deallocate (pbmpdata)
            return 0
        End If

        '' now read it in
        p = pbmpdata->ByteBuffer
        if bitmapinfoheader.biBitCount=24 then
            for index = 0 to noofpixels -1
                '' Change from BGR to RGB format
                get #f, , b
                get #f, , g
                get #f, , r
                *p = r : p += 1
                *p = g : p += 1
                *p = b : p += 1
            next
        else
            for index = 0 to noofpixels -1
                '' Change from BGR to RGB format while converting to 24 bit
                get #f, , b
                *p = bmpalette(b).peBlue  : p += 1
                *p = bmpalette(b).peGreen : p += 1
                *p = bmpalette(b).peRed   : p += 1
            next
        end if
        else
            close #f
            return 0
        end if
       
        '' Success!
        close #f
       
        'nino comment added this code here to format the rgb bytes and flip the image into
        'the new integer_buffer
        dim as integer y,x
        dim as ubyte ptr my_pointer1
        dim as integer ptr my_pointer2
        dim as ubyte r,g,b
       
        my_pointer1 = pbmpdata->ByteBuffer
       
        for y= pbmpdata->sizey - 1 to 0 step - 1
           
            for x=0 to pbmpdata->sizex - 1
               
                r = *my_pointer1
                my_pointer1 += 1
                g = *my_pointer1
                my_pointer1 += 1
                b = *my_pointer1
                my_pointer1 += 1
                pbmpdata->IntegerBuffer[y*pbmpdata->sizex+x] = ((r shl 16) or (g shl 8) or b)
               
            next
           
        next

        return pbmpdata
        end if

        return 0

end function



Function LuaDrawBmp cdecl ( ByVal L As Lua_State Ptr ) As Integer
        ' 1 = BmpStackNo 2 = x 3 = y
        Dim X As Integer
        Dim Y As Integer
        Dim BmpStackNo As Integer = Lua_ToNumber( L , 1 )
        Dim PosX As Integer = Lua_ToNumber( L , 2 )
        Dim PosY As Integer = Lua_ToNumber( L , 3 )
        Dim ImageWidth As Integer = BmpImageStack[ BmpStackNo ]->Image->SizeX
        Dim ImageHeight As Integer = BmpImageStack[ BmpStackNo ]->Image->SizeY
        Dim DestPtr As UInteger Ptr
       
        DestPtr = Cast(UInteger Ptr, ScreenPtr) + ( PosY * screen_width ) + PosX
       
        For Y = 0 To ImageHeight - 1
            For X = 0 To ImageWidth - 1
                *DestPtr = BmpImageStack[ BmpStackNo ]->Image->IntegerBuffer[ Y * ImageWidth + X ]
                DestPtr += 1
            Next
            DestPtr += ( screen_width - ImageWidth )
        Next
        Return(0)
       
End Function
lua_register( L, "LuaDrawBmp", @LuaDrawBmp )

and here is how interpreter.bas should look with this
Code: [Select]
#include once "fbgfx.bi"
#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.

dim shared screen_width as integer 'needed for freetype
dim shared screen_height as integer 'needed for freetype

#include once "FBsystem.bi"
#include once "FBgraphics.bi"
#include once "lua freetype.bi"
#include once "ImageLoader.Bi"

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

lua_close( L )
DeAllocateBmpStack()
Sleep
End

and heres how to use it in lua script
Code: [Select]
--These lines should go before main loop
TestImage = 1
LuaBmpSlots(1)
LuaLoadBmp( TestImage , "test.bmp" )

--and this line should go in the while loop
LuaDrawBmp( TestImage , 10 , 10 )

testimage refers to the slot in the bmpstack the bmp will be held in
luabmpslots allocates slots of memory on a bmp stack for images to be held set this to however many images you intend to load
lualoadbmp takes a refrence to the bmp image stack and the filename of the bmp

luadrawbmp takes a refrence to the bmp stack and the x and y positions to draw the image.

ohh and one more thing this loader only works properly with image widths divisable by eight ie 32/8 = an even 4
hop this helps :cheers:
« Last Edit: June 05, 2007 by ninogenio »
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #57 on: June 05, 2007 »
Hey thanks, I'll try this out as soon as I can figure out how to get it to compile. I just upgraded FB to v0.17 and it seems the lua headers were completely re-written and I'm having some trouble figuring out what I need to change in order to make it work.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: lua?
« Reply #58 on: June 05, 2007 »
no problem i had some difficultes getting some of my stuff to build with 0.17 too so i just reverted back i guess if you switch also to a newer platform 0.16 users wont be able to build our code.

you could always install both versions though.
Challenge Trophies Won:

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: lua?
« Reply #59 on: June 05, 2007 »
Ok I re-installed 0.16, but when I try this it crashes with the winXP generic "program had a problem" error

*edit*

Stupid me, I forgot to set a screen mode
« Last Edit: June 05, 2007 by Merick »