]> git.tdb.fi Git - libs/gltk.git/blob - source/graphic.cpp
01bfcdb6a177db0fc16d8acf04498fcb4db47124
[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         repeat(false)
13 { }
14
15 void Graphic::render(unsigned wd, unsigned ht) const
16 {
17         GL::VertexArray varr((GL::TEXCOORD2, GL::VERTEX2));
18         RefPtr<GL::VertexArrayBuilder> vab=varr.modify();
19
20         vector<float> x, y;
21         create_coords(0.0f-shadow.left, wd+shadow.right, border.left, border.right, slice.w-border.left-border.right, x);
22         create_coords(0.0f-shadow.bottom, ht+shadow.top, border.bottom, border.top, slice.h-border.bottom-border.top, y);
23
24         vector<float> u, v;
25         create_texcoords(slice.x, slice.x+slice.w, border.left, border.right, texture->get_width(), u);
26         create_texcoords(slice.y, slice.y+slice.h, border.bottom, border.top, texture->get_height(), v);
27
28         unsigned xmin=border.left ? 0 : 1;
29         unsigned xmax=x.size()-(border.right ? 2 : 3);
30         unsigned ymin=border.bottom ? 0 : 1;
31         unsigned ymax=y.size()-(border.top ? 2 : 3);
32
33         texture->bind();
34         GL::Immediate imm((GL::TEXCOORD2,GL::VERTEX2));
35         imm.begin(GL::QUADS);
36         for(unsigned i=ymin; i<=ymax; ++i)
37         {
38                 unsigned i2=(i==0 ? 0 : i==y.size()-2 ? 2 : 1);
39                 for(unsigned j=xmin; j<=xmax; ++j)
40                 {
41                         unsigned j2=(j==0 ? 0 : j==x.size()-2 ? 2 : 1);
42                         imm.texcoord(u[j2], v[i2]);
43                         imm.vertex(x[j], y[i]);
44                         imm.texcoord(u[j2+1], v[i2]);
45                         imm.vertex(x[j+1], y[i]);
46                         imm.texcoord(u[j2+1], v[i2+1]);
47                         imm.vertex(x[j+1], y[i+1]);
48                         imm.texcoord(u[j2], v[i2+1]);
49                         imm.vertex(x[j], y[i+1]);
50                 }
51         }
52         imm.end();
53 }
54
55 void Graphic::create_coords(float low, float high, float brd1, float brd2, float block, vector<float> &coords) const
56 {
57         coords.push_back(low);
58         coords.push_back(low+brd1);
59         if(repeat)
60         {
61                 float space=high-low-brd1-brd2;
62                 unsigned div=max(static_cast<unsigned>(space/block), 1U);
63                 float delta=space/div;
64                 for(unsigned i=1; i<div; ++i)
65                         coords.push_back(low+brd1+delta*i);
66         }
67         coords.push_back(high-brd2);
68         coords.push_back(high);
69 }
70
71 void Graphic::create_texcoords(float low, float high, float brd1, float brd2, float scale, vector<float> &coords) const
72 {
73         coords.push_back(low/scale);
74         coords.push_back((low+brd1)/scale);
75         coords.push_back((high-brd2)/scale);
76         coords.push_back(high/scale);
77 }
78
79
80 Graphic::Loader::Loader(Graphic &g, Resources &r):
81         graph(g),
82         res(r)
83 {
84         add("texture", &Loader::texture);
85         add("slice",   &Loader::slice);
86         add("border",  &Loader::border);
87         add("repeat",  &Graphic::repeat);
88         add("shadow",  &Loader::shadow);
89 }
90
91 void Graphic::Loader::texture(const string &n)
92 {
93         graph.texture=res.get<GL::Texture2D>(n);
94         graph.slice=Geometry(0, 0, graph.texture->get_width(), graph.texture->get_height());
95 }
96
97 void Graphic::Loader::slice(unsigned x, unsigned y, unsigned w, unsigned h)
98 {
99         graph.slice=Geometry(x, y, w, h);
100 }
101
102 void Graphic::Loader::border()
103 {
104         load_sub(graph.border);
105 }
106
107 void Graphic::Loader::shadow()
108 {
109         load_sub(graph.shadow);
110 }
111
112 } // namespace GLtk
113 } // namespace Msp