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

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

 #include <Windows.h>
#include <tchar.h>
#include <EGL/egl.h>
#include <gles2/gl2.h> class CELLWinApp
{
public:
/**
* 应用程序实例句柄
*/
HINSTANCE _hInstance;
/**
* 窗口句柄,操作窗口使用
*/
HWND _hWnd;
/**
* 窗口的宽度和高度
*/
int _winWidth;
int _winHeight;
EGLConfig _config;
EGLSurface _surface;
EGLContext _context;
EGLDisplay _display;
public:
CELLWinApp(HINSTANCE hInstance = )
{
_hWnd = ;
_winWidth = ;
_winHeight = ;
_hInstance = hInstance;
_config = ;
_surface = ;
_context = ;
_display = ;
/**
* 要想创建一个窗口,首先要注册一个窗口类
* 相关内存,可以了解windows变成,这里不做介绍。
*/
::WNDCLASSEX winClass;
winClass.lpszClassName = _T("CELLWinApp");
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = ;
winClass.hIconSm = ;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = ;
winClass.cbWndExtra = ;
RegisterClassEx(&winClass);
}
virtual ~CELLWinApp()
{
}
/**
* 渲染函数
*/
virtual void render()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glViewport(,,_winWidth,_winHeight);
{ }
eglSwapBuffers(_display, _surface);
} /**
* 入口函数
* width :创建窗口宽度
* height:创建窗口的高度
*/
int start(HWND hWnd,int width,int height)
{
_winWidth = width;
_winHeight = height; /**
* 创建窗口
*/
if (hWnd == )
{
if (!_createWindow(_winWidth,_winHeight))
{
return -;
}
}
else
{
_hWnd = hWnd;
}
/**
* 初始化gles环境。
*/
if (!initDevice())
{
return -;
}
onInit(); if (hWnd)
{
return ;
}
/**
* 进入消息循环
*/
MSG msg = {};
while(msg.message != WM_QUIT)
{
if (msg.message == WM_DESTROY ||
msg.message == WM_CLOSE)
{
break;
}
/**
* 有消息,处理消息,无消息,则进行渲染绘制
*/
if( PeekMessage( &msg, NULL, , , PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
render();
}
}
/**
* 关闭
*/
shutDownDevice(); return ;
}
/**
* 初始化OpenGL
*/
bool initDevice()
{ const EGLint attribs[] =
{
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, ,
EGL_GREEN_SIZE, ,
EGL_RED_SIZE, ,
EGL_DEPTH_SIZE,,
EGL_NONE
};
EGLint format();
EGLint numConfigs();
EGLint major;
EGLint minor; //! 1
_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); //! 2init
eglInitialize(_display, &major, &minor); //! 3
eglChooseConfig(_display, attribs, &_config, , &numConfigs); eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);
//! 4
_surface = eglCreateWindowSurface(_display, _config, _hWnd, NULL); //! 5
EGLint attr[] = { EGL_CONTEXT_CLIENT_VERSION, , EGL_NONE, EGL_NONE };
_context = eglCreateContext(_display, _config, , attr);
//! 6
if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE)
{
return false;
} eglQuerySurface(_display, _surface, EGL_WIDTH, &_winWidth);
eglQuerySurface(_display, _surface, EGL_HEIGHT, &_winHeight); //! windows api
SendMessage(_hWnd,WM_SIZE,,);
return true;
}
/**
* 关闭
*/
void shutDownDevice()
{ onDestroy();
if (_display != EGL_NO_DISPLAY)
{
eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (_context != EGL_NO_CONTEXT)
{
eglDestroyContext(_display, _context);
}
if (_surface != EGL_NO_SURFACE)
{
eglDestroySurface(_display, _surface);
}
eglTerminate(_display);
}
_display = EGL_NO_DISPLAY;
_context = EGL_NO_CONTEXT;
_surface = EGL_NO_SURFACE; UnregisterClass( _T("CELLWinApp"), _hInstance );
}
/**
* 事件
*/
virtual int events(unsigned msg, unsigned wParam, unsigned lParam)
{
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#endif #ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
#endif #ifndef GET_WHEEL_DELTA_WPARAM
#define GET_WHEEL_DELTA_WPARAM(wParam) (int)((short)HIWORD(wParam))
#endif switch( msg )
{
case WM_SIZE:
{
RECT rt;
GetClientRect(_hWnd,&rt);
_winWidth = rt.right - rt.left;
_winHeight = rt.bottom - rt.top;
}
break;
case WM_LBUTTONDOWN:
{
}
break;
case WM_LBUTTONUP:
{
}
break;
case WM_RBUTTONDOWN:
{
}
break;
case WM_RBUTTONUP:
{
}
break;
case WM_MOUSEMOVE:
{
}
break; case WM_MOUSEWHEEL:
{
}
break;
case WM_CHAR:
{
}
break;
case WM_KEYDOWN:
{
}
break;
case WM_CLOSE:
case WM_DESTROY:
{
::PostQuitMessage();
}
break;
default:
return DefWindowProc(_hWnd, msg, wParam, lParam );
}
return ;
}
public:
/**
* 增加一个初始化OpenGL的函数,第二课中增加
* 调用该函数完成对OpenGL的基本状态的初始化
* 在进入消息循环之前的一次通知,只调用一次
*/
virtual void onInit()
{
/**
* 清空窗口为黑色
*/
glClearColor(,,,);
/**
* 设置OpenGL视口的位置和大小。
*/
glViewport( , , (GLint) _winWidth, (GLint) _winHeight );
}
virtual void onDestroy()
{
}
protected:
/**
* 创建窗口函数
*/
bool _createWindow(int width,int height)
{
_hWnd = CreateWindowEx(
NULL,
_T("CELLWinApp"),
_T("CELLWinApp"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
NULL,
NULL,
_hInstance,
this //! 这里注意,将当前类的指针作为参数,传递,参见 windowProc函数.
); if( _hWnd == )
{
return false;
}
ShowWindow( _hWnd, SW_SHOW );
UpdateWindow( _hWnd );
return true;
}
/**
* Windows消息过程处理函数
*/
static LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
#define GWL_USERDATA (-21)
/**
* 使用this数据,将全局函数,转化为类的成员函数调用
*/
CELLWinApp* pThis = (CELLWinApp*)GetWindowLong(hWnd,GWL_USERDATA);
if (pThis)
{
return pThis->events(msg,wParam,lParam);
}
if (WM_CREATE == msg)
{
CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
SetWindowLong(hWnd,GWL_USERDATA,(DWORD_PTR)pCreate->lpCreateParams);
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
};

最新文章

  1. WPF之Binding
  2. view类的setVisibility
  3. mysql使用索引优化查询效率
  4. [Tex学习笔记]数学公式再次测试
  5. ViEmuVS2013-3.2.1 破解
  6. 【GOF23设计模式】享元模式
  7. x264_param_t结构
  8. update慢怎样处理?
  9. tmux简要介绍
  10. 解释session
  11. P5303 [GXOI/GZOI2019]逼死强迫症
  12. 章节十、1-用ID和XPath、name定位元素
  13. Download SQL Server Management Studio (SSMS)下载地址
  14. xshell的Solarized Dark配色方案
  15. dp练习(1)——马走日字
  16. webrtc--stun-turn
  17. 15JavaScript switch语句
  18. jquery如何判断checkbox(复选框)是否被选中(转)
  19. C# datatable竖行转换的问题
  20. linux命令(40):at命令

热门文章

  1. 玩转git分支
  2. The First Android App----Adding the Action Bar
  3. codeforces 261B Maxim and Restaurant(概率DP)
  4. Berlin 10.1 支持 iPhone 4 (iOS v7.x)
  5. 【TypeScript】TypeScript 学习 2——接口
  6. git archive命令详解
  7. Get User CustomNotificationAddresses
  8. BitAdminCore框架更新日志20180522
  9. STM32的时钟配置随笔
  10. winform程序更新