]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.cpp
Use default member initializers for simple types
[libs/gl.git] / source / core / texture2d.cpp
1 #include "error.h"
2 #include "texture2d.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 Texture2D::~Texture2D()
10 {
11         set_manager(0);
12 }
13
14 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
15 {
16         if(width>0)
17         {
18                 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
19                         throw incompatible_data("Texture2D::storage");
20                 return;
21         }
22         if(wd==0 || ht==0)
23                 throw invalid_argument("Texture2D::storage");
24
25         set_format(fmt);
26         width = wd;
27         height = ht;
28         levels = get_n_levels();
29         if(lv>0)
30                 levels = min(levels, lv);
31
32         allocate();
33 }
34
35 void Texture2D::image(unsigned level, const void *data)
36 {
37         LinAl::Vector<unsigned, 2> size = get_level_size(level);
38         return sub_image(level, 0, 0, size.x, size.y, data);
39 }
40
41 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
42 {
43         if(width==0 || height==0)
44                 throw invalid_operation("Texture2D::sub_image");
45         if(level>=levels)
46                 throw out_of_range("Texture2D::sub_image");
47
48         Texture2DBackend::sub_image(level, x, y, wd, ht, data);
49 }
50
51 void Texture2D::image(const Graphics::Image &img, unsigned lv)
52 {
53         storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
54         image(0, img.get_pixels());
55 }
56
57 unsigned Texture2D::get_n_levels() const
58 {
59         unsigned n = 0;
60         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
61         return n;
62 }
63
64 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
65 {
66         unsigned w = width>>level;
67         unsigned h = height>>level;
68
69         if(!w && h)
70                 w = 1;
71         else if(!h && w)
72                 h = 1;
73
74         return LinAl::Vector<unsigned, 2>(w, h);
75 }
76
77 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
78 {
79         return create_async_loader(io);
80 }
81
82 uint64_t Texture2D::get_data_size() const
83 {
84         return id ? width*height*get_pixel_size(format) : 0;
85 }
86
87
88 Texture2D::Loader::Loader(Texture2D &t):
89         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
90 {
91         init();
92 }
93
94 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
95         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
96 {
97         init();
98 }
99
100 void Texture2D::Loader::init()
101 {
102         add("raw_data", &Loader::raw_data);
103         add("storage", &Loader::storage);
104         add("storage", &Loader::storage_levels);
105 }
106
107 void Texture2D::Loader::raw_data(const string &data)
108 {
109         if(obj.manager)
110                 obj.set_manager(0);
111         obj.image(0, data.data());
112 }
113
114 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
115 {
116         obj.storage(fmt, w, h);
117 }
118
119 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
120 {
121         obj.storage(fmt, w, h, l);
122 }
123
124 } // namespace GL
125 } // namespace Msp