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