Max 5 API Reference
00001 /** 00002 @file 00003 plussz.c - one of the simplest max objects you can make -rdd 2001 00004 (plussz is/was the name of a Hungarian vitamin C tablet-drink from the early 90s) 00005 00006 this example is provided for musicians who want to learn to write their own Max externals but who only 00007 have rudimentary computer programming skills and feel somewhat overwhelmed by the other examples in the Max SDK 00008 00009 this object has 2 inlets and one outlet 00010 it responds to ints in its inlets and the 'bang' message in the left inlet 00011 it responds to the 'assistance' message sent by Max when the mouse is positioned over an inlet or outlet 00012 (including an assistance method is optional, but strongly sugggested) 00013 it adds its input values together and outputs their sum 00014 00015 @ingroup examples 00016 */ 00017 00018 #include "ext.h" // you must include this - it contains the external object's link to available Max functions 00019 #include "ext_obex.h" // this is required for all objects using the newer style for writing objects. 00020 00021 typedef struct _plussz { // defines our object's internal variables for each instance in a patch 00022 t_object p_ob; // object header - ALL objects MUST begin with this... 00023 long p_value0; // int value - received from the left inlet and stored internally for each object instance 00024 long p_value1; // int value - received from the right inlet and stored internally for each object instance 00025 void *p_outlet; // outlet creation - inlets are automatic, but objects must "own" their own outlets 00026 } t_plussz; 00027 00028 00029 // these are prototypes for the methods that are defined below 00030 void plussz_bang(t_plussz *x); 00031 void plussz_int(t_plussz *x, long n); 00032 void plussz_in1(t_plussz *x, long n); 00033 void plussz_assist(t_plussz *x, void *b, long m, long a, char *s); 00034 void *plussz_new(long n); 00035 00036 00037 t_class *plussz_class; // global pointer to the object class - so max can reference the object 00038 00039 00040 //-------------------------------------------------------------------------- 00041 00042 int main(void) 00043 { 00044 t_class *c; 00045 00046 c = class_new("plussz", (method)plussz_new, (method)NULL, sizeof(t_plussz), 0L, A_DEFLONG, 0); 00047 // class_new() loads our external's class into Max's memory so it can be used in a patch 00048 // plussz_new = object creation method defined below 00049 00050 class_addmethod(c, (method)plussz_bang, "bang", 0); // the method it uses when it gets a bang in the left inlet 00051 class_addmethod(c, (method)plussz_int, "int", A_LONG, 0); // the method for an int in the left inlet (inlet 0) 00052 class_addmethod(c, (method)plussz_in1, "in1", A_LONG, 0); // the method for an int in the right inlet (inlet 1) 00053 // "ft1" is the special message for floats 00054 class_addmethod(c, (method)plussz_assist, "assist", A_CANT, 0); // (optional) assistance method needs to be declared like this 00055 00056 class_register(CLASS_BOX, c); 00057 plussz_class = c; 00058 00059 post("plussz object loaded...",0); // post any important info to the max window when our class is loaded 00060 return 0; 00061 } 00062 00063 00064 //-------------------------------------------------------------------------- 00065 00066 void *plussz_new(long n) // n = int argument typed into object box (A_DEFLONG) -- defaults to 0 if no args are typed 00067 { 00068 t_plussz *x; // local variable (pointer to a t_plussz data structure) 00069 00070 x = (t_plussz *)object_alloc(plussz_class); // create a new instance of this object 00071 00072 intin(x,1); // create a second int inlet (leftmost inlet is automatic - all objects have one inlet by default) 00073 x->p_outlet = intout(x); // create an int outlet and assign it to our outlet variable in the instance's data structure 00074 00075 x->p_value0 = 0; // set initial (default) left operand value in the instance's data structure 00076 x->p_value1 = n; // set initial (default) right operand value (n = variable passed to plussz_new) 00077 00078 post(" new plussz object instance added to patch...",0); // post important info to the max window when new instance is created 00079 00080 return(x); // return a reference to the object instance 00081 } 00082 00083 00084 //-------------------------------------------------------------------------- 00085 00086 void plussz_assist(t_plussz *x, void *b, long m, long a, char *s) // 4 final arguments are always the same for the assistance method 00087 { 00088 if (m == ASSIST_OUTLET) 00089 sprintf(s,"Sum of Left and Right Inlets"); 00090 else { 00091 switch (a) { 00092 case 0: 00093 sprintf(s,"Inlet %ld: Left Operand (Causes Output)", a); 00094 break; 00095 case 1: 00096 sprintf(s,"Inlet %ld: Right Operand (Added to Left)", a); 00097 break; 00098 } 00099 } 00100 } 00101 00102 00103 void plussz_bang(t_plussz *x) // x = reference to this instance of the object 00104 { 00105 long sum; // local variable for this method 00106 00107 sum = x->p_value0+x->p_value1; // add left and right operands 00108 outlet_int(x->p_outlet, sum); // send out the sum on bang 00109 } 00110 00111 00112 void plussz_int(t_plussz *x, long n) // x = the instance of the object; n = the int received in the left inlet 00113 { 00114 x->p_value0 = n; // store left operand value in instance's data structure 00115 plussz_bang(x); // ... call the bang method to sum and send out our outlet 00116 } 00117 00118 00119 void plussz_in1(t_plussz *x, long n) // x = the instance of the object, n = the int received in the right inlet 00120 { 00121 x->p_value1 = n; // just store right operand value in instance's data structure and do nothing else 00122 } 00123 00124
Copyright © 2008, Cycling '74