Skip to content

ezequielgarcia/directfb-lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

directfb-lua Lua binding to DirectFB. It is an automated binding generated from the DirectFB headers.

Like most Lua automatic binding generators, directfb-lua produces ~10k lines of code out of almost nothing: just a Perl script and a bunch of headers.

Unlike most Lua automatic binding generators, directfb-lua produces human readable source code (try it!). This is a direct consequence of DirectFB's clean interface.

For more info, contact me:

Support

So far the supported interfaces are:

  • IDirectFB
  • IDirectFBSurface
  • IDirectFBDisplayLayer
  • IDirectFBWindow
  • IDirectFBImageProvider
  • IDirectFBFont
  • IDirectFBInputDevice
  • IDirectFBEventBuffer
  • IDirectFBVideoProvider (experimental)
  • IDirectFBDataBuffer (experimental)

The rest is still not supported, but the plan is to support (almost) everything. Since Lua is not a low-level language there won't be support for low level buffer handling (e.g. as in Surface::Lock), as it would not make any sense anyway. If you wan't low level, stick to C :)

Features

Automatic interface release:

Taking advantage of Lua garbage collection, there is no need to explicitly release interfaces. You can just let Lua decide when to deallocate it, calling Release() for you. If you do need to release some interface immediately, of course you can.

Automatic flags detection:

As undefined table members are nil-valued in Lua you can let him detect your description flags. For instance you can do:

desc = {}
desc.caps = 'DSCAPS_PRIMARY|DSCAPS_FLIPPING'
surface = dfb:CreateSurface(desc)

without the need to define desc.flags member. If you want to define this member, then the automatic detection gets disabled.

Safe enums types

So far we support two forms of enums: string and number. You can see both in action here:

surf1 = dfb:CreateSurface {caps='DSCAPS_PRIMARY|DSCAPS_FLIPPING'}
surf2 = dfb:CreateSurface {caps=DSCAPS_PRIMARY+DSCAPS_FLIPPING}

The string form is the recommended one, since it checks type coherence. Anyway, you won't have to use enums too much because of automatic flag detection. Plus, you can use any token you like to separate names, this is all the same (or should be):

caps = 'DSCAPS_PRIMARY|DSCAPS_FLIPPING'
caps = 'DSCAPS_PRIMARY,DSCAPS_FLIPPING'
caps = 'DSCAPS_PRIMARY @ DSCAPS_FLIPPING'
caps = 'DSCAPS_PRIMARY ###   DSCAPS_FLIPPING'

History

directfb-lua 0.2

  • Provide Lua 5.2 compatibility, thanks to Matt Sickler!

directfb-lua 0.1

  • Public alpha, basic functionality provided: font rendering, image rendering, window management, basic surface stuff.

Requirements

To compile this library you will need:

  • Some compiling stuff (nothing fancy): gcc, make, pkgconfig.
  • Lua 5.1
  • Perl 5.12
  • DirectFB headers, 1.4.15 is recomended.

To run it you will only need a lua interpreter and DirectFB libraries. (a cpu and some memory is usually also recommended :)

Configuring

If you want to cross compile the library, or you want to use LuaJIT instead of Lua, you can edit the config file. This file has just a couple of variables that control the make process.

Building

All the sources are dynamically generated by a perl script, fed up with DirectFB headers. Sources are generated with:

make gen

The library is compiled and installed with:

make
make install

This will create directfb.so shared library and will install it to an appropiate location.

Usage

A quick example with font and image rendering:

require 'directfb'

-- DFB Initialization
directfb.DirectFBInit()
dfb = directfb.DirectFBCreate()
dfb:SetCooperativeLevel('DFSCL_EXCLUSIVE')

-- Surface creation
surface = dfb:CreateSurface {caps='DSCAPS_PRIMARY|DSCAPS_FLIPPING'}
surface:Clear( 0x80, 0x80, 0x80, 0xff )

-- Font creation
font_path = '/usr/share/fonts/TTF/DejaVuSans.ttf'
font = dfb:CreateFont(font_path, {flags=DFDESC_HEIGHT, height=30})

surface:SetFont(font)

-- Image creation
image = dfb:CreateImageProvider('lua.gif')
image_surf = dfb:CreateSurface(image:GetSurfaceDescription())
image:RenderTo(image_surf)
image:Release()

surface:Blit(image_surf, nil, 100, 100)
surface:SetColor(0, 0, 0, 0xff)
surface:DrawString('DirectFB meets Lua', -1, 10, 10, 'DSTF_TOPLEFT')

surface:Flip()

Another example with a couple of windows:

require 'directfb'

-- DFB Initialization
directfb.DirectFBInit()
dfb = directfb.DirectFBCreate()
dfb:SetCooperativeLevel('DFSCL_FULLSCREEN')

-- Get layer
layer = dfb:GetDisplayLayer()

-- Create window
desc = {}
desc.width = 100
desc.height = 100
desc.surface_caps = 'DSCAPS_FLIPPING'
w1 = layer:CreateWindow(desc)
w2 = layer:CreateWindow(desc)

-- Get windows surface
s1 = w1:GetSurface()
s2 = w2:GetSurface()

s1:Clear( 0xff, 0, 0, 0xff)
s2:Clear( 0, 0xff, 0, 0xff)

w1:MoveTo(100, 100)
w2:MoveTo(150, 150)

w1:SetOpacity(0x80)
w2:SetOpacity(0x80)

Examples

I have provided a few examples to show and to test the binding. Many of them are directly taken from DirectFB examples; for instance examples/bugs.lua is based in df_andi example.

The main point in these examples is to show how clean DirectFB sintax can get through lua.

To run the examples you must first compile and install the library. Then any example can be launched from the examples dir:

$ cd examples
$ lua bugs.lua

or

$ cd examples
$ lua input.lua

If you have any example you'd like to share, just send me a patch or pull request and I'll be glad to add it.

License

Copyright (c) 2011 Ezequiel Garcia

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.