91
Gemini Lua Scripting for iOS Games James Norton @jnorton Lua Workshop 2012 Saturday, December 1, 12

Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

GeminiLua Scripting for

iOS Games

James Norton@jnorton

Lua Workshop 2012

Saturday, December 1, 12

Page 2: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Gemini

Saturday, December 1, 12

Page 3: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Gemini

• Provides Lua bindings for 2D game dev

Saturday, December 1, 12

Page 4: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Gemini

• Provides Lua bindings for 2D game dev

• Allows games to be coded entirely in Lua

Saturday, December 1, 12

Page 5: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Gemini

• Provides Lua bindings for 2D game dev

• Allows games to be coded entirely in Lua

• Consists of a set of Lua C libraries plus Objective C project code and templates

Saturday, December 1, 12

Page 6: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Gemini

• Provides Lua bindings for 2D game dev

• Allows games to be coded entirely in Lua

• Consists of a set of Lua C libraries plus Objective C project code and templates

• Similar API to the Corona™ SDK

Saturday, December 1, 12

Page 7: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Gemini

• Provides Lua bindings for 2D game dev

• Allows games to be coded entirely in Lua

• Consists of a set of Lua C libraries plus Objective C project code and templates

• Similar API to the Corona™ SDK

• Open source with MIT License

Saturday, December 1, 12

Page 8: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Why use scripting?

Saturday, December 1, 12

Page 9: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Why use scripting?

• Provides higher abstraction that can increase productivity.

Saturday, December 1, 12

Page 10: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Why use scripting?

• Provides higher abstraction that can increase productivity.

• Level designers and non-programmers can work with it.

Saturday, December 1, 12

Page 11: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Why use scripting?

• Provides higher abstraction that can increase productivity.

• Level designers and non-programmers can work with it.

• Functionality can be changed without recompiling during development.

Saturday, December 1, 12

Page 12: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Features

Saturday, December 1, 12

Page 13: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Features

• 2D Layer based rendering

Saturday, December 1, 12

Page 14: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Features

• 2D Layer based rendering

• Event Driven

Saturday, December 1, 12

Page 15: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Features

• 2D Layer based rendering

• Event Driven

• Graphics elements

Saturday, December 1, 12

Page 16: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Features

• 2D Layer based rendering

• Event Driven

• Graphics elements

• Physics

Saturday, December 1, 12

Page 17: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Features

• 2D Layer based rendering

• Event Driven

• Graphics elements

• Physics

• Sound

Saturday, December 1, 12

Page 18: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Features

• 2D Layer based rendering

• Event Driven

• Graphics elements

• Physics

• Sound

• Scene Management

Saturday, December 1, 12

Page 19: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Layers

• Provide depth

• Parallax effect

• Blending functions

• Render callbacks

Saturday, December 1, 12

Page 20: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Layer Blending

• Use any OpenGL blending functions

• Example: alpha blend (transparency)

