2010-06-16 10:13:21 +00:00
# include "engine.hpp"
2013-01-12 21:52:26 +00:00
2014-02-19 17:40:29 +00:00
# include <iomanip>
2020-05-16 11:45:02 +00:00
# include <fstream>
2020-06-25 19:46:07 +00:00
# include <chrono>
# include <thread>
2010-06-16 10:13:21 +00:00
2015-05-13 13:03:21 +00:00
# include <boost/filesystem/fstream.hpp>
2015-04-12 13:34:50 +00:00
# include <osgViewer/ViewerEventHandlers>
2015-05-13 13:03:21 +00:00
# include <osgDB/ReadFile>
2015-06-03 15:25:18 +00:00
# include <osgDB/WriteFile>
2015-04-12 13:34:50 +00:00
2013-11-16 09:31:46 +00:00
# include <SDL.h>
2018-08-14 19:05:43 +00:00
# include <components/debug/debuglog.hpp>
2015-04-22 15:58:55 +00:00
# include <components/misc/rng.hpp>
2015-03-15 01:07:47 +00:00
2015-04-12 13:34:50 +00:00
# include <components/vfs/manager.hpp>
# include <components/vfs/registerarchives.hpp>
2015-05-13 00:52:04 +00:00
# include <components/sdlutil/sdlgraphicswindow.hpp>
2015-05-13 13:03:21 +00:00
# include <components/sdlutil/imagetosurface.hpp>
2015-05-13 00:52:04 +00:00
2015-04-12 13:34:50 +00:00
# include <components/resource/resourcesystem.hpp>
2016-02-05 17:51:51 +00:00
# include <components/resource/scenemanager.hpp>
2017-02-22 01:18:18 +00:00
# include <components/resource/stats.hpp>
2015-04-12 13:34:50 +00:00
2013-08-07 17:16:20 +00:00
# include <components/compiler/extensions0.hpp>
2013-08-07 00:38:41 +00:00
2017-02-14 02:37:45 +00:00
# include <components/sceneutil/workqueue.hpp>
2012-01-21 00:14:35 +00:00
# include <components/files/configurationmanager.hpp>
2015-07-18 01:01:06 +00:00
# include <components/version/version.hpp>
2017-07-13 15:58:48 +00:00
/*
Start of tes3mp addition
Include additional headers for multiplayer purposes
*/
2019-08-19 18:39:33 +00:00
# include <components/openmw-mp/TimedLog.hpp>
2017-07-13 15:58:48 +00:00
# include "mwmp/Main.hpp"
# include "mwmp/GUIController.hpp"
/*
End of tes3mp addition
*/
2013-12-05 13:23:39 +00:00
2019-03-17 17:18:53 +00:00
# include <components/detournavigator/navigator.hpp>
2015-05-03 15:24:35 +00:00
# include "mwinput/inputmanagerimp.hpp"
2010-07-03 13:41:20 +00:00
2012-08-12 16:11:09 +00:00
# include "mwgui/windowmanagerimp.hpp"
2011-10-09 11:12:44 +00:00
2012-08-08 13:18:55 +00:00
# include "mwscript/scriptmanagerimp.hpp"
2012-11-10 13:31:58 +00:00
# include "mwscript/interpretercontext.hpp"
2010-07-02 07:00:06 +00:00
2012-08-09 12:33:21 +00:00
# include "mwsound/soundmanagerimp.hpp"
2010-07-03 13:17:02 +00:00
2010-08-03 20:06:48 +00:00
# include "mwworld/class.hpp"
2011-01-04 14:58:22 +00:00
# include "mwworld/player.hpp"
2012-07-03 10:30:50 +00:00
# include "mwworld/worldimp.hpp"
2010-08-03 11:17:31 +00:00
2020-04-20 16:47:14 +00:00
# include "mwrender/vismask.hpp"
2010-08-03 11:17:31 +00:00
# include "mwclass/classes.hpp"
2010-06-16 10:13:21 +00:00
2012-08-09 08:35:53 +00:00
# include "mwdialogue/dialoguemanagerimp.hpp"
2012-08-09 10:56:03 +00:00
# include "mwdialogue/journalimp.hpp"
2014-12-13 01:47:04 +00:00
# include "mwdialogue/scripttest.hpp"
2010-08-06 16:01:34 +00:00
2012-08-11 15:30:55 +00:00
# include "mwmechanics/mechanicsmanagerimp.hpp"
2010-07-26 09:15:38 +00:00
2013-11-16 09:31:46 +00:00
# include "mwstate/statemanagerimp.hpp"
2012-01-10 05:34:29 +00:00
2015-06-11 23:08:58 +00:00
namespace
{
void checkSDLError ( int ret )
{
if ( ret ! = 0 )
2018-08-14 19:05:43 +00:00
Log ( Debug : : Error ) < < " SDL error: " < < SDL_GetError ( ) ;
2015-06-11 23:08:58 +00:00
}
2020-06-27 23:44:56 +00:00
struct UserStats
{
const std : : string mLabel ;
const std : : string mBegin ;
const std : : string mEnd ;
const std : : string mTaken ;
UserStats ( const std : : string & label , const std : : string & prefix )
: mLabel ( label ) ,
mBegin ( prefix + " _time_begin " ) ,
mEnd ( prefix + " _time_end " ) ,
mTaken ( prefix + " _time_taken " )
{ }
} ;
enum class UserStatsType : std : : size_t
{
Input ,
Sound ,
State ,
Script ,
Mechanics ,
Physics ,
World ,
Gui ,
Number ,
} ;
template < UserStatsType type >
struct UserStatsValue
{
static const UserStats sValue ;
} ;
template < >
const UserStats UserStatsValue < UserStatsType : : Input > : : sValue { " Input " , " input " } ;
template < >
const UserStats UserStatsValue < UserStatsType : : Sound > : : sValue { " Sound " , " sound " } ;
template < >
const UserStats UserStatsValue < UserStatsType : : State > : : sValue { " State " , " state " } ;
template < >
const UserStats UserStatsValue < UserStatsType : : Script > : : sValue { " Script " , " script " } ;
template < >
const UserStats UserStatsValue < UserStatsType : : Mechanics > : : sValue { " Mech " , " mechanics " } ;
template < >
const UserStats UserStatsValue < UserStatsType : : Physics > : : sValue { " Phys " , " physics " } ;
template < >
const UserStats UserStatsValue < UserStatsType : : World > : : sValue { " World " , " world " } ;
template < >
const UserStats UserStatsValue < UserStatsType : : Gui > : : sValue { " Gui " , " gui " } ;
template < UserStatsType type >
struct ForEachUserStatsValue
{
template < class F >
static void apply ( F & & f )
{
f ( UserStatsValue < type > : : sValue ) ;
using Next = ForEachUserStatsValue < static_cast < UserStatsType > ( static_cast < std : : size_t > ( type ) + 1 ) > ;
Next : : apply ( std : : forward < F > ( f ) ) ;
}
} ;
template < >
struct ForEachUserStatsValue < UserStatsType : : Number >
{
template < class F >
static void apply ( F & & ) { }
} ;
template < class F >
void forEachUserStatsValue ( F & & f )
{
ForEachUserStatsValue < static_cast < UserStatsType > ( 0 ) > : : apply ( std : : forward < F > ( f ) ) ;
}
template < UserStatsType sType >
class ScopedProfile
{
public :
ScopedProfile ( osg : : Timer_t frameStart , unsigned int frameNumber , const osg : : Timer & timer , osg : : Stats & stats )
: mScopeStart ( timer . tick ( ) ) ,
mFrameStart ( frameStart ) ,
mFrameNumber ( frameNumber ) ,
mTimer ( timer ) ,
mStats ( stats )
{
}
ScopedProfile ( const ScopedProfile & ) = delete ;
ScopedProfile & operator = ( const ScopedProfile & ) = delete ;
~ ScopedProfile ( )
{
const osg : : Timer_t end = mTimer . tick ( ) ;
const UserStats & stats = UserStatsValue < sType > : : sValue ;
mStats . setAttribute ( mFrameNumber , stats . mBegin , mTimer . delta_s ( mFrameStart , mScopeStart ) ) ;
mStats . setAttribute ( mFrameNumber , stats . mTaken , mTimer . delta_s ( mScopeStart , end ) ) ;
mStats . setAttribute ( mFrameNumber , stats . mEnd , mTimer . delta_s ( mFrameStart , end ) ) ;
}
private :
const osg : : Timer_t mScopeStart ;
const osg : : Timer_t mFrameStart ;
const unsigned int mFrameNumber ;
const osg : : Timer & mTimer ;
osg : : Stats & mStats ;
} ;
void initStatsHandler ( Resource : : Profiler & profiler )
{
const osg : : Vec4f textColor ( 1.f , 1.f , 1.f , 1.f ) ;
const osg : : Vec4f barColor ( 1.f , 1.f , 1.f , 1.f ) ;
const float multiplier = 1000 ;
const bool average = true ;
const bool averageInInverseSpace = false ;
const float maxValue = 10000 ;
forEachUserStatsValue ( [ & ] ( const UserStats & v )
{
profiler . addUserStatsLine ( v . mLabel , textColor , barColor , v . mTaken , multiplier ,
average , averageInInverseSpace , v . mBegin , v . mEnd , maxValue ) ;
} ) ;
}
2015-06-11 23:08:58 +00:00
}
2010-07-02 14:18:25 +00:00
void OMW : : Engine : : executeLocalScripts ( )
{
2015-08-16 21:56:44 +00:00
MWWorld : : LocalScripts & localScripts = mEnvironment . getWorld ( ) - > getLocalScripts ( ) ;
2011-10-06 10:29:59 +00:00
localScripts . startIteration ( ) ;
2016-02-03 15:09:20 +00:00
std : : pair < std : : string , MWWorld : : Ptr > script ;
while ( localScripts . getNext ( script ) )
2010-07-02 14:18:25 +00:00
{
2012-04-23 13:27:03 +00:00
MWScript : : InterpreterContext interpreterContext (
2011-10-06 10:29:59 +00:00
& script . second . getRefData ( ) . getLocals ( ) , script . second ) ;
2016-10-25 19:44:15 +00:00
2017-02-26 14:59:53 +00:00
/*
Start of tes3mp addition
By comparing its name with a list of script names , check if this script
is allowed to send packets about its value changes
If it is , set a tes3mp - only boolean to true in its interpreterContext
*/
2016-10-25 19:44:15 +00:00
if ( mwmp : : Main : : isValidPacketScript ( script . first ) )
{
interpreterContext . sendPackets = true ;
}
2017-02-26 14:59:53 +00:00
/*
End of tes3mp addition
*/
2016-10-25 19:44:15 +00:00
2018-07-22 21:46:07 +00:00
/*
Start of tes3mp addition
2020-06-06 23:59:18 +00:00
Mark this InterpreterContext as having a SCRIPT_LOCAL context ,
2018-07-22 21:46:07 +00:00
so that packets sent by the Interpreter can have their
origin determined by serverside scripts
*/
interpreterContext . setContextType ( Interpreter : : Context : : SCRIPT_LOCAL ) ;
/*
End of tes3mp addition
*/
2015-08-16 21:56:44 +00:00
mEnvironment . getScriptManager ( ) - > run ( script . first , interpreterContext ) ;
2010-07-02 14:18:25 +00:00
}
}
2010-07-01 18:49:00 +00:00
2017-11-16 16:56:44 +00:00
bool OMW : : Engine : : frame ( float frametime )
2010-07-02 15:30:26 +00:00
{
2011-10-06 10:34:13 +00:00
try
2011-02-18 14:46:24 +00:00
{
2020-06-27 23:44:56 +00:00
const osg : : Timer_t frameStart = mViewer - > getStartTick ( ) ;
const unsigned int frameNumber = mViewer - > getFrameStamp ( ) - > getFrameNumber ( ) ;
const osg : : Timer * const timer = osg : : Timer : : instance ( ) ;
osg : : Stats * const stats = mViewer - > getViewerStats ( ) ;
2011-10-08 08:15:03 +00:00
2017-11-16 16:56:44 +00:00
mEnvironment . setFrameDuration ( frametime ) ;
2012-04-05 19:16:51 +00:00
// update input
2020-06-27 23:44:56 +00:00
{
ScopedProfile < UserStatsType : : Input > profile ( frameStart , frameNumber , * timer , * stats ) ;
mEnvironment . getInputManager ( ) - > update ( frametime , false ) ;
}
2012-04-05 19:16:51 +00:00
2015-09-04 01:44:14 +00:00
// When the window is minimized, pause the game. Currently this *has* to be here to work around a MyGUI bug.
// If we are not currently rendering, then RenderItems will not be reused resulting in a memory leak upon changing widget textures (fixed in MyGUI 3.3.2),
// and destroyed widgets will not be deleted (not fixed yet, https://github.com/MyGUI/mygui/issues/21)
2020-03-31 09:15:26 +00:00
{
2020-06-27 23:44:56 +00:00
ScopedProfile < UserStatsType : : Sound > profile ( frameStart , frameNumber , * timer , * stats ) ;
2014-09-03 02:54:07 +00:00
2020-06-27 23:44:56 +00:00
if ( ! mEnvironment . getWindowManager ( ) - > isWindowVisible ( ) )
{
mEnvironment . getSoundManager ( ) - > pausePlayback ( ) ;
2020-07-04 13:50:54 +00:00
/*
Start of tes3mp change ( major )
The game cannot be paused in multiplayer , so prevent that from happening even here
*/
//return false;
/*
End of tes3mp change ( major )
*/
2020-06-27 23:44:56 +00:00
}
else
mEnvironment . getSoundManager ( ) - > resumePlayback ( ) ;
2014-09-03 02:54:07 +00:00
2020-06-27 23:44:56 +00:00
// sound
if ( mUseSound )
mEnvironment . getSoundManager ( ) - > update ( frametime ) ;
}
2010-10-31 16:23:03 +00:00
2017-07-15 09:44:00 +00:00
/*
Start of tes3mp addition
Update multiplayer processing for the current frame
*/
2016-11-15 19:54:06 +00:00
mwmp : : Main : : frame ( frametime ) ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp addition
*/
2016-01-04 12:17:30 +00:00
2014-06-14 22:09:12 +00:00
// Main menu opened? Then scripts are also paused.
2017-07-15 09:44:00 +00:00
bool paused = mEnvironment . getWindowManager ( ) - > containsMode ( MWGui : : GM_MainMenu ) ;
/*
Start of tes3mp change ( major )
Time should not be frozen in multiplayer , so the paused boolean is always set to
false instead
*/
paused = false ;
/*
End of tes3mp change ( major )
*/
2014-01-25 14:54:24 +00:00
2014-01-25 21:20:46 +00:00
// update game state
2020-06-27 23:44:56 +00:00
{
ScopedProfile < UserStatsType : : State > profile ( frameStart , frameNumber , * timer , * stats ) ;
mEnvironment . getStateManager ( ) - > update ( frametime ) ;
}
2017-07-15 09:44:00 +00:00
/*
Start of tes3mp change ( major )
Whether the GUI is active should have no relevance in multiplayer , so the guiActive
boolean is always set to false instead
*/
2020-07-04 13:50:54 +00:00
//bool guiActive = mEnvironment.getWindowManager()->isGuiMode();
bool guiActive = false ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp change ( major )
*/
2015-06-14 13:27:33 +00:00
2013-12-16 19:02:21 +00:00
{
2020-06-27 23:44:56 +00:00
ScopedProfile < UserStatsType : : Script > profile ( frameStart , frameNumber , * timer , * stats ) ;
if ( mEnvironment . getStateManager ( ) - > getState ( ) ! = MWBase : : StateManager : : State_NoGame )
2014-06-14 22:09:12 +00:00
{
2020-06-27 23:44:56 +00:00
if ( ! paused )
2015-02-10 19:25:57 +00:00
{
2020-06-27 23:44:56 +00:00
if ( mEnvironment . getWorld ( ) - > getScriptsEnabled ( ) )
{
// local scripts
executeLocalScripts ( ) ;
2010-07-22 10:46:24 +00:00
2020-06-27 23:44:56 +00:00
// global scripts
mEnvironment . getScriptManager ( ) - > getGlobalScripts ( ) . run ( ) ;
}
2014-09-18 02:33:49 +00:00
2020-06-27 23:44:56 +00:00
mEnvironment . getWorld ( ) - > markCellAsUnchanged ( ) ;
}
2010-08-01 08:25:50 +00:00
2020-06-27 23:44:56 +00:00
if ( ! guiActive )
{
double hours = ( frametime * mEnvironment . getWorld ( ) - > getTimeScaleFactor ( ) ) / 3600.0 ;
mEnvironment . getWorld ( ) - > advanceTime ( hours , true ) ;
mEnvironment . getWorld ( ) - > rechargeItems ( frametime , true ) ;
}
2015-08-27 19:20:45 +00:00
}
2013-12-16 19:02:21 +00:00
}
2010-08-01 08:25:50 +00:00
2020-06-27 23:44:56 +00:00
// update mechanics
2014-05-15 22:58:12 +00:00
{
2020-06-27 23:44:56 +00:00
ScopedProfile < UserStatsType : : Mechanics > profile ( frameStart , frameNumber , * timer , * stats ) ;
2013-11-28 08:33:50 +00:00
2020-06-27 23:44:56 +00:00
if ( mEnvironment . getStateManager ( ) - > getState ( ) ! = MWBase : : StateManager : : State_NoGame )
{
mEnvironment . getMechanicsManager ( ) - > update ( frametime , guiActive ) ;
}
if ( mEnvironment . getStateManager ( ) - > getState ( ) = = MWBase : : StateManager : : State_Running )
{
MWWorld : : Ptr player = mEnvironment . getWorld ( ) - > getPlayerPtr ( ) ;
2020-07-04 13:50:54 +00:00
/*
Start of tes3mp change ( major )
In multiplayer , the game should not end when the player dies ,
so the code here has been commented out
*/
//if(!guiActive && player.getClass().getCreatureStats(player).isDead())
// mEnvironment.getStateManager()->endGame();
/*
End of tes3mp change ( major )
*/
2020-06-27 23:44:56 +00:00
}
2013-11-16 16:46:48 +00:00
}
2010-07-22 10:29:23 +00:00
2019-01-07 10:27:31 +00:00
// update physics
{
2020-06-27 23:44:56 +00:00
ScopedProfile < UserStatsType : : Physics > profile ( frameStart , frameNumber , * timer , * stats ) ;
if ( mEnvironment . getStateManager ( ) - > getState ( ) ! = MWBase : : StateManager : : State_NoGame )
{
mEnvironment . getWorld ( ) - > updatePhysics ( frametime , guiActive ) ;
}
2019-01-07 10:27:31 +00:00
}
2012-01-23 13:33:06 +00:00
// update world
2014-05-15 22:58:12 +00:00
{
2020-06-27 23:44:56 +00:00
ScopedProfile < UserStatsType : : World > profile ( frameStart , frameNumber , * timer , * stats ) ;
if ( mEnvironment . getStateManager ( ) - > getState ( ) ! = MWBase : : StateManager : : State_NoGame )
{
mEnvironment . getWorld ( ) - > update ( frametime , guiActive ) ;
}
2014-05-15 22:58:12 +00:00
}
2012-01-23 13:33:06 +00:00
2012-04-14 15:47:44 +00:00
// update GUI
2020-06-27 23:44:56 +00:00
{
ScopedProfile < UserStatsType : : Gui > profile ( frameStart , frameNumber , * timer , * stats ) ;
mEnvironment . getWindowManager ( ) - > update ( frametime ) ;
}
2019-01-07 10:27:31 +00:00
2017-02-22 01:18:18 +00:00
if ( stats - > collectStats ( " resource " ) )
{
2020-05-21 22:11:23 +00:00
stats - > setAttribute ( frameNumber , " FrameNumber " , frameNumber ) ;
2017-02-22 01:18:18 +00:00
mResourceSystem - > reportStats ( frameNumber , stats ) ;
stats - > setAttribute ( frameNumber , " WorkQueue " , mWorkQueue - > getNumItems ( ) ) ;
stats - > setAttribute ( frameNumber , " WorkThread " , mWorkQueue - > getNumActiveThreads ( ) ) ;
2019-03-17 17:18:53 +00:00
2020-05-21 22:11:23 +00:00
mEnvironment . reportStats ( frameNumber , * stats ) ;
2017-02-22 01:18:18 +00:00
}
2010-09-18 06:39:21 +00:00
}
catch ( const std : : exception & e )
{
2018-08-14 19:05:43 +00:00
Log ( Debug : : Error ) < < " Error in frame: " < < e . what ( ) ;
2010-08-03 18:40:45 +00:00
}
2017-11-16 16:56:44 +00:00
return true ;
2010-07-02 15:30:26 +00:00
}
2012-01-21 00:14:35 +00:00
OMW : : Engine : : Engine ( Files : : ConfigurationManager & configurationManager )
2018-10-09 06:21:12 +00:00
: mWindow ( nullptr )
2015-05-13 00:52:04 +00:00
, mEncoding ( ToUTF8 : : WINDOWS_1252 )
2018-10-09 06:21:12 +00:00
, mEncoder ( nullptr )
2018-08-01 16:18:37 +00:00
, mScreenCaptureOperation ( nullptr )
2013-11-16 10:33:20 +00:00
, mSkipMenu ( false )
2010-08-18 09:16:15 +00:00
, mUseSound ( true )
2010-10-06 12:52:53 +00:00
, mCompileAll ( false )
2014-12-13 01:47:04 +00:00
, mCompileAllDialogue ( false )
2014-02-02 13:09:59 +00:00
, mWarningsMode ( 1 )
2012-07-30 09:43:28 +00:00
, mScriptConsoleMode ( false )
2013-07-31 16:46:32 +00:00
, mActivationDistanceOverride ( - 1 )
2013-11-29 19:06:54 +00:00
, mGrab ( true )
2014-08-11 18:37:29 +00:00
, mExportFonts ( false )
2019-03-19 05:12:31 +00:00
, mRandomSeed ( 0 )
2015-04-25 18:37:42 +00:00
, mScriptContext ( 0 )
, mFSStrict ( false )
, mScriptBlacklistUse ( true )
2014-09-01 09:55:12 +00:00
, mNewGame ( false )
2015-05-23 20:44:00 +00:00
, mCfgMgr ( configurationManager )
2010-08-01 08:25:50 +00:00
{
2010-08-03 11:17:31 +00:00
MWClass : : registerClasses ( ) ;
2013-01-14 01:32:45 +00:00
2020-03-14 18:24:36 +00:00
SDL_SetHint ( SDL_HINT_ACCELEROMETER_AS_JOYSTICK , " 0 " ) ; // We use only gamepads
Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE | SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK | SDL_INIT_SENSOR ;
2013-01-14 01:32:45 +00:00
if ( SDL_WasInit ( flags ) = = 0 )
{
2014-01-11 10:00:34 +00:00
SDL_SetMainReady ( ) ;
2013-01-14 01:32:45 +00:00
if ( SDL_Init ( flags ) ! = 0 )
2013-06-23 23:45:23 +00:00
{
throw std : : runtime_error ( " Could not initialize SDL! " + std : : string ( SDL_GetError ( ) ) ) ;
}
2013-01-14 01:32:45 +00:00
}
2010-06-27 23:44:15 +00:00
}
2010-06-16 10:13:21 +00:00
2010-07-02 11:12:05 +00:00
OMW : : Engine : : ~ Engine ( )
{
2017-07-15 09:44:00 +00:00
/*
Start of tes3mp addition
2019-12-04 06:39:33 +00:00
Free up memory allocated by multiplayer ' s GUIController , but make sure
mwmp : : Main has actually been initialized
2017-07-15 09:44:00 +00:00
*/
2019-12-04 06:39:33 +00:00
if ( mwmp : : Main : : isInitialized ( ) )
mwmp : : Main : : get ( ) . getGUIController ( ) - > cleanUp ( ) ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp addition
*/
2015-05-13 01:36:20 +00:00
mEnvironment . cleanup ( ) ;
2017-07-15 09:44:00 +00:00
/*
Start of tes3mp addition
Free up memory allocated by multiplayer ' s Main class
*/
2016-11-15 19:54:06 +00:00
mwmp : : Main : : destroy ( ) ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp addition
*/
2016-10-01 02:54:42 +00:00
2015-05-13 01:36:20 +00:00
delete mScriptContext ;
2018-10-09 06:21:12 +00:00
mScriptContext = nullptr ;
2015-05-13 01:36:20 +00:00
2018-10-09 06:21:12 +00:00
mWorkQueue = nullptr ;
2017-02-14 02:37:45 +00:00
2018-10-09 06:21:12 +00:00
mViewer = nullptr ;
2015-05-13 00:52:04 +00:00
2019-05-06 13:07:22 +00:00
mResourceSystem . reset ( ) ;
2019-02-17 08:07:14 +00:00
delete mEncoder ;
mEncoder = nullptr ;
2015-05-15 17:34:18 +00:00
if ( mWindow )
{
SDL_DestroyWindow ( mWindow ) ;
2018-10-09 06:21:12 +00:00
mWindow = nullptr ;
2015-05-15 17:34:18 +00:00
}
2015-05-13 00:52:04 +00:00
2013-01-14 01:32:45 +00:00
SDL_Quit ( ) ;
2016-10-01 02:54:42 +00:00
2017-07-15 09:44:00 +00:00
/*
Start of tes3mp addition
2016-10-01 02:54:42 +00:00
2017-07-15 09:44:00 +00:00
Free up memory allocated by multiplayer ' s logger
*/
2016-10-01 02:54:42 +00:00
LOG_QUIT ( ) ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp addition
*/
2010-07-02 11:12:05 +00:00
}
2011-08-19 19:06:09 +00:00
void OMW : : Engine : : enableFSStrict ( bool fsStrict )
2011-05-05 17:56:16 +00:00
{
2011-08-19 19:06:09 +00:00
mFSStrict = fsStrict ;
2011-05-05 17:56:16 +00:00
}
2010-06-16 10:13:21 +00:00
// Set data dir
2011-09-02 20:45:21 +00:00
void OMW : : Engine : : setDataDirs ( const Files : : PathContainer & dataDirs )
2010-06-16 10:13:21 +00:00
{
2012-01-29 19:27:03 +00:00
mDataDirs = dataDirs ;
2019-06-01 11:57:26 +00:00
mDataDirs . insert ( mDataDirs . begin ( ) , ( mResDir / " vfs " ) ) ;
mFileCollections = Files : : Collections ( mDataDirs , ! mFSStrict ) ;
2010-06-16 10:13:21 +00:00
}
2013-03-09 20:08:08 +00:00
// Add BSA archive
void OMW : : Engine : : addArchive ( const std : : string & archive ) {
mArchives . push_back ( archive ) ;
}
2011-01-04 00:34:55 +00:00
// Set resource dir
void OMW : : Engine : : setResourceDir ( const boost : : filesystem : : path & parResDir )
{
2014-05-21 11:39:58 +00:00
mResDir = parResDir ;
2011-01-04 00:34:55 +00:00
}
2017-08-06 13:03:48 +00:00
// Set start cell name
2010-06-16 10:13:21 +00:00
void OMW : : Engine : : setCell ( const std : : string & cellName )
{
mCellName = cellName ;
}
2013-09-29 07:11:57 +00:00
void OMW : : Engine : : addContentFile ( const std : : string & file )
2010-06-16 10:13:21 +00:00
{
2014-01-27 12:27:42 +00:00
mContentFiles . push_back ( file ) ;
2010-06-16 10:13:21 +00:00
}
2014-09-01 09:55:12 +00:00
void OMW : : Engine : : setSkipMenu ( bool skipMenu , bool newGame )
2010-07-04 14:00:32 +00:00
{
2013-11-16 10:33:20 +00:00
mSkipMenu = skipMenu ;
2014-09-01 09:55:12 +00:00
mNewGame = newGame ;
2010-07-04 14:00:32 +00:00
}
2013-01-08 17:45:01 +00:00
std : : string OMW : : Engine : : loadSettings ( Settings : : Manager & settings )
2010-06-16 10:13:21 +00:00
{
2012-04-02 11:47:25 +00:00
// Create the settings manager and load default settings file
2015-11-26 16:15:28 +00:00
const std : : string localdefault = ( mCfgMgr . getLocalPath ( ) / " settings-default.cfg " ) . string ( ) ;
const std : : string globaldefault = ( mCfgMgr . getGlobalPath ( ) / " settings-default.cfg " ) . string ( ) ;
2012-04-01 15:14:49 +00:00
// prefer local
if ( boost : : filesystem : : exists ( localdefault ) )
settings . loadDefault ( localdefault ) ;
else if ( boost : : filesystem : : exists ( globaldefault ) )
settings . loadDefault ( globaldefault ) ;
2012-05-07 21:16:29 +00:00
else
throw std : : runtime_error ( " No default settings file found! Make sure the file \" settings-default.cfg \" was properly installed. " ) ;
2012-04-01 14:59:35 +00:00
2015-02-02 23:53:30 +00:00
// load user settings if they exist
2015-11-26 16:15:28 +00:00
const std : : string settingspath = ( mCfgMgr . getUserConfigPath ( ) / " settings.cfg " ) . string ( ) ;
2012-04-01 14:59:35 +00:00
if ( boost : : filesystem : : exists ( settingspath ) )
settings . loadUser ( settingspath ) ;
2012-04-02 14:10:54 +00:00
2013-01-08 17:45:01 +00:00
return settingspath ;
}
2015-05-13 00:52:04 +00:00
void OMW : : Engine : : createWindow ( Settings : : Manager & settings )
2013-01-08 17:45:01 +00:00
{
2015-05-13 00:52:04 +00:00
int screen = settings . getInt ( " screen " , " Video " ) ;
int width = settings . getInt ( " resolution x " , " Video " ) ;
int height = settings . getInt ( " resolution y " , " Video " ) ;
bool fullscreen = settings . getBool ( " fullscreen " , " Video " ) ;
bool windowBorder = settings . getBool ( " window border " , " Video " ) ;
bool vsync = settings . getBool ( " vsync " , " Video " ) ;
int antialiasing = settings . getInt ( " antialiasing " , " Video " ) ;
int pos_x = SDL_WINDOWPOS_CENTERED_DISPLAY ( screen ) ,
pos_y = SDL_WINDOWPOS_CENTERED_DISPLAY ( screen ) ;
if ( fullscreen )
{
pos_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY ( screen ) ;
pos_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY ( screen ) ;
}
2013-11-16 09:31:46 +00:00
2015-05-13 00:52:04 +00:00
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE ;
if ( fullscreen )
flags | = SDL_WINDOW_FULLSCREEN ;
if ( ! windowBorder )
flags | = SDL_WINDOW_BORDERLESS ;
2013-01-14 01:32:45 +00:00
2014-03-27 03:15:47 +00:00
SDL_SetHint ( SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS ,
settings . getBool ( " minimize on focus loss " , " Video " ) ? " 1 " : " 0 " ) ;
2015-06-11 23:08:58 +00:00
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_RED_SIZE , 8 ) ) ;
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_GREEN_SIZE , 8 ) ) ;
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_BLUE_SIZE , 8 ) ) ;
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_ALPHA_SIZE , 0 ) ) ;
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_DEPTH_SIZE , 24 ) ) ;
2015-06-11 22:12:12 +00:00
2015-05-13 00:52:04 +00:00
if ( antialiasing > 0 )
{
2015-06-11 23:08:58 +00:00
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_MULTISAMPLEBUFFERS , 1 ) ) ;
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_MULTISAMPLESAMPLES , antialiasing ) ) ;
2015-05-13 00:52:04 +00:00
}
2015-06-05 00:53:10 +00:00
while ( ! mWindow )
2015-05-13 00:52:04 +00:00
{
2015-06-05 00:53:10 +00:00
mWindow = SDL_CreateWindow ( " OpenMW " , pos_x , pos_y , width , height , flags ) ;
if ( ! mWindow )
{
// Try with a lower AA
if ( antialiasing > 0 )
{
2018-08-14 19:05:43 +00:00
Log ( Debug : : Warning ) < < " Warning: " < < antialiasing < < " x antialiasing not supported, trying " < < antialiasing / 2 ;
2015-06-05 00:53:10 +00:00
antialiasing / = 2 ;
Settings : : Manager : : setInt ( " antialiasing " , " Video " , antialiasing ) ;
2015-06-11 23:08:58 +00:00
checkSDLError ( SDL_GL_SetAttribute ( SDL_GL_MULTISAMPLESAMPLES , antialiasing ) ) ;
2015-06-05 00:53:10 +00:00
continue ;
}
else
{
std : : stringstream error ;
2018-08-14 19:05:43 +00:00
error < < " Failed to create SDL window: " < < SDL_GetError ( ) ;
2015-06-05 00:53:10 +00:00
throw std : : runtime_error ( error . str ( ) ) ;
}
}
2015-05-13 00:52:04 +00:00
}
2015-05-13 13:03:21 +00:00
setWindowIcon ( ) ;
2015-05-13 00:52:04 +00:00
osg : : ref_ptr < osg : : GraphicsContext : : Traits > traits = new osg : : GraphicsContext : : Traits ;
SDL_GetWindowPosition ( mWindow , & traits - > x , & traits - > y ) ;
SDL_GetWindowSize ( mWindow , & traits - > width , & traits - > height ) ;
traits - > windowName = SDL_GetWindowTitle ( mWindow ) ;
traits - > windowDecoration = ! ( SDL_GetWindowFlags ( mWindow ) & SDL_WINDOW_BORDERLESS ) ;
traits - > screenNum = SDL_GetWindowDisplayIndex ( mWindow ) ;
2018-06-27 20:22:01 +00:00
// We tried to get rid of the hardcoding but failed: https://github.com/OpenMW/openmw/pull/1771
// Here goes kcat's quote:
// It's ultimately a chicken and egg problem, and the reason why the code is like it was in the first place.
// It needs a context to get the current attributes, but it needs the attributes to set up the context.
// So it just specifies the same values that were given to SDL in the hopes that it's good enough to what the window eventually gets.
2015-05-13 00:52:04 +00:00
traits - > red = 8 ;
traits - > green = 8 ;
traits - > blue = 8 ;
2015-06-14 14:32:34 +00:00
traits - > alpha = 0 ; // set to 0 to stop ScreenCaptureHandler reading the alpha channel
2015-05-13 00:52:04 +00:00
traits - > depth = 24 ;
traits - > stencil = 8 ;
traits - > vsync = vsync ;
traits - > doubleBuffer = true ;
traits - > inheritedWindowData = new SDLUtil : : GraphicsWindowSDL2 : : WindowData ( mWindow ) ;
2015-05-13 13:08:47 +00:00
osg : : ref_ptr < SDLUtil : : GraphicsWindowSDL2 > graphicsWindow = new SDLUtil : : GraphicsWindowSDL2 ( traits ) ;
if ( ! graphicsWindow - > valid ( ) ) throw std : : runtime_error ( " Failed to create GraphicsContext " ) ;
2015-05-13 00:52:04 +00:00
osg : : ref_ptr < osg : : Camera > camera = mViewer - > getCamera ( ) ;
2015-05-13 13:08:47 +00:00
camera - > setGraphicsContext ( graphicsWindow ) ;
2019-04-30 18:25:24 +00:00
camera - > setViewport ( 0 , 0 , traits - > width , traits - > height ) ;
2015-05-13 00:52:04 +00:00
mViewer - > realize ( ) ;
2017-02-22 00:49:54 +00:00
2019-04-30 18:25:24 +00:00
mViewer - > getEventQueue ( ) - > getCurrentEventState ( ) - > setWindowRectangle ( 0 , 0 , traits - > width , traits - > height ) ;
2015-05-13 00:52:04 +00:00
}
2015-05-13 13:03:21 +00:00
void OMW : : Engine : : setWindowIcon ( )
{
boost : : filesystem : : ifstream windowIconStream ;
std : : string windowIcon = ( mResDir / " mygui " / " openmw.png " ) . string ( ) ;
2015-07-18 02:41:53 +00:00
windowIconStream . open ( windowIcon , std : : ios_base : : in | std : : ios_base : : binary ) ;
2015-05-13 13:03:21 +00:00
if ( windowIconStream . fail ( ) )
2018-08-14 19:05:43 +00:00
Log ( Debug : : Error ) < < " Error: Failed to open " < < windowIcon ;
2015-05-13 13:03:21 +00:00
osgDB : : ReaderWriter * reader = osgDB : : Registry : : instance ( ) - > getReaderWriterForExtension ( " png " ) ;
2015-06-05 00:57:31 +00:00
if ( ! reader )
{
2018-08-14 19:05:43 +00:00
Log ( Debug : : Error ) < < " Error: Failed to read window icon, no png readerwriter found " ;
2015-06-05 00:57:31 +00:00
return ;
}
2015-05-13 13:03:21 +00:00
osgDB : : ReaderWriter : : ReadResult result = reader - > readImage ( windowIconStream ) ;
if ( ! result . success ( ) )
2018-08-14 19:05:43 +00:00
Log ( Debug : : Error ) < < " Error: Failed to read " < < windowIcon < < " : " < < result . message ( ) < < " code " < < result . status ( ) ;
2015-05-13 13:03:21 +00:00
else
{
osg : : ref_ptr < osg : : Image > image = result . getImage ( ) ;
2018-06-16 10:12:32 +00:00
auto surface = SDLUtil : : imageToSurface ( image , true ) ;
SDL_SetWindowIcon ( mWindow , surface . get ( ) ) ;
2015-05-13 13:03:21 +00:00
}
}
2015-05-13 00:52:04 +00:00
void OMW : : Engine : : prepareEngine ( Settings : : Manager & settings )
{
mEnvironment . setStateManager (
new MWState : : StateManager ( mCfgMgr . getUserDataPath ( ) / " saves " , mContentFiles . at ( 0 ) ) ) ;
createWindow ( settings ) ;
2015-04-12 13:34:50 +00:00
osg : : ref_ptr < osg : : Group > rootNode ( new osg : : Group ) ;
2015-04-24 21:30:30 +00:00
mViewer - > setSceneData ( rootNode ) ;
2015-04-12 13:34:50 +00:00
mVFS . reset ( new VFS : : Manager ( mFSStrict ) ) ;
VFS : : registerArchives ( mVFS . get ( ) , mFileCollections , mArchives , true ) ;
mResourceSystem . reset ( new Resource : : ResourceSystem ( mVFS . get ( ) ) ) ;
2016-02-05 22:21:54 +00:00
mResourceSystem - > getSceneManager ( ) - > setUnRefImageDataAfterApply ( false ) ; // keep to Off for now to allow better state sharing
2016-02-05 17:51:51 +00:00
mResourceSystem - > getSceneManager ( ) - > setFilterSettings (
2015-12-14 01:05:19 +00:00
Settings : : Manager : : getString ( " texture mag filter " , " General " ) ,
Settings : : Manager : : getString ( " texture min filter " , " General " ) ,
2015-12-14 00:51:27 +00:00
Settings : : Manager : : getString ( " texture mipmap " , " General " ) ,
2016-02-14 22:14:52 +00:00
Settings : : Manager : : getInt ( " anisotropy " , " General " )
2015-12-14 00:51:27 +00:00
) ;
2013-08-27 13:48:13 +00:00
2017-02-14 02:37:45 +00:00
int numThreads = Settings : : Manager : : getInt ( " preload num threads " , " Cells " ) ;
if ( numThreads < = 0 )
throw std : : runtime_error ( " Invalid setting: 'preload num threads' must be >0 " ) ;
mWorkQueue = new SceneUtil : : WorkQueue ( numThreads ) ;
2013-08-27 13:48:13 +00:00
// Create input and UI first to set up a bootstrapping environment for
// showing a loading screen and keeping the window responsive while doing so
2015-01-24 22:03:55 +00:00
std : : string keybinderUser = ( mCfgMgr . getUserConfigPath ( ) / " input_v3.xml " ) . string ( ) ;
2013-08-27 13:48:13 +00:00
bool keybinderUserExists = boost : : filesystem : : exists ( keybinderUser ) ;
2015-01-24 22:44:17 +00:00
if ( ! keybinderUserExists )
{
2015-01-26 18:50:44 +00:00
std : : string input2 = ( mCfgMgr . getUserConfigPath ( ) / " input_v2.xml " ) . string ( ) ;
if ( boost : : filesystem : : exists ( input2 ) ) {
boost : : filesystem : : copy_file ( input2 , keybinderUser ) ;
keybinderUserExists = boost : : filesystem : : exists ( keybinderUser ) ;
2019-10-30 11:22:24 +00:00
Log ( Debug : : Info ) < < " Loading keybindings file: " < < keybinderUser ;
2015-01-26 18:50:44 +00:00
}
2015-01-24 22:44:17 +00:00
}
2019-10-30 11:22:24 +00:00
else
Log ( Debug : : Info ) < < " Loading keybindings file: " < < keybinderUser ;
2015-01-20 00:55:17 +00:00
2020-03-14 18:24:36 +00:00
const std : : string userdefault = mCfgMgr . getUserConfigPath ( ) . string ( ) + " /gamecontrollerdb.txt " ;
const std : : string localdefault = mCfgMgr . getLocalPath ( ) . string ( ) + " /gamecontrollerdb.txt " ;
const std : : string globaldefault = mCfgMgr . getGlobalPath ( ) . string ( ) + " /gamecontrollerdb.txt " ;
2019-08-03 19:55:58 +00:00
std : : string userGameControllerdb ;
if ( boost : : filesystem : : exists ( userdefault ) ) {
userGameControllerdb = userdefault ;
}
else
userGameControllerdb = " " ;
2015-01-20 00:55:17 +00:00
std : : string gameControllerdb ;
if ( boost : : filesystem : : exists ( localdefault ) )
gameControllerdb = localdefault ;
else if ( boost : : filesystem : : exists ( globaldefault ) )
gameControllerdb = globaldefault ;
else
gameControllerdb = " " ; //if it doesn't exist, pass in an empty string
2015-04-24 19:55:30 +00:00
std : : string myguiResources = ( mResDir / " mygui " ) . string ( ) ;
osg : : ref_ptr < osg : : Group > guiRoot = new osg : : Group ;
2017-02-02 20:46:25 +00:00
guiRoot - > setName ( " GUI Root " ) ;
2020-04-20 16:47:14 +00:00
guiRoot - > setNodeMask ( MWRender : : Mask_GUI ) ;
2015-04-24 19:55:30 +00:00
rootNode - > addChild ( guiRoot ) ;
2020-04-16 13:31:20 +00:00
MWGui : : WindowManager * window = new MWGui : : WindowManager ( mWindow , mViewer , guiRoot , mResourceSystem . get ( ) , mWorkQueue . get ( ) ,
2015-04-24 19:55:30 +00:00
mCfgMgr . getLogPath ( ) . string ( ) + std : : string ( " / " ) , myguiResources ,
2019-01-22 06:08:48 +00:00
mScriptConsoleMode , mTranslationDataStorage , mEncoding , mExportFonts ,
2018-09-04 05:15:07 +00:00
Version : : getOpenmwVersionDescription ( mResDir . string ( ) ) , mCfgMgr . getUserConfigPath ( ) . string ( ) ) ;
2013-08-27 13:48:13 +00:00
mEnvironment . setWindowManager ( window ) ;
2020-04-16 13:31:20 +00:00
MWInput : : InputManager * input = new MWInput : : InputManager ( mWindow , mViewer , mScreenCaptureHandler , mScreenCaptureOperation , keybinderUser , keybinderUserExists , userGameControllerdb , gameControllerdb , mGrab ) ;
mEnvironment . setInputManager ( input ) ;
2014-03-27 03:15:47 +00:00
// Create sound system
2019-01-22 06:08:48 +00:00
mEnvironment . setSoundManager ( new MWSound : : SoundManager ( mVFS . get ( ) , mUseSound ) ) ;
2014-03-27 03:15:47 +00:00
2014-03-27 18:59:33 +00:00
if ( ! mSkipMenu )
{
2019-01-22 06:08:48 +00:00
const std : : string & logo = Fallback : : Map : : getString ( " Movies_Company_Logo " ) ;
2014-03-27 18:59:33 +00:00
if ( ! logo . empty ( ) )
2015-05-03 15:24:35 +00:00
window - > playVideo ( logo , true ) ;
2014-03-27 18:59:33 +00:00
}
2014-03-27 03:15:47 +00:00
2010-07-02 07:00:06 +00:00
// Create the world
2017-02-14 02:37:45 +00:00
mEnvironment . setWorld ( new MWWorld : : World ( mViewer , rootNode , mResourceSystem . get ( ) , mWorkQueue . get ( ) ,
2019-01-22 06:08:48 +00:00
mFileCollections , mContentFiles , mEncoder , mActivationDistanceOverride , mCellName ,
mStartupScript , mResDir . string ( ) , mCfgMgr . getUserDataPath ( ) . string ( ) ) ) ;
2015-08-16 21:56:44 +00:00
mEnvironment . getWorld ( ) - > setupPlayer ( ) ;
2013-08-27 13:48:13 +00:00
2015-12-09 00:06:53 +00:00
window - > setStore ( mEnvironment . getWorld ( ) - > getStore ( ) ) ;
2013-08-27 13:48:13 +00:00
window - > initUI ( ) ;
2011-02-26 23:50:08 +00:00
2012-12-23 19:23:24 +00:00
//Load translation data
2013-01-12 00:18:36 +00:00
mTranslationDataStorage . setEncoder ( mEncoder ) ;
2013-09-29 07:11:57 +00:00
for ( size_t i = 0 ; i < mContentFiles . size ( ) ; i + + )
mTranslationDataStorage . loadTranslationData ( mFileCollections , mContentFiles [ i ] ) ;
2012-12-23 19:23:24 +00:00
2013-11-16 09:31:46 +00:00
Compiler : : registerExtensions ( mExtensions ) ;
2010-07-07 16:48:06 +00:00
2010-07-26 09:15:38 +00:00
// Create script system
2012-04-23 13:27:03 +00:00
mScriptContext = new MWScript : : CompilerContext ( MWScript : : CompilerContext : : Type_Full ) ;
2010-07-03 10:12:13 +00:00
mScriptContext - > setExtensions ( & mExtensions ) ;
2010-07-02 15:21:27 +00:00
2017-02-20 20:09:15 +00:00
mEnvironment . setScriptManager ( new MWScript : : ScriptManager ( mEnvironment . getWorld ( ) - > getStore ( ) , * mScriptContext , mWarningsMode ,
2014-07-21 07:34:10 +00:00
mScriptBlacklistUse ? mScriptBlacklist : std : : vector < std : : string > ( ) ) ) ;
2010-08-01 08:25:50 +00:00
2010-07-26 09:15:38 +00:00
// Create game mechanics system
2013-11-14 13:41:10 +00:00
MWMechanics : : MechanicsManager * mechanics = new MWMechanics : : MechanicsManager ;
mEnvironment . setMechanicsManager ( mechanics ) ;
2010-07-02 15:30:26 +00:00
2010-08-06 16:01:34 +00:00
// Create dialog system
2012-04-23 13:27:03 +00:00
mEnvironment . setJournal ( new MWDialogue : : Journal ) ;
2017-02-20 20:09:15 +00:00
mEnvironment . setDialogueManager ( new MWDialogue : : DialogueManager ( mExtensions , mTranslationDataStorage ) ) ;
2010-08-06 16:01:34 +00:00
2010-10-06 12:52:53 +00:00
// scripts
if ( mCompileAll )
{
2015-08-16 21:56:44 +00:00
std : : pair < int , int > result = mEnvironment . getScriptManager ( ) - > compileAll ( ) ;
2011-10-09 10:05:13 +00:00
if ( result . first )
2018-08-14 19:05:43 +00:00
Log ( Debug : : Info )
2011-10-09 10:05:13 +00:00
< < " compiled " < < result . second < < " of " < < result . first < < " scripts ( "
< < 100 * static_cast < double > ( result . second ) / result . first
2018-08-14 19:05:43 +00:00
< < " %) " ;
2010-10-06 12:52:53 +00:00
}
2014-12-13 01:47:04 +00:00
if ( mCompileAllDialogue )
{
2014-12-18 09:20:15 +00:00
std : : pair < int , int > result = MWDialogue : : ScriptTest : : compileAll ( & mExtensions , mWarningsMode ) ;
2014-12-13 01:47:04 +00:00
if ( result . first )
2018-08-14 19:05:43 +00:00
Log ( Debug : : Info )
2014-12-13 01:47:04 +00:00
< < " compiled " < < result . second < < " of " < < result . first < < " dialogue script/actor combinations a( "
< < 100 * static_cast < double > ( result . second ) / result . first
2018-08-14 19:05:43 +00:00
< < " %) " ;
2014-12-13 01:47:04 +00:00
}
2013-01-08 17:45:01 +00:00
}
2015-06-03 15:25:18 +00:00
class WriteScreenshotToFileOperation : public osgViewer : : ScreenCaptureHandler : : CaptureOperation
{
public :
WriteScreenshotToFileOperation ( const std : : string & screenshotPath , const std : : string & screenshotFormat )
: mScreenshotPath ( screenshotPath )
, mScreenshotFormat ( screenshotFormat )
{
}
virtual void operator ( ) ( const osg : : Image & image , const unsigned int context_id )
{
// Count screenshots.
int shotCount = 0 ;
// Find the first unused filename with a do-while
std : : ostringstream stream ;
do
{
// Reset the stream
stream . str ( " " ) ;
stream . clear ( ) ;
stream < < mScreenshotPath < < " /screenshot " < < std : : setw ( 3 ) < < std : : setfill ( ' 0 ' ) < < shotCount + + < < " . " < < mScreenshotFormat ;
} while ( boost : : filesystem : : exists ( stream . str ( ) ) ) ;
boost : : filesystem : : ofstream outStream ;
outStream . open ( boost : : filesystem : : path ( stream . str ( ) ) , std : : ios : : binary ) ;
osgDB : : ReaderWriter * readerwriter = osgDB : : Registry : : instance ( ) - > getReaderWriterForExtension ( mScreenshotFormat ) ;
if ( ! readerwriter )
{
2018-08-14 19:05:43 +00:00
Log ( Debug : : Error ) < < " Error: Can't write screenshot, no ' " < < mScreenshotFormat < < " ' readerwriter found " ;
2015-06-03 15:25:18 +00:00
return ;
}
osgDB : : ReaderWriter : : WriteResult result = readerwriter - > writeImage ( image , outStream ) ;
if ( ! result . success ( ) )
{
2018-08-14 19:05:43 +00:00
Log ( Debug : : Error ) < < " Error: Can't write screenshot: " < < result . message ( ) < < " code " < < result . status ( ) ;
2015-06-03 15:25:18 +00:00
}
}
private :
std : : string mScreenshotPath ;
std : : string mScreenshotFormat ;
} ;
2013-01-08 17:45:01 +00:00
// Initialise and enter main loop.
void OMW : : Engine : : go ( )
{
2013-09-29 07:11:57 +00:00
assert ( ! mContentFiles . empty ( ) ) ;
2017-07-15 09:44:00 +00:00
/*
Start of tes3mp change ( major )
Attempt multiplayer initialization and proceed no further if it fails
*/
if ( ! mwmp : : Main : : init ( mContentFiles , mFileCollections ) )
2016-12-03 15:36:53 +00:00
return ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp change ( major )
*/
2015-04-24 21:30:30 +00:00
2018-08-14 19:05:43 +00:00
Log ( Debug : : Info ) < < " OSG version: " < < osgGetVersion ( ) ;
2019-03-15 16:04:47 +00:00
SDL_version sdlVersion ;
SDL_GetVersion ( & sdlVersion ) ;
Log ( Debug : : Info ) < < " SDL version: " < < ( int ) sdlVersion . major < < " . " < < ( int ) sdlVersion . minor < < " . " < < ( int ) sdlVersion . patch ;
2017-02-18 02:15:15 +00:00
2019-02-24 19:41:11 +00:00
Misc : : Rng : : init ( mRandomSeed ) ;
2018-06-28 14:05:00 +00:00
// Load settings
2013-01-08 17:45:01 +00:00
Settings : : Manager settings ;
2013-08-07 00:38:41 +00:00
std : : string settingspath ;
2013-01-08 17:45:01 +00:00
settingspath = loadSettings ( settings ) ;
2018-06-28 14:05:00 +00:00
// Create encoder
2019-02-17 08:07:14 +00:00
mEncoder = new ToUTF8 : : Utf8Encoder ( mEncoding ) ;
2018-06-28 14:05:00 +00:00
// Setup viewer
mViewer = new osgViewer : : Viewer ;
mViewer - > setReleaseContextAtEndOfFrameHint ( false ) ;
2019-04-22 17:14:51 +00:00
# if OSG_VERSION_GREATER_OR_EQUAL(3,5,5)
// Do not try to outsmart the OS thread scheduler (see bug #4785).
mViewer - > setUseConfigureAffinity ( false ) ;
# endif
2020-04-19 23:34:00 +00:00
mScreenCaptureOperation = new WriteScreenshotToFileOperation (
2020-04-20 16:22:50 +00:00
mCfgMgr . getScreenshotPath ( ) . string ( ) ,
2017-11-09 17:26:27 +00:00
Settings : : Manager : : getString ( " screenshot format " , " General " ) ) ;
mScreenCaptureHandler = new osgViewer : : ScreenCaptureHandler ( mScreenCaptureOperation ) ;
2015-06-03 15:25:18 +00:00
mViewer - > addEventHandler ( mScreenCaptureHandler ) ;
2017-08-30 21:26:30 +00:00
mEnvironment . setFrameRateLimit ( Settings : : Manager : : getFloat ( " framerate limit " , " Video " ) ) ;
2013-01-08 17:45:01 +00:00
prepareEngine ( settings ) ;
2017-07-15 09:44:00 +00:00
/*
Start of tes3mp addition
Handle post - initialization for multiplayer classes
*/
2016-11-15 19:54:06 +00:00
mwmp : : Main : : postInit ( ) ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp addition
*/
/*
Start of tes3mp change ( major )
Always skip the main menu in multiplayer
*/
2016-01-04 12:17:30 +00:00
mSkipMenu = true ;
2017-07-15 09:44:00 +00:00
/*
End of tes3mp change ( major )
*/
2013-01-08 17:45:01 +00:00
2018-06-28 14:05:00 +00:00
// Setup profiler
osg : : ref_ptr < Resource : : Profiler > statshandler = new Resource : : Profiler ;
2020-06-27 23:44:56 +00:00
initStatsHandler ( * statshandler ) ;
2018-06-28 14:05:00 +00:00
mViewer - > addEventHandler ( statshandler ) ;
osg : : ref_ptr < Resource : : StatsHandler > resourceshandler = new Resource : : StatsHandler ;
mViewer - > addEventHandler ( resourceshandler ) ;
// Start the game
2015-01-07 02:03:56 +00:00
if ( ! mSaveGameFile . empty ( ) )
{
2015-08-16 21:56:44 +00:00
mEnvironment . getStateManager ( ) - > loadGame ( mSaveGameFile ) ;
2015-01-07 02:03:56 +00:00
}
2015-05-07 19:57:02 +00:00
else if ( ! mSkipMenu )
2014-03-27 03:15:47 +00:00
{
2015-01-07 02:03:56 +00:00
// start in main menu
2015-08-16 21:56:44 +00:00
mEnvironment . getWindowManager ( ) - > pushGuiMode ( MWGui : : GM_MainMenu ) ;
2019-03-03 22:31:51 +00:00
mEnvironment . getSoundManager ( ) - > playTitleMusic ( ) ;
2019-01-22 06:08:48 +00:00
const std : : string & logo = Fallback : : Map : : getString ( " Movies_Morrowind_Logo " ) ;
2019-03-03 22:31:51 +00:00
if ( ! logo . empty ( ) )
mEnvironment . getWindowManager ( ) - > playVideo ( logo , true ) ;
2014-03-27 03:15:47 +00:00
}
2013-11-16 11:22:28 +00:00
else
2014-05-20 16:35:17 +00:00
{
2015-08-16 21:56:44 +00:00
mEnvironment . getStateManager ( ) - > newGame ( ! mNewGame ) ;
2014-05-20 16:35:17 +00:00
}
2014-03-13 12:19:32 +00:00
2019-02-23 19:39:13 +00:00
if ( ! mStartupScript . empty ( ) & & mEnvironment . getStateManager ( ) - > getState ( ) = = MWState : : StateManager : : State_Running )
{
mEnvironment . getWindowManager ( ) - > executeInConsole ( mStartupScript ) ;
}
2020-05-16 11:45:02 +00:00
std : : ofstream stats ;
if ( const auto path = std : : getenv ( " OPENMW_OSG_STATS_FILE " ) )
{
stats . open ( path , std : : ios_base : : out ) ;
if ( ! stats )
Log ( Debug : : Warning ) < < " Failed to open file for stats: " < < path ;
}
2010-06-16 10:13:21 +00:00
// Start the main rendering loop
2015-04-14 13:55:56 +00:00
osg : : Timer frameTimer ;
2015-06-14 13:27:33 +00:00
double simulationTime = 0.0 ;
2015-08-16 21:56:44 +00:00
while ( ! mViewer - > done ( ) & & ! mEnvironment . getStateManager ( ) - > hasQuitRequest ( ) )
2015-04-12 13:34:50 +00:00
{
2015-04-14 13:55:56 +00:00
double dt = frameTimer . time_s ( ) ;
frameTimer . setStartTick ( ) ;
2015-05-26 14:40:44 +00:00
dt = std : : min ( dt , 0.2 ) ;
2015-04-14 13:55:56 +00:00
2015-06-14 13:27:33 +00:00
mViewer - > advance ( simulationTime ) ;
2017-11-16 16:56:44 +00:00
if ( ! frame ( dt ) )
2015-09-04 01:44:14 +00:00
{
2020-06-25 19:46:07 +00:00
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 5 ) ) ;
2015-09-07 14:05:51 +00:00
continue ;
2015-09-04 01:44:14 +00:00
}
else
{
mViewer - > eventTraversal ( ) ;
mViewer - > updateTraversal ( ) ;
2017-07-19 11:05:51 +00:00
mEnvironment . getWorld ( ) - > updateWindowManager ( ) ;
2015-09-04 01:44:14 +00:00
mViewer - > renderingTraversals ( ) ;
2017-11-16 16:56:44 +00:00
bool guiActive = mEnvironment . getWindowManager ( ) - > isGuiMode ( ) ;
2017-11-16 23:26:31 +00:00
/*
Start of tes3mp change ( major )
Whether the GUI is active should have no relevance in multiplayer , so the guiActive
boolean is always set to false instead
*/
guiActive = false ;
/*
End of tes3mp change ( major )
*/
2017-11-16 16:56:44 +00:00
if ( ! guiActive )
simulationTime + = dt ;
2015-09-04 01:44:14 +00:00
}
2015-09-07 14:05:51 +00:00
2020-05-16 11:45:02 +00:00
if ( stats )
{
const auto frameNumber = mViewer - > getFrameStamp ( ) - > getFrameNumber ( ) ;
mViewer - > getViewerStats ( ) - > report ( stats , frameNumber ) ;
}
2017-08-30 21:26:30 +00:00
mEnvironment . limitFrameRate ( frameTimer . time_s ( ) ) ;
2015-04-12 13:34:50 +00:00
}
2012-04-01 16:48:37 +00:00
// Save user settings
settings . saveUser ( settingspath ) ;
2018-08-14 19:05:43 +00:00
Log ( Debug : : Info ) < < " Quitting peacefully. " ;
2010-06-16 10:13:21 +00:00
}
2010-08-05 11:36:33 +00:00
2010-10-06 12:52:53 +00:00
void OMW : : Engine : : setCompileAll ( bool all )
{
mCompileAll = all ;
}
Added new command line option: "encoding"
Added new command line option: "encoding" which allow to
change font encoding used in game messages.
Currently there are three evailable encodings:
win1250 - Central and Eastern European (languages
that use Latin script, such as Polish,
Czech, Slovak, Hungarian, Slovene, Bosnian,
Croatian, Serbian (Latin script),
Romanian and Albanian)
win1251 - languages that use the Cyrillic alphabet
such as Russian, Bulgarian, Serbian Cyrillic
and others
win1252 - Western European (Latin) - default
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
2011-07-17 20:16:50 +00:00
2014-12-13 01:47:04 +00:00
void OMW : : Engine : : setCompileAllDialogue ( bool all )
{
mCompileAllDialogue = all ;
}
Added new command line option: "encoding"
Added new command line option: "encoding" which allow to
change font encoding used in game messages.
Currently there are three evailable encodings:
win1250 - Central and Eastern European (languages
that use Latin script, such as Polish,
Czech, Slovak, Hungarian, Slovene, Bosnian,
Croatian, Serbian (Latin script),
Romanian and Albanian)
win1251 - languages that use the Cyrillic alphabet
such as Russian, Bulgarian, Serbian Cyrillic
and others
win1252 - Western European (Latin) - default
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
2011-07-17 20:16:50 +00:00
2011-08-19 19:06:09 +00:00
void OMW : : Engine : : setSoundUsage ( bool soundUsage )
{
mUseSound = soundUsage ;
}
2012-12-23 19:23:24 +00:00
void OMW : : Engine : : setEncoding ( const ToUTF8 : : FromType & encoding )
Added new command line option: "encoding"
Added new command line option: "encoding" which allow to
change font encoding used in game messages.
Currently there are three evailable encodings:
win1250 - Central and Eastern European (languages
that use Latin script, such as Polish,
Czech, Slovak, Hungarian, Slovene, Bosnian,
Croatian, Serbian (Latin script),
Romanian and Albanian)
win1251 - languages that use the Cyrillic alphabet
such as Russian, Bulgarian, Serbian Cyrillic
and others
win1252 - Western European (Latin) - default
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
2011-07-17 20:16:50 +00:00
{
mEncoding = encoding ;
2011-08-19 19:06:09 +00:00
}
2012-04-02 18:47:09 +00:00
2012-07-30 09:43:28 +00:00
void OMW : : Engine : : setScriptConsoleMode ( bool enabled )
{
mScriptConsoleMode = enabled ;
}
2012-07-30 10:37:46 +00:00
void OMW : : Engine : : setStartupScript ( const std : : string & path )
{
mStartupScript = path ;
}
2013-01-09 03:52:18 +00:00
void OMW : : Engine : : setActivationDistanceOverride ( int distance )
{
mActivationDistanceOverride = distance ;
2013-08-16 11:01:52 +00:00
}
2014-02-02 13:09:59 +00:00
void OMW : : Engine : : setWarningsMode ( int mode )
{
mWarningsMode = mode ;
2014-02-28 16:23:58 +00:00
}
2014-07-21 07:34:10 +00:00
void OMW : : Engine : : setScriptBlacklist ( const std : : vector < std : : string > & list )
{
mScriptBlacklist = list ;
}
void OMW : : Engine : : setScriptBlacklistUse ( bool use )
{
mScriptBlacklistUse = use ;
2014-08-11 18:37:29 +00:00
}
void OMW : : Engine : : enableFontExport ( bool exportFonts )
{
mExportFonts = exportFonts ;
}
2015-01-07 02:03:56 +00:00
void OMW : : Engine : : setSaveGameFile ( const std : : string & savegame )
{
mSaveGameFile = savegame ;
}
2019-02-24 19:41:11 +00:00
void OMW : : Engine : : setRandomSeed ( unsigned int seed )
{
mRandomSeed = seed ;
}