📄 window.cpp
字号:
//////////////////////////////////////////////////////////////////////////////////////////
// WINDOW.cpp
// functions to set up an opengl capable window
// Downloaded from: www.paulsprojects.net
// Created: 21st June 2002
// Modified: 26th August 2002 - Added Input management
//
// Copyright (c) 2006, Paul Baker
// Distributed under the New BSD Licence. (See accompanying file License.txt or copy at
// http://www.paulsprojects.net/NewBSDLicense.txt)
//////////////////////////////////////////////////////////////////////////////////////////
#include "windows.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include "LOG.h"
#include "WINDOW.h"
extern LOG errorLog;
bool WINDOW::Init(char * windowTitle,
int newWidth, int newHeight,
int newColorBits, int newDepthBits, int newStencilBits,
int fullscreenflag)
//CREATE WINDOW
{
WNDCLASS wc; //windows class structure
DWORD dwExStyle; //extended style info.
DWORD dwStyle; //style info
//set class's member variables
title=windowTitle;
width=newWidth;
height=newHeight;
colorBits=newColorBits;
depthBits=newDepthBits;
stencilBits=newStencilBits;
//set class's fullscreen flag
if(fullscreenflag == FULL_SCREEN)
{
fullscreen=true;
}
if(fullscreenflag == WINDOWED_SCREEN)
{
fullscreen=false;
}
if(fullscreenflag == CHOOSE_SCREEN) //Ask user if fullscreen
{
if(MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?","Start FullScreen",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=false; //If answered no
}
else
{
fullscreen=true; //if answered yes
}
}
RECT WindowRect; //grab rect. upper left/lower right values
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)height;
hInstance= GetModuleHandle(NULL); //Grab an instance for window
wc.style= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
//window style: redraw on move, own DC
wc.lpfnWndProc= (WNDPROC) WndProc; //Wndproc handles messages
wc.cbClsExtra= 0;
wc.cbWndExtra= 0; //no extra window data
wc.hInstance= hInstance; //Set the instance
wc.hIcon= LoadIcon(NULL, IDI_WINLOGO); //load default icon
wc.hCursor= LoadCursor(NULL, IDC_ARROW); //Load arrow cursor
wc.hbrBackground=NULL; //No background rqd for GL
wc.lpszMenuName=NULL; //No menu
wc.lpszClassName="OpenGL"; //set class name
if(!RegisterClass(&wc)) //try to register class
{
errorLog.OutputError("Failed to register the window class");
return FALSE;
}
else
errorLog.OutputSuccess("Window Class Registered");
if(fullscreen) //try to set up fullscreen?
{
DEVMODE dmScreenSettings; //Device mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
//clear memory
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
//size of devmode structure
dmScreenSettings.dmPelsWidth=width; //selected width
dmScreenSettings.dmPelsHeight=height; //selected height
dmScreenSettings.dmBitsPerPel=colorBits; //selected bpp
dmScreenSettings.dmFields=DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
//try to set mode.CDS_FULLSCREEN removes start bar
{
//If mode fails, give 2 options, quit or run in window
if(MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?",title, MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; //if "yes", try windowed
}
else
{
//tell user program is closing
errorLog.OutputError("Program Closed, As Fullscreen Mode Not Supported.");
return FALSE; //exit and return FALSE
}
}
}
if (fullscreen) //still fullscreen?
{
dwExStyle=WS_EX_APPWINDOW; //window extended style
dwStyle=WS_POPUP | WS_VISIBLE; //window style (no border), visible
ShowCursor(FALSE); //hide mouse pointer
}
else
{
dwExStyle=WS_EX_CLIENTEDGE; //window extended style(3d look)
dwStyle=WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE;
//window style (close button, title bar, border, visible)
}
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
//adjust window to actual requested size, rather than including borders in size. in fullscreen, no effect
if(!(hWnd=CreateWindowEx( dwExStyle, //extended style for window
"OpenGL", //class name
title, //window title
WS_CLIPSIBLINGS | //required style
WS_CLIPCHILDREN | //required style
dwStyle, //Selected style
0, 0, //window position
WindowRect.right-WindowRect.left, //calculate adjusted width
WindowRect.bottom-WindowRect.top, //calculate adjusted height
NULL, // no parent window
NULL, //No Menu
hInstance, //Instance
NULL))) //Dont pass anything to WM_CREATE
{
Shutdown(); //if not set up, reset display
errorLog.OutputError("Window Creation Error.");
//pop up error message
return FALSE; //return false, to quit program
}
else
errorLog.OutputSuccess("Window Created.");
//set up pixel format(openGL supporting, RGBA, correct bits
GLuint pixelFormat; //holds result after searching for mode match
//calculate alpha bits
int alphaBits=0;
if(colorBits==32)
alphaBits=8;
static PIXELFORMATDESCRIPTOR pfd= //pfd tells windows how we want things to be
{
sizeof(PIXELFORMATDESCRIPTOR), //size of Pixel format descriptor
1, //Version Number
PFD_DRAW_TO_WINDOW | //must support window
PFD_SUPPORT_OPENGL | //must support opengl
PFD_DOUBLEBUFFER, //must support double buffer
PFD_TYPE_RGBA, //request RGBA format
colorBits, //select colour depth
0, 0, 0, 0, 0, 0, //colour bits ignored
alphaBits, //alpha buffer bits
0, //shift bit ignored
0, //no accumulation buffer
0, 0, 0, 0, //accumulation bits ignored
depthBits, //z buffer bits
stencilBits, //stencil buffer bits
0, //no auxiliary buffer
PFD_MAIN_PLANE, //main drawing layer
0, //reserved
0, 0, 0 //layer masks ignored
};
if(!(hDC=GetDC(hWnd))) //did we get a device context?
{ //if not
Shutdown(); //Reset display
errorLog.OutputError("Can't Create a GL Device context.");
return FALSE; //return false, to exit
}
else
errorLog.OutputSuccess("DC Created");
if(!(pixelFormat=ChoosePixelFormat(hDC,&pfd))) //found a matching pixel format?
{ //if not
Shutdown();
errorLog.OutputError("Can't Find a Suitable PixelFormat.");
return FALSE;
}
else
errorLog.OutputSuccess("Pixel Format Found.");
if(!SetPixelFormat(hDC, pixelFormat,&pfd)) //are we able to set pixel format?
{ //if not
Shutdown();
errorLog.OutputError("Can't set the pixelformat.");
return FALSE;
}
else
errorLog.OutputSuccess("Pixel Format set.");
if(!(hRC=wglCreateContext(hDC))) //are we able to get rendering context?
{ //if not
Shutdown();
errorLog.OutputError("Can't create a GL rendering context.");
return FALSE;
}
else
errorLog.OutputSuccess("GL Rendering Context Created.");
if(!wglMakeCurrent(hDC,hRC)) //are we able to activate rendering context?
{ //if not
Shutdown();
errorLog.OutputError("Can't activate the GL rendering context.");
return FALSE;
}
else
errorLog.OutputSuccess("GL Rendering Context Activated.");
//get pixel format parameters
static PIXELFORMATDESCRIPTOR finalPfd;
DescribePixelFormat(hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &finalPfd);
//output window parameters
errorLog.OutputNewline();
errorLog.OutputSuccess("Window Size: (%d, %d)", width, height);
errorLog.OutputSuccess("Color Buffer Bits (R, G, B, A): (%d, %d, %d, %d)",
finalPfd.cRedBits,
finalPfd.cGreenBits,
finalPfd.cBlueBits,
finalPfd.cAlphaBits);
errorLog.OutputSuccess("Depth Buffer Bits: %d", finalPfd.cDepthBits);
errorLog.OutputSuccess("Stencil Buffer Bits: %d", finalPfd.cStencilBits);
errorLog.OutputNewline();
ShowWindow(hWnd,SW_SHOW); //show window
SetForegroundWindow(hWnd); //slightly higher priority
SetFocus(hWnd); //Set keyboard focus to the window
errorLog.OutputSuccess("Window Created!");
errorLog.OutputNewline();
//Init the font
HFONT font; //windows font ID
//create 96 display lists
base = glGenLists(96);
font = CreateFont( -18, //font height
0, //default width
0, 0, //angles - escapement, orientation
FW_BOLD, //font weight, 0-1000, NORMAL, BOLD
false, //italic
false, //underline
false, //strikeout
ANSI_CHARSET, //character set
OUT_TT_PRECIS, //precision
CLIP_DEFAULT_PRECIS, //clip precision
ANTIALIASED_QUALITY, //output quality
FF_DONTCARE | DEFAULT_PITCH,//family and pitch
"Courier New"); //font name
//select the font
SelectObject(hDC, font);
//create 96 display lists, starting at 32
wglUseFontBitmaps(hDC, 32, 96, base);
errorLog.OutputSuccess("Font created successfully.");
return TRUE; //success!
}
void WINDOW::Shutdown(void) //PROPERLY KILL WINDOW
{
//Delete font display lists
glDeleteLists(base, 96);
if (fullscreen)
{
ChangeDisplaySettings(NULL, 0); //restore desktop mode
ShowCursor(TRUE); //show mouse cursor
}
errorLog.OutputNewline();
if(hRC) //have a rendering context?
{
if(!wglMakeCurrent(NULL, NULL)) //try to release rend cont
{
errorLog.OutputError("Release of DC and RC Failed.");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -