3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "texture2d.h"
17 Texture2D::Texture2D():
25 void Texture2D::storage(PixelFormat fmt, sizei wd, sizei ht, int brd)
28 throw InvalidState("Texture storage may only be specified once");
30 throw InvalidParameterValue("Invalid texture dimensions");
38 void Texture2D::image(int level, PixelFormat fmt, DataType type, const void *data)
41 throw InvalidState("Texture storage has not been specified");
45 glTexImage2D(target, level, ifmt, width, height, border, fmt, type, data);
48 void Texture2D::sub_image(int level, int x, int y, sizei wd, sizei ht, PixelFormat fmt, DataType type, const void *data)
51 throw InvalidState("Texture storage has not been specified");
55 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
58 void Texture2D::load_image(const string &fn)
66 void Texture2D::image(const Image &img)
68 unsigned w=img.get_width();
69 unsigned h=img.get_height();
70 PixelFormat fmt=img.get_format();
72 storage(fmt, w, h, 0);
73 else if(w!=width || h!=height)
74 throw IncompatibleData("Image does not match texture storage");
76 image(0, fmt, UNSIGNED_BYTE, img.get_data());
80 Texture2D::Loader::Loader(Texture2D &t):
83 add("image_data", &Loader::image_data);
84 add("raw_data", &Loader::raw_data);
85 add("storage", &Loader::storage);
88 void Texture2D::Loader::image_data(const string &data)
91 img.load_lump(data.data(), data.size());
93 static_cast<Texture2D &>(tex).image(img);
96 void Texture2D::Loader::raw_data(const string &data)
98 Texture2D &t2d=static_cast<Texture2D &>(tex);;
99 t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
102 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned b)
104 static_cast<Texture2D &>(tex).storage(fmt, w, h, b);