UpdateViewport() static private method

static private UpdateViewport ( ViewPortChangeMode Mode ) : void
Mode ViewPortChangeMode
return void
Exemplo n.º 1
0
 /// <summary>Resizes the OpenGL viewport if the window is resized</summary>
 internal static void WindowResize(int newWidth, int newHeight)
 {
     Width  = newWidth;
     Height = newHeight;
     if (Loading.Complete)
     {
         MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
         World.InitializeCameraRestriction();
         if (Renderer.OptionBackfaceCulling)
         {
             GL.Enable(EnableCap.CullFace);
         }
         else
         {
             GL.Disable(EnableCap.CullFace);
         }
         Renderer.ReAddObjects();
     }
     else
     {
         GL.Viewport(0, 0, Width, Height);
         GL.MatrixMode(MatrixMode.Projection);
         GL.LoadIdentity();
         GL.Ortho(0.0, (double)Width, (double)Height, 0.0, -1.0, 1.0);
     }
 }
Exemplo n.º 2
0
 /// <summary>Changes to or from fullscreen mode.</summary>
 internal static void ToggleFullscreen()
 {
     Fullscreen = !Fullscreen;
     // begin HACK //
     Renderer.ClearDisplayLists();
     if (World.MouseGrabEnabled)
     {
         SDL.SDL_SetRelativeMouseMode(SDL.SDL_bool.SDL_FALSE);
     }
     GL.Disable(EnableCap.Fog);
     Renderer.FogEnabled = false;
     GL.Disable(EnableCap.Lighting);
     Renderer.LightingEnabled = false;
     Textures.UnloadAllTextures();
     if (Fullscreen)
     {
         SDL.SDL_SetWindowSize(Window, Options.Current.FullscreenWidth, Options.Current.FullscreenHeight);
         SDL.SDL_SetWindowFullscreen(Window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN);
         Width  = Options.Current.FullscreenWidth;
         Height = Options.Current.FullscreenHeight;
     }
     else
     {
         SDL.SDL_SetWindowSize(Window, Options.Current.WindowWidth, Options.Current.WindowHeight);
         SDL.SDL_SetWindowFullscreen(Window, 0);
         Width  = Options.Current.WindowWidth;
         Height = Options.Current.WindowHeight;
     }
     Renderer.InitializeLighting();
     MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
     MainLoop.InitializeMotionBlur();
     Timetable.CreateTimetable();
     Timetable.UpdateCustomTimetable(null, null);
     if (World.MouseGrabEnabled)
     {
         SDL.SDL_SetRelativeMouseMode(SDL.SDL_bool.SDL_TRUE);
     }
     World.MouseGrabTarget     = new OpenBveApi.Math.Vector2D(0.0, 0.0);
     World.MouseGrabIgnoreOnce = true;
     World.InitializeCameraRestriction();
     if (Renderer.OptionBackfaceCulling)
     {
         GL.Enable(EnableCap.CullFace);
     }
     else
     {
         GL.Disable(EnableCap.CullFace);
     }
     Renderer.ReAddObjects();
     // end HACK //
 }
Exemplo n.º 3
0
 /// <summary>Changes to or from fullscreen mode.</summary>
 internal static void ToggleFullscreen()
 {
     Fullscreen = !Fullscreen;
     // begin HACK //
     Renderer.ClearDisplayLists();
     if (World.MouseGrabEnabled)
     {
         Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_OFF);
     }
     Gl.glDisable(Gl.GL_FOG);
     Renderer.FogEnabled = false;
     Gl.glDisable(Gl.GL_LIGHTING);
     Renderer.LightingEnabled = false;
     Textures.UnloadAllTextures();
     if (Fullscreen)
     {
         Sdl.SDL_SetVideoMode(Interface.CurrentOptions.FullscreenWidth, Interface.CurrentOptions.FullscreenHeight, Interface.CurrentOptions.FullscreenBits, Sdl.SDL_OPENGL | Sdl.SDL_DOUBLEBUF | Sdl.SDL_FULLSCREEN);
         Width  = Interface.CurrentOptions.FullscreenWidth;
         Height = Interface.CurrentOptions.FullscreenHeight;
     }
     else
     {
         Sdl.SDL_SetVideoMode(Interface.CurrentOptions.WindowWidth, Interface.CurrentOptions.WindowHeight, 32, Sdl.SDL_OPENGL | Sdl.SDL_DOUBLEBUF);
         Width  = Interface.CurrentOptions.WindowWidth;
         Height = Interface.CurrentOptions.WindowHeight;
     }
     Renderer.InitializeLighting();
     MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
     MainLoop.InitializeMotionBlur();
     Timetable.CreateTimetable();
     Timetable.UpdateCustomTimetable(null, null);
     if (World.MouseGrabEnabled)
     {
         Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON);
     }
     World.MouseGrabTarget     = new World.Vector2D(0.0, 0.0);
     World.MouseGrabIgnoreOnce = true;
     World.InitializeCameraRestriction();
     if (Renderer.OptionBackfaceCulling)
     {
         Gl.glEnable(Gl.GL_CULL_FACE);
     }
     else
     {
         Gl.glDisable(Gl.GL_CULL_FACE);
     }
     Renderer.ReAddObjects();
     // end HACK //
 }
Exemplo n.º 4
0
 protected override void OnLoad(EventArgs e)
 {
     Renderer.DetermineMaxAFLevel();
     //Initialise the loader thread queues
     jobs  = new Queue <ThreadStart>(10);
     locks = new Queue <object>(10);
     Renderer.Initialize();
     MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
     Renderer.InitializeMotionBlur();
     Loading.LoadAsynchronously(MainLoop.currentResult.RouteFile, MainLoop.currentResult.RouteEncoding, MainLoop.currentResult.TrainFolder, MainLoop.currentResult.TrainEncoding);
     LoadingScreenLoop();
     //Add event handler hooks for keyboard and mouse buttons
     //Do this after the renderer has init and the loop has started to prevent timing issues
     KeyDown    += MainLoop.keyDownEvent;
     KeyUp      += MainLoop.keyUpEvent;
     MouseDown  += MainLoop.mouseDownEvent;
     MouseMove  += MainLoop.mouseMoveEvent;
     MouseWheel += MainLoop.mouseWheelEvent;
 }
