zoukankan      html  css  js  c++  java
  • gles2.0环境的在windows上的建立

    这里也有一个视频来讲解,大家可以看下,可以多提问题,意见,建议

    http://edu.csdn.net/course/detail/606

      1 #include <Windows.h>
      2 #include <tchar.h>
      3 #include <EGL/egl.h>
      4 #include <gles2/gl2.h>
      5 
      6 
      7 
      8 class   CELLWinApp
      9 {
     10 public:
     11     /**
     12     *   应用程序实例句柄
     13     */
     14     HINSTANCE           _hInstance;
     15     /**
     16     *   窗口句柄,操作窗口使用
     17     */
     18     HWND                _hWnd;
     19     /**
     20     *   窗口的宽度和高度
     21     */
     22     int                 _winWidth;
     23     int                 _winHeight;
     24     EGLConfig            _config;
     25     EGLSurface             _surface;
     26     EGLContext             _context;
     27     EGLDisplay             _display;
     28 public:
     29     CELLWinApp(HINSTANCE hInstance = 0)
     30     {
     31         _hWnd       =   0;
     32         _winWidth   =   0;
     33         _winHeight  =   0;
     34         _hInstance  =   hInstance;
     35         _config     =   0;
     36         _surface    =   0;
     37         _context    =   0;
     38         _display    =   0;
     39         /**
     40         *   要想创建一个窗口,首先要注册一个窗口类
     41         *   相关内存,可以了解windows变成,这里不做介绍。
     42         */
     43         ::WNDCLASSEX winClass;
     44         winClass.lpszClassName  =   _T("CELLWinApp");
     45         winClass.cbSize         =   sizeof(::WNDCLASSEX);
     46         winClass.style          =   CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
     47         winClass.lpfnWndProc    =   windowProc;
     48         winClass.hInstance      =   hInstance;
     49         winClass.hIcon            =   0;
     50         winClass.hIconSm        =   0;
     51         winClass.hCursor        =   LoadCursor(NULL, IDC_ARROW);
     52         winClass.hbrBackground  =   (HBRUSH)GetStockObject(BLACK_BRUSH);
     53         winClass.lpszMenuName   =   NULL;
     54         winClass.cbClsExtra     =   0;
     55         winClass.cbWndExtra     =   0;
     56         RegisterClassEx(&winClass);
     57     }
     58     virtual ~CELLWinApp()
     59     {
     60     }
     61     /**
     62     *   渲染函数
     63     */
     64     virtual void    render()
     65     {
     66         glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
     67         glViewport(0,0,_winWidth,_winHeight);
     68         {
     69 
     70         }
     71         eglSwapBuffers(_display, _surface);
     72     }
     73 
     74     /**
     75     *   入口函数
     76     *   width :创建窗口宽度
     77     *   height:创建窗口的高度
     78     */
     79     int     start(HWND hWnd,int width,int height)
     80     {
     81         _winWidth   =   width;
     82         _winHeight  =   height;
     83 
     84         /**
     85         *   创建窗口
     86         */
     87         if (hWnd == 0)
     88         {
     89             if (!_createWindow(_winWidth,_winHeight))
     90             {
     91                 return  -1;
     92             }
     93         }
     94         else
     95         {
     96             _hWnd   =   hWnd;
     97         }
     98         /**
     99         *   初始化gles环境。
    100         */
    101         if (!initDevice())
    102         {
    103             return  -2;
    104         }
    105         onInit();
    106        
    107         if (hWnd)
    108         {
    109             return  0;
    110         }
    111         /**
    112         *   进入消息循环
    113         */
    114         MSG msg =   {0};
    115         while(msg.message != WM_QUIT)
    116         {
    117             if (msg.message == WM_DESTROY || 
    118                 msg.message == WM_CLOSE)
    119             {
    120                 break;
    121             }
    122             /**
    123             *   有消息,处理消息,无消息,则进行渲染绘制
    124             */
    125             if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
    126             { 
    127                 TranslateMessage( &msg );
    128                 DispatchMessage( &msg );
    129             }
    130             else
    131             {
    132                 render();
    133             }
    134         }
    135         /**
    136         *   关闭
    137         */
    138         shutDownDevice();
    139 
    140         return  0;
    141     }
    142     /**
    143     *   初始化OpenGL
    144     */
    145     bool    initDevice()
    146     {
    147 
    148         const EGLint attribs[] =
    149         {
    150             EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
    151             EGL_BLUE_SIZE, 8,
    152             EGL_GREEN_SIZE, 8,
    153             EGL_RED_SIZE, 8,
    154             EGL_DEPTH_SIZE,24,
    155             EGL_NONE
    156         };
    157         EGLint     format(0);
    158         EGLint    numConfigs(0);
    159         EGLint  major;
    160         EGLint  minor;
    161 
    162         //! 1
    163         _display        =    eglGetDisplay(EGL_DEFAULT_DISPLAY);
    164 
    165         //! 2init
    166         eglInitialize(_display, &major, &minor);
    167 
    168         //! 3
    169         eglChooseConfig(_display, attribs, &_config, 1, &numConfigs);
    170 
    171         eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);
    172         //! 4 
    173         _surface        =     eglCreateWindowSurface(_display, _config, _hWnd, NULL);
    174 
    175         //! 5
    176         EGLint attr[]   =   { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
    177         _context         =     eglCreateContext(_display, _config, 0, attr);
    178         //! 6
    179         if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE)
    180         {
    181             return false;
    182         }
    183 
    184         eglQuerySurface(_display, _surface, EGL_WIDTH,  &_winWidth);
    185         eglQuerySurface(_display, _surface, EGL_HEIGHT, &_winHeight);
    186 
    187         //! windows api
    188         SendMessage(_hWnd,WM_SIZE,0,0);
    189         return  true;
    190     }
    191     /**
    192     *   关闭
    193     */
    194     void    shutDownDevice()
    195     {
    196        
    197          onDestroy();
    198          if (_display != EGL_NO_DISPLAY)
    199          {
    200              eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    201              if (_context != EGL_NO_CONTEXT) 
    202              {
    203                  eglDestroyContext(_display, _context);
    204              }
    205              if (_surface != EGL_NO_SURFACE) 
    206              {
    207                  eglDestroySurface(_display, _surface);
    208              }
    209              eglTerminate(_display);
    210          }
    211          _display    =   EGL_NO_DISPLAY;
    212          _context    =   EGL_NO_CONTEXT;
    213          _surface    =   EGL_NO_SURFACE;
    214 
    215         UnregisterClass( _T("CELLWinApp"), _hInstance );
    216     }
    217     /**
    218     *   事件
    219     */
    220     virtual int     events(unsigned msg, unsigned wParam, unsigned lParam)
    221     {
    222     #ifndef GET_X_LPARAM
    223         #define GET_X_LPARAM(lp)                        ((int)(short)LOWORD(lp))
    224     #endif
    225 
    226     #ifndef GET_Y_LPARAM
    227         #define GET_Y_LPARAM(lp)                        ((int)(short)HIWORD(lp))
    228     #endif
    229 
    230     #ifndef GET_WHEEL_DELTA_WPARAM
    231         #define GET_WHEEL_DELTA_WPARAM(wParam)          (int)((short)HIWORD(wParam))
    232     #endif
    233 
    234         switch( msg )
    235         {
    236         case WM_SIZE:
    237             {
    238                 RECT    rt;
    239                 GetClientRect(_hWnd,&rt);
    240                 _winWidth   =   rt.right - rt.left;
    241                 _winHeight  =   rt.bottom - rt.top;
    242             }
    243             break;
    244         case WM_LBUTTONDOWN:
    245             {
    246             }
    247             break;
    248         case WM_LBUTTONUP:
    249             {
    250             }
    251             break;
    252         case WM_RBUTTONDOWN:
    253             {
    254             }
    255             break;
    256         case WM_RBUTTONUP:
    257             {
    258             }
    259             break;
    260         case WM_MOUSEMOVE:
    261             {
    262             }
    263             break;
    264 
    265         case WM_MOUSEWHEEL:
    266             {
    267             }
    268             break;
    269         case WM_CHAR:
    270             {
    271             }
    272             break;
    273         case WM_KEYDOWN:
    274             {
    275             }
    276             break;
    277         case WM_CLOSE:
    278         case WM_DESTROY:
    279             {
    280                 ::PostQuitMessage(0);
    281             }
    282             break;
    283         default:
    284             return DefWindowProc(_hWnd, msg, wParam, lParam );
    285         }
    286         return  0;
    287     }
    288 public:
    289     /**
    290     *   增加一个初始化OpenGL的函数,第二课中增加
    291     *   调用该函数完成对OpenGL的基本状态的初始化
    292     *   在进入消息循环之前的一次通知,只调用一次
    293     */
    294     virtual void    onInit()
    295     {
    296         /**
    297         *   清空窗口为黑色
    298         */
    299         glClearColor(0,0,0,1);
    300         /**
    301         *   设置OpenGL视口的位置和大小。
    302         */
    303         glViewport( 0, 0, (GLint) _winWidth, (GLint) _winHeight );
    304     }
    305     virtual void        onDestroy()
    306     {
    307     }
    308 protected:
    309     /**
    310     *   创建窗口函数
    311     */
    312     bool    _createWindow(int width,int height)
    313     {
    314         _hWnd   =   CreateWindowEx(
    315                                     NULL,
    316                                     _T("CELLWinApp"),
    317                                     _T("CELLWinApp"),
    318                                     WS_OVERLAPPEDWINDOW,
    319                                     CW_USEDEFAULT,
    320                                     CW_USEDEFAULT,
    321                                     width,
    322                                     height, 
    323                                     NULL, 
    324                                     NULL,
    325                                     _hInstance, 
    326                                     this    //! 这里注意,将当前类的指针作为参数,传递,参见 windowProc函数.
    327                                     );
    328        
    329         if( _hWnd == 0 )
    330         {
    331             return  false;
    332         }
    333         ShowWindow( _hWnd, SW_SHOW );
    334         UpdateWindow( _hWnd );
    335         return  true;
    336     }
    337     /**
    338     *   Windows消息过程处理函数
    339     */
    340     static  LRESULT CALLBACK    windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    341     {
    342 #define GWL_USERDATA (-21)
    343         /**
    344         *   使用this数据,将全局函数,转化为类的成员函数调用
    345         */
    346         CELLWinApp*  pThis   =   (CELLWinApp*)GetWindowLong(hWnd,GWL_USERDATA);
    347         if (pThis)
    348         {
    349             return  pThis->events(msg,wParam,lParam);
    350         }
    351         if (WM_CREATE == msg)
    352         {
    353             CREATESTRUCT*   pCreate =   (CREATESTRUCT*)lParam;
    354             SetWindowLong(hWnd,GWL_USERDATA,(DWORD_PTR)pCreate->lpCreateParams);
    355         }
    356         return  DefWindowProc( hWnd, msg, wParam, lParam );
    357     }
    358 };
  • 相关阅读:
    扫雷游戏:C++生成一个扫雷底板
    为《信息学奥赛一本通》平反(除了OJ很差和代码用宋体)
    关于最大公约数欧几里得算法辗转相除及最小公倍数算法拓展C++学习记录
    【Python】如何用较快速度用Python程序增加博客访问量——CSDN、博客园、知乎(应该包括简书)
    STL学习笔记之next_permutation函数
    C++集合容器STL结构Set
    11111111111
    express + mysql实践
    express实践
    ws:一个 Node.js WebSocket 库
  • 原文地址:https://www.cnblogs.com/zhanglitong/p/4427540.html
Copyright © 2011-2022 走看看