aboutsummaryrefslogtreecommitdiff
path: root/doc/tutorials/externals-howto/example2
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2006-01-30 21:04:37 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2006-01-30 21:04:37 +0000
commit5ea005d109105a1d1532014f94b929806726c7a4 (patch)
tree42a04f93aa92589c734698b239a36bb40ef6dd54 /doc/tutorials/externals-howto/example2
parent3052e39e95f2ffe03180418dc46e96b89adb812c (diff)
the externals-HOWTO (i almost lost the tex-sources, so here they are now)
svn path=/trunk/; revision=4524
Diffstat (limited to 'doc/tutorials/externals-howto/example2')
-rw-r--r--doc/tutorials/externals-howto/example2/counter.c86
1 files changed, 86 insertions, 0 deletions
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);
+}