OpenGLES---分离窗口和OpenGLES代码

AppProtocol  协议(初始化,绘制,销毁)

AppWin 窗口实现

AppDelegate 实现OpenGLES初始化,绘制,销毁

注释:cocos2dx中抄的(去掉父类中的单例,就这点不太一样,都是一个对象)

--  AppProtocol AppWin 窗口实现 (AppWin.hpp)

#pragma once

#include 
#include 

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_SHOW

		if(!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 
#include 
#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:初始化 egl
		EGLint  major;//返回主版本号
		EGLint  minor;//返回次版本号
		eglInitialize(_display, &major, &minor);
		
		//3:选择 Config
		const 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);
		EGLint	numConfigs(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
				//销毁Surface
				eglDestroySurface(_display, _surface);
			}
			//终止Dispay
			eglTerminate(_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_BIT
		glClear(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;
}





你可能感兴趣的:(OpenGLES)