]> git.tdb.fi Git - libs/gl.git/blob - source/ilwrap.cpp
Unbind vertex buffer after updating array data
[libs/gl.git] / source / ilwrap.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifdef WITH_DEVIL
9 #include <IL/il.h>
10 #endif
11 #include <msp/core/except.h>
12 #include "ilwrap.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 Image::Image()
20 {
21 #ifdef WITH_DEVIL
22         static bool init_done=false;
23
24         if(!init_done)
25         {
26                 ilInit();
27                 ilEnable(IL_ORIGIN_SET);
28                 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
29                 init_done=true;
30         }
31
32         ilGenImages(1, &id);
33 #else
34         throw Exception("DevIL support not compiled in");
35 #endif
36 }
37
38 Image::~Image()
39 {
40 #ifdef WITH_DEVIL
41         ilDeleteImages(1, &id);
42 #endif
43 }
44
45 void Image::load_file(const string &fn)
46 {
47 #ifdef WITH_DEVIL
48         ilBindImage(id);
49         if(!ilLoadImage(const_cast<char *>(fn.c_str())))
50                 throw Exception("Error loading image "+fn);
51 #else
52         (void)fn;
53 #endif
54 }
55
56 void Image::load_lump(const void *data, unsigned size)
57 {
58 #ifdef WITH_DEVIL
59         ilBindImage(id);
60         if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
61                 throw Exception("Error loading image from lump");
62 #else
63         (void)data; (void)size;
64 #endif
65 }
66
67 PixelFormat Image::get_format() const
68 {
69 #ifdef WITH_DEVIL
70         switch(ilGetInteger(IL_IMAGE_FORMAT))
71         {
72         case IL_COLOR_INDEX: return COLOR_INDEX;
73         case IL_LUMINANCE: return LUMINANCE;
74         case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
75         case IL_RGB: return RGB;
76         case IL_RGBA: return RGBA;
77         case IL_BGR: return BGR;
78         case IL_BGRA: return BGRA;
79         default: throw InvalidParameterValue("Unknown pixel format in image");
80         }
81 #else
82         return RGB;
83 #endif
84 }
85
86 unsigned Image::get_width() const
87 {
88 #ifdef WITH_DEVIL
89         return ilGetInteger(IL_IMAGE_WIDTH);
90 #else
91         return 0;
92 #endif
93 }
94
95 unsigned Image::get_height() const
96 {
97 #ifdef WITH_DEVIL
98         return ilGetInteger(IL_IMAGE_HEIGHT);
99 #else
100         return 0;
101 #endif
102 }
103
104 const void *Image::get_data() const
105 {
106 #ifdef WITH_DEVIL
107         return ilGetData();
108 #else
109         return 0;
110 #endif
111 }
112
113 } // namespace GL
114 } // namespace Msp