]> git.tdb.fi Git - libs/gltk.git/blob - source/image.cpp
Store the Resources reference only in Root widget
[libs/gltk.git] / source / image.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/bindable.h>
9 #include <msp/gl/immediate.h>
10 #include <msp/gl/matrix.h>
11 #include "image.h"
12 #include "part.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GLtk {
18
19 Image::Image(const GL::Texture2D *i):
20         image(i),
21         keep_aspect(true)
22 {
23         focusable = false;
24 }
25
26 void Image::set_image(const GL::Texture2D *i)
27 {
28         image = i;
29 }
30
31 void Image::set_keep_aspect(bool ka)
32 {
33         keep_aspect = ka;
34 }
35
36 void Image::render_special(const Part &part) const
37 {
38         if(part.get_name()=="image")
39         {
40                 if(!image)
41                         return;
42
43                 const Alignment &align = part.get_alignment();
44                 Geometry rgeom = part.get_geometry();
45                 align.apply(rgeom, geom, part.get_margin());
46
47                 if(keep_aspect)
48                 {
49                         float aspect = static_cast<float>(image->get_width())/image->get_height();
50                         if(rgeom.w<rgeom.h*aspect)
51                         {
52                                 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
53                                 rgeom.y += (rgeom.h-h)*align.y;
54                                 rgeom.h = h;
55                         }
56                         else
57                         {
58                                 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
59                                 rgeom.x += (rgeom.w-w)*align.x;
60                                 rgeom.w = w;
61                         }
62                 }
63
64                 GL::Bind _bind_tex(image);
65                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
66                 imm.color(1.0f, 1.0f, 1.0f);
67                 imm.begin(GL::QUADS);
68                 imm.texcoord(0.0, 0.0);
69                 imm.vertex(rgeom.x, rgeom.y);
70                 imm.texcoord(1.0, 0.0);
71                 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
72                 imm.texcoord(1.0, 1.0);
73                 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
74                 imm.texcoord(0.0, 1.0);
75                 imm.vertex(rgeom.x, rgeom.y+rgeom.h);
76                 imm.end();
77         }
78 }
79
80 } // namespace GLtk
81 } // namespace Msp