OpenGL入门笔记(一)

本文介绍了一个使用OpenGL在Windows环境下创建窗口程序的完整框架。该框架包括初始化OpenGL、设置视口大小、绘制场景等功能,并提供了窗口全屏/窗口模式切换的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif
None.gif#include
<windows.h>//HeaderFileForWindows
None.gif
#include<gl/gl.h>//HeaderFileForTheOpenGL32Library
None.gif
#include<gl/glu.h>//HeaderFileForTheGLu32Library
None.gif
#include<gl/glaux.h>//HeaderFileForTheGlauxLibrary
None.gif

None.gifHDChDC
=NULL;//PrivateGDIDeviceContext
None.gif
HGLRChRC=NULL;//PermanentRenderingContext
None.gif
HWNDhWnd=NULL;//HoldsOurWindowHandle
None.gif
HINSTANCEhInstance;//HoldsTheInstanceOfTheApplication
None.gif

None.gif
boolkeys[256];//ArrayUsedForTheKeyboardRoutine
None.gif
boolactive=TRUE;//WindowActiveFlagSetToTRUEByDefault
None.gif
boolfullscreen=TRUE;//FullscreenFlagSetToFullscreenModeByDefault
None.gif

None.gifLRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
//DeclarationForWndProc
None.gif

None.gifGLvoidReSizeGLScene(GLsizeiwidth,GLsizeiheight)
//ResizeAndInitializeTheGLWindow
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(height==0)//PreventADivideByZeroBy
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifheight
=1;//MakingHeightEqualOne
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gifglViewport(
0,0,width,height);//ResetTheCurrentViewport
InBlock.gif

InBlock.gifglMatrixMode(GL_PROJECTION);
//SelectTheProjectionMatrix
InBlock.gif
glLoadIdentity();//ResetTheProjectionMatrix
InBlock.gif
InBlock.gif
//CalculateTheAspectRatioOfTheWindow
InBlock.gif
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
InBlock.gif
InBlock.gifglMatrixMode(GL_MODELVIEW);
//SelectTheModelviewMatrix
InBlock.gif
glLoadIdentity();//ResetTheModelviewMatrix
ExpandedBlockEnd.gif
}

None.gif
None.gif
intInitGL(GLvoid)//AllSetupForOpenGLGoesHere
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifglShadeModel(GL_SMOOTH);
//EnableSmoothShading
InBlock.gif
glClearColor(0.0f,0.0f,0.0f,0.5f);//BlackBackground
InBlock.gif
glClearDepth(1.0f);//DepthBufferSetup
InBlock.gif
glEnable(GL_DEPTH_TEST);//EnablesDepthTesting
InBlock.gif
glDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDo
InBlock.gif
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculations
InBlock.gif
returnTRUE;//InitializationWentOK
ExpandedBlockEnd.gif
}

None.gif
None.gif
intDrawGLScene(GLvoid)//Here'sWhereWeDoAllTheDrawing
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifglClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
InBlock.gif
glLoadIdentity();//ResetTheCurrentModelviewMatrix
InBlock.gif
returnTRUE;//EverythingWentOK
ExpandedBlockEnd.gif
}

None.gif
None.gifGLvoidKillGLWindow(GLvoid)
//ProperlyKillTheWindow
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(fullscreen)//AreWeInFullscreenMode?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifChangeDisplaySettings(NULL,
0);//IfSoSwitchBackToTheDesktop
InBlock.gif
ShowCursor(TRUE);//ShowMousePointer
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(hRC)//DoWeHaveARenderingContext?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(!wglMakeCurrent(NULL,NULL))//AreWeAbleToReleaseTheDCAndRCContexts?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(NULL,
"ReleaseOfDCAndRCFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(!wglDeleteContext(hRC))//AreWeAbleToDeleteTheRC?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(NULL,
"ReleaseRenderingContextFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);
ExpandedSubBlockEnd.gif}

