1 /****************************************************************************
2
3 GLUI User Interface Toolkit
4 ---------------------------
5
6 glui_bitmaps.cpp
7
8 Draws the hardcoded images listed in glui_bitmap_img_data with OpenGL.
9
10 FIXME: upload the images to a texture. This will allow them to be:
11 - Drawn with alpha blending
12 - Drawn at random sizes and angles onscreen
13 - Drawn much faster than with glDrawPixels
14
15 --------------------------------------------------
16
17 Copyright (c) 1998 Paul Rademacher
18
19 WWW: http://sourceforge.net/projects/glui/
20 Forums: http://sourceforge.net/forum/?group_id=92496
21
22 This library is free software; you can redistribute it and/or
23 modify it under the terms of the GNU Lesser General Public
24 License as published by the Free Software Foundation; either
25 version 2.1 of the License, or (at your option) any later version.
26
27 This library is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 Lesser General Public License for more details.
31
32 You should have received a copy of the GNU Lesser General Public
33 License along with this library; if not, write to the Free Software
34 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35
36 *****************************************************************************/
37
38 #include "GL/glui.h"
39 #include "glui_internal.h"
40 #include <cassert>
41
42 /************ Image Bitmap arrays **********/
43
44 extern unsigned char glui_img_checkbox_0[];
45 extern unsigned char glui_img_checkbox_1[];
46 extern unsigned char glui_img_radiobutton_0[];
47 extern unsigned char glui_img_radiobutton_1[];
48 extern unsigned char glui_img_uparrow[];
49 extern unsigned char glui_img_downarrow[];
50 extern unsigned char glui_img_leftarrow[];
51 extern unsigned char glui_img_rightarrow[];
52 extern unsigned char glui_img_spinup_0[];
53 extern unsigned char glui_img_spinup_1[];
54 extern unsigned char glui_img_spindown_0[];
55 extern unsigned char glui_img_spindown_1[];
56 extern unsigned char glui_img_checkbox_0_dis[];
57 extern unsigned char glui_img_checkbox_1_dis[];
58 extern unsigned char glui_img_radiobutton_0_dis[];
59 extern unsigned char glui_img_radiobutton_1_dis[];
60 extern unsigned char glui_img_spinup_dis[];
61 extern unsigned char glui_img_spindown_dis[];
62 extern unsigned char glui_img_listbox_up[];
63 extern unsigned char glui_img_listbox_down[];
64 extern unsigned char glui_img_listbox_up_dis[];
65
66
67 // These must be in the same order as the GLUI_STDBITMAP enums from glui.h!
68 unsigned char *bitmap_arrays[] = {
69 glui_img_checkbox_0,
70 glui_img_checkbox_1,
71 glui_img_radiobutton_0,
72 glui_img_radiobutton_1,
73 glui_img_uparrow,
74 glui_img_downarrow,
75 glui_img_leftarrow,
76 glui_img_rightarrow,
77 glui_img_spinup_0,
78 glui_img_spinup_1,
79 glui_img_spindown_0,
80 glui_img_spindown_1,
81 glui_img_checkbox_0_dis,
82 glui_img_checkbox_1_dis,
83 glui_img_radiobutton_0_dis,
84 glui_img_radiobutton_1_dis,
85 glui_img_spinup_dis,
86 glui_img_spindown_dis,
87 glui_img_listbox_up,
88 glui_img_listbox_down,
89 glui_img_listbox_up_dis,
90 };
91
92
93 /************************************ GLUI_Bitmap::load_from_array() ********/
94
GLUI_Bitmap()95 GLUI_Bitmap::GLUI_Bitmap()
96 : pixels(NULL),
97 w(0),
98 h(0)
99 {
100 }
101
~GLUI_Bitmap()102 GLUI_Bitmap::~GLUI_Bitmap()
103 {
104 if (pixels)
105 {
106 free(pixels);
107 pixels = NULL;
108 }
109 }
110
111 /* Create bitmap from greyscale byte array */
init_grey(unsigned char * array)112 void GLUI_Bitmap::init_grey(unsigned char *array)
113 {
114 w = array[0]; h = array[1];
115 pixels = (unsigned char *) malloc(w*h*3);
116 assert(pixels);
117
118 for(int i = 0; i<w*h; i++ )
119 for (int j = 0; j<3; j++) /* copy grey to r,g,b channels */
120 pixels[i*3+j] = (unsigned char) array[i+2];
121 }
122
123
124 /* Create bitmap from color int array.
125 (OSL) This used to be how all GLUI bitmaps were stored, which was horribly
126 inefficient--three ints per pixel, or 12 bytes per pixel!
127 */
init(int * array)128 void GLUI_Bitmap::init(int *array)
129 {
130 w = array[0]; h = array[1];
131 pixels = (unsigned char *) malloc(w*h*3);
132 assert(pixels);
133
134 for (int i = 0; i<w*h*3; i++)
135 pixels[i] = (unsigned char) array[i+2];
136 }
137
138
139 /*********************************** GLUI_StdBitmaps::draw() *****************/
140
GLUI_StdBitmaps()141 GLUI_StdBitmaps::GLUI_StdBitmaps()
142 {
143 for (int i=0; i<GLUI_STDBITMAP_NUM_ITEMS; i++)
144 bitmaps[i].init_grey(bitmap_arrays[i]);
145 }
146
~GLUI_StdBitmaps()147 GLUI_StdBitmaps::~GLUI_StdBitmaps()
148 {
149 }
150
width(int i) const151 int GLUI_StdBitmaps::width(int i) const
152 {
153 assert(i>=0 && i<GLUI_STDBITMAP_NUM_ITEMS);
154 return bitmaps[i].w;
155 }
156
height(int i) const157 int GLUI_StdBitmaps::height(int i) const
158 {
159 assert(i>=0 && i<GLUI_STDBITMAP_NUM_ITEMS);
160 return bitmaps[i].h;
161 }
162
draw(int i,int x,int y) const163 void GLUI_StdBitmaps::draw(int i, int x, int y) const
164 {
165 assert(i>=0 && i<GLUI_STDBITMAP_NUM_ITEMS);
166
167 if (bitmaps[i].pixels != NULL )
168 {
169 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
170 glRasterPos2f(0.5f+x, 0.5f+y+bitmaps[i].h);
171 glDrawPixels(
172 bitmaps[i].w, bitmaps[i].h,
173 GL_RGB, GL_UNSIGNED_BYTE, bitmaps[i].pixels);
174 }
175 }
176
177