⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogles_demo_02.cpp

📁 此程序需要Brew sdk2.1版本以上,是关于OpenGL的手机编程.
💻 CPP
字号:
/*=================================================================================
FILE:			ogles_demo_02.cpp
  
DESCRIPTION:	This file is provide as a standard sample Brew source file. 
				Please refer to this OpenGL(R)ES brew sample application as a 
				reference on how to use the standard OpenGL-ES and EGL APIs.
                          
ABSTRACT:		The ogles_demo_02 application is a spinning cube that displays 
				textures on each faces or smooth shading color.

AUTHOR:			QUALCOMM
                        
				Copyright (c) 2004 QUALCOMM Incorporated.
					   All Rights Reserved.
					QUALCOMM Proprietary/GTDR
=================================================================================*/

/*-------------------------------------------------------------------------------*
 *                      I N C L U D E   F I L E S                                *
 *-------------------------------------------------------------------------------*/

#include "ogles_demo_02.h"

/*-------------------------------------------------------------------------------*
 *       FOR OEM REFERENCE ONLY, USED ONLY FOR BUILDING STATIC APPLICATION       *
 *       Brew developers do not need this code snippet in their appliations      *
 *-------------------------------------------------------------------------------*/

#if defined(BREW_STATIC_APP)
extern "C"
{
	int   ogles_demo_02_CreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj);
	static int  ogles_demo_02_Load(IShell *ps, void * pHelpers, IModule ** pMod);

	// Constant Data...
	static const AEEAppInfo gai_ogles_demo_02 = { AEECLSID_OGLES_DEMO_02, OGLES_DEMO_02_RES_FILE, IDS_APPNAME, 0, 0, 0, 0, AFLAG_GAME };

	PFNMODENTRY ogles_demo_02_GetModInfo(IShell * ps, AEECLSID ** ppClasses, AEEAppInfo ** pApps, uint16 * pnApps, uint16 * pwMinPriv)
	{
	   *pApps = (AEEAppInfo *)&gai_ogles_demo_02;
	   *pnApps = 1;
	   return((PFNMODENTRY)ogles_demo_02_Load);
	}

	static int ogles_demo_02_Load(IShell *ps, void * pHelpers, IModule ** pMod)
	{
		return( AEEStaticMod_New((int16)(sizeof(AEEMod)),ps,pHelpers,pMod,ogles_demo_02_CreateInstance,NULL) );
	}
}
#endif  //BREW_STATIC_APP


/*-------------------------------------------------------------------------------*
 *                          B E G I N   P R O G R A M                            *
 *-------------------------------------------------------------------------------*/

/*===========================================================================
FUNCTION: CreateInstance
  
DESCRIPTION:
	This function will create an instance for the ogles_demo_02 application 
  
PROTOTYPE:
	int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
      
PARAMETERS:
	none
            
DEPENDENCIES
	none
              
RETURN VALUE
	none
                
===========================================================================*/
#if defined(BREW_STATIC_APP)
int ogles_demo_02_CreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
#else
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
#endif
{
	*ppObj = NULL;
	
DBGPRINTF("[AEEClsCreateInstance] ");
	if(ClsId == AEECLSID_OGLES_DEMO_02){
DBGPRINTF("[AEEClsCreateInstance] class id okay");
		if(  AEEApplet_New(sizeof(ogles_demo_02), ClsId, pIShell, po, (IApplet**)ppObj,
			(AEEHANDLER)ogles_demo_02::HandleEvent,(PFNFREEAPPDATA)ogles_demo_02::Destroy)== TRUE )
		{
DBGPRINTF("[AEEClsCreateInstance] AEEApplet_New success");
			
			return AEE_SUCCESS;
		}
DBGPRINTF("[AEEClsCreateInstance] AEEApplet_New fail");
	}
DBGPRINTF("[AEEClsCreateInstance] fail");
	return EFAILED;
}


