SFML community forums
General => SFML projects => Topic started 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
-- 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.
-
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 ?
-
Thank you, i will consider it.
Regards.
-
This might be usefull for a OO syntax, as syntax with : is a bit confusing.
--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:
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:)
-
Thanks everybody.
This is the new syntax using OO. http://lua.pastey.net/77016
-- 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.
-
That was fast ;)
It looks very good now. Have you converted every SFML package ?
-
Thank you.
Have you converted every SFML package ?
No, i'm just finishing the Window package.
Diego.-
-
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 ?
-
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.
-
Ah ok, I didn't catch ;)
But that's fine !