平台相关函数的隔离
以为通过EGLView类隔离平台相关性,已经可以了,结果发现gl在三个平台居然定义也有差别。比如GL_BGR,android居然没有。好吧,我们的渲染类renderer里面要根据三个不同的平台选择编译代码。肿么破?
考虑了下,决定用宏隔开好了。在编译之前,还有一个预处理阶段。来看代码。
//////////////////////////////////////////////////////////////////////////
//PlatformConfig.h
//////////////////////////////////////////////////////////////////////////
#pragma once
#define PLATFORM_UNKNOWN 0
#define PLATFORM_IOS 1
#define PLATFORM_ANDROID 2
#define PLATFORM_WIN32 3
// Determine target platform by compile environment macro.
#define TARGET_PLATFORM PLATFORM_UNKNOWN
// iphone
#if defined(TARGET_OS_IPHONE)
#undef TARGET_PLATFORM
#define TARGET_PLATFORM PLATFORM_IOS
#endif
// android
#if defined(ANDROID)
#undef TARGET_PLATFORM
#define TARGET_PLATFORM PLATFORM_ANDROID
#endif
// win32
#if defined(WIN32) && defined(_WINDOWS)
#undef TARGET_PLATFORM
#define TARGET_PLATFORM PLATFORM_WIN32
#endif
这样不同的平台,TARGET_PLATFORM的值会不一样。
再来看gl头文件的引用
//////////////////////////////////////////////////////////////////////////
//GLFix.h
//////////////////////////////////////////////////////////////////////////
#pragma once
#include "PlatformConfig.h"
#if (TARGET_PLATFORM == PLATFORM_WIN32)
#include "glew.h"
#endif
#if (TARGET_PLATFORM == PLATFORM_ANDROID)
#define glClearDepth glClearDepthf
#define glDeleteVertexArrays glDeleteVertexArraysOES
#define glGenVertexArrays glGenVertexArraysOES
#define glBindVertexArray glBindVertexArrayOES
#define glMapBuffer glMapBufferOES
#define glUnmapBuffer glUnmapBufferOES
#define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
#define GL_WRITE_ONLY GL_WRITE_ONLY_OES
// GL_GLEXT_PROTOTYPES isn't defined in glplatform.h on android ndk r7
// we manually define it here
#include <GLES2/gl2platform.h>
#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES 1
#endif
// normal process
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
// gl2.h doesn't define GLchar on Android
typedef char GLchar;
// android defines GL_BGRA_EXT but not GL_BRGA
#ifndef GL_BGRA
#define GL_BGRA 0x80E1
#endif
#ifndef GL_BGR
#define GL_BGR 0x80E0
#endif
//declare here while define in EGLView_android.cpp
extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT;
extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT;
extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT;
#define glGenVertexArraysOES glGenVertexArraysOESEXT
#define glBindVertexArrayOES glBindVertexArrayOESEXT
#define glDeleteVertexArraysOES glDeleteVertexArraysOESEXT
#endif
好了,搞定。