InBlock.gifhRC
=NULL;//SetRCToNULL
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(hDC&&!ReleaseDC(hWnd,hDC))//AreWeAbleToReleaseTheDC
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(NULL,
"ReleaseDeviceContextFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);
InBlock.gifhDC
=NULL;//SetDCToNULL
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(hWnd&&!DestroyWindow(hWnd))//AreWeAbleToDestroyTheWindow?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(NULL,
"CouldNotReleasehWnd.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);
InBlock.gifhWnd
=NULL;//SethWndToNULL
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(!UnregisterClass("OpenGL",hInstance))//AreWeAbleToUnregisterClass
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(NULL,
"CouldNotUnregisterClass.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);
InBlock.gifhInstance
=NULL;//SethInstanceToNULL
ExpandedSubBlockEnd.gif
}

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*ThisCodeCreatesOurOpenGLWindow.ParametersAre:*
InBlock.gif*title-TitleToAppearAtTheTopOfTheWindow*
InBlock.gif*width-WidthOfTheGLWindowOrFullscreenMode*
InBlock.gif*height-HeightOfTheGLWindowOrFullscreenMode*
InBlock.gif*bits-NumberOfBitsToUseForColor(8/16/24/32)*
ExpandedBlockEnd.gif*fullscreenflag-UseFullscreenMode(TRUE)OrWindowedMode(FALSE)
*/

None.gif
None.gifBOOLCreateGLWindow(
char*title,intwidth,intheight,intbits,boolfullscreenflag)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifGLuintPixelFormat;
//HoldsTheResultsAfterSearchingForAMatch
InBlock.gif
WNDCLASSwc;//WindowsClassStructure
InBlock.gif
DWORDdwExStyle;//WindowExtendedStyle
InBlock.gif
DWORDdwStyle;//WindowStyle
InBlock.gif
RECTWindowRect;//GrabsRectangleUpperLeft/LowerRightValues
InBlock.gif
WindowRect.left=(long)0;//SetLeftValueTo0
InBlock.gif
WindowRect.right=(long)width;//SetRightValueToRequestedWidth
InBlock.gif
WindowRect.top=(long)0;//SetTopValueTo0
InBlock.gif
WindowRect.bottom=(long)height;//SetBottomValueToRequestedHeight
InBlock.gif

InBlock.giffullscreen
=fullscreenflag;//SetTheGlobalFullscreenFlag
InBlock.gif

InBlock.gifhInstance
=GetModuleHandle(NULL);//GrabAnInstanceForOurWindow
InBlock.gif
wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;//RedrawOnSize,AndOwnDCForWindow.
InBlock.gif
wc.lpfnWndProc=(WNDPROC)WndProc;//WndProcHandlesMessages
InBlock.gif
wc.cbClsExtra=0;//NoExtraWindowData
InBlock.gif
wc.cbWndExtra=0;//NoExtraWindowData
InBlock.gif
wc.hInstance=hInstance;//SetTheInstance
InBlock.gif
wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);//LoadTheDefaultIcon
InBlock.gif
wc.hCursor=LoadCursor(NULL,IDC_ARROW);//LoadTheArrowPointer
InBlock.gif
wc.hbrBackground=NULL;//NoBackgroundRequiredForGL
InBlock.gif
wc.lpszMenuName=NULL;//WeDon'tWantAMenu
InBlock.gif
wc.lpszClassName="OpenGL";//SetTheClassName
InBlock.gif

