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

📄 wikilauncherappview.cpp

📁 这是在s60第五版上用Open GL开发的软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* ====================================================================
 * File: WikiLauncherAppView.cpp
 * Created: 11/28/08
 * Author: 
 * Copyright (c): , All rights reserved
 * ==================================================================== */

#include <coemain.h>
#include <eikenv.h>
#include <e32math.h>

#include <WikiLauncher.rsg>
#include "WikiLauncherAppView.h"
#include "SimpleCube.h"
#include "Texture.h"

const TUint KMinimumPoints = 10;
const TUint KMaxYStepDifferencePx = 10;
//#define __DRAW_DEBUG__ 1
//#define __SIMPLE_CUBE__ 1
#define __TEXTURE_CUBE__ 1

// Standard construction sequence
CWikiLauncherAppView* CWikiLauncherAppView::NewL(const TRect& aRect)
{
  CWikiLauncherAppView* self = CWikiLauncherAppView::NewLC(aRect);
  CleanupStack::Pop(self);
  return self;
}

CWikiLauncherAppView* CWikiLauncherAppView::NewLC(const TRect& aRect)
{
  CWikiLauncherAppView* self = new (ELeave) CWikiLauncherAppView;
  CleanupStack::PushL(self);
  self->ConstructL(aRect);
  return self;
}

CWikiLauncherAppView::CWikiLauncherAppView()
{
  iState = EIdle;
  iAnimation = EFalse;
}

// ---------------------------------------------
// 
// Close the array
//
// ---------------------------------------------
CWikiLauncherAppView::~CWikiLauncherAppView()
{
  iPoints.Close();
        
  #if defined( __SIMPLE_CUBE__) || defined(__TEXTURE_CUBE__)
    
    #if defined( __SIMPLE_CUBE__ )
      iSimpleCube->AppExit();
      delete iSimpleCube;
    #else
      iTextureCube->AppExit();
      delete iTextureCube;      
    #endif
    
    // Release resources associated with EGL and OpenGL ES  
    eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
    eglDestroySurface( iEglDisplay, iEglSurface ); 
    eglDestroyContext( iEglDisplay, iEglContext );
    eglTerminate( iEglDisplay );     
    
    iPeriodic->Cancel();
    delete iPeriodic;
  #endif             
}

// ---------------------------------------------
// 
// 2nd stage constructor
//
// ---------------------------------------------
void CWikiLauncherAppView::ConstructL(const TRect& aRect)
{
  // Create a window for this application view
  CreateWindowL();

  // Set the windows size
  SetRect(aRect);
  
  // Enable drag events
  EnableDragEvents();
  
  // Take the whole screen into use
  SetExtentToWholeScreen();  
  
   // Activate the window, which makes it ready to be drawn
  ActivateL();
   
  #if defined( __SIMPLE_CUBE__) || defined(__TEXTURE_CUBE__)
    InitOpenGL();
    
    #if defined (__SIMPLE_CUBE__)
      iSimpleCube = CSimpleCube::NewL( Size().iWidth, Size().iHeight ); 
      iSimpleCube->AppInit();                                             
      iSimpleCube->AppCycle( 0, ERotationUp );
    #else
      iTextureCube = CTexture::NewL( Size().iWidth, Size().iHeight );
      iTextureCube->AppInitL();                                            
    #endif
    
    eglSwapBuffers( iEglDisplay, iEglSurface );
    iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );  
    
    #if defined (__TEXTURE_CUBE__)  
      // Necessary to draw the screen after textures are loaded
      iTextureCubeState = CTexture::ELoadingTextures;
      iPeriodic->Start( 50, 50, TCallBack( CWikiLauncherAppView::DrawCallBack, this ) );   
    #endif 
  #endif          
}