Exemplo n.º 5
0
        /// <summary>This method is called once the route and train data have been preprocessed, in order to physically setup the simulation</summary>
        private void SetupSimulation()
        {
            if (Loading.Cancel)
            {
                Close();
            }
            Timetable.CreateTimetable();
            //Check if any critical errors have occured during the route or train loading
            for (int i = 0; i < Interface.MessageCount; i++)
            {
                if (Interface.Messages[i].Type == Interface.MessageType.Critical)
                {
                    MessageBox.Show("A critical error has occured:\n\n" + Interface.Messages[i].Text + "\n\nPlease inspect the error log file for further information.", "Load", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    Close();
                }
            }
            Renderer.InitializeLighting();
            Game.LogRouteName = System.IO.Path.GetFileName(MainLoop.currentResult.RouteFile);
            Game.LogTrainName = System.IO.Path.GetFileName(MainLoop.currentResult.TrainFolder);
            Game.LogDateTime  = DateTime.Now;

            if (Interface.CurrentOptions.LoadInAdvance)
            {
                Textures.LoadAllTextures();
            }
            else
            {
                Textures.UnloadAllTextures();
            }
            // camera
            ObjectManager.InitializeVisibility();
            World.CameraTrackFollower.Update(0.0, true, false);
            World.CameraTrackFollower.Update(-0.1, true, false);
            World.CameraTrackFollower.Update(0.1, true, false);
            World.CameraTrackFollower.TriggerType = TrackManager.EventTriggerType.Camera;
            // starting time and track position
            Game.SecondsSinceMidnight = 0.0;
            Game.StartupTime          = 0.0;
            int    PlayerFirstStationIndex = -1;
            double PlayerFirstStationPosition;
            int    os = -1;
            bool   f  = false;

            for (int i = 0; i < Game.Stations.Length; i++)
            {
                if (!String.IsNullOrEmpty(Game.InitialStationName))
                {
                    if (Game.InitialStationName.ToLowerInvariant() == Game.Stations[i].Name.ToLowerInvariant())
                    {
                        PlayerFirstStationIndex = i;
                    }
                }
                if (Game.Stations[i].StopMode == StationStopMode.AllStop | Game.Stations[i].StopMode == StationStopMode.PlayerStop & Game.Stations[i].Stops.Length != 0)
                {
                    if (f == false)
                    {
                        os = i;
                        f  = true;
                    }
                }
            }
            if (PlayerFirstStationIndex == -1)
            {
                PlayerFirstStationIndex = os;
            }
            {
                int s = Game.GetStopIndex(PlayerFirstStationIndex, TrainManager.PlayerTrain.Cars.Length);
                if (s >= 0)
                {
                    PlayerFirstStationPosition = Game.Stations[PlayerFirstStationIndex].Stops[s].TrackPosition;

                    double TrainLength = 0.0;
                    for (int c = 0; c < TrainManager.Trains[TrainManager.PlayerTrain.TrainIndex].Cars.Length; c++)
                    {
                        TrainLength += TrainManager.Trains[TrainManager.PlayerTrain.TrainIndex].Cars[c].Length;
                    }

                    for (int j = 0; j < Game.BufferTrackPositions.Length; j++)
                    {
                        if (PlayerFirstStationPosition > Game.BufferTrackPositions[j] && PlayerFirstStationPosition - TrainLength < Game.BufferTrackPositions[j])
                        {
                            /*
                             * HACK: The initial start position for the player train is stuck on a set of buffers
                             * This means we have to make some one the fly adjustments to the first station stop position
                             */

                            //Set the start position to be the buffer position plus the train length plus 1m
                            PlayerFirstStationPosition = Game.BufferTrackPositions[j] + TrainLength + 1;
                            //Update the station stop location
                            if (s >= 0)
                            {
                                Game.Stations[PlayerFirstStationIndex].Stops[s].TrackPosition = PlayerFirstStationPosition;
                            }
                            else
                            {
                                Game.Stations[PlayerFirstStationIndex].DefaultTrackPosition = PlayerFirstStationPosition;
                            }
                            break;
                        }
                    }
                }
                else
                {
                    PlayerFirstStationPosition = Game.Stations[PlayerFirstStationIndex].DefaultTrackPosition;
                }
                if (Game.InitialStationTime != -1)
                {
                    Game.SecondsSinceMidnight = Game.InitialStationTime;
                    Game.StartupTime          = Game.InitialStationTime;
                }
                else
                {
                    if (Game.Stations[PlayerFirstStationIndex].ArrivalTime < 0.0)
                    {
                        if (Game.Stations[PlayerFirstStationIndex].DepartureTime < 0.0)
                        {
                            Game.SecondsSinceMidnight = 0.0;
                            Game.StartupTime          = 0.0;
                        }
                        else
                        {
                            Game.SecondsSinceMidnight = Game.Stations[PlayerFirstStationIndex].DepartureTime -
                                                        Game.Stations[PlayerFirstStationIndex].StopTime;
                            Game.StartupTime = Game.Stations[PlayerFirstStationIndex].DepartureTime -
                                               Game.Stations[PlayerFirstStationIndex].StopTime;
                        }
                    }
                    else
                    {
                        Game.SecondsSinceMidnight = Game.Stations[PlayerFirstStationIndex].ArrivalTime;
                        Game.StartupTime          = Game.Stations[PlayerFirstStationIndex].ArrivalTime;
                    }
                }
            }
            int    OtherFirstStationIndex    = -1;
            double OtherFirstStationPosition = 0.0;
            double OtherFirstStationTime     = 0.0;

            for (int i = 0; i < Game.Stations.Length; i++)
            {
                if (Game.Stations[i].StopMode == StationStopMode.AllStop | Game.Stations[i].StopMode == StationStopMode.PlayerPass & Game.Stations[i].Stops.Length != 0)
                {
                    OtherFirstStationIndex = i;
                    int s = Game.GetStopIndex(i, TrainManager.PlayerTrain.Cars.Length);
                    if (s >= 0)
                    {
                        OtherFirstStationPosition = Game.Stations[i].Stops[s].TrackPosition;
                    }
                    else
                    {
                        OtherFirstStationPosition = Game.Stations[i].DefaultTrackPosition;
                    }
                    if (Game.Stations[i].ArrivalTime < 0.0)
                    {
                        if (Game.Stations[i].DepartureTime < 0.0)
                        {
                            OtherFirstStationTime = 0.0;
                        }
                        else
                        {
                            OtherFirstStationTime = Game.Stations[i].DepartureTime - Game.Stations[i].StopTime;
                        }
                    }
                    else
                    {
                        OtherFirstStationTime = Game.Stations[i].ArrivalTime;
                    }
                    break;
                }
            }
            if (Game.PrecedingTrainTimeDeltas.Length != 0)
            {
                OtherFirstStationTime -= Game.PrecedingTrainTimeDeltas[Game.PrecedingTrainTimeDeltas.Length - 1];
                if (OtherFirstStationTime < Game.SecondsSinceMidnight)
                {
                    Game.SecondsSinceMidnight = OtherFirstStationTime;
                }
            }
            // initialize trains
            for (int i = 0; i < TrainManager.Trains.Length; i++)
            {
                TrainManager.Trains[i].Initialize();
                int s = i == TrainManager.PlayerTrain.TrainIndex ? PlayerFirstStationIndex : OtherFirstStationIndex;
                if (s >= 0)
                {
                    if (Game.Stations[s].OpenLeftDoors)
                    {
                        for (int j = 0; j < TrainManager.Trains[i].Cars.Length; j++)
                        {
                            TrainManager.Trains[i].Cars[j].Doors[0].AnticipatedOpen = true;
                        }
                    }
                    if (Game.Stations[s].OpenRightDoors)
                    {
                        for (int j = 0; j < TrainManager.Trains[i].Cars.Length; j++)
                        {
                            TrainManager.Trains[i].Cars[j].Doors[1].AnticipatedOpen = true;
                        }
                    }
                }
                if (Game.Sections.Length != 0)
                {
                    Game.Sections[0].Enter(TrainManager.Trains[i]);
                }
                for (int j = 0; j < TrainManager.Trains[i].Cars.Length; j++)
                {
                    double length = TrainManager.Trains[i].Cars[0].Length;
                    TrainManager.Trains[i].Cars[j].Move(-length, 0.01);
                    TrainManager.Trains[i].Cars[j].Move(length, 0.01);
                }
            }
            // score
            Game.CurrentScore.ArrivalStation   = PlayerFirstStationIndex + 1;
            Game.CurrentScore.DepartureStation = PlayerFirstStationIndex;
            Game.CurrentScore.Maximum          = 0;
            for (int i = 0; i < Game.Stations.Length; i++)
            {
                if (i != PlayerFirstStationIndex & Game.PlayerStopsAtStation(i))
                {
                    if (i == 0 || Game.Stations[i - 1].Type != StationType.ChangeEnds)
                    {
                        Game.CurrentScore.Maximum += Game.ScoreValueStationArrival;
                    }
                }
            }
            if (Game.CurrentScore.Maximum <= 0)
            {
                Game.CurrentScore.Maximum = Game.ScoreValueStationArrival;
            }
            // signals
            if (Game.Sections.Length > 0)
            {
                Game.UpdateSection(Game.Sections.Length - 1);
            }
            // move train in position
            for (int i = 0; i < TrainManager.Trains.Length; i++)
            {
                double p;
                if (i == TrainManager.PlayerTrain.TrainIndex)
                {
                    p = PlayerFirstStationPosition;
                }
                else if (TrainManager.Trains[i].State == TrainManager.TrainState.Bogus)
                {
                    p = Game.BogusPretrainInstructions[0].TrackPosition;
                    TrainManager.Trains[i].AI = new Game.BogusPretrainAI(TrainManager.Trains[i]);
                }
                else
                {
                    p = OtherFirstStationPosition;
                }
                for (int j = 0; j < TrainManager.Trains[i].Cars.Length; j++)
                {
                    TrainManager.Trains[i].Cars[j].Move(p, 0.01);
                }
            }
            // timetable
            if (Timetable.DefaultTimetableDescription.Length == 0)
            {
                Timetable.DefaultTimetableDescription = Game.LogTrainName;
            }

            // initialize camera
            if (World.CameraRestriction == World.CameraRestrictionMode.NotAvailable)
            {
                World.CameraMode = World.CameraViewMode.InteriorLookAhead;
            }
            //Place the initial camera in the driver car
            TrainManager.PlayerTrain.Cars[TrainManager.PlayerTrain.DriverCar].UpdateCamera();
            World.CameraTrackFollower.Update(-1.0, true, false);
            ObjectManager.UpdateVisibility(World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z);
            World.CameraSavedExterior = new World.CameraAlignment(new OpenBveApi.Math.Vector3(-2.5, 1.5, -15.0), 0.3, -0.2, 0.0, PlayerFirstStationPosition, 1.0);
            World.CameraSavedTrack    = new World.CameraAlignment(new OpenBveApi.Math.Vector3(-3.0, 2.5, 0.0), 0.3, 0.0, 0.0, TrainManager.PlayerTrain.Cars[0].FrontAxle.Follower.TrackPosition - 10.0, 1.0);
            // signalling sections
            for (int i = 0; i < TrainManager.Trains.Length; i++)
            {
                int s = TrainManager.Trains[i].CurrentSectionIndex;
                Game.Sections[s].Enter(TrainManager.Trains[i]);
            }
            if (Game.Sections.Length > 0)
            {
                Game.UpdateSection(Game.Sections.Length - 1);
            }
            // fast-forward until start time
            {
                Game.MinimalisticSimulation = true;
                const double w = 0.25;
                double       u = Game.StartupTime - Game.SecondsSinceMidnight;
                if (u > 0)
                {
                    while (true)
                    {
                        double v = u < w ? u : w; u -= v;
                        Game.SecondsSinceMidnight += v;
                        TrainManager.UpdateTrains(v);
                        if (u <= 0.0)
                        {
                            break;
                        }
                        TotalTimeElapsedForSectionUpdate += v;
                        if (TotalTimeElapsedForSectionUpdate >= 1.0)
                        {
                            if (Game.Sections.Length > 0)
                            {
                                Game.UpdateSection(Game.Sections.Length - 1);
                            }
                            TotalTimeElapsedForSectionUpdate = 0.0;
                        }
                    }
                }
                Game.MinimalisticSimulation = false;
            }
            // animated objects
            ObjectManager.UpdateAnimatedWorldObjects(0.0, true);
            TrainManager.UpdateTrainObjects(0.0, true);
            //HACK: This function calls a single update on all objects attached to the player's train
            //      but ignores any specified damping so that all needles etc. are in the correct place
            //      for the first frame, rather than spinning wildly to get to the starting point.
            TrainManager.PlayerTrain.UpdateCabObjects();
            // timetable
            if (TrainManager.PlayerTrain.Station >= 0)
            {
                Timetable.UpdateCustomTimetable(Game.Stations[TrainManager.PlayerTrain.Station].TimetableDaytimeTexture, Game.Stations[TrainManager.PlayerTrain.Station].TimetableNighttimeTexture);
                if (Timetable.CustomObjectsUsed != 0 & Timetable.CustomTimetableAvailable && Interface.CurrentOptions.TimeTableStyle != Interface.TimeTableMode.AutoGenerated && Interface.CurrentOptions.TimeTableStyle != Interface.TimeTableMode.None)
                {
                    Timetable.CurrentTimetable = Timetable.TimetableState.Custom;
                }
            }
            //Create AI driver for the player train if specified via the commmand line
            if (Game.InitialAIDriver == true)
            {
                TrainManager.PlayerTrain.AI = new Game.SimpleHumanDriverAI(TrainManager.PlayerTrain);
                if (TrainManager.PlayerTrain.Plugin != null && !TrainManager.PlayerTrain.Plugin.SupportsAI)
                {
                    Game.AddMessage(Interface.GetInterfaceString("notification_aiunable"), MessageManager.MessageDependency.None, Interface.GameMode.Expert,
                                    OpenBveApi.Colors.MessageColor.White, Game.SecondsSinceMidnight + 10.0, null);
                }
            }

            // warnings / errors
            if (Interface.MessageCount != 0)
            {
                int filesNotFound = 0;
                int errors        = 0;
                int warnings      = 0;
                for (int i = 0; i < Interface.MessageCount; i++)
                {
                    if (Interface.Messages[i].FileNotFound)
                    {
                        filesNotFound++;
                    }
                    else if (Interface.Messages[i].Type == Interface.MessageType.Error)
                    {
                        errors++;
                    }
                    else if (Interface.Messages[i].Type == Interface.MessageType.Warning)
                    {
                        warnings++;
                    }
                }
                string NotFound = null;
                string Messages = null;
                if (filesNotFound != 0)
                {
                    NotFound = filesNotFound.ToString() + " file(s) not found";
                    Game.AddMessage(NotFound, MessageManager.MessageDependency.None, Interface.GameMode.Expert, MessageColor.Magenta, Game.SecondsSinceMidnight + 10.0, null);
                }
                if (errors != 0 & warnings != 0)
                {
                    Messages = errors.ToString() + " error(s), " + warnings.ToString() + " warning(s)";
                    Game.AddMessage(Messages, MessageManager.MessageDependency.None, Interface.GameMode.Expert, MessageColor.Magenta, Game.SecondsSinceMidnight + 10.0, null);
                }
                else if (errors != 0)
                {
                    Messages = errors.ToString() + " error(s)";
                    Game.AddMessage(Messages, MessageManager.MessageDependency.None, Interface.GameMode.Expert, MessageColor.Magenta, Game.SecondsSinceMidnight + 10.0, null);
                }
                else
                {
                    Messages = warnings.ToString() + " warning(s)";
                    Game.AddMessage(Messages, MessageManager.MessageDependency.None, Interface.GameMode.Expert, MessageColor.Magenta, Game.SecondsSinceMidnight + 10.0, null);
                }
                Game.RouteInformation.FilesNotFound     = NotFound;
                Game.RouteInformation.ErrorsAndWarnings = Messages;
                //Print the plugin error encountered (If any) for 10s
                //This must be done after the simulation has init, as otherwise the timeout doesn't work
                if (Loading.PluginError != null)
                {
                    Game.AddMessage(Loading.PluginError, MessageManager.MessageDependency.None, Interface.GameMode.Expert, OpenBveApi.Colors.MessageColor.Red, Game.SecondsSinceMidnight + 5.0, null);
                    Game.AddMessage(Interface.GetInterfaceString("errors_plugin_failure2"), MessageManager.MessageDependency.None, Interface.GameMode.Expert, OpenBveApi.Colors.MessageColor.Red, Game.SecondsSinceMidnight + 5.0, null);
                }
            }
            loadComplete          = true;
            RenderRealTimeElapsed = 0.0;
            RenderTimeElapsed     = 0.0;
            World.InitializeCameraRestriction();
            Loading.SimulationSetup = true;
            switch (Game.InitialViewpoint)
            {
            case 1:
                //Switch camera to exterior
                MainLoop.SaveCameraSettings();
                World.CameraMode = World.CameraViewMode.Exterior;
                MainLoop.RestoreCameraSettings();
                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].ChangeCarSection(TrainManager.CarSectionType.Exterior);
                }
                //Make bogies visible
                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].FrontBogie.ChangeSection(0);
                    TrainManager.PlayerTrain.Cars[j].RearBogie.ChangeSection(0);
                }
                World.CameraAlignmentDirection = new World.CameraAlignment();
                World.CameraAlignmentSpeed     = new World.CameraAlignment();
                MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
                World.UpdateAbsoluteCamera(0.0);
                World.UpdateViewingDistances();
                break;

            case 2:
                //Switch camera to track
                MainLoop.SaveCameraSettings();
                World.CameraMode = World.CameraViewMode.Track;
                MainLoop.RestoreCameraSettings();
                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].ChangeCarSection(TrainManager.CarSectionType.Exterior);
                }

                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].FrontBogie.ChangeSection(0);
                    TrainManager.PlayerTrain.Cars[j].RearBogie.ChangeSection(0);
                }

                World.CameraAlignmentDirection = new World.CameraAlignment();
                World.CameraAlignmentSpeed     = new World.CameraAlignment();
                MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
                World.UpdateAbsoluteCamera(0.0);
                World.UpdateViewingDistances();
                break;

            case 3:
                //Switch camera to flyby
                MainLoop.SaveCameraSettings();
                World.CameraMode = World.CameraViewMode.FlyBy;
                MainLoop.RestoreCameraSettings();
                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].ChangeCarSection(TrainManager.CarSectionType.Exterior);
                }

                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].FrontBogie.ChangeSection(0);
                    TrainManager.PlayerTrain.Cars[j].RearBogie.ChangeSection(0);
                }

                World.CameraAlignmentDirection = new World.CameraAlignment();
                World.CameraAlignmentSpeed     = new World.CameraAlignment();
                MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
                World.UpdateAbsoluteCamera(0.0);
                World.UpdateViewingDistances();
                break;

            case 4:
                //Switch camera to flyby
                MainLoop.SaveCameraSettings();
                World.CameraMode = World.CameraViewMode.FlyByZooming;
                MainLoop.RestoreCameraSettings();
                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].ChangeCarSection(TrainManager.CarSectionType.Exterior);
                }

                for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                {
                    TrainManager.PlayerTrain.Cars[j].FrontBogie.ChangeSection(0);
                    TrainManager.PlayerTrain.Cars[j].RearBogie.ChangeSection(0);
                }

                World.CameraAlignmentDirection = new World.CameraAlignment();
                World.CameraAlignmentSpeed     = new World.CameraAlignment();
                MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
                World.UpdateAbsoluteCamera(0.0);
                World.UpdateViewingDistances();
                break;
            }
        }
