📄 gl_app.cpp
字号:
//find the end of the extension list
cEndExtensions= szSupExt+strlen( m_szSupportedGLExtensions );
//search through the entire list
while( szSupExt<cEndExtensions )
{
//find the next extension in the list
uiNextExtension= strcspn( szSupExt, " " );
//check the extension to the one given in the argument list
if( ( strlen( szExtensionName )==uiNextExtension ) &&
( strncmp( szExtensionName, szSupExt, uiNextExtension )==0 ) )
{
//the extension is supported
g_log.Write( LOG_RENDERER, "Your video card supports extension: %s", szExtensionName );
return true;
}
//move to the nexte extension in the list
szSupExt+= ( uiNextExtension+1 );
}
//the extension is not supported
g_log.Write( LOG_RENDERER, "Your video card does not support extension: %s", szExtensionName );
return false;
}
//--------------------------------------------------------------
// Name: CGL_APP::CreateTTFont - public
// Description: Create a truetype font (for font rendering)
// Arguments: -szFontName: the truetype font to be created
// -iSize: the size of the font
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::CreateTTFont( char* szFontName, int iSize )
{
HFONT font;
m_uiFont= glGenLists( 256 );
font= CreateFont( -iSize,
0,
0,
0,
FW_NORMAL,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE | DEFAULT_PITCH,
szFontName);
SelectObject( m_hDC, font );
wglUseFontBitmaps( m_hDC, 0, 256, m_uiFont );
}
//--------------------------------------------------------------
// Name: CGL_APP::DestroyFont - public
// Description: Destroy the font system created earlier
// Arguments: None
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::DestroyFont( void )
{ glDeleteLists( m_uiFont, 256 ); }
//--------------------------------------------------------------
// Name: CGL_APP::Print - public
// Description: Print a string onto the screen
// Arguments: -iX, iY: Position on the screen to start printing
// -vecColor: The color of the printed text
// -szString: the string to be printed
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::Print( int iX, int iY, CVECTOR vecColor, char* szString, ... )
{
va_list va;
char szParsedString[256];
//make sure that the string actually exists
if( szString==NULL )
return;
//parse the string
va_start( va, szString );
vsprintf( szParsedString, szString, va );
va_end( va );
//set the color for the text
glColor3f( vecColor[0], vecColor[1], vecColor[2] );
glPushMatrix( );
glLoadIdentity( );
//position and render the text
glRasterPos2d( iX, iY );
glListBase( m_uiFont );
glCallLists( strlen( szParsedString ), GL_UNSIGNED_BYTE, szParsedString );
glPopMatrix( );
}
//--------------------------------------------------------------
// Name: CGL_APP::BeginTextMode - public
// Description: Begin text mode (2D mode)
// Arguments: None
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::BeginTextMode( void )
{
glMatrixMode( GL_PROJECTION ); //select the projection matrix
glPushMatrix( ); //store the projection matrix
glLoadIdentity( ); //reset the projection matrix
glOrtho( 0, m_iWidth, 0, m_iHeight, -1, 1 );//set up an ortho screen
glMatrixMode( GL_MODELVIEW ); //select the modelview matrix
}
//--------------------------------------------------------------
// Name: CGL_APP::EndTextMode - public
// Description: End text mode (2D mode)
// Arguments: None
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::EndTextMode( void )
{
glMatrixMode(GL_PROJECTION); //select the projection matrix
glPopMatrix(); //restore the old projection matrix
glMatrixMode(GL_MODELVIEW); //select the modelview matrix
}
//--------------------------------------------------------------
// Name: CGL_APP::BeginRendering - public
// Description: Begin rendering, and update the timer
// Arguments: None
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::BeginRendering( void )
{
m_timer.Update( );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
//--------------------------------------------------------------
// Name: CGL_APP::EndRendering - public
// Description: Finish all rendering and swap buffers
// Arguments: None
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::EndRendering( void )
{
glFlush( );
SwapBuffers( m_hDC );
}
//--------------------------------------------------------------
// Name: CGL_APP::EndRendering - public
// Description: Finish all rendering and swap buffers
// Arguments: None
// Return Value: None
//--------------------------------------------------------------
void CGL_APP::TakeScreenshot( char* szFilename )
{
CIMAGE screenshot;
//allocate the memory
screenshot.Create( m_iWidth+10, m_iHeight, 24 );
//read the screen data
glReadPixels( 0, 0, m_iWidth+10, m_iHeight, GL_RGB, GL_UNSIGNED_BYTE, screenshot.GetData( ) );
//write the image data
screenshot.Save( szFilename );
}
//--------------------------------------------------------------
// Name: CGL_APP::HandleMessages - public
// Description: Handle the messages that the window is receiving
// Arguments: None
// Return Value: A boolean variable: -true: continue running the program
// -false: end the program
//--------------------------------------------------------------
bool CGL_APP::HandleMessages(void)
{
MSG msg;
m_menuCommand= 0;
//These are functions that are needed to process the messages
//that the window is recieving.
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
if( msg.message==WM_QUIT )
return false;
}
return true;
}
//--------------------------------------------------------------
// Name: CGL_APP::WindowProc - public
// Description: Handle all window events
// Arguments: None (that you need to worry about :))
// Return Value: A LRESULT variable
//--------------------------------------------------------------
LRESULT CALLBACK CGL_APP::WindowProc( HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam )
{
switch( uiMsg )
{
case WM_ACTIVATE:
if( !HIWORD( wParam ) )
m_bActive= APP_ACTIVE;
else
m_bActive= APP_IDLE;
return 0;
break;
case WM_SYSCOMMAND:
switch( wParam )
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
case WM_COMMAND:
m_menuCommand= LOWORD( wParam );
break;
case WM_KEYDOWN:
m_bKeys[wParam]= true;
return 0;
break;
case WM_KEYUP:
m_bKeys[wParam]= false;
return 0;
break;
case WM_MOUSEMOVE:
//get the current mouse position
m_iMouseX= ( int )LOWORD( lParam );
m_iMouseY= ( int )HIWORD( lParam );
//get the current mouse button being pressed
m_iMouseButton= ( int )wParam;
break;
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
break;
case WM_SIZE:
m_iWidth = LOWORD( lParam );
m_iHeight= HIWORD( lParam );
m_bChangeSize= true;
return 0;
break;
}
return DefWindowProc( hWnd, uiMsg, wParam, lParam );
}
//--------------------------------------------------------------
// Name: ResizeScene - global
// Description: Resize the perspective view according to the
// width and height of the window
// Arguments: None
// Return Value: None
//--------------------------------------------------------------
void ResizeScene( int iWidth, int iHeight )
{
if( iHeight==0 ) //Prevent a divide by zero (bad)
iHeight= 1; //Making height equal one
glViewport( 0, 0, iWidth, iHeight ); //Reset the current viewport
glMatrixMode( GL_PROJECTION ); //Select the projection matrix
glLoadIdentity( ); //Reset the projection matrix
//Calculate the aspect ratio of the window
gluPerspective( 45, iWidth/iHeight, 0.01f, 100.0f );
glMatrixMode( GL_MODELVIEW ); //Select the modelview matrix
glLoadIdentity( ); //Reset The modelview matrix
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -