core/vgui/internals/vgui_gl_selection_macros.h
Go to the documentation of this file.
00001 #ifndef VGUI_GL_SELECTION_MACROS_H_
00002 #define VGUI_GL_SELECTION_MACROS_H_
00003 //:
00004 // \file
00005 // \author Amitha Perera
00006 //
00007 // This collection of macros is used to simplify the selection of the
00008 // vgui_pixel_* type based on the GL format and GL type. Suppose the
00009 // current format is in fmt and the current type is in typ. First
00010 // define a macro "Code" that containing the code that relies on the
00011 // corresponding vgui_pixel_type, and then call ConditionListBegin,
00012 // ConditionListBody, ConditionListFail. This will generate a
00013 // collection of if statements that will run "Code" with the
00014 // appropriate type. For example:
00015 //
00016 // \code
00017 // #define Code(pixel_type) buffer_of<pixel_type > buffer; convert_to_buffer(in_image,buffer);
00018 //
00019 //    ConditionListBegin;
00020 //    ConditionListBody( fmt, typ );
00021 //    ConditionListFail
00022 //    {
00023 //       vcl_cerr << "don't know " << fmt << " and " << typ << "\n";
00024 //       return false;
00025 //    }
00026 //
00027 // #undef Code
00028 // \endcode
00029 //
00030 // will generate code like
00031 //
00032 // \code
00033 // if ( fmt==GL_RGB && typ==GL_UNSIGNED )
00034 // {
00035 //   buffer_of<vgui_pixel_rgb888 > buffer;
00036 //   convert_to_buffer(in_image,buffer);
00037 // } else if ( fmt==GL_RGB && typ==GL_UNSIGNED_SHORT_5_6_5 )
00038 // {
00039 //   buffer_of<vgui_pixel_rgb565 > buffer;
00040 //   convert_to_buffer(in_image,buffer);
00041 // } else if (
00042 //      ...
00043 // } else
00044 // {
00045 //   vcl_cerr << "don't know " << fmt << " and " << typ << "\n";
00046 //   return false;
00047 // }
00048 // \endcode
00049 //
00050 // If you don't want to handle the failure condition, you can replace
00051 // ConditionListFail with ConditionListEnd.
00052 
00053 #include <vgui/vgui_gl.h>
00054 #include <vgui/vgui_pixel.h>
00055 
00056 #define ConditionList0( format, type ) \
00057   else if ( format==GL_RGB && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_rgb888 ) } \
00058   else if ( format==GL_BGR && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_bgr888 ) } \
00059   else if ( format==GL_RGBA && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_rgba8888 ) }
00060 
00061 #if defined(GL_UNSIGNED_SHORT_5_6_5)
00062 #define ConditionList1( format, type ) \
00063   else if ( format==GL_RGB && type==GL_UNSIGNED_SHORT_5_6_5 ) { Code( vgui_pixel_rgb565 ) }
00064 #else
00065 #define ConditionList1( format, type ) /* null */
00066 #endif
00067 
00068 // Is this right? GL_RGB and pixel_bgra? It is missing some endian issues?
00069 #if defined(GL_UNSIGNED_SHORT_5_5_5_1)
00070 #define ConditionList2( format, type ) \
00071   else if ( format==GL_RGB && type==GL_UNSIGNED_SHORT_5_5_5_1 ) { Code( vgui_pixel_bgra5551 ) }
00072 #else
00073 #define ConditionList2( format, type ) /* null */
00074 #endif
00075 
00076 #if defined(GL_BGRA)
00077 #define ConditionList3( format, type ) \
00078   else if ( format==GL_BGRA && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_bgra8888 ) }
00079 #else
00080 #define ConditionList3( format, type ) /* null */
00081 #endif
00082 
00083 #if defined(GL_EXT_abgr) || defined(GL_ABGR_EXT)
00084 #define ConditionList4( format, type ) \
00085   else if ( format==GL_ABGR_EXT && type==GL_UNSIGNED_BYTE ) { Code( vgui_pixel_abgr8888 ) }
00086 #else
00087 #define ConditionList4( format, type ) /* null */
00088 #endif
00089 
00090 
00091 #define ConditionListBody( format, type ) \
00092   ConditionList0(format,type) \
00093   ConditionList1(format,type) \
00094   ConditionList2(format,type) \
00095   ConditionList3(format,type) \
00096   ConditionList4(format,type) \
00097   else if (0)do{}while (0)
00098 
00099 #define ConditionListBegin if (0) do {} while (0)
00100 #define ConditionListFail else
00101 #define ConditionListEnd  else { /* null */ }
00102 
00103 #endif // VGUI_GL_SELECTION_MACROS_H_