3 This file is part of libmspgltk
4 Copyright © 2010-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/bindable.h>
9 #include <msp/gl/immediate.h>
10 #include <msp/gl/matrix.h>
20 Image::Image(const GL::Texture2D *i):
27 void Image::autosize()
34 if(const Part *image_part = style->get_part("image"))
36 const Sides &margin = image_part->get_margin();
39 geom.w = max(geom.w, image->get_width()+margin.left+margin.right);
40 geom.h = max(geom.h, image->get_height()+margin.top+margin.bottom);
44 geom.w = max(geom.w, margin.left+margin.right);
45 geom.h = max(geom.h, margin.top+margin.bottom);
50 void Image::set_image(const GL::Texture2D *i)
55 void Image::set_keep_aspect(bool ka)
60 void Image::render_special(const Part &part) const
62 if(part.get_name()=="image")
67 const Alignment &align = part.get_alignment();
68 Geometry rgeom = part.get_geometry();
69 align.apply(rgeom, geom, part.get_margin());
73 float aspect = static_cast<float>(image->get_width())/image->get_height();
74 if(rgeom.w<rgeom.h*aspect)
76 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
77 rgeom.y += (rgeom.h-h)*align.y;
82 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
83 rgeom.x += (rgeom.w-w)*align.x;
88 GL::Bind _bind_tex(image);
89 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
90 imm.color(1.0f, 1.0f, 1.0f);
92 imm.texcoord(0.0, 0.0);
93 imm.vertex(rgeom.x, rgeom.y);
94 imm.texcoord(1.0, 0.0);
95 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
96 imm.texcoord(1.0, 1.0);
97 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
98 imm.texcoord(0.0, 1.0);
99 imm.vertex(rgeom.x, rgeom.y+rgeom.h);