Object的基本构成、创建&销毁&行为函数的定义、属性的赋值、以及相关注意事项。
#include "ext.h" // standard Max include, always required
#include "ext_obex.h" // required for new style Max object// object struct,定义属性
typedef struct _simplemaxDiffTypes {t_object ob; // the object itself (must be first)/* 自定义属性 */long value_long;double value_float;
} t_simplemaxDiffTypes;// 必备函数
void* simplemaxDiffTypes_new(t_symbol* s, long argc, t_atom* argv);
void simplemaxDiffTypes_free(t_simplemaxDiffTypes* x);
void simplemaxDiffTypes_assist(t_simplemaxDiffTypes* x, void* b, long m, long a, char* s);
// 自定义函数
void simplemaxDiffTypes_int(t_simplemaxDiffTypes* x, long l);
void simplemaxDiffTypes_double(t_simplemaxDiffTypes* x, double f);
void simplemaxDiffTypes_bang(t_simplemaxDiffTypes* x);// global class pointer variable
void* simplemaxDiffTypes_class;void ext_main(void* r) {t_class* c;// "simplemaxDiffTypes" 建议和项目名一样,否则在max/msp中创建自定义组件会出问题。c = class_new("simplemaxDiffTypes", (method)simplemaxDiffTypes_new, (method)simplemaxDiffTypes_free,(long)sizeof(t_simplemaxDiffTypes), 0L /* leave NULL!! */, A_GIMME, 0);/* you CAN'T call this from the patcher */class_addmethod(c, (method)simplemaxDiffTypes_assist, "assist", A_CANT, 0);// 自定义方法class_addmethod(c, (method)simplemaxDiffTypes_int, "int", A_LONG, 0); // 接收入口传入的long型数据,引号中必须写intclass_addmethod(c, (method)simplemaxDiffTypes_double, "float", A_FLOAT, 0); // 接收入口传入的double型数据class_addmethod(c, (method)simplemaxDiffTypes_bang, "bang", 0); // 接收入口传入的bang消息class_register(CLASS_BOX, c); /* CLASS_NOBOX */simplemaxDiffTypes_class = c;post("I am the simplemaxDiffTypes object");
}void simplemaxDiffTypes_assist(t_simplemaxDiffTypes* 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 simplemaxDiffTypes_free(t_simplemaxDiffTypes* x) {;
}// 要操作Object中的属性,第一个形参通常为Object的指针
void simplemaxDiffTypes_int(t_simplemaxDiffTypes* x, long l) { // 接收入口传入的long型数据,为value_long赋值x->value_long = l;
}void simplemaxDiffTypes_double(t_simplemaxDiffTypes* x, double f) { // 注意,形参类型绝不能写错. 比如这里不能用float接受object中定义的double,否则无法传参.x->value_float = f;
}void simplemaxDiffTypes_bang(t_simplemaxDiffTypes* x) {post("value_long: %d, value_float: %lf", x->value_long, x->value_float);
}/* argc指在创建组件时,直接跟在组件后面的参数个数;argv存储参数具体值*/
void* simplemaxDiffTypes_new(t_symbol* s, long argc, t_atom* argv) { // 分配内存创建object,初始化属性,创建入口和出口(默认一个入口,无出口)t_simplemaxDiffTypes* x = NULL;long i;if ((x = (t_simplemaxDiffTypes*)object_alloc(simplemaxDiffTypes_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->value_long = 1;x->value_float = 2.2;for (i = 0; i < argc; i++) { // 打印出创建object时跟在Object名后的参数的详细信息if ((argv + i)->a_type == A_LONG) {object_post((t_object*)x, "arg %ld: long (%ld)", i, atom_getlong(argv + i));} else if ((argv + i)->a_type == A_FLOAT) {object_post((t_object*)x, "arg %ld: float (%f)", i, atom_getfloat(argv + i));} else if ((argv + i)->a_type == A_SYM) {object_post((t_object*)x, "arg %ld: symbol (%s)", i, atom_getsym(argv + i)->s_name);} else {object_error((t_object*)x, "forbidden argument");}}}return (x);
}
运行结果:
1. 创建Object时,打印出创建object时跟在Object名后的参数的详细信息。
2. 传参测试: