Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / doc / example-smob / image-type.c
CommitLineData
9803ec8f 1/* image-type.c
28b9264d
LC
2 *
3 * Copyright (C) 1998, 2000, 2004, 2006, 2011 Free Software Foundation, Inc.
4 *
53befeb7
NJ
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.
28b9264d 9 *
53befeb7
NJ
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.
28b9264d 14 *
53befeb7
NJ
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
6f7e3d59 19 */
90b71476
JB
20
21#include <stdlib.h>
22#include <libguile.h>
23
d115af0e 24static scm_t_bits image_tag;
90b71476 25
28b9264d
LC
26struct image
27{
90b71476
JB
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
40static SCM
41make_image (SCM name, SCM s_width, SCM s_height)
42{
43631deb 43 SCM smob;
90b71476 44 struct image *image;
43631deb
MV
45 int width = scm_to_int (s_width);
46 int height = scm_to_int (s_height);
90b71476 47
43631deb
MV
48 /* Step 1: Allocate the memory block.
49 */
d115af0e 50 image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
43631deb
MV
51
52 /* Step 2: Initialize it with straight code.
53 */
90b71476
JB
54 image->width = width;
55 image->height = height;
43631deb
MV
56 image->pixels = NULL;
57 image->name = SCM_BOOL_F;
90b71476
JB
58 image->update_func = SCM_BOOL_F;
59
43631deb
MV
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;
28b9264d 67 image->pixels = scm_gc_malloc_pointerless (width * height, "image pixels");
43631deb
MV
68
69 return smob;
90b71476
JB
70}
71
43631deb 72SCM
90b71476
JB
73clear_image (SCM image_smob)
74{
75 int area;
76 struct image *image;
77
43631deb 78 scm_assert_smob_type (image_tag, image_smob);
90b71476 79
bd5e6840 80 image = (struct image *) SCM_SMOB_DATA (image_smob);
90b71476
JB
81 area = image->width * image->height;
82 memset (image->pixels, 0, area);
83
43631deb
MV
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);
90b71476
JB
90
91 return SCM_UNSPECIFIED;
92}
93
90b71476
JB
94static int
95print_image (SCM image_smob, SCM port, scm_print_state *pstate)
96{
bd5e6840 97 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
90b71476
JB
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
90b71476 107void
7977a487 108init_image_type (void)
90b71476 109{
82ac0a63 110 image_tag = scm_make_smob_type ("image", sizeof (struct image));
82ac0a63 111 scm_set_smob_print (image_tag, print_image);
90b71476 112
7977a487
MG
113 scm_c_define_gsubr ("clear-image", 1, 0, 0, clear_image);
114 scm_c_define_gsubr ("make-image", 3, 0, 0, make_image);
90b71476 115}