Exemplo n.º 6
0
        internal static void RenderScene(double TimeElapsed)
        {
            // initialize
            ResetOpenGlState();
            int OpenGlTextureIndex = 0;

            if (OptionWireframe | OpenGlTextureIndex == 0)
            {
                if (Game.CurrentFog.Start < Game.CurrentFog.End)
                {
                    const float fogdistance = 600.0f;
                    float       n           = (fogdistance - Game.CurrentFog.Start) / (Game.CurrentFog.End - Game.CurrentFog.Start);
                    float       cr          = n * inv255 * (float)Game.CurrentFog.Color.R;
                    float       cg          = n * inv255 * (float)Game.CurrentFog.Color.G;
                    float       cb          = n * inv255 * (float)Game.CurrentFog.Color.B;
                    GL.ClearColor(cr, cg, cb, 1.0f);
                }
                else
                {
                    GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                }
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            }
            else
            {
                GL.Clear(ClearBufferMask.DepthBufferBit);
            }
            GL.PushMatrix();
            MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.ChangeToScenery);
            if (LoadTexturesImmediately == LoadTextureImmediatelyMode.NotYet)
            {
                LoadTexturesImmediately = LoadTextureImmediatelyMode.Yes;
            }
            // set up camera
            double   cx     = World.AbsoluteCameraPosition.X;
            double   cy     = World.AbsoluteCameraPosition.Y;
            double   cz     = World.AbsoluteCameraPosition.Z;
            double   dx     = World.AbsoluteCameraDirection.X;
            double   dy     = World.AbsoluteCameraDirection.Y;
            double   dz     = World.AbsoluteCameraDirection.Z;
            double   ux     = World.AbsoluteCameraUp.X;
            double   uy     = World.AbsoluteCameraUp.Y;
            double   uz     = World.AbsoluteCameraUp.Z;
            Matrix4d lookat = Matrix4d.LookAt(0.0, 0.0, 0.0, dx, dy, dz, ux, uy, uz);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref lookat);
            GL.Light(LightName.Light0, LightParameter.Position, new float[] { (float)OptionLightPosition.X, (float)OptionLightPosition.Y, (float)OptionLightPosition.Z, 0.0f });
            // fog
            double fd = Game.NextFog.TrackPosition - Game.PreviousFog.TrackPosition;

            if (fd != 0.0)
            {
                float fr  = (float)((World.CameraTrackFollower.TrackPosition - Game.PreviousFog.TrackPosition) / fd);
                float frc = 1.0f - fr;
                Game.CurrentFog.Start   = Game.PreviousFog.Start * frc + Game.NextFog.Start * fr;
                Game.CurrentFog.End     = Game.PreviousFog.End * frc + Game.NextFog.End * fr;
                Game.CurrentFog.Color.R = (byte)((float)Game.PreviousFog.Color.R * frc + (float)Game.NextFog.Color.R * fr);
                Game.CurrentFog.Color.G = (byte)((float)Game.PreviousFog.Color.G * frc + (float)Game.NextFog.Color.G * fr);
                Game.CurrentFog.Color.B = (byte)((float)Game.PreviousFog.Color.B * frc + (float)Game.NextFog.Color.B * fr);
            }
            else
            {
                Game.CurrentFog = Game.PreviousFog;
            }
            // render background
            if (FogEnabled)
            {
                GL.Disable(EnableCap.Fog); FogEnabled = false;
            }
            GL.Disable(EnableCap.DepthTest);
            UpdateBackground(TimeElapsed);
            RenderEvents(World.AbsoluteCameraPosition);
            // fog
            float aa = Game.CurrentFog.Start;
            float bb = Game.CurrentFog.End;

            if (aa < bb & aa < World.BackgroundImageDistance)
            {
                if (!FogEnabled)
                {
                    GL.Fog(FogParameter.FogMode, (int)FogMode.Linear);
                }
                GL.Fog(FogParameter.FogStart, aa);
                GL.Fog(FogParameter.FogEnd, bb);
                GL.Fog(FogParameter.FogColor, new float[] { inv255 *(float)Game.CurrentFog.Color.R, inv255 * (float)Game.CurrentFog.Color.G, inv255 * (float)Game.CurrentFog.Color.B, 1.0f });
                if (!FogEnabled)
                {
                    GL.Enable(EnableCap.Fog); FogEnabled = true;
                }
            }
            else if (FogEnabled)
            {
                GL.Disable(EnableCap.Fog); FogEnabled = false;
            }
            // world layer
            bool optionLighting = OptionLighting;

            LastBoundTexture = null;
            if (OptionLighting)
            {
                if (!LightingEnabled)
                {
                    GL.Enable(EnableCap.Lighting); LightingEnabled = true;
                }
                if (World.CameraRestriction == Camera.RestrictionMode.NotAvailable)
                {
                    GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { inv255 *(float)OptionAmbientColor.R, inv255 * (float)OptionAmbientColor.G, inv255 * (float)OptionAmbientColor.B, 1.0f });
                    GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { inv255 *(float)OptionDiffuseColor.R, inv255 * (float)OptionDiffuseColor.G, inv255 * (float)OptionDiffuseColor.B, 1.0f });
                }
            }
            else if (LightingEnabled)
            {
                GL.Disable(EnableCap.Lighting); LightingEnabled = false;
            }
            // static opaque
            if (Interface.CurrentOptions.DisableDisplayLists)
            {
                ResetOpenGlState();
                for (int i = 0; i < StaticOpaque.Length; i++)
                {
                    if (StaticOpaque[i] != null)
                    {
                        if (StaticOpaque[i].List != null)
                        {
                            for (int j = 0; j < StaticOpaque[i].List.FaceCount; j++)
                            {
                                if (StaticOpaque[i].List.Faces[j] != null)
                                {
                                    RenderFace(ref StaticOpaque[i].List.Faces[j], cx, cy, cz);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < StaticOpaque.Length; i++)
                {
                    if (StaticOpaque[i] != null)
                    {
                        if (StaticOpaque[i].Update | StaticOpaqueForceUpdate)
                        {
                            StaticOpaque[i].Update = false;
                            if (StaticOpaque[i].OpenGlDisplayListAvailable)
                            {
                                GL.DeleteLists(StaticOpaque[i].OpenGlDisplayList, 1);
                                StaticOpaque[i].OpenGlDisplayListAvailable = false;
                            }
                            if (StaticOpaque[i].List.FaceCount != 0)
                            {
                                StaticOpaque[i].OpenGlDisplayList          = GL.GenLists(1);
                                StaticOpaque[i].OpenGlDisplayListAvailable = true;
                                ResetOpenGlState();
                                GL.NewList(StaticOpaque[i].OpenGlDisplayList, ListMode.Compile);
                                for (int j = 0; j < StaticOpaque[i].List.FaceCount; j++)
                                {
                                    if (StaticOpaque[i].List.Faces[j] != null)
                                    {
                                        RenderFace(ref StaticOpaque[i].List.Faces[j], cx, cy, cz);
                                    }
                                }
                                GL.EndList();
                            }
                            StaticOpaque[i].WorldPosition = World.AbsoluteCameraPosition;
                        }
                    }
                }
                StaticOpaqueForceUpdate = false;
                for (int i = 0; i < StaticOpaque.Length; i++)
                {
                    if (StaticOpaque[i] != null && StaticOpaque[i].OpenGlDisplayListAvailable)
                    {
                        ResetOpenGlState();
                        GL.PushMatrix();
                        GL.Translate(StaticOpaque[i].WorldPosition.X - World.AbsoluteCameraPosition.X, StaticOpaque[i].WorldPosition.Y - World.AbsoluteCameraPosition.Y, StaticOpaque[i].WorldPosition.Z - World.AbsoluteCameraPosition.Z);
                        GL.CallList(StaticOpaque[i].OpenGlDisplayList);
                        GL.PopMatrix();
                    }
                }
                //Update bounding box positions now we've rendered the objects
                int currentBox = 0;
                for (int i = 0; i < StaticOpaque.Length; i++)
                {
                    if (StaticOpaque[i] != null)
                    {
                        currentBox++;
                    }
                }
            }
            // dynamic opaque
            ResetOpenGlState();
            for (int i = 0; i < DynamicOpaque.FaceCount; i++)
            {
                RenderFace(ref DynamicOpaque.Faces[i], cx, cy, cz);
            }
            // dynamic alpha
            ResetOpenGlState();
            SortPolygons(DynamicAlpha);
            if (Interface.CurrentOptions.TransparencyMode == TransparencyMode.Performance)
            {
                GL.Enable(EnableCap.Blend); BlendEnabled = true;
                GL.DepthMask(false);
                SetAlphaFunc(AlphaFunction.Greater, 0.0f);
                for (int i = 0; i < DynamicAlpha.FaceCount; i++)
                {
                    RenderFace(ref DynamicAlpha.Faces[i], cx, cy, cz);
                }
            }
            else
            {
                GL.Disable(EnableCap.Blend); BlendEnabled = false;
                SetAlphaFunc(AlphaFunction.Equal, 1.0f);
                GL.DepthMask(true);
                for (int i = 0; i < DynamicAlpha.FaceCount; i++)
                {
                    int r = (int)ObjectManager.Objects[DynamicAlpha.Faces[i].ObjectIndex].Mesh.Faces[DynamicAlpha.Faces[i].FaceIndex].Material;
                    if (ObjectManager.Objects[DynamicAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].BlendMode == World.MeshMaterialBlendMode.Normal & ObjectManager.Objects[DynamicAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].GlowAttenuationData == 0)
                    {
                        if (ObjectManager.Objects[DynamicAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].Color.A == 255)
                        {
                            RenderFace(ref DynamicAlpha.Faces[i], cx, cy, cz);
                        }
                    }
                }
                GL.Enable(EnableCap.Blend); BlendEnabled = true;
                SetAlphaFunc(AlphaFunction.Less, 1.0f);
                GL.DepthMask(false);
                bool additive = false;
                for (int i = 0; i < DynamicAlpha.FaceCount; i++)
                {
                    int r = (int)ObjectManager.Objects[DynamicAlpha.Faces[i].ObjectIndex].Mesh.Faces[DynamicAlpha.Faces[i].FaceIndex].Material;
                    if (ObjectManager.Objects[DynamicAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].BlendMode == World.MeshMaterialBlendMode.Additive)
                    {
                        if (!additive)
                        {
                            UnsetAlphaFunc();
                            additive = true;
                        }
                        RenderFace(ref DynamicAlpha.Faces[i], cx, cy, cz);
                    }
                    else
                    {
                        if (additive)
                        {
                            SetAlphaFunc(AlphaFunction.Less, 1.0f);
                            additive = false;
                        }
                        RenderFace(ref DynamicAlpha.Faces[i], cx, cy, cz);
                    }
                }
            }
            // motion blur
            GL.Disable(EnableCap.DepthTest);
            GL.DepthMask(false);
            SetAlphaFunc(AlphaFunction.Greater, 0.0f);
            if (Interface.CurrentOptions.MotionBlur != Interface.MotionBlurMode.None)
            {
                if (LightingEnabled)
                {
                    GL.Disable(EnableCap.Lighting);
                    LightingEnabled = false;
                }
                RenderFullscreenMotionBlur();
            }
            // overlay layer
            if (FogEnabled)
            {
                GL.Disable(EnableCap.Fog); FogEnabled = false;
            }
            GL.LoadIdentity();
            MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.ChangeToCab);
            lookat = Matrix4d.LookAt(0.0, 0.0, 0.0, dx, dy, dz, ux, uy, uz);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref lookat);
            if (World.CameraRestriction == Camera.RestrictionMode.NotAvailable)
            {
                // 3d cab
                ResetOpenGlState();                 // TODO: inserted
                GL.DepthMask(true);
                GL.Enable(EnableCap.DepthTest);
                GL.Clear(ClearBufferMask.DepthBufferBit);
                if (!LightingEnabled)
                {
                    GL.Enable(EnableCap.Lighting); LightingEnabled = true;
                }
                OptionLighting = true;
                GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { 0.7f, 0.7f, 0.7f, 1.0f });
                GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { 0.7f, 0.7f, 0.7f, 1.0f });
                // overlay opaque
                SetAlphaFunc(AlphaFunction.Greater, 0.9f);
                for (int i = 0; i < OverlayOpaque.FaceCount; i++)
                {
                    RenderFace(ref OverlayOpaque.Faces[i], cx, cy, cz);
                }
                // overlay alpha
                SortPolygons(OverlayAlpha);
                if (Interface.CurrentOptions.TransparencyMode == TransparencyMode.Performance)
                {
                    GL.Enable(EnableCap.Blend); BlendEnabled = true;
                    GL.DepthMask(false);
                    SetAlphaFunc(AlphaFunction.Greater, 0.0f);
                    for (int i = 0; i < OverlayAlpha.FaceCount; i++)
                    {
                        RenderFace(ref OverlayAlpha.Faces[i], cx, cy, cz);
                    }
                }
                else
                {
                    GL.Disable(EnableCap.Blend); BlendEnabled = false;
                    SetAlphaFunc(AlphaFunction.Equal, 1.0f);
                    GL.DepthMask(true);
                    for (int i = 0; i < OverlayAlpha.FaceCount; i++)
                    {
                        int r = (int)ObjectManager.Objects[OverlayAlpha.Faces[i].ObjectIndex].Mesh.Faces[OverlayAlpha.Faces[i].FaceIndex].Material;
                        if (ObjectManager.Objects[OverlayAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].BlendMode == World.MeshMaterialBlendMode.Normal & ObjectManager.Objects[OverlayAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].GlowAttenuationData == 0)
                        {
                            if (ObjectManager.Objects[OverlayAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].Color.A == 255)
                            {
                                RenderFace(ref OverlayAlpha.Faces[i], cx, cy, cz);
                            }
                        }
                    }
                    GL.Enable(EnableCap.Blend); BlendEnabled = true;
                    SetAlphaFunc(AlphaFunction.Less, 1.0f);
                    GL.DepthMask(false);
                    bool additive = false;
                    for (int i = 0; i < OverlayAlpha.FaceCount; i++)
                    {
                        int r = (int)ObjectManager.Objects[OverlayAlpha.Faces[i].ObjectIndex].Mesh.Faces[OverlayAlpha.Faces[i].FaceIndex].Material;
                        if (ObjectManager.Objects[OverlayAlpha.Faces[i].ObjectIndex].Mesh.Materials[r].BlendMode == World.MeshMaterialBlendMode.Additive)
                        {
                            if (!additive)
                            {
                                UnsetAlphaFunc();
                                additive = true;
                            }
                            RenderFace(ref OverlayAlpha.Faces[i], cx, cy, cz);
                        }
                        else
                        {
                            if (additive)
                            {
                                SetAlphaFunc(AlphaFunction.Less, 1.0f);
                                additive = false;
                            }
                            RenderFace(ref OverlayAlpha.Faces[i], cx, cy, cz);
                        }
                    }
                }
            }
            else
            {
                /*
                 * Render 2D Cab
                 * This is actually an animated object generated on the fly and held in memory
                 */
                if (LightingEnabled)
                {
                    GL.Disable(EnableCap.Lighting); LightingEnabled = false;                     // TODO: was 'true' before
                }
                OptionLighting = false;
                if (!BlendEnabled)
                {
                    GL.Enable(EnableCap.Blend); BlendEnabled = true;
                }
                GL.DepthMask(false);
                GL.Disable(EnableCap.DepthTest);
                UnsetAlphaFunc();
                SortPolygons(OverlayAlpha);
                for (int i = 0; i < OverlayAlpha.FaceCount; i++)
                {
                    RenderFace(ref OverlayAlpha.Faces[i], cx, cy, cz);
                }
            }
            // render overlays
            OptionLighting = optionLighting;
            if (LightingEnabled)
            {
                GL.Disable(EnableCap.Lighting); LightingEnabled = false;
            }
            if (FogEnabled)
            {
                GL.Disable(EnableCap.Fog); FogEnabled = false;
            }
            if (BlendEnabled)
            {
                GL.Disable(EnableCap.Blend); BlendEnabled = false;
            }
            UnsetAlphaFunc();
            GL.Disable(EnableCap.DepthTest);
            RenderOverlays(TimeElapsed);
            // finalize rendering
            GL.PopMatrix();
            LoadTexturesImmediately = LoadTextureImmediatelyMode.NoLonger;
        }
