Hi,
During a long time I was looking for a simple and fast GameEngine but I could find something as simple and light weight as SFML. So I found myself writing one, but more the time pass, less I wanted to lost time with C++. I wanted to be able to fast prototyping like it can be done with GameMaker for example. And here I am porting SFML to Lua.
I wanted it to be simple (mirroiring SFML API) light weight (no Boost at all). It's not finished yet (no network for example). I've added some extra classes like TileMap or SceneGraph Item.
I've not reach yet my objective to fast prototyping but it's been pretty fun. I hope you will enjoy it.
The source code is avaible https://github.com/Canadadry/luaSFML
On the repo you will find some test project I've made in the demo folder. There is a Tetris, a pong, and a Minesweeper.
Here an example of script :
math.randomseed(os.time())
window = sfRenderWindow.new(sfVideoMode.new(640,480,32),"Test",sfWindowStyle.Default);
window:setFramerateLimit(30)
circle = sfeSGItem.new();
circle:move(50,50);
circle:setWidth(100);
circle:setHeight(100);
child1 = sfeSGItem.new(circle);
child1:setWidth(50);
child1:setHeight(50);
clearColor = sfColor.new(math.random(256)-1,math.random(256)-1,math.random(256)-1);
event = sfEvent.new();
while window:isOpen() do
i = 0;
while window:pollEvent(event) do
if(event:type() == sfEventType.Closed) then window:close(); end
if(event:type() == sfEventType.KeyReleased and event:key():code() == sfKey.Escape ) then window:close(); end
if(event:type() == sfEventType.KeyReleased and event:key():code() == sfKey.Q and event:key():system() == true ) then window:close(); end
end
circle:rotate(1);
child1:rotate(-2);
window:clear(clearColor);
window:draw(circle);
window:display();
end
Thanks for reading me :)
I really like Lua and such but I don't think that completely/automatically mirroring the c++ API 1 to 1 is a good idea.
Also there is LOVE which packs Box2D, PhysFS and some other things along graphics/input.
I'm semi interested in this because if I wanted to write a game in Lua I'd take LOVE not the c++ library I like. :P
But at the same time I like Lua and SFML and this is related to both. ;D
Lua has convention of using strings as enums(and so do I in SFG unless it's bitflag enum).
It's a bit weird event is object, maybe it should be a table made by user and get filled with right fields and then you check them directly with . and compare with strings. Also it could be so that if type is key pressed then the event table has set field event.code, not event.key.code, because that's just side effect of needing struct for even union in c++.
if(event:type() == sfEventType.KeyReleased and event:key():code() == sfKey.Escape ) then window:close(); end
vs.
if event.type == "KeyReleased" and event.code == "Escape" then window:close() end
The second one looks much nicer IMO(I don't use semicolons or redundant parenthesis but that's not the point here).
Same for color and video mode, both are just few bytes of numbers, color could be a single number or four numbers and video mode could be replaced by 3 number args with one being optional.
math.random(256)-1
is same result and number of characters as
math.random(0,255)
but that one is more clear. ;)
Maybe also creating preloader for SFML module and removing sf from front so it'd be possible to do:
local sf = require"SFMLua"
--use sf.ClassName just like a c++ namespace
Perhaps there could be even some Lua code to go along the binding, for example, iterator for events.
event = sfEvent.new();
while window:pollEvent(event) do
-- do stuff...
end
vs(iterative + non c++ events).
for event in window:iterateEvents() do
-- do stuff...
end
Iterate events could be:
function iterateEvents(self)
local event = {}
return function()
if self:pollEvent(event) then return event end
end
end
I think.. I'm writing from memory ;) , I'm still lacking in Lua.
There are no enums in Lua anyway, the expression
sfEventType.KeyReleased
is just a shortcut for
sfEventType["KeyReleased"]
i.e. you use a string as table key.
I wonder however if it would be worth investigating an error reporting mechanism if an invalid string is used?
if event.key.code == "Backspace" -- correct would be "BackSpace"
will just always be false, and such errors may be tedious to debug.
There are no enums in Lua anyway, the expression
sfEventType.KeyReleased
is just a shortcut for
sfEventType["KeyReleased"]
i.e. you use a string as table key.
Believe me, I know. :P It's still a convention in Lua to pass and return strings not numbers as enums.
And == "KeyReleased" is* one compare and constant(in 5.2 at least, that's what I have) and == sfEventType.KeyReleased is fetching the table by name, then fetching a field by name and then comparing(= two constants, two fetches from table, one compare).
Also it's longer and uglier.
Same for event:type() vs. event.type, first is fetch 'event', fetch 'type' for self call and (cross language) call, second is fetch event, fetch field.
event:key():code() is even worse: fetch, self, call, self, call vs. event.code fetch, fetch.
I wonder however if it would be worth investigating an error reporting mechanism if an invalid string is used?
Lua doesn't do that in it's own library.
Also the first approach has the same error, if you use wrong key you get nil that'll compare as different from anything. ;D
*checked by luac.