• (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

• Example: additive blend (particle system)

• (GL_ONE, GL_ONE)

Saturday, December 1, 12

Page 21: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Render Callbacks

• Render layers using custom Objective C code / OpenGL

• Example: scrolling background

Frame 1 (Lua)

Frame 0 (C/C++/Objective C)

Saturday, December 1, 12

Page 22: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Events

Saturday, December 1, 12

Page 23: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Events

• EnterFrame - fires every render loop

Saturday, December 1, 12

Page 24: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Events

• EnterFrame - fires every render loop

• Touch - touch events with phases

Saturday, December 1, 12

Page 25: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Events

• EnterFrame - fires every render loop

• Touch - touch events with phases

• Scene events

Saturday, December 1, 12

Page 26: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Events

• EnterFrame - fires every render loop

• Touch - touch events with phases

• Scene events

• Timer - execute code after a delay

Saturday, December 1, 12

Page 27: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Events

• EnterFrame - fires every render loop

• Touch - touch events with phases

• Scene events

• Timer - execute code after a delay

• Physics events - collisions

Saturday, December 1, 12

Page 28: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Graphics Elements

• Sprites

• Lines

• Rectangles

• Circles

• Polygons

• Text via Text Candy™

• Display Groups

Saturday, December 1, 12

Page 29: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Physics

• Uses Box2D Physics

• All graphics elements can have physics properties

• Bodies, fixtures, joints, forces, etc.

• All Box2D body types supported (dynamic, static, kinematic)

• Collision events

Saturday, December 1, 12

Page 30: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Sound

• Based on the ObjectiveAL API

• Sound Effects

• Music

Saturday, December 1, 12

Page 31: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Transitions

• Alter a display objects properties over time

• Position, size, color, etc.

• Can be used to product “canned” animations or effects

Saturday, December 1, 12

Page 32: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Transitions

• Alter a display objects properties over time

• Position, size, color, etc.

• Can be used to product “canned” animations or effects

Saturday, December 1, 12

Page 33: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Scene ManagementThe Director API

Saturday, December 1, 12

Page 34: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Scene ManagementThe Director API

• Scenes are collections of layers and the objects they contain

Saturday, December 1, 12

Page 35: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Scene ManagementThe Director API

• Scene transition effects

• slide • page curl

• Scenes are collections of layers and the objects they contain

Saturday, December 1, 12

Page 36: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Scene ManagementThe Director API

• Scene transition effects

• createScene

• sceneWillEnter• sceneEntered

• sceneWillExit• sceneExited

• destroyScene

• Scene management Events

• slide • page curl

• Scenes are collections of layers and the objects they contain

Saturday, December 1, 12

Page 37: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Implementation

Saturday, December 1, 12

Page 38: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Implementation

• Libraries of C methods that delegate to methods on Objective C objects

Saturday, December 1, 12

Page 39: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Implementation

• Libraries of C methods that delegate to methods on Objective C objects

• Libraries includes factory method to create objects

Saturday, December 1, 12

Page 40: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Implementation

• Libraries of C methods that delegate to methods on Objective C objects

• Libraries includes factory method to create objects

• Use metatables to define custom “types” e.g., createMetatable(L, GEMINI_RECTANGLE_LUA_KEY, rectangle_m);

Saturday, December 1, 12

Page 41: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Custom Lua Types

Saturday, December 1, 12

Page 42: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Custom Lua Types

• Wrap Objective C functionality in Lua “objects”

Saturday, December 1, 12

Page 43: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Custom Lua Types

• Wrap Objective C functionality in Lua “objects”

• Create, manipulate, and destroy Objective C objects from Lua

Saturday, December 1, 12

Page 44: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Custom Lua Types

• Wrap Objective C functionality in Lua “objects”

• Create, manipulate, and destroy Objective C objects from Lua

• Custom types defined in C “libraries” with library factory methods as well as instance methods

Saturday, December 1, 12

Page 45: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Representing Objective C Types in Lua

• Userdata - provides a block of raw memory with no predefined Lua operations

• Can store anything here - we will store pointer to our Objective C object

Saturday, December 1, 12

Page 46: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Representing Objective C Types in Lua

• Userdata - provides a block of raw memory with no predefined Lua operations

• Can store anything here - we will store pointer to our Objective C object

lua_newuserdata(lua_State *, size_t size)

Saturday, December 1, 12

Page 47: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Metatables

Saturday, December 1, 12

Page 48: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Metatables

• Tables that can change the behavior of other tables or userdata

Saturday, December 1, 12

Page 49: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Metatables

• Tables that can change the behavior of other tables or userdata

• Used to assign a ‘type’ to userdata

Saturday, December 1, 12

Page 50: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Metatables

• Tables that can change the behavior of other tables or userdata

• Used to assign a ‘type’ to userdata

• Can hold method mappings for userdata

Saturday, December 1, 12

Page 51: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Gemini Classes

Saturday, December 1, 12

Page 52: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

GemObject.h@interface GemObject : NSObject { NSMutableDictionary *eventHandlers; lua_State *L; int selfRef; int propertyTableRef; int eventListenerTableRef; NSString *name;}

@property (nonatomic) int selfRef;@property (nonatomic) int propertyTableRef;@property (nonatomic) int eventListenerTableRef;@property (readonly) lua_State *L;@property (nonatomic, retain) NSString *name;

-(id) initWithLuaState:(lua_State *)luaState LuaKey:(const char *)luaKey;-(BOOL)getBooleanForKey:(const char*) key withDefault:(BOOL)dflt;-(double)getDoubleForKey:(const char*) key withDefault:(double)dflt;-(int)getIntForKey:(const char*) key withDefault:(int)dflt;-(NSString *)getStringForKey:(const char*) key withDefault:(NSString *)dflt;-(void)setBOOL:(BOOL)val forKey:(const char*) key;-(void)setDouble:(double)val forKey:(const char*) key;-(void)setInt:(int)val forKey:(const char*) key;-(void)setString:(NSString *)val forKey:(const char*) key;-(BOOL)handleEvent:(GemEvent *)event;@end

Saturday, December 1, 12

Page 53: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

GemObject Initializer-(id) initWithLuaState:(lua_State *)luaState LuaKey:(const char *)luaKey { self = [super init]; if (self) { L = luaState; __unsafe_unretained GemObject **lgo = (__unsafe_unretained GemObject **)lua_newuserdata(L, sizeof(self)); *lgo = self;

} return self;}

Saturday, December 1, 12

Page 54: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

GemObject Initializer-(id) initWithLuaState:(lua_State *)luaState LuaKey:(const char *)luaKey { self = [super init]; if (self) { L = luaState; __unsafe_unretained GemObject **lgo = (__unsafe_unretained GemObject **)lua_newuserdata(L, sizeof(self)); *lgo = self; luaL_getmetatable(L, luaKey); lua_setmetatable(L, -2);

} return self;}

Saturday, December 1, 12

Page 55: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

GemObject Initializer-(id) initWithLuaState:(lua_State *)luaState LuaKey:(const char *)luaKey { self = [super init]; if (self) { L = luaState; __unsafe_unretained GemObject **lgo = (__unsafe_unretained GemObject **)lua_newuserdata(L, sizeof(self)); *lgo = self; luaL_getmetatable(L, luaKey); lua_setmetatable(L, -2); // append a lua table to this user data to allow the user to store values in it lua_newtable(L); lua_pushvalue(L, -1); // make a copy of the table becaue the next line pops the top value // store a reference to this table so our object methods can access it propertyTableRef = luaL_ref(L, LUA_REGISTRYINDEX); // set the table as the user value for the Lua object lua_setuservalue(L, -2);

} return self;}

Saturday, December 1, 12

Page 56: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

GemObject Initializer-(id) initWithLuaState:(lua_State *)luaState LuaKey:(const char *)luaKey { self = [super init]; if (self) { L = luaState; __unsafe_unretained GemObject **lgo = (__unsafe_unretained GemObject **)lua_newuserdata(L, sizeof(self)); *lgo = self; luaL_getmetatable(L, luaKey); lua_setmetatable(L, -2); // append a lua table to this user data to allow the user to store values in it lua_newtable(L); lua_pushvalue(L, -1); // make a copy of the table becaue the next line pops the top value // store a reference to this table so our object methods can access it propertyTableRef = luaL_ref(L, LUA_REGISTRYINDEX); // set the table as the user value for the Lua object lua_setuservalue(L, -2); // create a table for the event listeners lua_newtable(L); eventListenerTableRef = luaL_ref(L, LUA_REGISTRYINDEX);

} return self;}

Saturday, December 1, 12

Page 57: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

GemObject Initializer-(id) initWithLuaState:(lua_State *)luaState LuaKey:(const char *)luaKey { self = [super init]; if (self) { L = luaState; __unsafe_unretained GemObject **lgo = (__unsafe_unretained GemObject **)lua_newuserdata(L, sizeof(self)); *lgo = self; luaL_getmetatable(L, luaKey); lua_setmetatable(L, -2); // append a lua table to this user data to allow the user to store values in it lua_newtable(L); lua_pushvalue(L, -1); // make a copy of the table becaue the next line pops the top value // store a reference to this table so our object methods can access it propertyTableRef = luaL_ref(L, LUA_REGISTRYINDEX); // set the table as the user value for the Lua object lua_setuservalue(L, -2); // create a table for the event listeners lua_newtable(L); eventListenerTableRef = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushvalue(L, -1); // make another copy of the userdata since the next line will pop it off selfRef = luaL_ref(L, LUA_REGISTRYINDEX); } return self;}

Saturday, December 1, 12

Page 58: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

Saturday, December 1, 12

Page 59: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

• Libraries consist of two classes of functions

Saturday, December 1, 12

Page 60: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

• Libraries consist of two classes of functions

• Library functions

Saturday, December 1, 12

Page 61: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

• Libraries consist of two classes of functions

• Library functions

• Factory methods

Saturday, December 1, 12

Page 62: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

• Libraries consist of two classes of functions

• Library functions

• Factory methods

• Methods that effect global state

Saturday, December 1, 12

Page 63: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

• Libraries consist of two classes of functions

• Library functions

• Factory methods

• Methods that effect global state

• Object methods attached via metatables

Saturday, December 1, 12

Page 64: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

• Libraries consist of two classes of functions

• Library functions

• Factory methods

• Methods that effect global state

• Object methods attached via metatables

• :setFillColor, :insert, etc.

Saturday, December 1, 12

Page 65: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

The Library Bindings

• Libraries consist of two classes of functions

• Library functions

• Factory methods

• Methods that effect global state

• Object methods attached via metatables

• :setFillColor, :insert, etc.

• index, newIndex, __gc, etc.

Saturday, December 1, 12

Page 66: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Important typedefs for Registering a Library

typedef struct luaL_Reg { const char *name; lua_CFunction func;} luaL_Reg;

typedef int (*lua_CFunction) (lua_State *L);

Saturday, December 1, 12

Page 67: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Library Binding Function

int luaopen_display_lib (lua_State *L){

}

Saturday, December 1, 12

Page 68: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Library Binding Function

int luaopen_display_lib (lua_State *L){ // create meta tables for our various types /////////

// .... other types not shown for brevity /////////// // lines createMetatable(L, GEMINI_LINE_LUA_KEY, line_m); // rectangles createMetatable(L, GEMINI_RECTANGLE_LUA_KEY, rectangle_m); /////// finished with metatables ///////////

}

Saturday, December 1, 12

Page 69: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Library Binding Function

int luaopen_display_lib (lua_State *L){ // create meta tables for our various types /////////

// .... other types not shown for brevity /////////// // lines createMetatable(L, GEMINI_LINE_LUA_KEY, line_m); // rectangles createMetatable(L, GEMINI_RECTANGLE_LUA_KEY, rectangle_m); /////// finished with metatables /////////// // create the table for this library and populate it with

// our functions luaL_newlib(L, displayLib_f); return 1;}

Saturday, December 1, 12

Page 70: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Display Library Function Mapping

// the mappings for the library functionsstatic const struct luaL_Reg displayLib_f [] = { {"newLayer", newLayer}, {"newGroup", newDisplayGroup}, {"newLine", newLine}, {"newRect", newRectangle}, {"newCircle", newCircle}, {"newShape", newShape}, {NULL, NULL}};

Saturday, December 1, 12

Page 71: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Mapping for the Rectangle Methods

// mappings for the rectangle methodsstatic const struct luaL_Reg rectangle_m [] = { {"__gc", genericGC}, {"__index", rectangleIndex}, {"__newindex", rectangleNewIndex}, {"setFillColor", rectangleSetFillColor}, {"setGradient", rectangleSetGradient}, {"setStrokeColor", rectangleSetStrokeColor}, {"setStrokeWidth", rectangleSetStrokeWidth}, {"delete", genericDelete}, {"addEventListener", addEventListener}, {"removeEventListener", removeEventListener}, {"applyForce", applyForce}, {NULL, NULL}};

Saturday, December 1, 12

Page 72: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Rectangle Factory Method

local rectangle = display.newRect(x, y, width, height)

Lua:

Saturday, December 1, 12

Page 73: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Rectangle Factory Method

static int newRectangle(lua_State *L){ GLfloat x = luaL_checknumber(L, 1); GLfloat y = luaL_checknumber(L, 2); GLfloat width = luaL_checknumber(L, 3); GLfloat height = luaL_checknumber(L, 4);

GemRectangle *rect = [[GemRectangle alloc] initWithLuaState:L X:x Y:y Width:width Height:height];

[[((GemGLKViewController *)([Gemini shared].viewController)).director getDefaultScene] addObject:rect];

return 1;}

local rectangle = display.newRect(x, y, width, height)

Lua:

C Factory Method:

Saturday, December 1, 12

Page 74: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Adding Object Bindings

Saturday, December 1, 12

Page 75: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Adding Object Bindings

• Cannot use dynamic libraries on iOS

Saturday, December 1, 12

Page 76: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Adding Object Bindings

• Cannot use dynamic libraries on iOS

• Must use static linking

Saturday, December 1, 12

Page 77: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Adding Object Bindings

• Cannot use dynamic libraries on iOS

• Must use static linking

• Best way to do this is to modify linit.c

Saturday, December 1, 12

Page 78: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Opening Our Library in linit.c

static const luaL_Reg loadedlibs[] = { {"_G", luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_COLIBNAME, luaopen_coroutine}, {LUA_TABLIBNAME, luaopen_table}, {LUA_IOLIBNAME, luaopen_io}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, {LUA_BITLIBNAME, luaopen_bit32}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, {NULL, NULL}};

Saturday, December 1, 12

Page 79: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Opening Our Library in linit.c

extern int luaopen_display_lib (lua_State *L);

static const luaL_Reg loadedlibs[] = { {"_G", luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_COLIBNAME, luaopen_coroutine}, {LUA_TABLIBNAME, luaopen_table}, {LUA_IOLIBNAME, luaopen_io}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, {LUA_BITLIBNAME, luaopen_bit32}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, {NULL, NULL}};

Saturday, December 1, 12

Page 80: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Opening Our Library in linit.c

extern int luaopen_display_lib (lua_State *L);

static const luaL_Reg loadedlibs[] = { {"_G", luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_COLIBNAME, luaopen_coroutine}, {LUA_TABLIBNAME, luaopen_table}, {LUA_IOLIBNAME, luaopen_io}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, {LUA_BITLIBNAME, luaopen_bit32}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, "display", luaopen_my_math_lib, {NULL, NULL}};

Saturday, December 1, 12

Page 81: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

Demos

Saturday, December 1, 12

Page 82: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?

Saturday, December 1, 12

Page 83: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

Saturday, December 1, 12

Page 84: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

• More scene transitions

Saturday, December 1, 12

Page 85: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

• More scene transitions

• Particle system

Saturday, December 1, 12

Page 86: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

• More scene transitions

• Particle system

• Built in text support

Saturday, December 1, 12

Page 87: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

• More scene transitions

• Particle system

• Built in text support

• Multithreading

Saturday, December 1, 12

Page 88: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

• More scene transitions

• Particle system

• Built in text support

• Multithreading

• Physics

Saturday, December 1, 12

Page 89: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

• More scene transitions

• Particle system

• Built in text support

• Multithreading

• Physics

• Scene loading

Saturday, December 1, 12

Page 90: Gemini Lua Scripting for iOS Games · Gemini • Provides Lua bindings for 2D game dev • Allows games to be coded entirely in Lua • Consists of a set of Lua C libraries plus Objective

What’s Next?• New features

• More scene transitions

• Particle system

• Built in text support

• Multithreading

• Physics

• Scene loading

• Support for more third party tools (level builders, etc.)

Saturday, December 1, 12