Multiplayer and viewports
Ok, a few weeks ago, i implemented multiplayer views on my race game, and, i have to admit, it was pretty easy. (Thanks to XNA, defining a viewport is only a few lines of code)
so, here is the code i use to set each player it’s part in the screen (for one to four players, you just have to call this function before you draw your player X stuff).
private void SetupViewport() { // 1 player if (m_raceScreen.InitDatas.TotalPlayerCount == 0) return; // don't touch viewport, we're obviously the only player :) Viewport v = new Viewport(); v.X = 0; v.Y = 0; v.Width = XeGame.Device.PresentationParameters.BackBufferWidth; v.Height = XeGame.Device.PresentationParameters.BackBufferHeight; #region 2 players if (m_raceScreen.InitDatas.TotalPlayerCount == 1) // 2 player { if (v.Height > v.Width) // cut height in 2 { v.Height = v.Height / 2; if (this.m_playerIndex == PlayerIndex.One) { v.Y = 0; } if (this.m_playerIndex == PlayerIndex.Two) { v.Y = v.Height; } } else { v.Width = v.Width / 2; if (this.m_playerIndex == PlayerIndex.One) { v.X = 0; } if (this.m_playerIndex == PlayerIndex.Two) { v.X = v.Width; } } } #endregion #region 3 players if (m_raceScreen.InitDatas.TotalPlayerCount == 2) // 3 player { if (v.Height > v.Width) { v.Height = v.Height / 3; if (this.m_playerIndex == PlayerIndex.One) v.Y = 0; if (this.m_playerIndex == PlayerIndex.Two) v.Y = v.Height; if (this.m_playerIndex == PlayerIndex.Three) v.Y = v.Height * 2; } else // standard case (4/3, 16/9, 16/10) { v.Width = v.Width / 2; if (this.m_playerIndex == PlayerIndex.One) { v.Y = 0; v.X = 0; } if (this.m_playerIndex == PlayerIndex.Two) { v.Height = v.Height / 2; v.X = v.Width; } if (this.m_playerIndex == PlayerIndex.Three) { v.Height = v.Height / 2; v.X = v.Width; v.Y = v.Height; } } } #endregion #region 4 players if (m_raceScreen.InitDatas.TotalPlayerCount == 3) // 4 player { if (v.Height > v.Width) { v.Height = v.Height / 4; if (this.m_playerIndex == PlayerIndex.One) v.Y = 0; if (this.m_playerIndex == PlayerIndex.Two) v.Y = v.Height; if (this.m_playerIndex == PlayerIndex.Three) v.Y = v.Height * 2; if (this.m_playerIndex == PlayerIndex.Four) v.Y = v.Height * 3; } else // standard case { v.Width = v.Width / 2; v.Height = v.Height / 2; if (this.m_playerIndex == PlayerIndex.One) { v.Y = 0; v.X = 0; } if (this.m_playerIndex == PlayerIndex.Two) { v.Y = 0; v.X = v.Width; } if (this.m_playerIndex == PlayerIndex.Three) { v.X = 0; v.Y = v.Height; } if (this.m_playerIndex == PlayerIndex.Four) { v.X = v.Width; v.Y = v.Height; } } } #endregion XeGame.Device.Viewport = v; }
Actually, a lot of work have been made when i see the screenshots i took 3 months ago. (Sun is now a big heap of particles, models have been smoothed a lot, physics are almost in place, post scree processing engine have been integrated, and the SceneRenderManager is almost ready, and will makes render a lot simpler)