I use Ogre and SDL too, that's my old code (only code into the main, but it's work), I hope that can help you :
Code: Select all
#include "Ogre.h"
#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>
int main(int argc, char *argv[])
{
//=================================================
//-------------------------------------------------
//=================================================
//setting is an object who read a xml file, just to setting the window
Setting config;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_WM_SetCaption(config.getWinName().c_str(), NULL); //name from the config object
putenv("SDL_VIDEO_WINDOW_POS=center"); //pour centrer la fenêtre/to center the window
int winSetting; //get fullscreen setting from config object
if(config.getWinFullscreen())
winSetting = SDL_FULLSCREEN | SDL_OPENGL;
else
winSetting = SDL_OPENGL;
SDL_SetVideoMode(config.getWinSizeWidth(), config.getWinSizeHeight(), config.getWinColors(), winSetting);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
// Create Ogre Root and initialise it (but don't try create a window)
LogManager* logMgr = new LogManager;
Log *log = LogManager::getSingleton().createLog("dooce.log", true, true, false);
Root *root = new Root("plugins.cfg");
root->restoreConfig();
RenderWindow *renderWindow = root->initialise(false);
//=================================================
//-------------------------------------------------
//=================================================
// Set parameters to pass to createRenderWindow()
NameValuePairList misc;
misc["vsync"] = String("No"); // TODO: Need to figure out why I need this under Linux...
#ifdef _WINDOWS
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWMInfo(&wmInfo);
unsigned long winHandle = reinterpret_cast<size_t>(wmInfo.window);
unsigned long winGlContext = reinterpret_cast<size_t>(wmInfo.hglrc);
misc["externalWindowHandle"] = StringConverter::toString(winHandle);
misc["externalGLContext"] = StringConverter::toString(winGlContext);
misc["externalGLControl"] = String("True");
#else
misc["currentGLContext"] = String("True");
#endif
renderWindow = root->createRenderWindow(config.getWinName(), config.getWinSizeWidth(), config.getWinSizeHeight(), config.getWinFullscreen(), &misc);
//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
//OLD Version
// Setup resources... manual, I prefer
TextureManager::getSingleton().setDefaultNumMipmaps(5);
ResourceGroupManager::getSingleton().addResourceLocation("media", "FileSystem");
ResourceGroupManager::getSingleton().addResourceLocation("media/materials/scripts", "FileSystem");
ResourceGroupManager::getSingleton().addResourceLocation("media/materials/textures", "FileSystem");
ResourceGroupManager::getSingleton().addResourceLocation("media/materials/programs", "FileSystem");
/*
//New version, but make crash for me
Ogre::ConfigFile cf;
cf.load("resources.cfg");
// Go through all sections & settings in the file
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}*/
//Initialize :
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
// Create scene manager for displaying an object
Level level;
Ogre::SceneType scene_type;
if(level.getType() == "interior")
scene_type = ST_INTERIOR;
else if(level.getType() == "landscape")
scene_type = ST_EXTERIOR_REAL_FAR;
else
scene_type = ST_GENERIC;
cout << "1" << endl;
ColourValue fadeColour( 0,0,0 );
//cout << "colour = " << renderWindow->getViewport(0)->getBackgroundColour() << endl;
//renderWindow->getViewport(0)->setBackgroundColour( fadeColour );
SceneManager *sceneManager = root->createSceneManager(ST_EXTERIOR_REAL_FAR/*scene_type*/, "Default SceneManager");
sceneManager->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);
sceneManager->setShadowTextureSize(128);
sceneManager->setShadowColour(ColourValue(0.6, 0.6, 0.6));
sceneManager->setFog( FOG_LINEAR, fadeColour, 0.0, 50, 500 );
Plane plane2;
plane2.d = 100;
plane2.normal = Vector3::NEGATIVE_UNIT_Y;
//sceneManager->setWorldGeometry( "media/terrain.cfg" );
sceneManager->setSkyPlane( true, plane2, "Examples/CloudySky", 500, 20, true, 0.5, 150, 150 );
Plane plane;
plane.normal = Vector3::UNIT_Y;
plane.d = 100;
MeshManager::getSingleton().createPlane("Myplane",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
1500,1500,20,20,true,1,60,60,Vector3::UNIT_Z);
Entity* pPlaneEnt = sceneManager->createEntity( "plane", "Myplane" );
pPlaneEnt->setMaterialName("Examples/Rockwall");
pPlaneEnt->setCastShadows(false);
sceneManager->getRootSceneNode()->createChildSceneNode(Vector3(0,80,0/*-50,99,0*/))->attachObject(pPlaneEnt);
// Create camera and position it
Camera *camera = sceneManager->createCamera("Camera_tps");
camera->setPosition(250,5,250);
camera->lookAt(0,5,0);
camera->setNearClipDistance(1.0);
camera->setFarClipDistance(1000.0f);
// Create viewport and attach the camera to it
Viewport *viewport = renderWindow->addViewport(camera);
const Real aspectRatio = Real(viewport->getActualWidth()) / Real(viewport->getActualHeight());
camera->setAspectRatio(aspectRatio);
// Light everything up so it is visible
sceneManager->setAmbientLight(ColourValue(0.0, 0.0, 0.0));
SceneNode *node = sceneManager->getRootSceneNode()->createChildSceneNode("childNode");
Light* lightSun = sceneManager->createLight("sun");
lightSun->setPosition(0,25,250);
node->attachObject(lightSun);
Entity* head = sceneManager->createEntity("Head", "models/ogrehead.mesh");
head->setMaterialName("Examples/EnvMappedRustySteel");
head->setCastShadows( true );
node->attachObject(head);
//head2
SceneNode *node2 = sceneManager->getRootSceneNode()->createChildSceneNode("childNode2");
Entity* head2 = sceneManager->createEntity("Head2", "models/ogrehead.mesh");
head2->setMaterialName("Examples/EnvMappedRustySteel");
head2->setCastShadows( true );
node2->attachObject(head2);
node2->setPosition(-50,0,0);
//head3
SceneNode *node3 = sceneManager->getRootSceneNode()->createChildSceneNode("childNode3");
Entity* head3 = sceneManager->createEntity("Head3", "models/ogrehead.mesh");
head3->setMaterialName("Examples/EnvMappedRustySteel");
head3->setCastShadows( true );
node3->attachObject(head3);
node3->setPosition(100,0,75);
//head4
SceneNode *node4 = sceneManager->getRootSceneNode()->createChildSceneNode("childNode4");
Entity* head4 = sceneManager->createEntity("Head4", "models/ogrehead.mesh");
head4->setMaterialName("Examples/EnvMappedRustySteel");
head4->setCastShadows( true );
node4->attachObject(head4);
node4->setPosition(65,0,100);
//=================================================
//-------------------------------------------------
//=================================================
Camera_tps camera_tps;
Uint32 last_time,current_time,elapsed_time; //time animation
Uint32 start_time,stop_time; //frame limit
//Light
double pos = 100;
int dist = 250;
bool pos_bool = true;
//SDL
SDL_Event event;
bool continuer = true;
last_time = SDL_GetTicks();
SDL_WarpMouse(config.getWinSizeWidth()/2, config.getWinSizeHeight()/2);
SDL_WM_GrabInput(SDL_GRAB_ON);
SDL_ShowCursor(SDL_DISABLE);
while(continuer)
{
start_time = SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
continuer = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = false;
break;
default:
camera_tps.OnKeyboard(event.key);
break;
}
break;
case SDL_KEYUP: //on utilise pas de keyup, on donne donc tout à la caméra
camera_tps.OnKeyboard(event.key);
break;
case SDL_MOUSEMOTION: //la souris est bougée, ça n'interesse que la caméra
camera_tps.OnMouseMotion(event.motion);
break;
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN: //tous les évenements boutons (up ou down) sont donnés à la caméra
camera_tps.OnMouseButton(event.button);
break;
}
}
//timing
current_time = SDL_GetTicks();
elapsed_time = current_time - last_time;
last_time = current_time;
//input camera
camera_tps.animate(elapsed_time);
camera_tps.calcul_angles();
/*camera->setPosition(
camera_tps.get_px(),
camera_tps.get_py(),
camera_tps.get_pz()
);
camera->lookAt(
camera_tps.get_lx(),
camera_tps.get_ly(),
camera_tps.get_lz()
);
camera_tps.print();*/
//-----------------------------------------
lightSun->setPosition(0,50,pos);
camera->setPosition(pos,5,150);
camera->lookAt(0,0,0);
if(pos >= dist)
{
pos = dist;
pos_bool = false;
}
else if(pos <= -dist)
{
pos = -dist;
pos_bool = true;
}
if(pos_bool)
pos += 0.055*elapsed_time;
else
pos -= 0.055*elapsed_time;
//-----------------------------------------
//refresh screen after drawing
root->renderOneFrame();
///#ifdef _WINDOWS
SDL_GL_SwapBuffers();
///#endif
//SDL_Delay(16);
}
//=================================================
//-------------------------------------------------
//=================================================
SDL_WM_GrabInput(SDL_GRAB_OFF);
SDL_ShowCursor(SDL_ENABLE);
SDL_WarpMouse(config.getWinSizeWidth()/2, config.getWinSizeHeight()/2);
delete root;
root = 0;
SDL_Quit();
return 0;
}