3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
16 Texture2D::Texture2D():
24 void Texture2D::storage(PixelFormat fmt, sizei wd, sizei ht, int brd)
27 throw InvalidState("Texture storage may only be specified once");
29 throw InvalidParameterValue("Invalid texture dimensions");
37 void Texture2D::image(int level, PixelFormat fmt, DataType type, const void *data)
40 throw InvalidState("Texture storage has not been specified");
44 glTexImage2D(target, level, ifmt, width, height, border, fmt, type, data);
47 void Texture2D::sub_image(int level, int x, int y, sizei wd, sizei ht, PixelFormat fmt, DataType type, const void *data)
50 throw InvalidState("Texture storage has not been specified");
54 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
57 void Texture2D::load_image(const string &fn)
65 void Texture2D::image(const Graphics::Image &img)
67 unsigned w=img.get_width();
68 unsigned h=img.get_height();
69 PixelFormat fmt=pixelformat_from_graphics(img.get_format());
71 storage(fmt, w, h, 0);
72 else if(w!=width || h!=height)
73 throw IncompatibleData("Image does not match texture storage");
75 image(0, fmt, UNSIGNED_BYTE, img.get_data());
79 Texture2D::Loader::Loader(Texture2D &t):
82 add("image_data", &Loader::image_data);
83 add("raw_data", &Loader::raw_data);
84 add("storage", &Loader::storage);
87 void Texture2D::Loader::image_data(const string &data)
90 img.load_memory(data.data(), data.size());
92 static_cast<Texture2D &>(tex).image(img);
95 void Texture2D::Loader::raw_data(const string &data)
97 Texture2D &t2d=static_cast<Texture2D &>(tex);;
98 t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
101 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned b)
103 static_cast<Texture2D &>(tex).storage(fmt, w, h, b);