/// <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 // }
/// <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 // }
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); MainLoop.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; }
// 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); } }
/// <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; Textures.UnloadAllTextures(); 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; } } 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); MainLoop.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; if (World.CameraMode == World.CameraViewMode.Track) { TrackManager.UpdateTrackFollower(ref World.CameraTrackFollower, TrainManager.PlayerTrain.Cars[0].FrontAxle.Follower.TrackPosition, true, false); } }