From 5ea005d109105a1d1532014f94b929806726c7a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Mon, 30 Jan 2006 21:04:37 +0000 Subject: the externals-HOWTO (i almost lost the tex-sources, so here they are now) svn path=/trunk/; revision=4524 --- doc/tutorials/externals-howto/example2/counter.c | 86 ++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 doc/tutorials/externals-howto/example2/counter.c (limited to 'doc/tutorials/externals-howto/example2') diff --git a/doc/tutorials/externals-howto/example2/counter.c b/doc/tutorials/externals-howto/example2/counter.c new file mode 100644 index 00000000..afc72335 --- /dev/null +++ b/doc/tutorials/externals-howto/example2/counter.c @@ -0,0 +1,86 @@ +/* + * HOWTO write an External for Pure data + * (c) 2001-2006 IOhannes m zmölnig zmoelnig[AT]iem.at + * + * this is the source-code for the second example in the HOWTO + * it creates an object that increments and outputs a counter + * whenever it gets banged. + * + * this file is released under Pd's license (BSD-like) + */ + + + +/** + * include the interface to Pd + */ +#include "m_pd.h" + +/** + * define a new "class" + */ +static t_class *counter_class; + + + +/** + * this is the dataspace of our new object + * the first (mandatory) "t_object" + * and a variable that holds the current counter value + */ +typedef struct _counter { + t_object x_obj; + t_int i_count; +} t_counter; + + +/** + * this method is called whenever a "bang" is sent to the object + * a reference to the class-dataspace is given as argument + * this enables us to do something with the data (e.g. increment the counter) + */ +void counter_bang(t_counter *x) +{ + /* + * convert the current counter value to floating-point to output it later + */ + t_float f=x->i_count; + /* increment the counter */ + x->i_count++; + /* send the old counter-value to the 1st outlet of the object */ + outlet_float(x->x_obj.ob_outlet, f); +} + + +/** + * this is the "constructor" of the class + * we have one argument of type floating-point (as specified below in the counter_setup() routine) + */ +void *counter_new(t_floatarg f) +{ + t_counter *x = (t_counter *)pd_new(counter_class); + + /* set the counter value to the given argument */ + x->i_count=f; + + /* create a new outlet for floating-point values */ + outlet_new(&x->x_obj, &s_float); + + return (void *)x; +} + + +/** + * define the function-space of the class + */ +void counter_setup(void) { + counter_class = class_new(gensym("counter"), + (t_newmethod)counter_new, + 0, + sizeof(t_counter), + CLASS_DEFAULT, + A_DEFFLOAT, 0); /* the object takes one argument which is a floating-point and defaults to 0 */ + + /* call a function when object gets banged */ + class_addbang(counter_class, counter_bang); +} -- cgit v1.2.1