Max 5 API Reference
00001 /** 00002 @file 00003 plussz2.c - a version of plussz2 that demonstrates the use of proxy inlets. 00004 see plussz.c for more comments and information. 00005 00006 @ingroup examples 00007 */ 00008 00009 #include "ext.h" // you must include this - it contains the external object's link to available Max functions 00010 #include "ext_obex.h" // this is required for all objects using the newer style for writing objects. 00011 00012 typedef struct _plussz2 { // defines our object's internal variables for each instance in a patch 00013 t_object ob; // object header - ALL objects MUST begin with this... 00014 t_atom l; // stored value from left inlet 00015 t_atom r; // stored value from right inlet 00016 void *outlet; // outlet creation - inlets are automatic, but objects must "own" their own outlets 00017 void *proxy; // proxy inlet 00018 long proxy_inletnum; // # of inlet currently in use 00019 } t_plussz2; 00020 00021 00022 // these are prototypes for the methods that are defined below 00023 void *plussz2_new(long n); 00024 void plussz2_free(t_plussz2 *x); 00025 void plussz2_assist(t_plussz2 *x, void *b, long m, long a, char *s); 00026 void plussz2_bang(t_plussz2 *x); 00027 void plussz2_int(t_plussz2 *x, long n); 00028 void plussz2_float(t_plussz2 *x, double f); 00029 00030 00031 t_class *plussz2_class; // global pointer to the object class - so max can reference the object 00032 00033 00034 //-------------------------------------------------------------------------- 00035 00036 int main(void) 00037 { 00038 t_class *c; 00039 00040 c = class_new("plussz2", (method)plussz2_new, (method)plussz2_free, sizeof(t_plussz2), 0L, A_GIMME, 0); 00041 00042 class_addmethod(c, (method)plussz2_bang, "bang", 0); // the method it uses when it gets a bang in the left inlet 00043 class_addmethod(c, (method)plussz2_int, "int", A_LONG, 0); // the method for ints in any inlet 00044 class_addmethod(c, (method)plussz2_float, "float", A_FLOAT, 0); // the method for floats in any inlet 00045 class_addmethod(c, (method)plussz2_assist, "assist", A_CANT, 0); // (optional) assistance method needs to be declared like this 00046 00047 class_register(CLASS_BOX, c); 00048 plussz2_class = c; 00049 00050 post("plussz2 object loaded...",0); // post any important info to the max window when our class is loaded 00051 return 0; 00052 } 00053 00054 00055 //-------------------------------------------------------------------------- 00056 00057 void *plussz2_new(long n) // n = int argument typed into object box (A_DEFLONG) -- defaults to 0 if no args are typed 00058 { 00059 t_plussz2 *x; // local variable (pointer to a t_plussz2 data structure) 00060 00061 x = (t_plussz2 *)object_alloc(plussz2_class); // create a new instance of this object 00062 if(x){ 00063 x->proxy = proxy_new(x, 1, &x->proxy_inletnum); // fully-flexible inlet for any type 00064 x->outlet = outlet_new(x, NULL); // fully-flexible outlet for any type 00065 00066 // initialize L and R inlet atoms to (int)0 00067 atom_setlong(&x->l, 0); 00068 atom_setlong(&x->r, 0); 00069 00070 post(" new plussz2 object instance added to patch...",0); // post important info to the max window when new instance is created 00071 } 00072 return(x); // return a reference to the object instance 00073 } 00074 00075 00076 void plussz2_free(t_plussz2 *x) 00077 { 00078 object_free(x->proxy); // frees all resources associated with the proxy 00079 } 00080 00081 00082 //-------------------------------------------------------------------------- 00083 00084 void plussz2_assist(t_plussz2 *x, void *b, long m, long a, char *s) // 4 final arguments are always the same for the assistance method 00085 { 00086 if (m == ASSIST_INLET) { 00087 switch (a) { 00088 case 0: 00089 sprintf(s,"Inlet %ld: Left Operand (Causes Output)", a); 00090 break; 00091 case 1: 00092 sprintf(s,"Inlet %ld: Right Operand (Added to Left)", a); 00093 break; 00094 } 00095 } else 00096 sprintf(s,"Sum of Left and Right Inlets"); 00097 } 00098 00099 00100 void plussz2_bang(t_plussz2 *x) 00101 { 00102 long lop; 00103 float fop; 00104 00105 // if both L and R are INTEGER, OUTPUT AN INT 00106 if (x->l.a_type == A_LONG && x->r.a_type == A_LONG) { 00107 lop = atom_getlong(&x->l) + atom_getlong(&x->r); 00108 outlet_int(x->outlet, lop); 00109 } 00110 else { // OUTPUT A FLOAT 00111 fop = atom_getfloat(&x->l) + atom_getfloat(&x->r); 00112 outlet_float(x->outlet, fop); 00113 } 00114 } 00115 00116 00117 void plussz2_int(t_plussz2 *x, long n) 00118 { 00119 long inlet = proxy_getinlet((t_object *)x); // what inlet did this message come in through? 00120 00121 post("int came in via inlet %ld", inlet); 00122 00123 if (inlet == 1) { // RIGHT INLET 00124 atom_setlong(&x->r, n); // SET INT VAL 00125 } 00126 else { // LEFT INLET 00127 atom_setlong(&x->l, n); 00128 plussz2_bang(x); // bang for left inlet, trigger calculation 00129 } 00130 } 00131 00132 00133 void plussz2_float(t_plussz2 *x, double f) 00134 { 00135 long inlet = proxy_getinlet((t_object *)x); // what inlet did this message come in through? 00136 00137 post("float came in via inlet %ld", inlet); 00138 00139 if (inlet == 1) { // RIGHT INLET 00140 atom_setfloat(&x->r, f); // SET FLOAT VAL 00141 } 00142 else { // LEFT INLET 00143 atom_setfloat(&x->l, f); 00144 plussz2_bang(x); // bang for left inlet, trigger calculation 00145 } 00146 } 00147 00148
Copyright © 2008, Cycling '74