/*===========================================================================
FUNCTION: ogles_demo_02::Init
  
DESCRIPTION:
	This function will initalize the ogles_demo_02 application 
  
PROTOTYPE:
	boolean ogles_demo_02::Init()
      
PARAMETERS:
	none
            
DEPENDENCIES
	none
              
RETURN VALUE
	TRUE	: if initialize was successful
	FALSE	: if error occured
                
===========================================================================*/
boolean ogles_demo_02::Init()
{

	DBGPRINTF("_X_INIT ROUTINE STARTED");
	// IGL and IEGL
	if( ISHELL_CreateInstance(m_applet.m_pIShell, AEECLSID_GL, (void **)&m_pIGL) != SUCCESS ){
		return FALSE;
	}else 
	{
#if !defined(BREW_STATIC_APP)
		IGL_Init(m_pIGL);
#endif
	}

	if( ISHELL_CreateInstance(m_applet.m_pIShell, AEECLSID_EGL, (void **)&m_pIEGL) != SUCCESS ){
		return FALSE;
	}else
	{
#if !defined(BREW_STATIC_APP)
	   IEGL_Init(m_pIEGL);
#endif
	}


	// File I/O
	if( ISHELL_CreateInstance( m_applet.m_pIShell, AEECLSID_FILEMGR, (void **)(&m_pIFileMgr) ) != SUCCESS )
	{
		CleanUp();	
		return FALSE;
	}

	// device frame buffer info
	if( IDISPLAY_GetDeviceBitmap(m_applet.m_pIDisplay, &m_pDDBitmap) != SUCCESS )
	{
		CleanUp();		
		return FALSE;
	}
	if( IBITMAP_GetInfo(m_pDDBitmap, &m_DDBitmapInfo, sizeof(AEEBitmapInfo)) != SUCCESS )
	{
		CleanUp();
		return FALSE;
	}

	m_iFontHeight = IDISPLAY_GetFontMetrics( m_applet.m_pIDisplay, AEE_FONT_NORMAL, NULL, NULL );

	// Setup GL/EGL 
	// MUST INIT EGL FIRST
	SetupEGL();
	SetupGL();

	Cube.Init(&m_applet);
	DBGPRINTF("_X_INIT init CUBE");

	Cube.LoadTextures(m_applet.m_pIShell);
	DBGPRINTF("_X_INIT loaded textures");

	DBGPRINTF("_X_INIT __COMPLETED___");

    return TRUE;

}


/*===========================================================================
FUNCTION: ogles_demo_02::Destroy
  
DESCRIPTION:
	This function provides a wrapper for the Brew exit routine. 
  
PROTOTYPE:
	void ogles_demo_02::Destroy(ogles_demo_02* p)
      
PARAMETERS:
	p: pointer to the ogles_demo_02 application instance
            
DEPENDENCIES
	static fucntion
              
RETURN VALUE
	none
                
===========================================================================*/
void ogles_demo_02::Destroy(ogles_demo_02* p)
{
	p->CleanUp();
}


/*===========================================================================
FUNCTION: ogles_demo_02::CleanUp
  
DESCRIPTION:
	This function frees all resource for the application upon exit. 
  
PROTOTYPE:
	void ogles_demo_02::CleanUp()
      
PARAMETERS:
	none
            
DEPENDENCIES
	none
              
RETURN VALUE
	none
                
===========================================================================*/
void ogles_demo_02::CleanUp()
{
	Cube.FreeData();

	if (m_pDDBitmap)  IBITMAP_Release( m_pDDBitmap );
	if (m_pIFileMgr) IFILEMGR_Release( m_pIFileMgr );

	if( eglMakeCurrent( EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT ) == EGL_FALSE || eglGetError() != EGL_SUCCESS ){}
	if( eglDestroyContext( m_eglDisplay, m_eglContext ) == EGL_FALSE || eglGetError() != EGL_SUCCESS ){}
	if( eglDestroySurface( m_eglDisplay, m_eglSurface ) == EGL_FALSE || eglGetError() != EGL_SUCCESS ){}
	if( eglTerminate( m_eglDisplay ) == EGL_FALSE || eglGetError() != EGL_SUCCESS ){}

	// MUST CALL IGL/IEGL RELEASE LAST, NO EGL OR GL calls will work after you release the interfaces.
	if (m_pIEGL)         IEGL_Release( m_pIEGL     );
	if (m_pIGL)           IGL_Release( m_pIGL      );
		
}

