2ab449bd5d7c17df31637b267d5c6b645fd83331
[simavr] / examples / shared / libc3 / src / c3cairo.c
1 /*
2         c3cairo.c
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of libc3.
7
8         libc3 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         libc3 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 libc3.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "c3cairo.h"
24 #include "c3driver_geometry.h"
25
26 #if CONFIG_C3_CAIRO
27
28 void
29 _c3cairo_dispose(
30                 c3geometry_p g,
31                 const c3driver_geometry_t * d)
32 {
33         c3cairo_p c = (c3cairo_p)g;
34
35         if (c->cr)
36                 cairo_destroy(c->cr);
37         if (c->surface)
38                 cairo_surface_destroy(c->surface);
39         C3_DRIVER_INHERITED(g, d, dispose);
40 }
41
42 static void
43 _c3cairo_project(
44                 c3geometry_p g,
45                 const struct c3driver_geometry_t *d,
46                 c3mat4p m)
47 {
48         C3_DRIVER_INHERITED(g, d, project, m);
49 }
50
51 const c3driver_geometry_t c3cairo_base_driver = {
52         .dispose = _c3cairo_dispose,
53         .project = _c3cairo_project,
54 };
55 extern const c3driver_geometry_t c3texture_driver;
56 extern const c3driver_geometry_t c3geometry_driver;
57
58 c3cairo_p
59 c3cairo_new(
60                 struct c3object_t * parent)
61 {
62         c3cairo_p res = malloc(sizeof(*res));
63         return c3cairo_init(res, parent);
64 }
65
66 c3cairo_p
67 c3cairo_init(
68                 c3cairo_p o,
69                 struct c3object_t * parent)
70 {
71         memset(o, 0, sizeof(*o));
72         c3texture_init(&o->tex, parent);
73
74         static const c3driver_geometry_t * list[] = {
75                         &c3cairo_base_driver, &c3texture_driver, &c3geometry_driver, NULL,
76         };
77         ((c3geometry_p)o)->driver = list;
78
79         return o;
80 }
81
82 c3cairo_p
83 c3cairo_new_offscreen(
84                 struct c3object_t * parent,
85                 int w, int h)
86 {
87         c3cairo_p o = c3cairo_new(parent);
88
89         o->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
90         o->cr = cairo_create(o->surface);
91
92         c3pixels_p dst = c3pixels_new(w, h, 4,
93                         cairo_image_surface_get_stride(o->surface),
94                         cairo_image_surface_get_data(o->surface));
95         o->tex.geometry.mat.texture = dst;
96
97         return o;
98 }
99
100 #if 0
101 cairo_surface_destroy(_surface);
102 else
103 cairo_surface_finish(_surface);
104 #endif
105 #endif // CONFIG_C3_CAIRO