SFML community forums

General => SFML projects => Topic started by: Diego on November 12, 2007, 04:38:31 am

Title: LuaSFML
Post by: Diego on November 12, 2007, 04:38:31 am
I'm working on a module for Lua using SFML.

Here some simple Lua code example http://lua.pastey.net/76987

Code: [Select]

-- Requires the window module
require("LuaSFML.window")

--[[
  Create the window:
-- Width              = 800
-- Height             = 600
-- BPP                = 32
-- Caption            = "Title"
-- Style              = sfWindowFixed
-- Antialiasing level = 0
]]
Window = sfWindowCreate(800, 600, 32, "Title", sfWindowFixed, 0)

-- Start main loop
IsRunning = true

while (IsRunning) do

-- Manage events
Event = sfEventCreate()
while (sfWindowGetEvent(Window, Event)) do
-- Close event
if (sfEventType(Event) == sfEventClose) then
-- Terminate
IsRunning = false ;
end
end

-- Display the window
sfWindowDisplay(Window)
end


I don't know if someone is already working on a wrapper for lua, let me know.

Regards.
Title: LuaSFML
Post by: Laurent on November 12, 2007, 04:50:07 am
That's a great idea :)

However, just a suggestion, Lua supports some kind of OO syntax (using tables) ; I think it would be better and cleaner to use it, don't you think ?
Title: LuaSFML
Post by: Diego on November 12, 2007, 04:55:47 am
Thank you, i will consider it.

Regards.
Title: LuaSFML
Post by: Lord Delvin on November 12, 2007, 09:27:26 am
This might be usefull for a OO syntax, as syntax with : is a bit confusing.

Code: [Select]

--This is derived from Salmaso Raffaele by Timm Felden
--the origin and the license can be found at http://thread.gmane.org/gmane.comp.lang.lua.general/12344/focus=12344

function Class(super, typename)
    -- create a new class description
    local class = {}
    -- set the superclass (for object/function inheritance)
    setmetatable(class, {
        __index = super,
        __call = function(this, ...) --a call of Class is similar to C++'s new class(...)
            local tmp = {}
            setmetatable(tmp, class)
            if this.new then
            local err = this.new(tmp, ...); -- non-nil return values are exceptions and will be printed
            if err then
            print(err);
            return nil;
            end
            end
            return tmp
        end
    })
    class.__type = typename
    if super and super.__tostring then --einige Metamethods müssen scheinbar von Hand geerbt werden
    class.__tostring = super.__tostring
    end
    class.__index = function(table, key)
        local r = class[key]
        if type(r) == 'function' then
            local f = function(...) return r(table, ...) end
            table[key] = f
            return f
        else
      table[key] = r
            return r
        end
    end
    return class
end


usage:
Code: [Select]

A = class(nil, "A");
A.new = function(this, name) this.name = name end
A.__add = function(this, that) return A(this.name .. that.name) end
A.print = function(this) print(this.name) end

B = class(A, "B")
B.print = function(this) print(this.name .. " is a B!") end

hope it works for you and is usefull:)
Title: LuaSFML
Post by: Diego on November 12, 2007, 08:00:39 pm
Thanks everybody.
This is the new syntax using OO. http://lua.pastey.net/77016
Code: [Select]

-- Create the window
Window = sf.Window:New(800, 600, 32, "Title", sf.Window.Fixed, 0) ;

-- Main loop
IsRunning = true
while (IsRunning) do
-- Manage events
Event = sf.Event:New()
while (Window:GetEvent(Event)) do
-- If close event
if (Event.Type == sf.Event.Close) then
-- Terminate
IsRunning = false ;
end
end

-- Display the window
Window:Display()
end


Opinions? Thanks.

Regards.
Title: LuaSFML
Post by: Laurent on November 13, 2007, 02:18:53 am
That was fast ;)

It looks very good now. Have you converted every SFML package ?
Title: LuaSFML
Post by: Diego on November 13, 2007, 03:55:44 am
Thank you.

Quote

Have you converted every SFML package ?

No, i'm just finishing the Window package.

Diego.-
Title: LuaSFML
Post by: Adrien Poupin on January 25, 2008, 05:30:09 pm
Hello,
I've just seen that thread in fact, and I was wondering something... What do you think about the LuaBind wrapper for C++, which has a richer OO approach ?
Title: LuaSFML
Post by: Laurent on January 25, 2008, 05:47:05 pm
LuaBind is for dynamically binding C++ classes and functions so that they are available when your C++ program runs a Lua script.

Here we want to create (compile, or whatever) a module for Lua, which can be used without a C++ program running behind it.

By the way, is there any update on this binding ? It looked promising.
Title: LuaSFML
Post by: Adrien Poupin on January 25, 2008, 05:48:02 pm
Ah ok, I didn't catch ;)

But that's fine !