9d9d42705dd0411dee6c7225d89777596872fde5
[simavr] / examples / shared / libc3 / src / c3texture.c
1 /*
2         c3texture.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 <stdio.h>
24 #include "c3texture.h"
25 #include "c3driver_geometry.h"
26
27 void
28 _c3texture_dispose(
29                 c3geometry_p g,
30                 const c3driver_geometry_t * d)
31 {
32 //      c3texture_p t = (c3texture_p)g;
33         C3_DRIVER_INHERITED(g, d, dispose);
34 }
35
36 void
37 _c3texture_project(
38                 c3geometry_p g,
39                 const c3driver_geometry_t * d,
40                 c3mat4p m)
41 {
42         c3texture_p t = (c3texture_p)g;
43         c3pixels_p p = t->geometry.mat.texture;
44         if (!p) {
45                 C3_DRIVER_INHERITED(g, d, project, m);
46                 return;
47         }
48         c3vec2 qs = c3vec2f(
49                         t->size.x > 0 ? t->size.x : p->w,
50                         t->size.y > 0 ? t->size.y : p->h);
51         c3vec3 v[4] = {
52                         c3vec3f(0, 0, 0), c3vec3f(qs.x, 0, 0),
53                         c3vec3f(qs.x, qs.y, 0), c3vec3f(0, qs.y, 0)
54         };
55         c3vertex_array_clear(&g->vertice);
56         c3vertex_array_realloc(&g->vertice, 4);
57         c3vertex_array_insert(&g->vertice, 0, v, 4);
58
59         c3f tw = p->normalize ? 1.0 : p->w,
60                 th = p->normalize ? 1.0 : p->h;
61         c3vec2 ti[4] = {
62                         c3vec2f(0, th), c3vec2f(tw, th),
63                         c3vec2f(tw, 0), c3vec2f(0, 0)
64         };
65         if (p->trace)
66                 printf("%s size %.0fx%.0f tex %.0fx%.0f\n", __func__, qs.x, qs.y, tw, th);
67         c3tex_array_clear(&t->geometry.textures);
68         c3tex_array_realloc(&t->geometry.textures, 4);
69         c3tex_array_insert(&t->geometry.textures, 0, ti, 4);
70
71         C3_DRIVER_INHERITED(g, d, project, m);
72 }
73
74 const c3driver_geometry_t c3texture_driver = {
75                 .dispose = _c3texture_dispose,
76                 .project = _c3texture_project,
77 };
78 const c3driver_geometry_t c3geometry_driver;
79
80 c3texture_p
81 c3texture_new(
82                 struct c3object_t * o /* = NULL */)
83 {
84         c3texture_p res = malloc(sizeof(*res));
85         return c3texture_init(res, o);
86 }
87
88 c3texture_p
89 c3texture_init(
90                 c3texture_p t,
91                 struct c3object_t * o /* = NULL */)
92 {
93         memset(t, 0, sizeof(*t));
94         c3geometry_init(&t->geometry,
95                         c3geometry_type(C3_TEXTURE_TYPE, 0 /* GL_TRIANGLE_FAN */),
96                         o);
97         static const c3driver_geometry_t * list[] = {
98                         &c3texture_driver, &c3geometry_driver, NULL,
99         };
100         t->geometry.driver = list;
101
102         return t;
103 }
104
105 void
106 c3texture_setpixels(
107                 )
108 {
109
110 }