MAX/MSP SDK学习01:Object的基本构成、创建&销毁&行为函数的定义、属性的赋值、以及相关注意事项

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型数据,引号中必须写int
	class_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) { // inlet
		sprintf(s, "I am inlet %ld", a);
	} else {	// outlet
		sprintf(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;

	x = (t_simplemaxDiffTypes*)object_alloc(simplemaxDiffTypes_class);
	
    // 属性赋初值
	x->value_long = 1;
	x->value_float = 2.2;

	return (x);
}

运行结果:

1. 创建Object时,打印出创建object时跟在Object名后的参数的详细信息。

2. 传参测试:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟大的马师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值