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