/// <summary> /// Gets the current screen setup for the given parameters. /// </summary> /// <param name="width">The current screen width</param> /// <param name="height">The current screen height</param> /// <param name="targetHeight">The target height</param> /// <param name="pixelsPerUnit">The number of pixels per units</param> public static ScreenSetup Get(int width, int height, int targetHeight, int pixelsPerUnit) { var setup = new ScreenSetup(); var prev = 0f; // Get the zoom level that gets closest to target height for (setup.zoom = 1; setup.zoom < 10; setup.zoom++) { var curr = height / setup.zoom; if (prev != 0 && Math.Abs(targetHeight - prev) < Math.Abs(targetHeight - curr)) { setup.zoom--; break; } prev = curr; } // Now calculate the rest setup.width = Convert.ToInt32(Math.Ceiling((double)width / setup.zoom)); setup.height = Convert.ToInt32(Math.Ceiling((double)height / setup.zoom)); setup.unitWidth = (float)setup.width / pixelsPerUnit; setup.unitHeight = (float)setup.height / pixelsPerUnit; setup.cameraWidth = width / (pixelsPerUnit * 2f); setup.zoomedCameraWidth = width / (pixelsPerUnit * 2f * setup.zoom); setup.cameraHeight = height / (pixelsPerUnit * 2f); setup.zoomedCameraHeight = height / (pixelsPerUnit * 2f * setup.zoom); return(setup); }
/// <summary> /// Checks if any screen properties as been changed since the last update. /// </summary> void Update() { if (Screen.height != height) { var mgr = BlitEngine.Instance; var pix = pixelsPerUnit > 0 ? pixelsPerUnit : mgr.pixelsPerUnit; if (targetHeight != 0 && mgr.pixelsPerUnit != 0) { height = Screen.height; screen = ScreenSetup.Get(Screen.width, Screen.height, targetHeight, pix); mgr.Zoom = screen.zoom; } } }