/*===========================================================================
FUNCTION: ogles_demo_02::HandleEvent
  
DESCRIPTION:
	All events to the ogles_demo_02 application are handled in this function. 
  
PROTOTYPE:
	boolean ogles_demo_02::HandleEvent( ogles_demo_02 *p, AEEEvent event, uint16 wParam, uint32 dwParam )
      
PARAMETERS:
	p: pointer to a ogles_demo_02 application instance
	event: the event that needs to be handled
	wParam,dwParam:   Event specific data.
            
DEPENDENCIES
	none
              
RETURN VALUE
	TRUE	: If the event has been processed 
	FALSE	: If the event was not vaild or processed
                
===========================================================================*/
boolean ogles_demo_02::HandleEvent( ogles_demo_02 *p, AEEEvent event, uint16 wParam, uint32 dwParam )
{

	DBGPRINTF("%d event posted",event);
	switch (event) 
	{
		case EVT_APP_START:
			// intialize the applet
			p->Init();
			NextFrame(p);
			return TRUE;

		case EVT_APP_STOP:
			return TRUE;

			
		case EVT_APP_RESUME:
			DBGPRINTF("resume called");
			p->Init();
			NextFrame(p);

			return TRUE;
			
		case EVT_APP_SUSPEND:
			DBGPRINTF("suspend called");
			ISHELL_CancelTimer( p->m_applet.m_pIShell, NULL, (void *)p );
			p->CleanUp();
			return TRUE;
			
		case EVT_APP_NO_SLEEP:
//		case EVT_APP_NO_CLOSE:
			return TRUE;
			
		case EVT_COMMAND:
			break;
			
		case EVT_KEY_PRESS:
			p->m_key_state = 1;
			p->m_key_wParam = wParam;
			
			if (p->KeyEvent(wParam))
				return HandleEvent(p, EVT_KEY, wParam, dwParam);
			else
				return TRUE;

			break;

		case EVT_KEY_RELEASE:	
			p->m_key_state =0;
			p->m_key_wParam = wParam;
			return TRUE;
			
			break;
			
		case EVT_KEY:
			return (p->KeyEvent(wParam));
			break;
			
		default:
			break;
		
	}
	
	return  FALSE; 
	
}

/*===========================================================================
FUNCTION: ogles_demo_02::KeyEvent
  
DESCRIPTION:
	All key events to the ogles_demo_02 application are handled in this function. 
  
PROTOTYPE:
	boolean ogles_demo_02::KeyEvent( uint16 wParam)
      
PARAMETERS:
	wParam:   Event specific data.
            
DEPENDENCIES
	none
              
RETURN VALUE
	TRUE	: If the event has been processed 
	FALSE	: If the event was not vaild or processed
                
===========================================================================*/
boolean ogles_demo_02::KeyEvent( uint16 wParam)
{
	if (wParam == AVK_CLR)
	{
		ISHELL_CancelTimer( m_applet.m_pIShell, NULL, (void *)this ) ;
	}

	return Cube.KeyEvent(wParam);

}