// ---------------------------------------------
//
// OpenGL methods
//
// --------------------------------------------
TInt CWikiLauncherAppView::InitOpenGL()
{
  // Get to the default device screen (i.e. EGL display connection)
  iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
  if ( iEglDisplay == NULL )
  {
    _LIT(KGetDisplayFailed, "eglGetDisplay failed");
    User::Panic( KGetDisplayFailed, 0 );
  }

  // Initialize display (i.e. initialize an EGL display connection)
  if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
  {
    _LIT(KInitializeFailed, "eglInitialize failed");
    User::Panic( KInitializeFailed, 0 );
  }

  // Returns a list of all EGL frame buffer configurations 
  // that are available for the specified display
  EGLConfig *configList = NULL;            
  EGLint numOfConfigs = 0; 
  EGLint configSize   = 0;
  if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
  {
    _LIT(KGetConfigsFailed, "eglGetConfigs failed");
    User::Panic( KGetConfigsFailed, 0 );
  }

  // Allocate memory for the configList 
  configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*numOfConfigs );
  if ( configList == NULL )
  {
    _LIT(KConfigAllocFailed, "EGLConfig list alloc failed");
    User::Panic( KConfigAllocFailed, 0 );
  }

  configSize = numOfConfigs;
  
  // Define properties for the wanted EGLSurface.
  // To get the best possible performance, choose
  // an EGLConfig with a buffersize matching
  // the current window's display mode
  TDisplayMode displayMode = Window().DisplayMode();
  TInt bufferSize = 0;

  switch ( displayMode )
  {
    case (EColor4K)  : bufferSize = 12; break;
    case (EColor64K) : bufferSize = 16; break;
    case (EColor16M) : bufferSize = 24; break;
    case (EColor16MU): bufferSize = 32; break;
    default:
    {
      _LIT(KDisplayModeError, "unsupported display mode)");
      User::Panic( KDisplayModeError, 0 );
      break;
    }
  }

  // Define properties for the wanted EGLSurface 
  const EGLint attrib_list[] = { EGL_BUFFER_SIZE, bufferSize, EGL_NONE };

  // Return a list of EGL frame buffer configurations that match specified attributes
  if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
  {
    _LIT(KChooseConfigFailed, "eglChooseConfig failed");
    User::Panic( KChooseConfigFailed, 0 );
  }

  // Choose the best EGLConfig. EGLConfigs 
  // returned by eglChooseConfig are sorted so 
  // that the best matching EGLConfig is first in
  // the list.
  EGLConfig Config = configList[0];  
  
  // Free configList, not used anymore. 
  User::Free( configList ); 

  // Creates an EGL window surface - i.e. window where the graphics is blitted 
  iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL );
  if ( iEglSurface == NULL )
  {
    _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
    User::Panic( KCreateWindowSurfaceFailed, 0 );
  }

  // Creates an EGL rendering context
  iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
  if ( iEglContext == NULL )
  {
    _LIT(KCreateContextFailed, "eglCreateContext failed");
    User::Panic( KCreateContextFailed, 0 );
  }

  // Binds context to the current rendering thread and to the draw and read surface
  if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
  {
    _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
    User::Panic( KMakeCurrentFailed, 0 );
  }
  
  return KErrNone;
};
  
// ---------------------------------------------
// 
// Draw this application's view to the screen
//
// ---------------------------------------------
void CWikiLauncherAppView::Draw(const TRect& /*aRect*/) const
{
  #ifdef __DRAW_DEBUG__ 
    // Get the standard graphics context 
    CWindowGc& gc = SystemGc();

    // Clears the screen
    gc.Clear(Rect());
    
    DrawAxis( gc );
    DrawActualCoord( gc );
    DrawHandStroke( gc );
    DrawLine( gc );
    DrawDirection( gc );   
  #endif
}

// ---------------------------------------------
// 
// Handle any tap on the screen
// 
// ---------------------------------------------
void CWikiLauncherAppView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
 // LogPointerType( aPointerEvent.iType );
  switch( aPointerEvent.iType )
  {
    ////////////////////////////////////
    // Stylus tap
    case TPointerEvent::EButton1Down:
    {
      if ( !iAnimation )
      {
        iState = EIdle;
        
        // Reset the array with grabbed points
        iPoints.Reset();
        
        // Add the new point into array
        iPoints.Append(aPointerEvent.iPosition);    

        #if defined ( __DRAW_DEBUG__ )
          DrawDeferred();
        #endif
      }      
      break;
    };
    
    ////////////////////////////////////
    // Stylus removwed away
    case TPointerEvent::EButton1Up:
    {        
      if ( !iAnimation )
      {
        // Check number of points to be sure that user does not
        // tap just occasiobally
        if ( iPoints.Count() > KMinimumPoints )
        {
          if ( CheckHandStroke() )
          {
            iState = EMovement;
            CalculateLinearRegressionCoef();
         
            #if defined ( __DRAW_DEBUG__ )
              DrawDeferred();
            #else    
              
            TRotationDirection d;
            if ( Direction( d ) == KErrNone )
            { 
              if ( d == ERotationRight || d == ERotationLeft )
              { 
                iPeriodic->Start( 50, 50, TCallBack( CWikiLauncherAppView::DrawCallBack, this ) );
                iAnimation = ETrue;
              }
            }
            #endif
          }
        }
      }
                
      break;
    };
    
    ////////////////////////////////////
    // Stylus movement
    case TPointerEvent::EDrag:
    {    
      if ( !iAnimation )
      {
        iState = EDragging;
        iPoints.Append(aPointerEvent.iPosition); 
        
        #if defined ( __DRAW_DEBUG__ )
          DrawDeferred();
        #endif
      }
      break;
    };
  }
}

// -------------------------------------------------

⌨️ 快捷键说明

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