]> git.tdb.fi Git - libs/gltk.git/blob - source/graphic.cpp
Adapt to DataFile changes
[libs/gltk.git] / source / graphic.cpp
1 #include <msp/gl/immediate.h>
2 #include "graphic.h"
3 #include "resources.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GLtk {
9
10 Graphic::Graphic():
11         texture(0)
12 { }
13
14 void Graphic::render(unsigned wd, unsigned ht) const
15 {
16         float x[4];
17         float y[4];
18         float u[4];
19         float v[4];
20
21         x[0]=0.0f-shadow.left;
22         x[1]=x[0]+border.left;
23         x[3]=wd+shadow.right;
24         x[2]=x[3]-border.right;
25
26         y[0]=0.0f-shadow.bottom;
27         y[1]=y[0]+border.bottom;
28         y[3]=ht+shadow.top;
29         y[2]=y[3]-border.top;
30
31         const unsigned twidth=texture->get_width();
32         u[0]=static_cast<float>(slice.x)/twidth;
33         u[1]=static_cast<float>(slice.x+border.left)/twidth;
34         u[2]=static_cast<float>(slice.x+slice.w-border.right)/twidth;
35         u[3]=static_cast<float>(slice.x+slice.w)/twidth;
36
37         const unsigned theight=texture->get_height();
38         v[0]=static_cast<float>(slice.y)/theight;
39         v[1]=static_cast<float>(slice.y+border.bottom)/theight;
40         v[2]=static_cast<float>(slice.y+slice.h-border.top)/theight;
41         v[3]=static_cast<float>(slice.y+slice.h)/theight;
42
43         texture->bind();
44         unsigned xmin=border.left ? 0 : 1;
45         unsigned xmax=border.right ? 3 : 2;
46         unsigned ymin=border.bottom ? 0 : 1;
47         unsigned ymax=border.top ? 3 : 2;
48
49         GL::Immediate imm((GL::TEXCOORD2,GL::VERTEX2));
50         for(unsigned i=ymin; i<ymax; ++i)
51         {
52                 imm.begin(GL::QUAD_STRIP);
53                 for(unsigned j=xmin; j<=xmax; ++j)
54                 {
55                         imm.texcoord(u[j], v[i]);
56                         imm.vertex(x[j], y[i]);
57                         imm.texcoord(u[j], v[i+1]);
58                         imm.vertex(x[j], y[i+1]);
59                 }
60                 imm.end();
61         }
62 }
63
64
65 Graphic::Loader::Loader(Graphic &g, Resources &r):
66         graph(g),
67         res(r)
68 {
69         add("texture", &Loader::texture);
70         add("slice",   &Loader::slice);
71         add("border",  &Loader::border);
72         add("shadow",  &Loader::shadow);
73 }
74
75 void Graphic::Loader::texture(const string &n)
76 {
77         graph.texture=res.get<GL::Texture2D>(n);
78         graph.slice=Geometry(0, 0, graph.texture->get_width(), graph.texture->get_height());
79 }
80
81 void Graphic::Loader::slice(unsigned x, unsigned y, unsigned w, unsigned h)
82 {
83         graph.slice=Geometry(x, y, w, h);
84 }
85
86 void Graphic::Loader::border()
87 {
88         load_sub(graph.border);
89 }
90
91 void Graphic::Loader::shadow()
92 {
93         load_sub(graph.shadow);
94 }
95
96 } // namespace GLtk
97 } // namespace Msp