2010年2月23日 星期二

CuEGL

class CuEGL  
{

    int _red_size;
    int _green_size;
    int _bule_size;
    int _alpha_size;

public:

    // 
    void SwapBuffers();


    // 只要有呼叫 Init() 做初始化 就要呼叫 Exit() 
    // 在這裡 解構會自動呼叫
    void Exit();

    // 初始化 EGL
    // Return:  1 有問題初始化失敗   0: 一切正常
    bool Init(NativeWindowType hWnd);

    CuEGL();
    virtual ~CuEGL();

    EGLDisplay  _eglDisplay;
    EGLConfig   _eglConfig;
    EGLSurface  _eglSurface;
    EGLContext  _eglContext;



};

CuEGL::CuEGL()
{

    _eglDisplay = EGL_NO_DISPLAY;
    _eglConfig;
    _eglSurface = EGL_NO_SURFACE;
    _eglContext = EGL_NO_CONTEXT;

    _red_size = 5;
    _green_size = 6;
    _bule_size = 5;
    _alpha_size = 0;


}

CuEGL::~CuEGL()
{

    if( _eglDisplay != EGL_NO_DISPLAY )
        this->Exit();

}


bool CuEGL::Init(NativeWindowType hWnd)
{

    int buffer_size = _red_size + _green_size + _bule_size + _alpha_size;
    EGLint  attrs[] = {
        EGL_ALPHA_SIZE, _alpha_size,
        EGL_RED_SIZE, _red_size,
        EGL_GREEN_SIZE, _green_size,
        EGL_BLUE_SIZE, _bule_size,
        EGL_BUFFER_SIZE, buffer_size,
        EGL_DEPTH_SIZE, 16,
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_NONE};

    EGLint  numConfig;
    LONG        lRet = 1; 

    // NOTE: Theres a bunch of stuff we should be doing to release resources
    //       in the case of failure that we're not bothering with atm

    HDC hdc = GetWindowDC((HWND)hWnd);

    // Get the display device


    _eglDisplay = eglGetDisplay(hdc);

    Assert( _eglDisplay != EGL_NO_DISPLAY, L"CuEGL::Init:\n _eglDisplay == EGL_NO_DISPLAY");

    // Initialize the display
    if (!eglInitialize(_eglDisplay, NULL, NULL))
        {  
            Assert(0, L"CuEGL::Init:\n can't Initialize egl");
            return lRet;

        }

    // Obtain the first configuration with a depth buffer
    if (!eglChooseConfig(_eglDisplay, attrs, &_eglConfig, 1, &numConfig))
        { 
            Assert(0, L"CuEGL::Init:\n can't ChooseConfig");
            return lRet; 
        }

    // Create a surface for the main window
    _eglSurface = eglCreateWindowSurface(_eglDisplay, _eglConfig, (NativeWindowType)hWnd, NULL);
    Assert( _eglSurface != EGL_NO_SURFACE, L"CuEGL::Init:\n _eglSurface == EGL_NO_SURFACE");

    // Create an OpenGL ES context
    _eglContext = eglCreateContext(_eglDisplay, _eglConfig, EGL_NO_CONTEXT, NULL);
    Assert( _eglContext != EGL_NO_CONTEXT, L"CuEGL::Init:\n _eglContext == EGL_NO_CONTEXT");


    // Make the context and surface current
    if (!eglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext))
        {  
            Assert(0, L"CuEGL::Init:\n can't MakeCurrent");
            return lRet; 
        }

    return 0;

}

void CuEGL::Exit()
{

    // Set the current context to nothing
    eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

    // Free any EGL contexts; should be called per context created
    eglDestroyContext(_eglDisplay, _eglContext);

    // Free any EGL surfaces; should be called per surface created
    eglDestroySurface(_eglDisplay, _eglSurface);

    // Terminate any EGL displays; should be called per display initialized
    eglTerminate(_eglDisplay); 

}

void CuEGL::SwapBuffers()
{

    eglSwapBuffers(_eglDisplay, _eglSurface);

}


好舊的碼了~ 如果有更新會在加上去

沒有留言:

張貼留言