本文主要是介绍MAX/MSP SDK学习04:Messages selector的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
其实消息选择器在simplemax示例中就接触到了,但这文档非要讲那么抽象。目前为止对消息选择器的理解是:可判断接收过来的消息是否符合本Object的处理要求,比如加法对象只可接收数值型的消息以处理,但不能接收t_symbol型的消息,如下:
1. Messages selector
如下面自定义的myObjectABC可从出口发送消息选择器为 "ABC" 的消息,myObjectInt可发送消息选择器为 "int" 的消息。MAX/MSP SDK学习03:Atoms and Messages的使用中的IsMatchABC组件可接收处理 "ABC" 消息和 "int" 消息。
myObjectABC:
#include "ext.h" // standard Max include, always required
#include "ext_obex.h" // required for new style Max objecttypedef struct _myObjectABC {t_object ob; // the object itself (must be first)void* outLet; // 出口
} t_myObjectABC;// function prototypes
void* myObjectABC_new(t_symbol* s, long argc, t_atom* argv);
void myObjectABC_free(t_myObjectABC* x);
void myObjectABC_assist(t_myObjectABC* x, void* b, long m, long a, char* s);
void myObjectABC_bang(t_myObjectABC* x);// global class pointer variable
void* myObjectABC_class;void ext_main(void* r) {t_class* c;// "myObjectABC" 建议和项目名一样,否则在max/msp中创建自定义组件会出问题c = class_new("myObjectABC", (method)myObjectABC_new, (method)myObjectABC_free, (long)sizeof(t_myObjectABC),0L /* leave NULL!! */, A_GIMME, 0);/* you CAN'T call this from the patcher */// Object将接收到的消息(int、bang、assist等)与消息选择器中的对比,比对成功则可调用相关C方法,否则"Object dosen't understand message"// A_CANT、A_LONG等指定typed方法,A_GIMME:a list of atoms,适用于超过四个参数或者多个浮点数class_addmethod(c, (method)myObjectABC_assist, "assist", A_CANT, 0);class_addmethod(c, (method)myObjectABC_bang, "bang", 0);class_register(CLASS_BOX, c); /* CLASS_NOBOX */myObjectABC_class = c;post("I am the myObjectABC object");
}void myObjectABC_assist(t_myObjectABC* x, void* b, long m, long a, char* s) {if (m == ASSIST_INLET) { // inletsprintf(s, "I am inlet %ld", a);} else { // outletsprintf(s, "I am outlet %ld", a);}
}void myObjectABC_free(t_myObjectABC* x) {;
}/* argc指在创建组件时,直接跟在组件后面的参数个数;argv存储参数具体值*/
void* myObjectABC_new(t_symbol* s, long argc, t_atom* argv) { // 分配内存创建object,初始化属性,创建入口和出口(默认一个入口,无出口)t_myObjectABC* x = NULL;long i;if ((x = (t_myObjectABC*)object_alloc(myObjectABC_class))) { // 使用全局指针创建新实例object_post((t_object*)x, "a new %s object was instantiated: %p", s->s_name, x);object_post((t_object*)x, "it has %ld arguments", argc);x->outLet = outlet_new((t_object*)x, NULL);}return (x);
}void myObjectABC_bang(t_myObjectABC* x) {t_atom argv[3]; // t_atom类型可存储整型、浮点型、t_symbol类型、指针。atom_setlong(argv, 666);atom_setsym(argv + 1, gensym("MYM")); // 字符串需要用gensym转换成t_symbol* 类型atom_setfloat(argv + 2, 0.123);outlet_anything(x->outLet, gensym("ABC"), 3, argv);
}
myObjectInt:
#include "ext.h" // standard Max include, always required
#include "ext_obex.h" // required for new style Max objecttypedef struct _myObjectInt {t_object ob; // the object itself (must be first)void* outLet; // 出口
} t_myObjectInt;// function prototypes
void* myObjectInt_new(t_symbol* s, long argc, t_atom* argv);
void myObjectInt_free(t_myObjectInt* x);
void myObjectInt_assist(t_myObjectInt* x, void* b, long m, long a, char* s);
void myObjectInt_bang(t_myObjectInt* x);// global class pointer variable
void* myObjectInt_class;void ext_main(void* r) {t_class* c;// "myObjectInt" 建议和项目名一样,否则在max/msp中创建自定义组件会出问题c = class_new("myObjectInt", (method)myObjectInt_new, (method)myObjectInt_free, (long)sizeof(t_myObjectInt),0L /* leave NULL!! */, A_GIMME, 0);/* you CAN'T call this from the patcher */// Object将接收到的消息(int、bang、assist等)与消息选择器中的对比,比对成功则可调用相关C方法,否则"Object dosen't understand message"// A_CANT、A_LONG等指定typed方法,A_GIMME:a list of atoms,适用于超过四个参数或者多个浮点数class_addmethod(c, (method)myObjectInt_assist, "assist", A_CANT, 0);class_addmethod(c, (method)myObjectInt_bang, "bang", 0);class_register(CLASS_BOX, c); /* CLASS_NOBOX */myObjectInt_class = c;post("I am the myObjectInt object");
}void myObjectInt_assist(t_myObjectInt* x, void* b, long m, long a, char* s) {if (m == ASSIST_INLET) { // inletsprintf(s, "I am inlet %ld", a);} else { // outletsprintf(s, "I am outlet %ld", a);}
}void myObjectInt_free(t_myObjectInt* x) {;
}/* argc指在创建组件时,直接跟在组件后面的参数个数;argv存储参数具体值*/
void* myObjectInt_new(t_symbol* s, long argc, t_atom* argv) { // 分配内存创建object,初始化属性,创建入口和出口(默认一个入口,无出口)t_myObjectInt* x = NULL;long i;if ((x = (t_myObjectInt*)object_alloc(myObjectInt_class))) { // 使用全局指针创建新实例object_post((t_object*)x, "a new %s object was instantiated: %p", s->s_name, x);object_post((t_object*)x, "it has %ld arguments", argc);x->outLet = outlet_new((t_object*)x, NULL);}return (x);
}void myObjectInt_bang(t_myObjectInt* x) {t_atom argv[3]; // t_atom类型可存储整型、浮点型、t_symbol类型、指针。atom_setlong(argv, 666);atom_setsym(argv + 1, gensym("MYM")); // 字符串需要用gensym转换成t_symbol* 类型atom_setfloat(argv + 2, 0.123);outlet_anything(x->outLet, gensym("int"), 3, argv);
}
将myObjectABC和myObjectInt的出口都接入IsMatchABC的左入口,测试消息选择器的使用。
运行结果:
2. anything method
若某t_symbol消息匹配不到消息选择器,则可定义通用处理方法anything method,如下:
#include "ext.h" // standard Max include, always required
#include "ext_obex.h" // required for new style Max objecttypedef struct _anything {t_object ob; // the object itself (must be first)
} t_anything;void* anything_new(t_symbol* s, long argc, t_atom* argv);
void anything_free(t_anything* x);
void anything_assist(t_anything* x, void* b, long m, long a, char* s);void anything_anything(t_anything* x, t_symbol* s, long argc, t_atom* argv);void* anything_class;void ext_main(void* r) {t_class* c;// "anything" 建议和项目名一样,否则在max/msp中创建自定义组件会出问题c = class_new("anything", (method)anything_new, (method)anything_free, (long)sizeof(t_anything),0L /* leave NULL!! */, A_GIMME, 0);class_addmethod(c, (method)anything_assist, "assist", A_CANT, 0);class_addmethod(c, (method)anything_anything, "anything", A_GIMME, 0);class_register(CLASS_BOX, c); /* CLASS_NOBOX */anything_class = c;post("I am the anything object");
}void anything_assist(t_anything* x, void* b, long m, long a, char* s) {if (m == ASSIST_INLET) { // inletsprintf(s, "I am inlet %ld", a);} else { // outletsprintf(s, "I am outlet %ld", a);}
}void anything_free(t_anything* x) {;
}/* argc指在创建组件时,直接跟在组件后面的参数个数;argv存储参数具体值*/
void* anything_new(t_symbol* s, long argc, t_atom* argv) { // 分配内存创建object,初始化属性,创建入口和出口(默认一个入口,无出口)t_anything* x = NULL;x = (t_anything*)object_alloc(anything_class);return (x);
}void anything_anything(t_anything* x, t_symbol* s, long argc, t_atom* argv) {object_post((t_object*)x, "Invoked by [%s] message", s->s_name);
}
运行结果:
这篇关于MAX/MSP SDK学习04:Messages selector的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!