• Aucun résultat trouvé

Gemini Lua Scripting for iOS Games pdf – Cours et formation gratuit

N/A
N/A
Protected

Academic year: 2022

Partager "Gemini Lua Scripting for iOS Games pdf – Cours et formation gratuit"

Copied!
91
0
0

Texte intégral

(1)

Gemini

Lua Scripting for iOS Games

James Norton

@jnorton

Lua Workshop 2012

(2)

Gemini

Saturday, December 1, 12

(3)

Gemini

Provides Lua bindings for 2D game dev

(4)

Gemini

Provides Lua bindings for 2D game dev

Allows games to be coded entirely in Lua

Saturday, December 1, 12

(5)

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

(6)

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

(7)

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

(8)

Why use scripting?

Saturday, December 1, 12

(9)

Why use scripting?

Provides higher abstraction that can increase productivity.

(10)

Why use scripting?

Provides higher abstraction that can increase productivity.

Level designers and non-programmers can work with it.

Saturday, December 1, 12

(11)

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.

(12)

Features

Saturday, December 1, 12

(13)

Features

2D Layer based rendering

(14)

Features

2D Layer based rendering

Event Driven

Saturday, December 1, 12

(15)

Features

2D Layer based rendering

Event Driven

Graphics elements

(16)

Features

2D Layer based rendering

Event Driven

Graphics elements

Physics

Saturday, December 1, 12

(17)

Features

2D Layer based rendering

Event Driven

Graphics elements

Physics

Sound

(18)

Features

2D Layer based rendering

Event Driven

Graphics elements

Physics

Sound

Scene Management

Saturday, December 1, 12

(19)

Layers

Provide depth

Parallax effect

Blending functions

Render callbacks

(20)

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

(21)

Render Callbacks

Render layers using custom Objective C code / OpenGL

Example: scrolling background

Frame 0

(22)

Events

Saturday, December 1, 12

(23)

Events

EnterFrame - fires every render loop

(24)

Events

EnterFrame - fires every render loop

Touch - touch events with phases

Saturday, December 1, 12

(25)

Events

EnterFrame - fires every render loop

Touch - touch events with phases

Scene events

(26)

Events

EnterFrame - fires every render loop

Touch - touch events with phases

Scene events

Timer - execute code after a delay

Saturday, December 1, 12

(27)

Events

EnterFrame - fires every render loop

Touch - touch events with phases

Scene events

Timer - execute code after a delay

Physics events - collisions

(28)

Graphics Elements

Sprites

Lines

Rectangles

Circles

Polygons

Text via Text Candy™

Display Groups

Saturday, December 1, 12

(29)

Physics

Uses Box2D Physics

All graphics elements can have physics properties

Bodies, fixtures, joints, forces, etc.

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

(30)

Sound

Based on the ObjectiveAL API

Sound Effects

Music

Saturday, December 1, 12

(31)

Transitions

Alter a display objects properties over time

Position, size, color, etc.

Can be used to product “canned”

animations or effects

(32)

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

(33)

Scene Management

The Director API

(34)

Scene Management The Director API

Scenes are collections of layers and the objects they contain

Saturday, December 1, 12

(35)

Scene Management The Director API

Scene transition effects

slide

page curl

Scenes are collections of layers and the objects they contain

(36)

Scene Management The 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

(37)

Implementation

(38)

Implementation

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

Saturday, December 1, 12

(39)

Implementation

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

Libraries includes factory method to create objects

(40)

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

(41)

Custom Lua Types

(42)

Custom Lua Types

Wrap Objective C functionality in Lua

“objects”

Saturday, December 1, 12

(43)

Custom Lua Types

Wrap Objective C functionality in Lua

“objects”

Create, manipulate, and destroy Objective C objects from Lua

(44)

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

(45)

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

(46)

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

(47)

Metatables

(48)

Metatables

Tables that can change the behavior of other tables or userdata

Saturday, December 1, 12

(49)

Metatables

Tables that can change the behavior of other tables or userdata

Used to assign a ‘type’ to userdata

(50)

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

(51)

Gemini Classes

(52)

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

(53)

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;

(54)

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

(55)

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);

(56)

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

(57)

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);

(58)

The Library Bindings

Saturday, December 1, 12

(59)

The Library Bindings

Libraries consist of two classes of functions

(60)

The Library Bindings

