
Code: Select all
EnableExplicit
;TinyObj by Mijikai
;------------------------------------------------------------------ TINY OBJ STRUCTURES
Structure OOP_OBJECT_STRUCT
Map *method();<- stores the functions of an object
EndStructure
Structure OOP_STRUCT
Map *object.OOP_OBJECT_STRUCT();<- stores the objects
EndStructure
Global.OOP_STRUCT oop;<- object pool
;------------------------------------------------------------------ TINY OBJ MACROS
Macro objCreate(_object_,_type_,_pointer_)
_pointer_ = AllocateStructure(_type_):If _pointer_:oop\object(_object_) = _pointer_:EndIf
EndMacro
Macro objMethod(_object_,_name_,_function_)
oop\object(_object_)\method(_name_) = _function_
EndMacro
Macro objCall(_object_,_method_,_a_ = 0,_b_ = 0,_c_ = 0,_d_ = 0);<- hack prob. unsafe (limited to 4 parameter)!
CallFunctionFast(oop\object(_object_)\method(_method_),oop\object(_object_),_a_,_b_,_c_,_d_)
EndMacro
Macro objDestroy(_object_)
FreeStructure(oop\object(_object_)):DeleteMapElement(oop\object(),_object_)
EndMacro
;------------------------------------------------------------------ TINY OBJ EXAMPLE
Structure BALL_STRUCT Extends OOP_OBJECT_STRUCT;<- new ball object
x.i
y.i
name.s
EndStructure
Procedure.i ballAdd(*obj.BALL_STRUCT,x.i,y.i)
With *obj
\x + X
\y + y
EndWith
EndProcedure
Procedure.i ballSub(*obj.BALL_STRUCT,x.i,y.i)
With *obj
\x - x
\y - y
EndWith
EndProcedure
Procedure.i ballRename(*obj.BALL_STRUCT,*name)
With *obj
\name = PeekS(*name)
EndWith
EndProcedure
Procedure.i Main()
Protected.BALL_STRUCT *obj
With *obj
objCreate("ball",BALL_STRUCT,*obj);<- create the object
objMethod("ball","add",@ballAdd());<- add methods for this object
objMethod("ball","sub",@ballSub())
objMethod("ball","rename",@ballRename())
objCall("ball","add",10,20);<- call a method
Debug Str(\x) + " x " + Str(\y)
objCall("ball","sub",5,5)
Debug Str(\x) + " x " + Str(\y)
objCall("ball","rename",@"Hello World!")
Debug \name
objDestroy("ball")
ProcedureReturn #Null
EndWith
EndProcedure
End Main()