InBlock.gif
if(!RegisterClass(&wc))//AttemptToRegisterTheWindowClass
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(NULL,
"FailedToRegisterTheWindowClass.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(fullscreen)//AttemptFullscreenMode?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDEVMODEdmScreenSettings;
//DeviceMode
InBlock.gif
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));//MakesSureMemory'sCleared
InBlock.gif
dmScreenSettings.dmSize=sizeof(dmScreenSettings);//SizeOfTheDevmodeStructure
InBlock.gif
dmScreenSettings.dmPelsWidth=width;//SelectedScreenWidth
InBlock.gif
dmScreenSettings.dmPelsHeight=height;//SelectedScreenHeight
InBlock.gif
dmScreenSettings.dmBitsPerPel=bits;//SelectedBitsPerPixel
InBlock.gif
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
InBlock.gif
InBlock.gif
//TryToSetSelectedModeAndGetResults.NOTE:CDS_FULLSCREENGetsRidOfStartBar.
InBlock.gif
if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//IfTheModeFails,OfferTwoOptions.QuitOrUseWindowedMode.
InBlock.gif
if(MessageBox(NULL,"TheRequestedFullscreenModeIsNotSupportedBy/nYourVideoCard.UseWindowedModeInstead?","NeHeGL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffullscreen
=FALSE;//WindowedModeSelected.Fullscreen=FALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//PopUpAMessageBoxLettingUserKnowTheProgramIsClosing.
InBlock.gif
MessageBox(NULL,"ProgramWillNowClose.","ERROR",MB_OK|MB_ICONSTOP);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(fullscreen)//AreWeStillInFullscreenMode?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdwExStyle
=WS_EX_APPWINDOW;//WindowExtendedStyle
InBlock.gif
dwStyle=WS_POPUP;//WindowsStyle
InBlock.gif
ShowCursor(FALSE);//HideMousePointer
ExpandedSubBlockEnd.gif
}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdwExStyle
=WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;//WindowExtendedStyle
InBlock.gif
dwStyle=WS_OVERLAPPEDWINDOW;//WindowsStyle
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gifAdjustWindowRectEx(
&WindowRect,dwStyle,FALSE,dwExStyle);//AdjustWindowToTrueRequestedSize
InBlock.gif
InBlock.gif
//CreateTheWindow
InBlock.gif
if(!(hWnd=CreateWindowEx(dwExStyle,//ExtendedStyleForTheWindow
InBlock.gif
"OpenGL",//ClassName
InBlock.gif
title,//WindowTitle
InBlock.gif
dwStyle|//DefinedWindowStyle
InBlock.gif
WS_CLIPSIBLINGS|//RequiredWindowStyle
InBlock.gif
WS_CLIPCHILDREN,//RequiredWindowStyle
InBlock.gif
0,0,//WindowPosition
InBlock.gif
WindowRect.right-WindowRect.left,//CalculateWindowWidth
InBlock.gif
WindowRect.bottom-WindowRect.top,//CalculateWindowHeight
InBlock.gif
NULL,//NoParentWindow
InBlock.gif
NULL,//NoMenu
InBlock.gif
hInstance,//Instance
InBlock.gif
NULL)))//DontPassAnythingToWM_CREATE
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifKillGLWindow();
//ResetTheDisplay
InBlock.gif
MessageBox(NULL,"WindowCreationError.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
staticPIXELFORMATDESCRIPTORpfd=//pfdTellsWindowsHowWeWantThingsToBe
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
sizeof(PIXELFORMATDESCRIPTOR),//SizeOfThisPixelFormatDescriptor
InBlock.gif
1,//VersionNumber
InBlock.gif
PFD_DRAW_TO_WINDOW|//FormatMustSupportWindow
InBlock.gif
PFD_SUPPORT_OPENGL|//FormatMustSupportOpenGL
InBlock.gif
PFD_DOUBLEBUFFER,//MustSupportDoubleBuffering
InBlock.gif
PFD_TYPE_RGBA,//RequestAnRGBAFormat
InBlock.gif
bits,//SelectOurColorDepth
InBlock.gif
0,0,0,0,0,0,//ColorBitsIgnored
InBlock.gif
0,//NoAlphaBuffer
InBlock.gif
0,//ShiftBitIgnored
InBlock.gif
0,//NoAccumulationBuffer
InBlock.gif
0,0,0,0,//AccumulationBitsIgnored
InBlock.gif
16,//16BitZ-Buffer(DepthBuffer)
InBlock.gif
0,//NoStencilBuffer
InBlock.gif
0,//NoAuxiliaryBuffer
InBlock.gif
PFD_MAIN_PLANE,//MainDrawingLayer
InBlock.gif
0,//Reserved
InBlock.gif
0,0,0//LayerMasksIgnored
ExpandedSubBlockEnd.gif
}
;
InBlock.gif
InBlock.gif
if(!(hDC=GetDC(hWnd)))//DidWeGetADeviceContext?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifKillGLWindow();
//ResetTheDisplay
InBlock.gif
MessageBox(NULL,"Can'tCreateAGLDeviceContext.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))//DidWindowsFindAMatchingPixelFormat?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifKillGLWindow();
//ResetTheDisplay
InBlock.gif
MessageBox(NULL,"Can'tFindASuitablePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(!SetPixelFormat(hDC,PixelFormat,&pfd))//AreWeAbleToSetThePixelFormat?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifKillGLWindow();
//ResetTheDisplay
InBlock.gif
MessageBox(NULL,"Can'tSetThePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(!(hRC=wglCreateContext(hDC)))//AreWeAbleToGetARenderingContext?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifKillGLWindow();
//ResetTheDisplay
InBlock.gif
MessageBox(NULL,"Can'tCreateAGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
if(!wglMakeCurrent(hDC,hRC))//TryToActivateTheRenderingContext
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifKillGLWindow();
//ResetTheDisplay
InBlock.gif
MessageBox(NULL,"Can'tActivateTheGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gifShowWindow(hWnd,SW_SHOW);
//ShowTheWindow
InBlock.gif
SetForegroundWindow(hWnd);//SlightlyHigherPriority
InBlock.gif
SetFocus(hWnd);//SetsKeyboardFocusToTheWindow
InBlock.gif
ReSizeGLScene(width,height);//SetUpOurPerspectiveGLScreen
InBlock.gif

InBlock.gif
if(!InitGL())//InitializeOurNewlyCreatedGLWindow
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifKillGLWindow();
//ResetTheDisplay
InBlock.gif
MessageBox(NULL,"InitializationFailed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
returnFALSE;//ReturnFALSE
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
returnTRUE;//Success
ExpandedBlockEnd.gif
}

None.gif
None.gifLRESULTCALLBACKWndProc(HWNDhWnd,
//HandleForThisWindow
None.gif
UINTuMsg,//MessageForThisWindow
None.gif
WPARAMwParam,//AdditionalMessageInformation
None.gif
LPARAMlParam)//AdditionalMessageInformation
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
switch(uMsg)//CheckForWindowsMessages
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseWM_ACTIVATE://WatchForWindowActivateMessage
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(!HIWORD(wParam))//CheckMinimizationState
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifactive
=TRUE;//ProgramIsActive
ExpandedSubBlockEnd.gif
}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifactive
=FALSE;//ProgramIsNoLongerActive
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
return0;//ReturnToTheMessageLoop
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
caseWM_SYSCOMMAND://InterceptSystemCommands
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
switch(wParam)//CheckSystemCalls
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseSC_SCREENSAVE://ScreensaverTryingToStart?
InBlock.gif
caseSC_MONITORPOWER://MonitorTryingToEnterPowersave?
InBlock.gif
return0;//PreventFromHappening
ExpandedSubBlockEnd.gif
}

InBlock.gif
break;//Exit
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
caseWM_CLOSE://DidWeReceiveACloseMessage?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifPostQuitMessage(
0);//SendAQuitMessage
InBlock.gif
return0;//JumpBack
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
caseWM_KEYDOWN://IsAKeyBeingHeldDown?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifkeys[wParam]
=TRUE;//IfSo,MarkItAsTRUE
InBlock.gif
return0;//JumpBack
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
caseWM_KEYUP://HasAKeyBeenReleased?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifkeys[wParam]
=FALSE;//IfSo,MarkItAsFALSE
InBlock.gif
return0;//JumpBack
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
caseWM_SIZE://ResizeTheOpenGLWindow
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
//LoWord=Width,HiWord=Height
InBlock.gif
return0;//JumpBack
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//PassAllUnhandledMessagesToDefWindowProc
InBlock.gif
returnDefWindowProc(hWnd,uMsg,wParam,lParam);
ExpandedBlockEnd.gif}

None.gif
None.gif
intWINAPIWinMain(HINSTANCEhInstance,//Instance
None.gif
HINSTANCEhPrevInstance,//PreviousInstance
None.gif
LPSTRlpCmdLine,//CommandLineParameters
None.gif
intnCmdShow)//WindowShowState
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifMSGmsg;
//WindowsMessageStructure
InBlock.gif
BOOLdone=FALSE;//BoolVariableToExitLoop
InBlock.gif
InBlock.gif
//AskTheUserWhichScreenModeTheyPrefer
InBlock.gif
if(MessageBox(NULL,"WouldYouLikeToRunInFullscreenMode?","StartFullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffullscreen
=FALSE;//WindowedMode
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
//CreateOurOpenGLWindow
InBlock.gif
if(!CreateGLWindow("NeHe'sOpenGLFramework",640,480,16,fullscreen))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return0;//QuitIfWindowWasNotCreated
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
while(!done)//LoopThatRunsWhiledone=FALSE
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))//IsThereAMessageWaiting?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(msg.message==WM_QUIT)//HaveWeReceivedAQuitMessage?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdone
=TRUE;//IfSodone=TRUE
ExpandedSubBlockEnd.gif
}

InBlock.gif
else//IfNot,DealWithWindowMessages
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifTranslateMessage(
&msg);//TranslateTheMessage
InBlock.gif
DispatchMessage(&msg);//DispatchTheMessage
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

InBlock.gif
else//IfThereAreNoMessages
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//DrawTheScene.WatchForESCKeyAndQuitMessagesFromDrawGLScene()
InBlock.gif
if(active)//ProgramActive?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(keys[VK_ESCAPE])//WasESCPressed?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdone
=TRUE;//ESCSignalledAQuit
ExpandedSubBlockEnd.gif
}

InBlock.gif
else//NotTimeToQuit,UpdateScreen
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDrawGLScene();
//DrawTheScene
InBlock.gif
SwapBuffers(hDC);//SwapBuffers(DoubleBuffering)
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(keys[VK_F1])//IsF1BeingPressed?
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifkeys[VK_F1]
=FALSE;//IfSoMakeKeyFALSE
InBlock.gif
KillGLWindow();//KillOurCurrentWindow
InBlock.gif
fullscreen=!fullscreen;//ToggleFullscreen/WindowedMode
InBlock.gif
//RecreateOurOpenGLWindow
InBlock.gif
if(!CreateGLWindow("NeHe'sOpenGLFramework",640,480,16,fullscreen))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return0;//QuitIfWindowWasNotCreated
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Shutdown
InBlock.gif
KillGLWindow();//KillTheWindow
InBlock.gif
return(msg.wParam);//ExitTheProgram
ExpandedBlockEnd.gif
}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值