www.pudn.com > migongchengxu.rar > OpenGLCom.cpp
/********************************************************
模块名 OpenGLCom.cpp
方便OpenGL应用程序的编写:
作者:
潘李亮 XheartBlue@etang.com
XheartBlue@chinaren.com
XheartBlue.home.chinaren.com
2002-4-1;
2002-5-1日重新整理
********************************************************/
#include "windows.h"
#include "resource.h"
#include "stdio.h"
#include "OpenGLCom.h"
#include
using namespace std;
APPDATA g_AppData;
HGLRC g_hrc=0;
GLuint g_font_list;
BOOL g_bForceFPS;
int g_forceFPS;
//============================================
//============================================
//Function Name : CreateExcApp
//Description: 创建一个独占模式的应用
//============================================
BOOL CreateExcApp(int nCmdShow)
{
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = g_AppData.hApp;//hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject
(GRAY_BRUSH) ;
wndclass.lpszMenuName = g_AppData.szClassName;
wndclass.lpszClassName = g_AppData.szClassName;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
// Register the class
RegisterClassEx(&wndclass);
g_AppData.bIsExc=TRUE;
// Create a window
g_AppData.hWnd = CreateWindowEx(
WS_EX_TOPMOST, // Extended style
g_AppData.szClassName,
g_AppData.szAppTitle,
WS_POPUP, // Window style
0, // Horizontal origin
0, // Vertical origin
GetSystemMetrics(SM_CXSCREEN), // x size
GetSystemMetrics(SM_CYSCREEN), // y size
NULL, // Handle of parent
NULL,//LoadMenu(g_AppData.hApp,MAKEINTRESOURCE(IDR_MENU)), // Handle to menu
g_AppData.hApp,//hInstance, // Application instance
NULL); // Additional data
if (!g_AppData.hWnd)
return FALSE;
g_AppData.xWindow=(int)GetSystemMetrics(SM_CXSCREEN)*2/3.;
g_AppData.yWindow=(int)GetSystemMetrics(SM_CYSCREEN)*2/3.;
ShowWindow(g_AppData.hWnd, nCmdShow);
g_AppData.hdc=GetDC(g_AppData.hWnd);
return TRUE;
}
//================================================================
// Function Name: CreateNormalApp
// Description: 创建一个普通模式的应用
//================================================================
BOOL CreateNormalApp(int nCmdShow,int posx,int posy,int width,int height,ULONG uStyle)
{
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = g_AppData.hApp ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject
(GRAY_BRUSH) ;
wndclass.lpszMenuName = g_AppData.szClassName;
wndclass.lpszClassName = g_AppData.szClassName;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
// Register the class
RegisterClassEx(&wndclass);
g_AppData.bIsExc=FALSE;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//================Create Window=================
if(height==0)//Default size;
g_AppData.hWnd = CreateWindowEx(0, // Extended style
g_AppData.szClassName,
g_AppData.szAppTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, // Handle of parent
LoadMenu(g_AppData.hApp,MAKEINTRESOURCE(IDR_MENU)), // Handle to menu
g_AppData.hApp, // Application instance
NULL); // Additional data
else
g_AppData.hWnd = CreateWindowEx(0, // Extended style
g_AppData.szClassName,
g_AppData.szAppTitle,
uStyle,
posx,
posy,
width,
height,
NULL, // Handle of parent
LoadMenu(g_AppData.hApp,MAKEINTRESOURCE(IDR_MENU)), // Handle to menu
g_AppData.hApp, // Application instance
NULL); // Additional data
//=============================================================
if (!g_AppData.hWnd)
return FALSE;
RECT rct;
GetWindowRect(g_AppData.hWnd,&rct);
g_AppData.xWindow=rct.right-rct.left;
g_AppData.yWindow=rct.bottom-rct.top;
ShowWindow(g_AppData.hWnd,nCmdShow);
g_AppData.hdc=GetDC(g_AppData.hWnd);
return TRUE;
}
//================================
// Function Name: SetFullScreen
// Description :
//================================
BOOL SetFullScreen()
{
LONG style=::GetWindowLong(g_AppData.hWnd,GWL_STYLE);
style&=~WS_CAPTION;
SetWindowLong(g_AppData.hWnd,GWL_STYLE,style);
MoveWindow(g_AppData.hWnd,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),TRUE);
return TRUE;
}
//===================================================
// Function Name : Del/SetWindowStyle
// Description : Delete or Set A window Style
//===================================================
LONG DelWindowStyle(HWND hWnd,LONG newstyle)
{
LONG style=::GetWindowLong(hWnd,GWL_STYLE);
style&=!newstyle;
return SetWindowLong(hWnd,GWL_STYLE,style);
}
//--------------------------------------------------
LONG SetWindowStyle(HWND hWnd,LONG newstyle)
{
LONG style=::GetWindowLong(hWnd,GWL_STYLE);
style|=newstyle;
return SetWindowLong(hWnd,GWL_STYLE,style);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//===============================================================
// Function Name: ExitFullScreen
// Descrition:
//================================================================
BOOL ExitFullScreen()
{
LONG style=::GetWindowLong(g_AppData.hWnd,GWL_STYLE);
style|=WS_CAPTION;
SetWindowLong(g_AppData.hWnd,GWL_STYLE,style);
MoveWindow(g_AppData.hWnd,0,0,g_AppData.xWindow,g_AppData.yWindow,TRUE);
return TRUE;
}
//=====================================================
//Function Name: WinMain
//Description
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_AppData.hApp=hInstance;
g_AppData.szClassName=szClassName;
g_AppData.szAppTitle=szAppTitle;
g_AppData.bIsActive=FALSE;
g_AppData.fps=0;
return RunApp(lpCmdLine,nCmdShow);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//================================================================
// Function name: MainLoop
// Description : the Main message loop
//===============================================================
int MainLoop( PUPDATEFRAME pfUpdateFrame)
{
MSG msg;
DWORD thisTick;
DWORD lastTick;
int count=0;
lastTick=::GetTickCount();
while( 1 )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( !GetMessage( &msg, NULL, 0, 0 ) )//Remove The message From the Message Queue
{
return msg.wParam;
}
if ( !TranslateAccelerator( g_AppData.hWnd, g_AppData.hAccel, &msg ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else if ( g_AppData.bIsActive )
{
thisTick=GetTickCount();
if(g_bForceFPS)//强制帧率的帧率计算
{
while((thisTick-lastTick)1000)
{
g_AppData.fps=count*1000/(thisTick-lastTick);
count=0;
lastTick=thisTick;
}
else
count++;
}
if(pfUpdateFrame)
(*pfUpdateFrame)(g_AppData.fps);
}
else
{
WaitMessage();
}
}
return msg.wParam;
}
//=========================================
//Initialize the OpenGL and Release It
//
//=========================================
BOOL InitOpenGL()
{
PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0,0,0,0,0,0,0,0,
0,0,0,0,0,
16,
0,0,0,0,0,0,0
};
int n=ChoosePixelFormat(g_AppData.hdc,&pfd);
SetPixelFormat(g_AppData.hdc,n,&pfd);
g_hrc=wglCreateContext(g_AppData.hdc);
wglMakeCurrent(g_AppData.hdc,g_hrc);
glColor3f(1.0,1.0,1.0);
glClearColor(0.0,0.0,0.0,.50);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
CreateFont();
return 1;
}
void ReleaseGL()
{
glDeleteLists(g_font_list,196);
wglMakeCurrent(g_AppData.hdc,NULL);
wglDeleteContext(g_hrc);
ReleaseDC(g_AppData.hWnd,g_AppData.hdc);
}
//================================================================
//Function Name : LoadBmpFromFile
//Description : Load a Bitmap bits from a dot bmp file
//filename: the file contait the bitmap data
//pData : the buffer for keep the datas
//info : Infomation of the bitmap
//=================================================================
BOOL LoadBmpFromeFile(char* filename,GLubyte* &pData,BITMAPINFOHEADER& info)
{
BITMAPFILEHEADER fheader;
GLubyte* pdata;
ifstream file;
file.open(filename,ios::in|ios::binary);
file.read((char*)&fheader,sizeof(fheader));
if(fheader.bfType!=0x4d42)
return NULL;
file.read((char*)&info,sizeof(info));
int size=info.biHeight*info.biWidth*info.biBitCount/3;
file.seekg( fheader.bfOffBits, ios::beg);
pdata=new GLubyte[size];
file.read((char*)pdata,size);
pData=pdata;
return TRUE;
}
void SetForceFps(int fps)
{
if(fps==0)
{
g_bForceFPS=0;
return ;
}
g_forceFPS=1000/fps;
g_bForceFPS=1;
return;
}
GLuint CreateFont()
{
g_font_list=glGenLists(96);
BOOL hr=wglUseFontBitmaps(g_AppData.hdc,32,96,g_font_list);
return g_font_list;
}
void RenderFont(char* s,int x,int y)
{
glPushAttrib(GL_LIST_BIT);
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glRasterPos2i(x,y);
glListBase(g_font_list-32);
glCallLists(strlen(s),GL_UNSIGNED_BYTE,s);
glPopAttrib();
glFinish();
}
//++++++++++++++++++++++++++++++++++++++++++++