/*===========================================================================
FUNCTION: ogles_demo_02::SetupEGL
  
DESCRIPTION:
	This routine is used for EGL setup.  
   
PROTOTYPE:
	boolean ogles_demo_02::SetupEGL()
      
PARAMETERS:
	none
            
DEPENDENCIES
	none
              
RETURN VALUE
	TRUE	: if no errors
	FALSE	: if an error occured
                
===========================================================================*/
boolean ogles_demo_02::SetupEGL()
{

	EGLConfig myConfig;
	EGLint ncfg = 1;
	EGLint params[5] = {EGL_NONE,EGL_NONE,EGL_NONE,EGL_NONE,EGL_NONE};
	
	// Init State Data
	m_eglDisplay		= EGL_NO_DISPLAY;
	m_eglSurface		= EGL_NO_SURFACE;
	m_eglContext		= EGL_NO_CONTEXT;
	
	// Main Display
	m_eglDisplay = eglGetDisplay( m_applet.m_pIDisplay );
	if( m_eglDisplay == EGL_NO_DISPLAY || eglGetError() != EGL_SUCCESS ) 
		return FALSE;
	
	if( eglInitialize( m_eglDisplay, NULL, NULL ) == EGL_FALSE || eglGetError() != EGL_SUCCESS )
		return FALSE;
	
	// Get Configuration	
	eglGetConfigs( m_eglDisplay, &myConfig, 1, &ncfg );
		
	// Window Surface
	{
		IDIB *pDIB;
		
		if( IBITMAP_QueryInterface(m_pDDBitmap, AEECLSID_DIB, (void**)&pDIB) != SUCCESS )
			return EFAILED;
		
		m_eglSurface = eglCreateWindowSurface( m_eglDisplay, myConfig, pDIB, params );
		
		IDIB_Release( pDIB );
		
		if( m_eglSurface == EGL_NO_SURFACE || eglGetError() != EGL_SUCCESS )
			return FALSE;
	}
	
	// Context
	m_eglContext = eglCreateContext( m_eglDisplay, myConfig, 0, 0 );
	if( m_eglContext == EGL_NO_CONTEXT || eglGetError() != EGL_SUCCESS )
		return FALSE;
	
	if( eglMakeCurrent( m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext ) == EGL_FALSE || eglGetError() != EGL_SUCCESS )
		return FALSE;
	
	return TRUE;

}

/*===========================================================================
FUNCTION: ogles_demo_02::SetupGL
  
DESCRIPTION:
	This routine is used for GL setup.  
   
PROTOTYPE:
	boolean ogles_demo_02::SetupGL()
      
PARAMETERS:
	none
            
DEPENDENCIES
	none
              
RETURN VALUE
	TRUE	: if no errors
	FALSE	: if an error occured
                
===========================================================================*/
boolean ogles_demo_02::SetupGL()
{
	
	// Smooth shading
	glShadeModel( GL_SMOOTH );
	
	// Depth Test
	glEnable( GL_DEPTH_TEST );
	
	// Perspective Correction
	glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
	
	// GL Initialization
	glViewport( 0, 0, GetScreenWidth(), GetScreenHeight() );
	
	// Init Projection Matrix
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);
		
	// GL Initialization
	glViewport( 0, 0, GetScreenWidth(), GetScreenHeight() );
	
	// Init Projection Matrix
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	
	glFrustumx(		ITOX(-5),	ITOX(5),
					ITOX(-5),	ITOX(5),
					ITOX(10),	ITOX(100)	);
	
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	
	if (glGetError() != GL_NO_ERROR) return FALSE;
	
	return TRUE;
	
}

/*===========================================================================
FUNCTION: ogles_demo_02::NextFrame
  
DESCRIPTION:
	This routine sets up the frame timer, and then renders the next frame  
   
PROTOTYPE:
	void ogles_demo_02::NextFrame(ogles_demo_02* p)
      
PARAMETERS:
	p: pointer to the ogles_demo_02 application instance
            
DEPENDENCIES
	static fucntion
              
RETURN VALUE
	none
                
===========================================================================*/
void ogles_demo_02::NextFrame(ogles_demo_02* p)
{	
	p->RenderFrame();

	if (p->m_key_state ==1) 
		p->KeyEvent(p->m_key_wParam);

	ISHELL_SetTimer( p->m_applet.m_pIShell, 10,(PFNNOTIFY) NextFrame,(void*)p);	

}


/*===========================================================================
FUNCTION: ogles_demo_02::RenderFrame
  
DESCRIPTION:
	The main rendering routine for the ogles_demo_02 application  
   
PROTOTYPE:
	void ogles_demo_02::RenderFrame()
      
PARAMETERS:
	none
            
DEPENDENCIES
	none
              
RETURN VALUE
	none
                
===========================================================================*/
void ogles_demo_02::RenderFrame()
{
	Cube.Render();	
	
	// Swap the Buffers
	eglSwapBuffers( m_eglDisplay, m_eglSurface );	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -