/// <summary> /// Initializes the engine, services and components. /// </summary> /// <param name="sender">Ignored</param> /// <param name="e">Ignored</param> void InitializeEngine(object sender, EventArgs e) { // Attach the looping events looping = new Looping(this); Engine = new TVEngine(); #if DEBUG Engine.SetDebugMode(true, true, true, false); Engine.SetDebugFile(settings.DebugFile.FullName); #else Engine.SetDebugMode(false, false, false, false); Engine.DisplayFPS(false); #endif Engine.AllowMultithreading(settings.MultiThreading); if (File.Exists(GetType().Assembly.GetName().Name + ".lic")) { Engine.SetLicenseFile(GetType().Assembly.GetName().Name + ".lic"); } // When we use doubles, we need double-precision. Engine.SetFPUPrecision(true); // Set default component update frequency value. if (settings.UpdateFrequency.Equals(0f)) { settings.UpdateFrequency = 1f / 60f; } SetMultisample(); // Create render form. settings.RenderForm = new RenderForm(); // Apply v sync option. Engine.SetVSync(settings.VSync); // Use current settings by default if none are given. screenWidth = Screen.PrimaryScreen.Bounds.Width; screenHeight = Screen.PrimaryScreen.Bounds.Height; if (settings.ScreenMode.Width == 0 || settings.ScreenMode.Height == 0) { settings.ScreenMode.Width = screenWidth; settings.ScreenMode.Height = Screen.PrimaryScreen.Bounds.Height; } if (settings.ScreenMode.Format == 0) { settings.ScreenMode.Format = screenHeight; } bool initSuccess = false; if (settings.Fullscreen) { try { initSuccess = Engine.Init3DFullscreen(settings.ScreenMode.Width, settings.ScreenMode.Height, settings.ScreenMode.Format, true, settings.VSync, CONST_TV_DEPTHBUFFERFORMAT.TV_DEPTHBUFFER_BESTBUFFER, 1, settings.RenderForm.Handle); } catch (AccessViolationException) { /* License failure */ } } else { try { // Resize windowed form and move to center screen. settings.RenderForm.Size = new System.Drawing.Size(settings.ScreenMode.Width, settings.ScreenMode.Height); settings.RenderForm.Top = (screenHeight - settings.ScreenMode.Height) / 2; settings.RenderForm.Left = (screenWidth - settings.ScreenMode.Width) / 2; initSuccess = Engine.Init3DWindowed(settings.RenderForm.Handle, true); } catch (AccessViolationException) { /* License failure */ } } // Apply auto resize. Engine.GetViewport().SetAutoResize(true); // Probably a license failure if (!initSuccess) { MessageBox.Show("Couldn't initialize engine! Exiting.", "Critical error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Idle -= InitializeEngine; Dispose(); Application.Exit(); return; } // Initialize physics Physics.Initialize(); Physics.SetSolverModel(CONST_TV_PHYSICS_SOLVER.TV_SOLVER_ADAPTIVE); Physics.SetFrictionModel(CONST_TV_PHYSICS_FRICTION.TV_FRICTION_ADAPTIVE); Physics.SetGlobalGravity(new TV_3DVECTOR(0, -14.8f, 0)); Physics.EnableCPUOptimizations(true); // Initialize input InputEngine.Initialize(true, true); // If nothing is found, just don't hook the events if (settings.RenderForm != null) { settings.RenderForm.FormClosing += (_, __) => Exit(); //settings.RenderForm.Deactivate += (_, __) => looping.PauseLoop(); //settings.RenderForm.Activated += delegate //{ // // Silly workaround // InputEngine.ClearKeyBuffer(); // InputEngine.ForceUpdate(); // stopwatch.Reset(); // looping.ResumeLoop(); //}; } Initialize(); if (!settings.Fullscreen) { settings.RenderForm.Show(); } Application.Idle -= InitializeEngine; }