📄 boidswin.cpp
字号:
}
// If the camera is pointing to the boid, update it's position.
// This is to prevent an internal view of the boid.
if ( cameraView == CAM_BOID )
{
positionCamera( );
}
}
// Process the first slider for the position of the camera.
if ( static_cast< void * >( SB ) == &sliderCam )
{
cameraPosition = sliderCam.GetPos( );
positionCamera( );
}
// Process the second slider for the position of the camera.
if ( static_cast< void * >( SB ) == &sliderCam2 )
{
cameraPosition2 = sliderCam2.GetPos( );
positionCamera( );
}
SetFocus( ); // Set the focus back to the main window.
}
// Member function to start the simulation.
void BoidsWin::start( )
{
if ( stopped )
{
OnStartStop( ); // Call the toggle function to start.
}
}
// Member function to temporarily stop the simulation.
void BoidsWin::stop( )
{
if ( !stopped )
{
OnStartStop( ); // Call the toggle function to stop.
}
}
// Perform some maintenance tasks every second.
void BoidsWin::performEverySecond( )
{
if ( fullScreenMode == false )
{
// Update the frame rate display every second.
// Set the text of the static control to show the current frame rate.
CString framesText( "Frames per Second: " );
char buffer[ 50 ]; // Temporary buffer for string conversion.
framesText += _itoa( frameCount, buffer, 10 );
staticFrames.SetWindowText( framesText );
// Force the refresh of the whole viewport,
// to erase any unwanted junk.
viewPort -> ForceUpdate( 0, 0, device -> GetWidth( ) - 1,
device -> GetHeight( ) - 1 );
// Force the update the control panel's controls.
sliderNum.Invalidate( true );
sliderSize.Invalidate( true );
sliderCam.Invalidate( true );
sliderCam2.Invalidate( true );
topBorder.Invalidate( true );
border1.Invalidate( true );
border2.Invalidate( true );
border3.Invalidate( true );
border4.Invalidate( true );
border5.Invalidate( true );
staticNum.Invalidate( true );
staticSize.Invalidate( true );
staticCam.Invalidate( true );
staticCam2.Invalidate( true );
staticFrames.Invalidate( true );
staticTime.Invalidate( true );
btnCentre.Invalidate( true );
btnStop.Invalidate( true );
}
// Determine if the simulation is overloaded.
if ( frameCount < MINIMUM_FRAME_RATE )
{
overloaded = true; // Set the flag if true.
}
else
{
overloaded = false; // Unset the flag if false.
}
// Reset the counter for the next second.
frameCount = 0;
// Reset the start time for the next second period.
frameStartTime = clock( );
}
// Start/continue the shutdown of the simulation.
afx_msg void BoidsWin::OnClose( )
{
threadEnabled = false; // Unset the flag, to stop the thread rendering.
needShutdown = true; // Set the flag to shutdown the application.
// If the rendering thread has not finished then prospone the shutdown.
if ( threadFinished == false )
{
return;
}
// Shutdown the application by calling the standard window's function.
CFrameWnd::OnClose( );
}
// Shutdown the application gracefully.
afx_msg void BoidsWin::OnDestroy( )
{
// Release all of the window's COM interfaces.
// The order of the release of the interfaces is important.
if ( scene )
{
scene -> Release( );
scene = NULL;
}
if ( viewPort )
{
viewPort -> Release( );
viewPort = NULL;
}
if ( device )
{
device -> Release( );
device = NULL;
}
if ( directDraw )
{
directDraw -> FlipToGDISurface( );
directDraw -> RestoreDisplayMode( );
directDraw -> Release( );
directDraw = NULL;
frontSurface = NULL;
backSurface = NULL;
zBuffer = NULL;
}
if ( d3drm )
{
d3drm -> Release( );
d3drm = NULL;
}
if ( clipper )
{
clipper -> Release( );
clipper = NULL;
}
if ( camera )
{
camera -> Release( );
camera = NULL;
}
}
// Change the display mode to full screen FROM windowed mode.
afx_msg void BoidsWin::OnFullScreen( )
{
// Abort if the rendering thread is still active.
if ( threadFinished == false )
{
// Set the flag to retry the operation.
needFullScreen = true;
return;
}
// First stop the simulation.
bool alreadyStopped = stopped; // Make note of the start/stop state.
stop( );
// Make a record of the size and position of the window.
GetWindowPlacement( &windowedModePos );
// Release the relevant COM interfaces.
if ( viewPort )
{
viewPort -> Release( );
viewPort = NULL;
}
if ( device )
{
device -> Release( );
device = NULL;
}
if ( clipper )
{
clipper -> Release( );
clipper = NULL;
}
// Switch to FS mode.
// Disable the menu and the window's normal features.
CMenu *mnu;
mnu = GetMenu( );
mnu -> DestroyMenu( );
SetMenu( mnu );
mnu -> Detach( );
ModifyStyle( WS_OVERLAPPEDWINDOW, WS_POPUP, 0 );
ShowCursor( false ); // Hide the mouse pointer.
// Create the direct draw interface.
DirectDrawCreate( 0, &directDraw, 0 );
directDraw -> SetCooperativeLevel( GetSafeHwnd( ),
DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT );
directDraw -> SetDisplayMode( resolutionFS.widthPixels,
resolutionFS.heightPixels, resolutionFS.colourDepthBits );
fullScreenMode = true;
createFSDevice( );
// Restore the start/stop state of the simulation.
if ( !alreadyStopped )
{
start( );
}
// Set the flag to state that the operation has completed.
needFullScreen = false;
}
// Change the display mode back to windowed mode from full screen mode.
void BoidsWin::windowedMode( )
{
// Stop the simulation.
bool alreadyStopped = stopped; // Make note of the start/stop state.
stop( );
// Release the relevant COM interfaces.
if ( directDraw )
{
directDraw -> SetCooperativeLevel( GetSafeHwnd( ), DDSCL_NORMAL );
directDraw -> RestoreDisplayMode( );
directDraw -> Release( );
directDraw = NULL;
frontSurface = NULL;
backSurface = NULL;
zBuffer = NULL;
}
if ( viewPort )
{
viewPort -> Release( );
viewPort = NULL;
}
if ( device )
{
device -> Release( );
device = NULL;
}
if ( clipper )
{
clipper -> Release( );
clipper = NULL;
}
// Reposition the window.
// Check if the structure has been properly filled.
if ( windowedModePos.length != 0 )
{
// If valid use the structure.
SetWindowPlacement( &windowedModePos );
}
// If not a valid structure, reposition the window using default values.
else
{
MoveWindow( CRect( WINDOW_LEFT_POSITION, WINDOW_TOP_POSITION,
WINDOW_RIGHT_POSITION, WINDOW_BOTTOM_POSITION ), true );
}
// Switch to windowed mode.
// Enable the menu and the window's normal features.
CMenu mnu;
(&mnu) -> LoadMenu( "MFCMENU" );
SetMenu( &mnu );
mnu . Detach( );
ModifyStyle( WS_POPUP, WS_OVERLAPPEDWINDOW, SWP_FRAMECHANGED );
ShowCursor( true ); // Show the mouse pointer.
createWinDevice( );
fullScreenMode = false;
// Show the control panel if it should be showing.
if ( showControlPanel )
{
showControls( true );
}
// Repaint the sliders after Direct Draw FS by just giving them the focus.
// This is so because a repaint just won't do the update sufficientley.
sliderNum.SetFocus( );
sliderSize.SetFocus( );
sliderCam.SetFocus( );
sliderCam2.SetFocus( );
// Restore the start/stop state of the simulation.
if ( !alreadyStopped )
{
start( );
}
}
// Show or hide the control panel.
void BoidsWin::showControls( bool show )
{
int nCmdShow; // Used to hold the Window's command flags.
if ( show )
{
// Show the control panel.
nCmdShow = SW_SHOW; // Command to show the component.
reSizeD3D( ); // Resize D3D *BEFORE* drawing the controls.
}
else
{
// Hide the control panel.
nCmdShow = SW_HIDE; // Command to hide the component.
}
// Update the control panel's controls.
sliderNum.ShowWindow( nCmdShow );
sliderSize.ShowWindow( nCmdShow );
sliderCam.ShowWindow( nCmdShow );
sliderCam2.ShowWindow( nCmdShow );
topBorder.ShowWindow( nCmdShow );
border1.ShowWindow( nCmdShow );
border2.ShowWindow( nCmdShow );
border3.ShowWindow( nCmdShow );
border4.ShowWindow( nCmdShow );
border5.ShowWindow( nCmdShow );
staticNum.ShowWindow( nCmdShow );
staticSize.ShowWindow( nCmdShow );
staticCam.ShowWindow( nCmdShow );
staticCam2.ShowWindow( nCmdShow );
staticFrames.ShowWindow( nCmdShow );
staticTime.ShowWindow( nCmdShow );
btnCentre.ShowWindow( nCmdShow );
btnStop.ShowWindow( nCmdShow );
if ( !show )
{
reSizeD3D( ); // Resize D3D *AFTER* hiding the controls.
}
}
// Restore the window if maximised.
void BoidsWin::restore( )
{
if ( IsZoomed( ) )
{
WINDOWPLACEMENT placement;
GetWindowPlacement( &placement );
placement.showCmd = SW_RESTORE;
SetWindowPlacement( &placement );
}
}
// Message handler to respond to the user pressing the <ESC> key.
afx_msg void BoidsWin::OnEscKey( )
{
// If in full screen mode, return to windowed mode.
if ( fullScreenMode )
{
windowedMode( );
}
else // If in windowed mode, shutdown the application.
{
PostMessage( WM_CLOSE );
}
}
// Member function to update the static timer comtrol.
void BoidsWin::updateTimer( )
{
// Set the text of the static control to show the current time.
// Update the runTime.
clock_t time; // Local variable used to hold the time.
if ( stopped == false )
{
time = clock( ) - stopTime;
runTime = time;
}
else // The simulation is stopped so just display the current run time.
{
time = runTime;
}
CString timeText( "Time: " );
char buffer[ 50 ]; // Temporary buffer for string conversion.
long minutes = ( time / CLOCKS_PER_SEC ) / 60;
long seconds = ( time / CLOCKS_PER_SEC ) % 60;
long hundreds = ( time / ( CLOCKS_PER_SEC / 100 ) ) % 100;
timeText += _ltoa( minutes, buffer, 10 );
if ( seconds < 10 )
{
timeText += ".0";
}
else
{
timeText += ".";
}
timeText += _ltoa( seconds, buffer, 10 );
if ( hundreds < 10 )
{
timeText += ".0";
}
else
{
timeText += ".";
}
timeText += _ltoa( hundreds, buffer, 10 );
staticTime.SetWindowText( timeText );
}
// Overided windows function to execute when the left mouse -
// - button is pressed in the non client area.
afx_msg void BoidsWin::OnNcLButtonDown( UINT nHitTest, CPoint point )
{
// Set the multitasking flag.
appIdle = false;
// Restart the rendering thread if stopped.
threadEnabled = true;
if ( threadFinished == true )
{
threadFinished = false;
ptrD3DThread = AfxBeginThread( d3dThread, this,
THREAD_PRIORITY_NORMAL );
}
// Call the default windows function.
CWnd::OnLButtonDown( nHitTest, point );
}
// Overided windows function to execute when the right mouse -
// - button is pressed in the non client area.
afx_msg void BoidsWin::OnNcRButtonDown( UINT nHitTest, CPoint point )
{
// Set the multitasking flag.
appIdle = false;
// Restart the rendering thread if stopped.
threadEnabled = true;
if ( threadFinished == true )
{
threadFinished = false;
ptrD3DThread = AfxBeginThread( d3dThread, this,
THREAD_PRIORITY_NORMAL );
}
// Call the default windows function.
CWnd::OnRButtonDown( nHitTest, point );
}
// Function to stop the rendering thread if the window has been maximized.
afx_msg void BoidsWin::OnSize( UINT nType, int cx, int cy )
{
// Stop the rendering thread if the window has been maximized.
// This is needed to fix Window2000's high thread priorities.
if ( nType == SIZE_MAXIMIZED )
{
threadEnabled = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -