1 #ifndef VGUI_GL_SELECTION_MACROS_H_
2 #define VGUI_GL_SELECTION_MACROS_H_
3 //:
4 // \file
5 // \author Amitha Perera
6 //
7 // This collection of macros is used to simplify the selection of the
8 // vgui_pixel_* type based on the GL format and GL type. Suppose the
9 // current format is in fmt and the current type is in typ. First
10 // define a macro "Code" that containing the code that relies on the
11 // corresponding vgui_pixel_type, and then call ConditionListBegin,
12 // ConditionListBody, ConditionListFail. This will generate a
13 // collection of if statements that will run "Code" with the
14 // appropriate type. For example:
15 //
16 // \code
17 // #define Code(pixel_type) buffer_of<pixel_type > buffer; convert_to_buffer(in_image,buffer);
18 //
19 //    ConditionListBegin;
20 //    ConditionListBody( fmt, typ );
21 //    ConditionListFail
22 //    {
23 //       std::cerr << "don't know " << fmt << " and " << typ << "\n";
24 //       return false;
25 //    }
26 //
27 // #undef Code
28 // \endcode
29 //
30 // will generate code like
31 //
32 // \code
33 // if ( fmt==GL_RGB && typ==GL_UNSIGNED )
34 // {
35 //   buffer_of<vgui_pixel_rgb888 > buffer;
36 //   convert_to_buffer(in_image,buffer);
37 // } else if ( fmt==GL_RGB && typ==GL_UNSIGNED_SHORT_5_6_5 )
38 // {
39 //   buffer_of<vgui_pixel_rgb565 > buffer;
40 //   convert_to_buffer(in_image,buffer);
41 // } else if (
42 //      ...
43 // } else
44 // {
45 //   std::cerr << "don't know " << fmt << " and " << typ << "\n";
46 //   return false;
47 // }
48 // \endcode
49 //
50 // If you don't want to handle the failure condition, you can replace
51 // ConditionListFail with ConditionListEnd.
52 
53 #include <vgui/vgui_gl.h>
54 #include <vgui/vgui_pixel.h>
55 
56 #define ConditionList0( format, type ) \
57   else if ( format==GL_RGB && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_rgb888 ) } \
58   else if ( format==GL_BGR && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_bgr888 ) } \
59   else if ( format==GL_RGBA && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_rgba8888 ) }
60 
61 #if defined(GL_UNSIGNED_SHORT_5_6_5)
62 #define ConditionList1( format, type ) \
63   else if ( format==GL_RGB && type==GL_UNSIGNED_SHORT_5_6_5 ) { Code( vgui_pixel_rgb565 ) }
64 #else
65 #define ConditionList1( format, type ) /* null */
66 #endif
67 
68 // Is this right? GL_RGB and pixel_bgra? It is missing some endian issues?
69 #if defined(GL_UNSIGNED_SHORT_5_5_5_1)
70 #define ConditionList2( format, type ) \
71   else if ( format==GL_RGB && type==GL_UNSIGNED_SHORT_5_5_5_1 ) { Code( vgui_pixel_bgra5551 ) }
72 #else
73 #define ConditionList2( format, type ) /* null */
74 #endif
75 
76 #if defined(GL_BGRA)
77 #define ConditionList3( format, type ) \
78   else if ( format==GL_BGRA && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_bgra8888 ) }
79 #else
80 #define ConditionList3( format, type ) /* null */
81 #endif
82 
83 #if defined(GL_EXT_abgr) || defined(GL_ABGR_EXT)
84 #define ConditionList4( format, type ) \
85   else if ( format==GL_ABGR_EXT && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_abgr8888 ) }
86 #else
87 #define ConditionList4( format, type ) /* null */
88 #endif
89 
90 
91 #define ConditionListBody( format, type ) \
92   ConditionList0(format,type) \
93   ConditionList1(format,type) \
94   ConditionList2(format,type) \
95   ConditionList3(format,type) \
96   ConditionList4(format,type) \
97   else if (0)do{}while (0)
98 
99 #define ConditionListBegin if (0) do {} while (0)
100 #define ConditionListFail else
101 #define ConditionListEnd  else { /* null */ }
102 
103 #endif // VGUI_GL_SELECTION_MACROS_H_
104