stk-code_catmod/src/graphics/irr_driver.cpp

175 lines
6.0 KiB
C++
Raw Normal View History

// $Id: irr_driver.cpp 694 2006-08-29 07:42:36Z hiker $
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2009 Joerg Henrichs
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifdef HAVE_IRRLICHT
#include "graphics/irr_driver.hpp"
using namespace core;
#include "user_config.hpp"
#include "file_manager.hpp"
IrrDriver *irr_driver = NULL;
IrrDriver::IrrDriver()
{
file_manager->dropFileSystem();
// Try different drivers: start with opengl, then DirectX
for(int driver_type=0; driver_type<3; driver_type++)
{
video::E_DRIVER_TYPE type = driver_type==0
? video::EDT_OPENGL
: (driver_type==1
? video::EDT_DIRECT3D9
: video::EDT_DIRECT3D8);
// Try 32 and 16 bit per pixels
for(int bits=32; bits>15; bits -=16)
{
m_device = createDevice(type,
dimension2d<irr::s32>(user_config->m_width,
user_config->m_height ),
bits, //bits per pixel
user_config->m_fullscreen,
false, // stencil buffers
false, // vsync
this // event receiver
);
if(m_device) break;
} // for bits=24, 16
if(m_device) break;
} // for edt_types
if(!m_device)
{
fprintf(stderr, "Couldn't initialise irrlicht device. Quitting.\n");
exit(-1);
}
// Stores the new file system pointer.
file_manager->setDevice(m_device);
m_device->setWindowCaption(L"SuperTuxKart");
m_scene_manager = m_device->getSceneManager();
} // IrrDriver
// ----------------------------------------------------------------------------
IrrDriver::~IrrDriver()
{
m_device->drop();
} // ~IrrDriver
// ----------------------------------------------------------------------------
/** Loads an animated mesh and returns a pointer to it.
* \param filename File to load.
*/
scene::IAnimatedMesh *IrrDriver::getAnimatedMesh(const std::string &filename)
{
return m_scene_manager->getMesh(filename.c_str());
} // getAnimatedMesh
// ----------------------------------------------------------------------------
/** Loads a non-animated mesh and returns a pointer to it.
* \param filename File to load.
*/
scene::IMesh *IrrDriver::getMesh(const std::string &filename)
{
scene::IAnimatedMesh *m = m_scene_manager->getMesh(filename.c_str());
if(!m) return NULL;
return m->getMesh(0);
} // getMesh
// ----------------------------------------------------------------------------
/** Adds a mesh that will be optimised using an oct tree.
* \param mesh Mesh to add.
*/
scene::ISceneNode *IrrDriver::addOctTree(scene::IMesh *mesh)
{
return m_scene_manager->addOctTreeSceneNode(mesh);
} // addOctTree
// ----------------------------------------------------------------------------
/** Adds a static mesh to scene. This should be used for smaller objects,
* since the node is not optimised.
* \param mesh The mesh to add.
*/
scene::ISceneNode *IrrDriver::addMesh(scene::IMesh *mesh)
{
return m_scene_manager->addMeshSceneNode(mesh);
} // addMesh
// ----------------------------------------------------------------------------
/** Adds an animated mesh to the scene.
* \param mesh The animated mesh to add.
*/
scene::ISceneNode *IrrDriver::addAnimatedMesh(scene::IAnimatedMesh *mesh)
{
return m_scene_manager->addAnimatedMeshSceneNode(mesh);
} // addAnimatedMesh
// ----------------------------------------------------------------------------
/** Adds a camera to the scene.
*/
scene::ICameraSceneNode *IrrDriver::addCamera()
{
return m_scene_manager->addCameraSceneNode();
} // addCamera
// ----------------------------------------------------------------------------
/** Update, called once per frame.
* \param dt Time since last update
*/
void IrrDriver::update(float dt)
{
m_device->getVideoDriver()->beginScene(true, true, video::SColor(255,100,101,140));
m_scene_manager->drawAll();
m_device->getVideoDriver()->endScene();
} // update
// ----------------------------------------------------------------------------
// Irrlicht Event handler.
bool IrrDriver::OnEvent(const irr::SEvent &event)
{
switch (event.EventType)
{
case irr::EET_KEY_INPUT_EVENT: {
printf("key input event\n");
break;
} // keyboard
case irr::EET_GUI_EVENT: {
return false; }
case irr::EET_MOUSE_INPUT_EVENT: {
return false; }
case irr::EET_LOG_TEXT_EVENT:
{
// Ignore 'normal' messages
if(event.LogEvent.Level>=0)
{
printf("Level %d: %s\n",
event.LogEvent.Level,event.LogEvent.Text);
}
return true;
}
default:
printf("Event: %d -> ",event.EventType);
return false;
} // switch
return false;
} // OnEvent
// ----------------------------------------------------------------------------
#endif // HAVE_IRRLICHT