#include "OrgeWin32.h"
//将orge插入到WIN32API程序中
//LIB:OgreMain_d.lib OIS_d.lib
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;//将窗体背景色设为黑色
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
if (!hwnd)
{
return FALSE;
}
if (!setup(hwnd))
return FALSE;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
//if (mRoot)
//{
// mRoot->renderOneFrame();
//}
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
static bool isClose = false;
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_SIZE:
OutputDebugString(_T("wm_size"));
windowResized(hwnd);
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
return 0;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
windowClosed();
isClose = true;
PostQuitMessage (0) ;
return 0;
default:
//if (mRoot != NULL && isClose == false)
if (mRoot != NULL)
{
mRoot->renderOneFrame();
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
return 0 ;
}
//Ogre装配线
bool setup(HWND& hwnd)
{
if (!createRoot()) //1
return false;
setupResources(); //2
if (!configure()) //3
return false;
if (!createRenderWindow(hwnd)) //4
return false;
chooseSceneManager(); //5
createCamera(); //6
createViewports(); //7
TextureManager::getSingleton().setDefaultNumMipmaps(5);
createResourceListener(); //8
loadResources(); //9
createScene(); //10
return true;
}
//1//创建Root
bool createRoot()
{
String pluginsPath;
// only use plugins.cfg if not static
#ifndef OGRE_STATIC_LIB
pluginsPath = mResourcePath + "plugins.cfg";
#endif
mRoot = new Root(pluginsPath, mResourcePath + "ogre.cfg",
mResourcePath + "Ogre.log");
if (mRoot)
return true;
else
return false;
}
//2//setup resourcess
void setupResources(void)
{
ConfigFile cf;
cf.load(mResourcePath + "resources.cfg");
// Go through all sections & settings in the file
ConfigFile::SectionIterator seci = cf.getSectionIterator();
String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
ConfigFile::SettingsMultiMap *settings = seci.getNext();
ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
}
//3//configure
bool configure(void)
{
if (!mRoot)
return false;
if(mRoot->restoreConfig())//???
{
mRoot->initialise(false);//参数如果为true,将会自动创建一个窗口
return true;
}
else
{
if (mRoot->showConfigDialog() )//弹出配制对话框
{
mRoot->initialise(false);
return true;
}
}
return false;
}
//4//创建渲染窗口
bool createRenderWindow(HWND& hwnd)
{
NameValuePairList misc;
misc["externalWindowHandle"] = StringConverter::toString( (size_t)hwnd);
mRenderWindow = mRoot->createRenderWindow( "My sub render window", 800, 600, false, &misc );
if (mRenderWindow)
return true;
else
return false;
}
//5//创建场景管理器
void chooseSceneManager(void)
{
mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "ExampleSMInstance");
}
//6//创建摄像机
void createCamera(void)
{
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it at 500 in Z direction
mCamera->setPosition(Vector3(0,0,500));
// Look back along -Z
mCamera->lookAt(Vector3(0,0,-300));
mCamera->setNearClipDistance(5);
}
//7//创建视图
void createViewports(void)
{
// Create one viewport, entire window
Viewport* vp = mRenderWindow->addViewport(mCamera);
vp->setBackgroundColour(ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio( Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}
//8//创建资源监听器
void createResourceListener(void)
{
}
//9//加载资源
void loadResources(void)
{
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
//10//创建场景
void createScene(void)
{
Entity *ent1 = mSceneMgr->createEntity( "ogre", "ogrehead.mesh" );
SceneNode *node1 = mSceneMgr->getRootSceneNode()->createChildSceneNode( "RobotNode" );
node1->attachObject( ent1 );
}
// windowClosed
void windowClosed()
{
if (mRoot != NULL)
{
//mRoot->detachRenderTarget(mRenderWindow);
Safe_Delete(&mRoot);
}
//PostQuitMessage(0);
}
// windowResized
void windowResized(HWND& hwnd)
{
if (mCamera)
{
RECT rect;
GetClientRect(hwnd,&rect);
// notify "render window" instance
if (mRenderWindow)
{
mRenderWindow->windowMovedOrResized();
}
// Adjust camera's aspect ratio, too
if ((rect.bottom - rect.top) != 0 && mCamera != 0)
{
mCamera->setAspectRatio((Ogre::Real)mRenderWindow->getWidth() /
(Ogre::Real)mRenderWindow->getHeight());
}
mCamera->yaw(Radian(0));
//Render one frame.
//Updates all the render targets automatically and then returns, raising frame events before and after
Root::getSingletonPtr()->renderOneFrame();
}
}
// destroyScene
void destroyScene(void)
{
}