3 This file is part of libmspgltk
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/immediate.h>
10 #include "resources.h"
22 void Graphic::render(unsigned wd, unsigned ht) const
25 create_coords(0.0f-shadow.left, wd+shadow.right, border.left, border.right, slice.w-border.left-border.right, x);
26 create_coords(0.0f-shadow.bottom, ht+shadow.top, border.bottom, border.top, slice.h-border.bottom-border.top, y);
29 create_texcoords(slice.x, slice.x+slice.w, border.left, border.right, texture->get_width(), u);
30 create_texcoords(slice.y, slice.y+slice.h, border.bottom, border.top, texture->get_height(), v);
32 unsigned xmin=border.left ? 0 : 1;
33 unsigned xmax=x.size()-(border.right ? 2 : 3);
34 unsigned ymin=border.bottom ? 0 : 1;
35 unsigned ymax=y.size()-(border.top ? 2 : 3);
38 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
39 imm.color(1.0f, 1.0f, 1.0f);
41 for(unsigned i=ymin; i<=ymax; ++i)
43 unsigned i2=(i==0 ? 0 : i==y.size()-2 ? 2 : 1);
44 for(unsigned j=xmin; j<=xmax; ++j)
46 unsigned j2=(j==0 ? 0 : j==x.size()-2 ? 2 : 1);
47 imm.texcoord(u[j2], v[i2]);
48 imm.vertex(x[j], y[i]);
49 imm.texcoord(u[j2+1], v[i2]);
50 imm.vertex(x[j+1], y[i]);
51 imm.texcoord(u[j2+1], v[i2+1]);
52 imm.vertex(x[j+1], y[i+1]);
53 imm.texcoord(u[j2], v[i2+1]);
54 imm.vertex(x[j], y[i+1]);
60 void Graphic::create_coords(float low, float high, float brd1, float brd2, float block, vector<float> &coords) const
62 coords.push_back(low);
63 coords.push_back(low+brd1);
66 float space=high-low-brd1-brd2;
67 unsigned div=max(static_cast<unsigned>(space/block), 1U);
68 float delta=space/div;
69 for(unsigned i=1; i<div; ++i)
70 coords.push_back(low+brd1+delta*i);
72 coords.push_back(high-brd2);
73 coords.push_back(high);
76 void Graphic::create_texcoords(float low, float high, float brd1, float brd2, float scale, vector<float> &coords) const
78 coords.push_back(low/scale);
79 coords.push_back((low+brd1)/scale);
80 coords.push_back((high-brd2)/scale);
81 coords.push_back(high/scale);
85 Graphic::Loader::Loader(Graphic &g, Resources &r):
89 add("texture", &Loader::texture);
90 add("slice", &Loader::slice);
91 add("border", &Loader::border);
92 add("repeat", &Graphic::repeat);
93 add("shadow", &Loader::shadow);
96 void Graphic::Loader::texture(const string &n)
98 graph.texture=res.get<GL::Texture2D>(n);
99 graph.slice=Geometry(0, 0, graph.texture->get_width(), graph.texture->get_height());
102 void Graphic::Loader::slice(unsigned x, unsigned y, unsigned w, unsigned h)
104 graph.slice=Geometry(x, y, w, h);
107 void Graphic::Loader::border()
109 load_sub(graph.border);
112 void Graphic::Loader::shadow()
114 load_sub(graph.shadow);