9526f963cf182bc255af0a8e3eacc9209b40535a
[ruby-ecore.git] / src / ecore_evas / rb_buffer.c
1 /*
2  * $Id: rb_buffer.c 351 2006-02-10 15:25:40Z tilman $
3  *
4  * Copyright (C) 2004-2005 ruby-ecore team (see AUTHORS)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Ecore.h>
24 #include <Ecore_Evas.h>
25
26 #include "../ecore/rb_ecore.h"
27 #include "rb_ecore_evas_main.h"
28 #include "rb_ecore_evas.h"
29
30 static void c_free (RbEcoreEvas *ee)
31 {
32         c_ecore_evas_free (ee, true);
33 }
34
35 static VALUE c_alloc (VALUE klass)
36 {
37         RbEcoreEvas *ee = NULL;
38
39         ecore_init ();
40         ecore_evas_init ();
41
42         return Data_Make_Struct (klass, RbEcoreEvas,
43                                  c_ecore_evas_mark, c_free, ee);
44 }
45
46 /*
47  * call-seq:
48  *  Ecore::Evas::Buffer.new([w, h]) => buffer
49  *
50  * Creates an Ecore::Evas::Buffer object.
51  */
52 static VALUE c_init (int argc, VALUE *argv, VALUE self)
53 {
54         VALUE w, h;
55         int iw = 0, ih = 0;
56
57         GET_OBJ (self, RbEcoreEvas, ee);
58
59         rb_scan_args (argc, argv, "02", &w, &h);
60
61         if (!NIL_P (w)) {
62                 Check_Type (w, T_FIXNUM);
63                 iw = FIX2INT (w);
64         }
65
66         if (!NIL_P (h)) {
67                 Check_Type (h, T_FIXNUM);
68                 ih = FIX2INT (h);
69         }
70
71         ee->real = ecore_evas_buffer_new (iw, ih);
72
73         rb_call_super (argc, argv);
74
75         return self;
76 }
77
78 static VALUE c_pixels_get (VALUE self)
79 {
80         const int *p;
81         int w = 0, h = 0;
82
83         GET_OBJ (self, RbEcoreEvas, ee);
84
85         p = ecore_evas_buffer_pixels_get (ee->real);
86         if (!p)
87                 return rb_str_new2 (""); /* FIXME raise an error instead? */
88
89         ecore_evas_geometry_get (ee->real, NULL, NULL, &w, &h);
90
91         return rb_str_new ((char *) p, h * w * 4);
92 }
93
94 void Init_Buffer (void)
95 {
96         VALUE c = rb_define_class_under (mEvas, "Buffer", cEcoreEvas);
97
98         rb_define_alloc_func (c, c_alloc);
99         rb_define_method (c, "initialize", c_init, -1);
100         rb_define_method (c, "pixels", c_pixels_get, 0);
101 }