Bootstap Script

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
peaches
Prole
Posts: 9
Joined: Tue Jul 28, 2009 7:47 pm

Bootstap Script

Post by peaches »

So I've been using Love for 24 hours, and it's great! Coming over from Processing (http://www.processing.org), it's refreshing to find a similar community with a cleaner scripting language and more rich physics and geometry.

My only gripe is how verbose the platform API is (love.blah.blah), so I've been writing a bootstrap script to flatten the namespaces (lua, to the rescue!). Right now, it looks like this:

Code: Select all

default_mod = default_mod or _G

function GetFunctionsFrom(mod, into)
	into = into or default_mod
	for k,v in pairs(mod) do
		if type(v) == "function" then
			into[k:sub(1,1):upper()..k:sub(2,-1)] = v
		end
	end
end

function GetConstantsFrom(mod, into)
	into = into or default_mod
	for k,v in pairs(mod) do
		t = type(v)
		if t == "number" or t == "string" then
			into[k:upper()] = v
		end
	end
end

GetConstantsFrom(love)
GetFunctionsFrom(love.graphics)
GetFunctionsFrom(love.audio)
GetFunctionsFrom(love.physics)

input = {}
GetFunctionsFrom(love.mouse, input)
GetFunctionsFrom(love.keyboard, input)
GetFunctionsFrom(love.joystick, input)
I chose to Pascal-Case things both to avoid naming conflicts, and because it's my preference, coming from a C++ background.

Once run, code is much more economical:

Code: Select all

love.filesystem.require("bootstrap.lua")

function load()
	image = NewImage("icon.png")
	font = NewFont(DEFAULT_FONT, 12)
	SetFont(font)
	SetCaption("Hello, World")
	SetBackgroundColor(155,155,255)
	input.SetVisible(false)
end

function draw()
	SetColor(200,200,255)
	Circle(DRAW_FILL, 0.65*GetWidth(), GetHeight()+400, 750, 75)
	SetColor(255,255,255)
	Drawf("Hello, World!",input.GetX()-50, input.GetY()-35, 100, ALIGN_CENTER)
	Draw(image, input.GetX(), input.GetY())
end
Pointers, anyone?

Again, <3 <3 <3!!! :nyu:
User avatar
JSharpe
Prole
Posts: 6
Joined: Mon Jul 27, 2009 4:48 pm
Location: UK
Contact:

Re: Bootstap Script

Post by JSharpe »

It looks nice for small quick scripts. I may use it. Though i couldn't see me using this for a larger project, it would get far too confusing with such minimal information on what functions i'm calling unless i documented each line. I'm also new to LOVE and found the system it uses rather confusing at first, but i got use to it quickly.

Good job with it though :)
peaches
Prole
Posts: 9
Joined: Tue Jul 28, 2009 7:47 pm

Re: Bootstap Script

Post by peaches »

I've added some lua-magic for declaring classes:

Code: Select all

function DeclareClass(class)
	class.__index = class
	class.new = function(self,o)
		o = o or {}
		setmetatable(o,self)
		if class.initialize then
			o:initialize()		
		end
		return o
	end
end
So in app code:

Code: Select all

love.filesystem.require("bootstrap.lua")

Point = { x=0, y=0 }

function Point:initialize()
	print("creating a new point ("..self.x..","..self.y..")")
end

function Point:length()
	return math.sqrt(self.x*self.x + self.y*self.y)
end

DeclareClass(Point)

p = Point:new{x=7}
print(p:Length())
It's not too different from the stuff in the lua reference manual, but I'm having trouble defining DeclareSubclass(sub,base). Any tips?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Bootstap Script

Post by Robin »

For all that (and more) you could just use SECS, the class system by our own bartbes.
Declaring classes is done by replacing

Code: Select all

Point = {}
DeclareClass(Point)
by

Code: Select all

Point = class:new()
And subclassing is done by

Code: Select all

subclass = baseclass:new()
Help us help you: attach a .love.
User avatar
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

Re: Bootstap Script

Post by Zorbatron »

Yeah, I really wish rude or someone would flatten the name spaces, honestly I don't see why there has to be love.*, I think it should be graphics.*, filesystem.*, ect.

I made a class/deriving engine on my own without referencing anything other than my experience with C++. I'm a freak when it comes to optimizing scripting code, espeically at times like this. Since classes will be used so frequently I feel it is important they are as simple and optimized as possible while offering maximum flexibility.

My class dervice system can have any class derive as many times as it wants! With no added overhead!
It does this by adding my own custom command #IMPORT (args) and #EXPORT which is parsed by what you could call my custom 'preprocessor' all implmented in lua of course.

Since all my classes are inside a folder, with the filename as their actual name, it is easy for my preprocessor parser to determine what class and what function to import. I would explain this more indepth if someone wants me to. But overall, it copies the code for you into deriving classes and refactors the variables as specified by the deriving (sub) class. This is the most efficent way to do it I belive. I have tried other methods, but have not been able to come up with a method that allows instances to be created with no extra overhead (extra tables for each base class).
peaches
Prole
Posts: 9
Joined: Tue Jul 28, 2009 7:47 pm

Re: Bootstap Script

Post by peaches »

@Robin - thank you, you're LOVEly! ;) I ended up basically deriving his "basic" version through my own muddling, but I didn't know about the ellipsis (...) vararg syntax for the constructor. Score!

@Zorbatron - Micro-optimization is the meat of my day-job. LOVE is for blissfully indulging in the belief that the script cost is negligible and I can just MAKE COOL STUFF FAST. When one starts worrying about the cost of a table lookup, he's stopped thinking about how FAST that STUFF can be made COOLER.
peaches
Prole
Posts: 9
Joined: Tue Jul 28, 2009 7:47 pm

Re: Bootstap Script

Post by peaches »

In fairness, my last reply was a bit hypocritical. The first thing I did with love was read the C++ source and cringe at the use of STL containers. We're only human, teehee.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Bootstap Script

Post by bartbes »

@robin: thanks!

well, love isn't optimized, but then again, it's still beta (or pre-alpha, i'd link to the roadmap if i weren't on a psp)
User avatar
Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

Re: Bootstap Script

Post by Pliskin09 »

i dont mind how its done. its easy to remember :D

i love graphics (love.graphics), i love keyboard (love.keyboard), i love mouse (love.mouse) etc, i love LÖVE lol :ultraglee:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests