Thanks Mind and LiMuBei. Program still crashes. I'm lost.
This is the entire code:
Code: Select all
#include <windows.h>
#include <OgreRoot.h>
#include <OgreCamera.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreManualObject.h>
#include <OgrePrerequisites.h>
#include <OgreRenderQueueListener.h>
class LowLevelOgre
{
public:
LowLevelOgre(void);
LowLevelOgre(HWND h);
virtual ~LowLevelOgre(void);
bool go(void);
protected:
Ogre::Root *mRoot;
Ogre::Camera* mCamera;
Ogre::SceneManager* mSceneMgr;
Ogre::RenderWindow* mWindow;
};
#include <OgreLogManager.h>
#include <OgreViewport.h>
#include <OgreEntity.h>
#include <OgreWindowEventUtilities.h>
#include <OgrePlugin.h>
//-------------------------------------------------------------------------------------
LowLevelOgre::LowLevelOgre(HWND h) : mRoot(0), mCamera(0), mSceneMgr(0), mWindow((Ogre::RenderWindow*)h) {}
LowLevelOgre::~LowLevelOgre(void) {delete mRoot;}
//-------------------------------------------------------------------------------------
bool LowLevelOgre::go(void)
{
// construct Ogre::Root : no plugins filename, no config filename, using a custom log filename
mRoot = new Ogre::Root("", "", "LowLevelOgre.log");
// A list of required plugins
Ogre::StringVector required_plugins;
required_plugins.push_back("GL RenderSystem");
required_plugins.push_back("Octree & Terrain Scene Manager");
// List of plugins to load
Ogre::StringVector plugins_toLoad;
plugins_toLoad.push_back("RenderSystem_GL");
plugins_toLoad.push_back("Plugin_OctreeSceneManager");
// Load the OpenGL RenderSystem and the Octree SceneManager plugins
for (Ogre::StringVector::iterator j = plugins_toLoad.begin(); j != plugins_toLoad.end(); j++)
{
#ifdef _DEBUG
mRoot->loadPlugin(*j + Ogre::String("_d"));
#else
mRoot->loadPlugin(*j);
#endif;
}
// Check if the required plugins are installed and ready for use
// If not: exit the application
Ogre::Root::PluginInstanceList ip = mRoot->getInstalledPlugins();
for (Ogre::StringVector::iterator j = required_plugins.begin(); j != required_plugins.end(); j++)
{
bool found = false;
// try to find the required plugin in the current installed plugins
for (Ogre::Root::PluginInstanceList::iterator k = ip.begin(); k != ip.end(); k++)
{
if ((*k)->getName() == *j) {found = true;break;}
}//for
if (!found) {return false;} // return false because a required plugin is not available
}//for
//-------------------------------------------------------------------------------------
// setup resources. Only add the minimally required resource locations to load up the Ogre head mesh
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Ogre/ogre_src_v1-7-3/Samples/Media/materials/programs", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Ogre/ogre_src_v1-7-3/Samples/Media/materials/scripts", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Ogre/ogre_src_v1-7-3/Samples/Media/materials/textures", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Ogre/ogre_src_v1-7-3/Samples/Media/models", "FileSystem", "General");
//-------------------------------------------------------------------------------------
// configure
Ogre::RenderSystem* rs = mRoot->getRenderSystemByName("OpenGL Rendering Subsystem");
if(!(rs->getName() == "OpenGL Rendering Subsystem")) {return false;}//No RenderSystem found
// configure our RenderSystem
rs->setConfigOption("Full Screen", "No");
rs->setConfigOption("VSync", "No");
rs->setConfigOption("Video Mode", "1024 x 760 @ 32-bit");
mRoot->setRenderSystem(rs);
//mWindow = mRoot->initialise(false, "LowLevelOgre Render Window");
mRoot->initialise(false);
/*Ogre::String winHandle;
winHandle = Ogre::StringConverter::toString(mWindow);
Ogre::NameValuePairList params;
params["externalWindowHandle"] = winHandle;
mWindow = mRoot->createRenderWindow( "My window", 100, 100, false, ¶ms );*/
//-------------------------------------------------------------------------------------
// Get the SceneManager, in this case the OctreeSceneManager
mSceneMgr = mRoot->createSceneManager("OctreeSceneManager", "DefaultSceneManager");
//mSceneMgr = mRoot->createSceneManager("DefaultSceneManager");
//-------------------------------------------------------------------------------------
mCamera = mSceneMgr->createCamera("PlayerCam");// create camera
mCamera->setPosition(Ogre::Vector3(0,0,800));// Position it at 800 in Z direction
mCamera->lookAt(Ogre::Vector3(0,0,-50));// Look back along -Z
mCamera->setNearClipDistance(5);
//-------------------------------------------------------------------------------------
// Create one viewport, entire window
Ogre::Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
mCamera->setAspectRatio( Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()) );// Alter the camera aspect ratio to match the viewport
//-------------------------------------------------------------------------------------
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);// Set default mipmap level (NB some APIs ignore this)
//-------------------------------------------------------------------------------------
// Create any resource listeners (for loading screens)
//createResourceListener();
//-------------------------------------------------------------------------------------
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();// load resources
//-------------------------------------------------------------------------------------
Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");// Create the scene
Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
headNode->attachObject(ogreHead);
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));// Set ambient light
Ogre::Light* l = mSceneMgr->createLight("MainLight");// Create a light
l->setPosition(20,80,50);
//-------------------------------------------------------------------------------------
//mRoot->startRendering();
while(true)
{
Ogre::WindowEventUtilities::messagePump(); // Pump window messages for nice behaviour
mRoot->renderOneFrame();// Render a frame
if(mWindow->isClosed()) {return false;}
}
// We should never be able to reach this corner but return true to calm down our compiler
return true;
}//go
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
LowLevelOgre app(hwnd);// Create application object
try {app.go();}
catch( Ogre::Exception& e )
{
MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
}
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Running the program requires the following files to be present in your Debug directory:
OgreMain_d.dll
OIS_d.dll
Plugin_OctreeSceneManager_d.dll
RenderSystem_Direct3D9_d.dll
RenderSystem_GL_d.dll
OgreMain_d.ilk
Plugin_OctreeSceneManager_d.ilk
RenderSystem_Direct3D9_d.ilk
RenderSystem_GL_d.ilk
OgreMain.pdb
Plugin_OctreeSceneManager.pdb
RenderSystem_Direct3D9.pdb
RenderSystem_GL.pdb
C/C++ Additional include directories:
$(OGRE_HOME)\OgreMain\include;$(OGRE_HOME)\bin\include;$(OGRE_HOME)\Dependencies\include\OIS;
Linker Additional library directories:
$(OGRE_HOME)\bin\lib;$(OGRE_HOME)\bin\lib\Debug;$(OGRE_HOME)\Dependencies\lib\Debug
Linker Input Additional dependencies:
kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;OgreMain_d.lib;OIS_d.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)
Caution: In my program, I've specified some paths as absolute Eg: Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Ogre/ogre_src_v1-7-3/Samples/Media/materials/programs", "FileSystem", "General");