Exemplo n.º 7
0
        /// <summary>Changes to or from fullscreen mode.</summary>
        internal static void ToggleFullscreen()
        {
            Fullscreen = !Fullscreen;
            // begin HACK //
            Renderer.ClearDisplayLists();

            GL.Disable(EnableCap.Fog);
            GL.Disable(EnableCap.Lighting);
            Renderer.LightingEnabled = false;
            if (Fullscreen)
            {
                IList <DisplayResolution> resolutions = OpenTK.DisplayDevice.Default.AvailableResolutions;

                for (int i = 0; i < resolutions.Count; i++)
                {
                    //Test each resolution
                    if (resolutions[i].Width == Interface.CurrentOptions.FullscreenWidth &&
                        resolutions[i].Height == Interface.CurrentOptions.FullscreenHeight &&
                        resolutions[i].BitsPerPixel == Interface.CurrentOptions.FullscreenBits)
                    {
                        OpenTK.DisplayDevice.Default.ChangeResolution(resolutions[i]);
                        Program.currentGameWindow.Width  = resolutions[i].Width;
                        Program.currentGameWindow.Height = resolutions[i].Height;
                        Screen.Width  = Interface.CurrentOptions.FullscreenWidth;
                        Screen.Height = Interface.CurrentOptions.FullscreenHeight;
                        Program.currentGameWindow.WindowState = WindowState.Fullscreen;
                        break;
                    }
                }
                System.Threading.Thread.Sleep(20);
                if (Program.currentGameWindow.WindowState != WindowState.Fullscreen)
                {
                    MessageBox.Show(Interface.GetInterfaceString("errors_fullscreen_switch1") + System.Environment.NewLine +
                                    Interface.GetInterfaceString("errors_fullscreen_switch2"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    Fullscreen = false;
                }
            }
            else
            {
                OpenTK.DisplayDevice.Default.RestoreResolution();
                Program.currentGameWindow.WindowState = WindowState.Normal;
                Program.currentGameWindow.Width       = Interface.CurrentOptions.WindowWidth;
                Program.currentGameWindow.Height      = Interface.CurrentOptions.WindowHeight;

                Screen.Width  = Interface.CurrentOptions.WindowWidth;
                Screen.Height = Interface.CurrentOptions.WindowHeight;
            }
            Renderer.InitializeLighting();
            MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
            Renderer.InitializeMotionBlur();
            Timetable.CreateTimetable();
            Timetable.UpdateCustomTimetable(null, null);

            World.InitializeCameraRestriction();
            if (Renderer.OptionBackfaceCulling)
            {
                GL.Enable(EnableCap.CullFace);
            }
            else
            {
                GL.Disable(EnableCap.CullFace);
            }
            Renderer.ReAddObjects();
            // end HACK //

            //Reset the camera when switching between fullscreen and windowed mode
            //Otherwise, if the aspect ratio changes distortion will occur until the view is changed or the camera reset
            if (World.CameraMode == World.CameraViewMode.Interior | World.CameraMode == World.CameraViewMode.InteriorLookAhead)
            {
                World.CameraCurrentAlignment.Position = new OpenBveApi.Math.Vector3(0.0, 0.0, 0.0);
            }
            World.CameraCurrentAlignment.Yaw   = 0.0;
            World.CameraCurrentAlignment.Pitch = 0.0;
            World.CameraCurrentAlignment.Roll  = 0.0;
        }
Exemplo n.º 8
0
        // start
        private static void Start(string[] Args)
        {
            // initialize sdl video
            if (Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO) != 0)
            {
                MessageBox.Show("SDL failed to initialize the video subsystem.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            if (Sdl.SDL_Init(Sdl.SDL_INIT_JOYSTICK) != 0)
            {
                MessageBox.Show("SDL failed to initialize the joystick subsystem.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            // initialize sdl joysticks
            {
                int n = Sdl.SDL_NumJoysticks();
                Interface.CurrentJoysticks = new Interface.Joystick[n];
                for (int i = 0; i < n; i++)
                {
                    Interface.CurrentJoysticks[i].SdlHandle = Sdl.SDL_JoystickOpen(i);
                    if (CurrentPlatform == Platform.Windows)
                    {
                        string s = Sdl.SDL_JoystickName(i);
                        /* string returned is ascii packed in utf-16 (2 chars per codepoint) */
                        System.Text.StringBuilder t = new System.Text.StringBuilder(s.Length << 1);
                        for (int k = 0; k < s.Length; k++)
                        {
                            int a = (int)s[k];
                            t.Append(char.ConvertFromUtf32(a & 0xFF) + char.ConvertFromUtf32(a >> 8));
                        }
                        Interface.CurrentJoysticks[i].Name = t.ToString();
                    }
                    else
                    {
                        Interface.CurrentJoysticks[i].Name = Sdl.SDL_JoystickName(i);
                    }
                }
            }
            // load options and controls
            Interface.LoadOptions();
            Interface.LoadControls(null, out Interface.CurrentControls);
            {
                string f = Interface.GetCombinedFileName(Program.FileSystem.GetDataFolder("Controls"), "Default keyboard assignment.controls");
                Interface.Control[] c;
                Interface.LoadControls(f, out c);
                Interface.AddControls(ref Interface.CurrentControls, c);
            }
            // command line arguments
            formMain.MainDialogResult Result = new formMain.MainDialogResult();
            for (int i = 0; i < Args.Length; i++)
            {
                if (Args[i].StartsWith("/route=", StringComparison.OrdinalIgnoreCase))
                {
                    Result.RouteFile     = Args[i].Substring(7);
                    Result.RouteEncoding = System.Text.Encoding.UTF8;
                    for (int j = 0; j < Interface.CurrentOptions.RouteEncodings.Length; j++)
                    {
                        if (string.Compare(Interface.CurrentOptions.RouteEncodings[j].Value, Result.RouteFile, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            Result.RouteEncoding = System.Text.Encoding.GetEncoding(Interface.CurrentOptions.RouteEncodings[j].Codepage);
                            break;
                        }
                    }
                }
                else if (Args[i].StartsWith("/train=", StringComparison.OrdinalIgnoreCase))
                {
                    Result.TrainFolder   = Args[i].Substring(7);
                    Result.TrainEncoding = System.Text.Encoding.UTF8;
                    for (int j = 0; j < Interface.CurrentOptions.TrainEncodings.Length; j++)
                    {
                        if (string.Compare(Interface.CurrentOptions.TrainEncodings[j].Value, Result.TrainFolder, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            Result.TrainEncoding = System.Text.Encoding.GetEncoding(Interface.CurrentOptions.TrainEncodings[j].Codepage);
                            break;
                        }
                    }
                }
            }
            // train provided
            if (Result.TrainFolder != null)
            {
                if (System.IO.Directory.Exists(Result.TrainFolder))
                {
                    string File = Interface.GetCombinedFileName(Result.TrainFolder, "train.dat");
                    if (System.IO.File.Exists(File))
                    {
                        Result.TrainEncoding = System.Text.Encoding.UTF8;
                        for (int j = 0; j < Interface.CurrentOptions.TrainEncodings.Length; j++)
                        {
                            if (string.Compare(Interface.CurrentOptions.TrainEncodings[j].Value, Result.TrainFolder, StringComparison.InvariantCultureIgnoreCase) == 0)
                            {
                                Result.TrainEncoding = System.Text.Encoding.GetEncoding(Interface.CurrentOptions.TrainEncodings[j].Codepage);
                            }
                        }
                    }
                    else
                    {
                        Result.TrainFolder = null;
                    }
                }
                else
                {
                    Result.TrainFolder = null;
                }
            }
            // route provided
            if (Result.RouteFile != null)
            {
                if (!System.IO.File.Exists(Result.RouteFile))
                {
                    Result.RouteFile = null;
                }
            }
            // route provided but no train
            if (Result.RouteFile != null & Result.TrainFolder == null)
            {
                bool IsRW = string.Equals(System.IO.Path.GetExtension(Result.RouteFile), ".rw", StringComparison.OrdinalIgnoreCase);
                CsvRwRouteParser.ParseRoute(Result.RouteFile, IsRW, Result.RouteEncoding, null, null, null, true);
                if (Game.TrainName != null && Game.TrainName.Length != 0)
                {
                    string Folder = System.IO.Path.GetDirectoryName(Result.RouteFile);
                    while (true)
                    {
                        string TrainFolder = Interface.GetCombinedFolderName(Folder, "Train");
                        if (System.IO.Directory.Exists(TrainFolder))
                        {
                            Folder = Interface.GetCombinedFolderName(TrainFolder, Game.TrainName);
                            if (System.IO.Directory.Exists(Folder))
                            {
                                string File = Interface.GetCombinedFileName(Folder, "train.dat");
                                if (System.IO.File.Exists(File))
                                {
                                    // associated train found
                                    Result.TrainFolder   = Folder;
                                    Result.TrainEncoding = System.Text.Encoding.UTF8;
                                    for (int j = 0; j < Interface.CurrentOptions.TrainEncodings.Length; j++)
                                    {
                                        if (string.Compare(Interface.CurrentOptions.TrainEncodings[j].Value, Result.TrainFolder, StringComparison.InvariantCultureIgnoreCase) == 0)
                                        {
                                            Result.TrainEncoding = System.Text.Encoding.GetEncoding(Interface.CurrentOptions.TrainEncodings[j].Codepage);
                                            break;
                                        }
                                    }
                                }
                            }
                            break;
                        }
                        else
                        {
                            System.IO.DirectoryInfo Info = System.IO.Directory.GetParent(Folder);
                            if (Info != null)
                            {
                                Folder = Info.FullName;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                Game.Reset(false);
            }
            // show main menu if applicable
            if (Result.RouteFile == null | Result.TrainFolder == null)
            {
                Result = formMain.ShowMainDialog();
                if (!Result.Start)
                {
                    return;
                }
            }
            // screen
            int Width  = Interface.CurrentOptions.FullscreenMode ? Interface.CurrentOptions.FullscreenWidth : Interface.CurrentOptions.WindowWidth;
            int Height = Interface.CurrentOptions.FullscreenMode ? Interface.CurrentOptions.FullscreenHeight : Interface.CurrentOptions.WindowHeight;

            if (Width < 16)
            {
                Width = 16;
            }
            if (Height < 16)
            {
                Height = 16;
            }
            Renderer.ScreenWidth  = Width;
            Renderer.ScreenHeight = Height;
            World.AspectRatio     = (double)Renderer.ScreenWidth / (double)Renderer.ScreenHeight;
            const double degree = 0.0174532925199433;

            World.VerticalViewingAngle         = 45.0 * degree;
            World.HorizontalViewingAngle       = 2.0 * Math.Atan(Math.Tan(0.5 * World.VerticalViewingAngle) * World.AspectRatio);
            World.OriginalVerticalViewingAngle = World.VerticalViewingAngle;
            World.ExtraViewingDistance         = 50.0;
            World.ForwardViewingDistance       = (double)Interface.CurrentOptions.ViewingDistance;
            World.BackwardViewingDistance      = 0.0;
            World.BackgroundImageDistance      = (double)Interface.CurrentOptions.ViewingDistance;
            // load route and train
            SoundManager.Initialize();
            if (!Loading.Load(Result.RouteFile, Result.RouteEncoding, Result.TrainFolder, Result.TrainEncoding))
            {
                return;
            }
            Game.LogRouteName = System.IO.Path.GetFileName(Result.RouteFile);
            Game.LogTrainName = System.IO.Path.GetFileName(Result.TrainFolder);
            Game.LogDateTime  = DateTime.Now;
            // initialize sdl window
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DEPTH_SIZE, 24);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_RED_SIZE, 8);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_GREEN_SIZE, 8);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_BLUE_SIZE, 8);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_SWAP_CONTROL, Interface.CurrentOptions.VerticalSynchronization ? 1 : 0);
            Sdl.SDL_ShowCursor(Sdl.SDL_DISABLE);
            SdlWindowCreated = true;
            int Bits = Interface.CurrentOptions.FullscreenMode ? Interface.CurrentOptions.FullscreenBits : 32;

            // --- window caption and icon ---
            Sdl.SDL_WM_SetCaption(Application.ProductName, null);
            {
                string bitmapFile = OpenBveApi.Path.CombineFile(Program.FileSystem.DataFolder, "icon.bmp");
                IntPtr bitmap     = Sdl.SDL_LoadBMP(bitmapFile);
                if (bitmap != null)
                {
                    string maskFile = OpenBveApi.Path.CombineFile(Program.FileSystem.DataFolder, "mask.bin");
                    byte[] mask     = System.IO.File.ReadAllBytes(maskFile);
                    Sdl.SDL_WM_SetIcon(bitmap, mask);
                }
            }
            // create window
            int    fullscreen = Interface.CurrentOptions.FullscreenMode ? Sdl.SDL_FULLSCREEN : 0;
            IntPtr video      = Sdl.SDL_SetVideoMode(Width, Height, Bits, Sdl.SDL_OPENGL | Sdl.SDL_DOUBLEBUF | fullscreen);

            if (video != IntPtr.Zero)
            {
                // anisotropic filtering
                string[] Extensions = Gl.glGetString(Gl.GL_EXTENSIONS).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Interface.CurrentOptions.AnisotropicFilteringMaximum = 0;
                for (int i = 0; i < Extensions.Length; i++)
                {
                    if (string.Compare(Extensions[i], "GL_EXT_texture_filter_anisotropic", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        float n; Gl.glGetFloatv(Gl.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, out n);
                        Interface.CurrentOptions.AnisotropicFilteringMaximum = (int)Math.Round((double)n);
                        break;
                    }
                }
                if (Interface.CurrentOptions.AnisotropicFilteringMaximum <= 0)
                {
                    Interface.CurrentOptions.AnisotropicFilteringMaximum = 0;
                    Interface.CurrentOptions.AnisotropicFilteringLevel   = 0;
                }
                else if (Interface.CurrentOptions.AnisotropicFilteringLevel == 0 & Interface.CurrentOptions.AnisotropicFilteringMaximum > 0)
                {
                    Interface.CurrentOptions.AnisotropicFilteringLevel = Interface.CurrentOptions.AnisotropicFilteringMaximum;
                }
                else if (Interface.CurrentOptions.AnisotropicFilteringLevel > Interface.CurrentOptions.AnisotropicFilteringMaximum)
                {
                    Interface.CurrentOptions.AnisotropicFilteringLevel = Interface.CurrentOptions.AnisotropicFilteringMaximum;
                }
                // module initialization
                Fonts.Initialize();
                Renderer.Initialize();
                Renderer.InitializeLighting();
                Sdl.SDL_GL_SwapBuffers();
                Timetable.CreateTimetable();
                // camera
                MainLoop.UpdateViewport(MainLoop.ViewPortChangeMode.NoChange);
                MainLoop.InitializeMotionBlur();
                // start loop
                MainLoop.StartLoop();
            }
            else
            {
                // failed
                MessageBox.Show("SDL failed to create the window.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }