Add srfi-16 and srfi-30 to %cond-expand-features.
[bpt/guile.git] / doc / example-smob / image-type.c
1 /* image-type.c
2 *
3 * Copyright (C) 1998, 2000, 2004, 2006, 2011 Free Software Foundation, Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 3, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this software; see the file COPYING.LESSER. If
17 * not, write to the Free Software Foundation, Inc., 51 Franklin
18 * Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdlib.h>
22 #include <libguile.h>
23
24 static scm_t_bits image_tag;
25
26 struct image
27 {
28 int width, height;
29 char *pixels;
30
31 /* The name of this image */
32 SCM name;
33
34 /* A function to call when this image is
35 modified, e.g., to update the screen,
36 or SCM_BOOL_F if no action necessary */
37 SCM update_func;
38 };
39
40 static SCM
41 make_image (SCM name, SCM s_width, SCM s_height)
42 {
43 SCM smob;
44 struct image *image;
45 int width = scm_to_int (s_width);
46 int height = scm_to_int (s_height);
47
48 /* Step 1: Allocate the memory block.
49 */
50 image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
51
52 /* Step 2: Initialize it with straight code.
53 */
54 image->width = width;
55 image->height = height;
56 image->pixels = NULL;
57 image->name = SCM_BOOL_F;
58 image->update_func = SCM_BOOL_F;
59
60 /* Step 3: Create the smob.
61 */
62 SCM_NEWSMOB (smob, image_tag, image);
63
64 /* Step 4: Finish the initialization.
65 */
66 image->name = name;
67 image->pixels = scm_gc_malloc_pointerless (width * height, "image pixels");
68
69 return smob;
70 }
71
72 SCM
73 clear_image (SCM image_smob)
74 {
75 int area;
76 struct image *image;
77
78 scm_assert_smob_type (image_tag, image_smob);
79
80 image = (struct image *) SCM_SMOB_DATA (image_smob);
81 area = image->width * image->height;
82 memset (image->pixels, 0, area);
83
84 /* Invoke the image's update function.
85 */
86 if (scm_is_true (image->update_func))
87 scm_call_0 (image->update_func);
88
89 scm_remember_upto_here_1 (image_smob);
90
91 return SCM_UNSPECIFIED;
92 }
93
94 static int
95 print_image (SCM image_smob, SCM port, scm_print_state *pstate)
96 {
97 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
98
99 scm_puts ("#<image ", port);
100 scm_display (image->name, port);
101 scm_puts (">", port);
102
103 /* non-zero means success */
104 return 1;
105 }
106
107 void
108 init_image_type (void)
109 {
110 image_tag = scm_make_smob_type ("image", sizeof (struct image));
111 scm_set_smob_print (image_tag, print_image);
112
113 scm_c_define_gsubr ("clear-image", 1, 0, 0, clear_image);
114 scm_c_define_gsubr ("make-image", 3, 0, 0, make_image);
115 }