Libraries consist of two classes of functions

Library functions

Saturday, December 1, 12

(61)

The Library Bindings

Libraries consist of two classes of functions

Library functions

Factory methods

(62)

The Library Bindings

Libraries consist of two classes of functions

Library functions

Factory methods

Methods that effect global state

Saturday, December 1, 12

(63)

The Library Bindings

Libraries consist of two classes of functions

Library functions

Factory methods

Methods that effect global state

Object methods attached via metatables

(64)

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

(65)

The Library Bindings

Libraries consist of two classes of functions

Library functions

Factory methods

Methods that effect global state

Object methods attached via metatables

(66)

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

(67)

Library Binding Function

int luaopen_display_lib (lua_State *L){

(68)

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

(69)

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

(70)

Display Library Function Mapping

// the mappings for the library functions

static const struct luaL_Reg displayLib_f [] = { {"newLayer", newLayer},

{"newGroup", newDisplayGroup}, {"newLine", newLine},

{"newRect", newRectangle}, {"newCircle", newCircle}, {"newShape", newShape}, {NULL, NULL}

};

Saturday, December 1, 12

(71)

Mapping for the Rectangle Methods

// mappings for the rectangle methods

static const struct luaL_Reg rectangle_m [] = { {"__gc", genericGC},

{"__index", rectangleIndex},

{"__newindex", rectangleNewIndex},

{"setFillColor", rectangleSetFillColor}, {"setGradient", rectangleSetGradient},

{"setStrokeColor", rectangleSetStrokeColor}, {"setStrokeWidth", rectangleSetStrokeWidth}, {"delete", genericDelete},

{"addEventListener", addEventListener},

(72)

Rectangle Factory Method

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

Lua:

Saturday, December 1, 12

(73)

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 local rectangle = display.newRect(x, y, width, height)

Lua:

C Factory Method:

(74)

Adding Object Bindings

Saturday, December 1, 12

(75)

Adding Object Bindings

Cannot use dynamic libraries on iOS

(76)

Adding Object Bindings

Cannot use dynamic libraries on iOS

Must use static linking

Saturday, December 1, 12

(77)

Adding Object Bindings

Cannot use dynamic libraries on iOS

Must use static linking

Best way to do this is to modify linit.c

(78)

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

(79)

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},

(80)

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

(81)

Demos

(82)

What’s Next?

Saturday, December 1, 12

(83)

What’s Next?

New features

(84)

What’s Next?

New features

More scene transitions

Saturday, December 1, 12

(85)

What’s Next?

New features

More scene transitions

Particle system

(86)

What’s Next?

New features

More scene transitions

Particle system

Built in text support

Saturday, December 1, 12

(87)

What’s Next?

New features

More scene transitions

Particle system

Built in text support

Multithreading

(88)

What’s Next?

New features

More scene transitions

Particle system

Built in text support

Multithreading

Physics

Saturday, December 1, 12

(89)

What’s Next?

New features

More scene transitions

Particle system

Built in text support

Multithreading

Physics

(90)

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

(91)

Questions?

https://ithub.com:indiejames/GeminiSDK.git

Blog http://blog.stokedsoftware.com

Twitter @jnorton

Box2D - http://box2d.org

Références

Documents relatifs

Diego received a BEng in Computer Engineering and an MSc in Programming Lan- guages from PUC-Rio, under the supervision of Roberto Ierusalimschy.. He later received an MSc and a PhD

Then the global editing tab can be used to assign an overcurrent device definition to the set, such as from a transformer fusing schedule used at the utility.. Continue with the

[The engine] doesn't know anything about adventure games, or talking, or puzzles, or anything else that makes Grim Fandango the game it is.. It just knows how to render a set

Samsung Game Framework is a Lua script based software framework which is used for development of 2D games for Samsung Consumer devices.. It is a cross platform

Adc The adc library allows LUA script to read analogue value on the specified ADC channel instead of using AT commands.. Pcm The pcm library allows LUA script to switch PCM and

The value of this field, when present, should be a string: if this string contains the letter ‘k’, the keys in the table are weak; if this string contains the letter ‘v’, the

● This is a presentation about how Lua was used in Grim Fandango, an adventure game by LucasArts.. ● I don't work for

io.output ([file]) sets file as default output file (the current output file is not closed); file can be either an open file object or a file name; in the latter case the file is