reprap: c3 update
[simavr] / examples / board_reprap / src / c3 / c3cairo.c
1 /*
2         c3cairo.c
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "c3/c3cairo.h"
24 #include "c3/c3driver_geometry.h"
25
26 void
27 _c3cairo_dispose(
28                 c3geometry_p g,
29                 const c3driver_geometry_t * d)
30 {
31         c3cairo_p c = (c3cairo_p)g;
32
33         if (c->cr)
34                 cairo_destroy(c->cr);
35         if (c->surface)
36                 cairo_surface_destroy(c->surface);
37         C3_DRIVER_INHERITED(g, d, dispose);
38 }
39
40 static void
41 _c3cairo_project(
42                 c3geometry_p g,
43                 const struct c3driver_geometry_t *d,
44                 c3mat4p m)
45 {
46         C3_DRIVER_INHERITED(g, d, project, m);
47 }
48
49 const c3driver_geometry_t c3cairo_base_driver = {
50         .dispose = _c3cairo_dispose,
51         .project = _c3cairo_project,
52 };
53 const c3driver_geometry_t c3texture_driver;
54 const c3driver_geometry_t c3geometry_driver;
55
56 c3cairo_p
57 c3cairo_new(
58                 struct c3object_t * parent)
59 {
60         c3cairo_p res = malloc(sizeof(*res));
61         return c3cairo_init(res, parent);
62 }
63
64 c3cairo_p
65 c3cairo_init(
66                 c3cairo_p o,
67                 struct c3object_t * parent)
68 {
69         memset(o, 0, sizeof(*o));
70         c3texture_init(&o->tex, parent);
71
72         static const c3driver_geometry_t * list[] = {
73                         &c3cairo_base_driver, &c3texture_driver, &c3geometry_driver, NULL,
74         };
75         ((c3geometry_p)o)->driver = list;
76
77         return o;
78 }
79
80 c3cairo_p
81 c3cairo_new_offscreen(
82                 struct c3object_t * parent,
83                 int w, int h)
84 {
85         c3cairo_p o = c3cairo_new(parent);
86
87         o->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
88         o->cr = cairo_create(o->surface);
89
90         c3pixels_init(&o->tex.pixels, w, h, 4,
91                         cairo_image_surface_get_stride(o->surface),
92                         cairo_image_surface_get_data(o->surface));
93
94         return o;
95 }
96
97 #if 0
98 cairo_surface_destroy(_surface);
99 else
100 cairo_surface_finish(_surface);
101 #endif