OpenGLES---分离窗口和OpenGLES代码
来源:互联网 发布:产品外观设计软件 编辑:程序博客网 时间:2024/05/29 04:48
AppProtocol 协议(初始化,绘制,销毁)
AppWin 窗口实现
AppDelegate 实现OpenGLES初始化,绘制,销毁
注释:cocos2dx中抄的(去掉父类中的单例,就这点不太一样,都是一个对象)
-- AppProtocol AppWin 窗口实现 (AppWin.hpp)
#pragma once#include <Windows.h>#include <tchar.h>class AppProtocol{public:virtual bool ini()=0;//初始化virtual void renderer()=0;//画virtual void destroy()=0;//销毁};/*** 1:注册窗口 RegisterClassEx* 2:创建窗口 CreateWindowEx* 3:更新显示窗口 ShowWindow* 4:消息循环 PeekMessage/GetMessage***/class AppWin:public AppProtocol{public:AppWin(HINSTANCE hInstance):_hInstance(hInstance){WNDCLASSEX winClass;winClass.lpszClassName = _T("CELLWinApp");//窗口名字winClass.cbSize = sizeof(winClass);winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;//窗口类型winClass.lpfnWndProc = wndProc;winClass.hInstance = hInstance;winClass.hIcon = 0;//图标winClass.hIconSm = 0;winClass.hCursor = LoadCursor(hInstance, IDC_ARROW);//鼠标样式winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);winClass.lpszMenuName = NULL;//窗口菜单winClass.cbClsExtra = 0;winClass.cbWndExtra = 0;//1:注册窗口RegisterClassEx(&winClass);}virtual~AppWin(void){UnregisterClass(_T("CELLWinApp"),_hInstance);}virtual LRESULT onEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){switch (msg){ //处理得到的消息case WM_CLOSE:case WM_DESTROY://处理窗口关闭时的消息{::PostQuitMessage(0);}break;case WM_LBUTTONDOWN: //鼠标左键被按下的消息break;case WM_RBUTTONDOWN://鼠标右键被按下的消息break;case WM_MOUSEMOVE://鼠标移动break;case WM_KEYDOWN://监听键盘按键switch (wParam){case VK_LEFT:break;default:break;}break;default:return DefWindowProc( hWnd, msg, wParam, lParam ); }return S_OK;}static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){AppWin* pThis = (AppWin*)GetWindowLong(hWnd,GWL_USERDATA);if (pThis){return pThis->onEvent(hWnd,msg,wParam,lParam);}if (WM_CREATE == msg){CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;DWORD_PTR pCreateParams = (DWORD_PTR)pCreate->lpCreateParams;SetWindowLong(hWnd,GWL_USERDATA,pCreateParams);}return DefWindowProc( hWnd, msg, wParam, lParam );}int main(int wWidth,int wHegith){//2:创建窗口_hWnd = CreateWindowEx( NULL, _T("CELLWinApp"), _T("CELLWinApp"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, wWidth, wHegith, NULL, NULL, _hInstance,this );if (_hWnd == 0) {return -1;}//3:更新显示窗口ShowWindow(_hWnd,SW_SHOW);//SW_MAXIMIZE(扩大窗口),SW_SHOWif(!ini()){return 0;}//4:消息循环MSG msg = {0};while(msg.message != WM_QUIT){ //消息不等于退出if (msg.message == WM_DESTROY || msg.message == WM_CLOSE){break;}//有消息,处理消息,无消息,则进行渲染绘制if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ){ TranslateMessage( &msg );//消息处理DispatchMessage( &msg );//分发给当前程序}else{//绘制renderer();}}destroy();return 0;}private:HINSTANCE _hInstance;//实例句柄protected:HWND _hWnd; //窗口句柄public:};
-- 实现OpenGLES初始化,绘制,销毁 (AppDelegate.hpp)
#include "AppWin.hpp"#include <EGL/egl.h>#include <gles2/gl2.h>#include "freeImage/FreeImage.h"class AppDelegate :public AppWin{public:AppDelegate(HINSTANCE hInstance):AppWin(hInstance){}virtual ~AppDelegate(){}bool ini(){//1:获取 Display_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);//2:初始化 eglEGLint major;//返回主版本号EGLint minor;//返回次版本号eglInitialize(_display, &major, &minor);//3:选择 Configconst EGLint attribs[] ={//属性是一对一的初始化(类似与key->value)EGL_SURFACE_TYPE, EGL_WINDOW_BIT,//把数据画到窗口上EGL_BLUE_SIZE, 8,//R 占8个比特位EGL_GREEN_SIZE, 8,//G 占8个比特位EGL_RED_SIZE, 8, //B 占8个比特位EGL_DEPTH_SIZE,24,//深度值 占24个比特位 画图的深度值 (前后顺序的层)EGL_NONE //这个机构结束了};EGLint format(0);EGLintnumConfigs(0);//让EGL为你选择一个配置eglChooseConfig(_display, attribs, &_config, 1, &numConfigs);//可以查询某个配置的某个属性eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);//4:创建 Surface_surface = eglCreateWindowSurface(_display, _config, _hWnd, NULL);//5:创建上下文对象EGLint attr[] ={ EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };//创建上下文对象EGLContext _context = eglCreateContext(_display, _config, EGL_NO_CONTEXT, attr);//6:查看 Display Surface Context 是否创建成功if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE){return false;}//查询Suface的属性 获取Suface中的宽高eglQuerySurface(_display, _surface, EGL_WIDTH, &_width);eglQuerySurface(_display, _surface, EGL_HEIGHT, &_height);return true;}void destroy(){if (_display != EGL_NO_DISPLAY){//当前的Display 不等于null//清楚绑定的 Surface Context eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);if (_context != EGL_NO_CONTEXT){//不等于空Context//销毁上下文eglDestroyContext(_display, _context);}if (_surface != EGL_NO_SURFACE){//不等于空Surface//销毁SurfaceeglDestroySurface(_display, _surface);}//终止DispayeglTerminate(_display);}//把 Display Context Surface 设置成初始化_display = EGL_NO_DISPLAY;_context = EGL_NO_CONTEXT;_surface = EGL_NO_SURFACE;}void renderer(){//glDrawArrays(GL_TRIANGLE_STRIP,0,0);glClearColor(0,0,1,1);//刷屏//清除颜色GL_COLOR_BUFFER_BIT 和 清除深度值GL_DEPTH_BUFFER_BITglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glViewport(0,0,_width,_height);//绘制区域eglSwapBuffers(_display,_surface);}protected:int _width; // 视口 宽度int _height;// 视口 高度EGLDisplay _display;EGLContext _context;EGLSurface _surface;EGLConfig _config;};
-- 调用(main.cpp)
#include "AppDelegate.hpp"int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(hInstance);UNREFERENCED_PARAMETER(lpCmdLine);UNREFERENCED_PARAMETER(nCmdShow);AppDelegate app(hInstance);app.main(800,600);return 0;}
0 0
- OpenGLES---分离窗口和OpenGLES代码
- opengles
- opengles
- opengles
- Opengles
- OpenGLES
- OpenGLES---Windows窗口
- OpenGLES---小窗口
- opengles数据类型和基础知识
- OpenGLES着色器和编程
- OpenGLES入门一之渲染窗口
- 将OpenGL代码移植成OpenGLES
- 将OpenGL代码移植成OpenGLES
- 将OpenGL代码移植成OpenGLES
- 将OpenGL代码移植成OpenGLES
- opengles 画球
- opengles 投影
- opengles概览
- Cornerstone永久试用
- 关于android listview分割线隐藏的问题
- TCP的流量控制
- 推荐一个公司内部接口文档管理方案
- 韩顺平Spring框架学习,学习笔记(二)
- OpenGLES---分离窗口和OpenGLES代码
- java入门教程-1.5Java开发环境的搭建(JDK和Eclipse的安装)
- Ubantu下Eclipse代码提示功能怎么设置
- 视频编解码初步
- Android Studio 从1.4直接升级2.2 Preview1后的错误
- MySQL数据库表操作的经典语句
- Android install error
- NVIDIA Tesla K80:怪物般的双芯计算卡
- 给OpenCV(2.x)增加汉字输出功能