From bb295a744462d655fcfa22a06ab309cbd4c0cff6 Mon Sep 17 00:00:00 2001 From: joonas Date: Wed, 19 Aug 2015 23:34:20 +0300 Subject: [PATCH] Modified code to compile on Linux --- .../Source/EventInput/KeyboardDispatcher.cs | 7 +++ Subsurface/Source/GUI/GUI.cs | 2 +- Subsurface/Source/GUI/GUIButton.cs | 2 +- Subsurface/Source/GUI/GUIListBox.cs | 2 +- Subsurface/Source/GUI/GUIScrollBar.cs | 4 +- Subsurface/Source/GUI/GUITextBox.cs | 2 +- Subsurface/Source/GUI/GUITickBox.cs | 2 +- Subsurface/Source/Game1.cs | 7 +-- Subsurface/Source/Map/Hull.cs | 2 +- Subsurface/Source/Map/Lights/ConvexHull.cs | 5 ++- Subsurface/Source/Map/WaterRenderer.cs | 11 ++++- Subsurface/Source/Networking/GameServer.cs | 1 - Subsurface/Source/PlayerInput.cs | 13 +++++- Subsurface/Source/Properties.cs | 1 + Subsurface/Source/Screens/GameScreen.cs | 2 +- Subsurface/Source/Screens/MainMenu.cs | 2 +- Subsurface/Source/Sounds/SoundManager.cs | 45 ++++++++++--------- Subsurface/Source/Utils/TextureLoader.cs | 13 +++++- 18 files changed, 82 insertions(+), 41 deletions(-) diff --git a/Subsurface/Source/EventInput/KeyboardDispatcher.cs b/Subsurface/Source/EventInput/KeyboardDispatcher.cs index 9427af6d5..3026824bc 100644 --- a/Subsurface/Source/EventInput/KeyboardDispatcher.cs +++ b/Subsurface/Source/EventInput/KeyboardDispatcher.cs @@ -1,6 +1,8 @@ using System; using System.Threading; +#if WINDOWS using System.Windows; +#endif using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; @@ -42,12 +44,14 @@ namespace EventInput //ctrl-v if (e.Character == 0x16) { +#if WINDOWS //XNA runs in Multiple Thread Apartment state, which cannot recieve clipboard Thread thread = new Thread(PasteThread); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); _subscriber.ReceiveTextInput(_pasteResult); +#endif } else { @@ -74,6 +78,7 @@ namespace EventInput } } +#if WINDOWS //Thread has to be in Single Thread Apartment state in order to receive clipboard string _pasteResult = ""; [STAThread] @@ -81,5 +86,7 @@ namespace EventInput { _pasteResult = Clipboard.ContainsText() ? Clipboard.GetText() : ""; } +#endif + } } diff --git a/Subsurface/Source/GUI/GUI.cs b/Subsurface/Source/GUI/GUI.cs index 37732dc5d..c0a0bfd7e 100644 --- a/Subsurface/Source/GUI/GUI.cs +++ b/Subsurface/Source/GUI/GUI.cs @@ -274,7 +274,7 @@ namespace Subsurface bool clicked = false; - if (rect.Contains(PlayerInput.GetMouseState.Position)) + if (rect.Contains(PlayerInput.MousePosition)) { clicked = (PlayerInput.GetMouseState.LeftButton == ButtonState.Pressed); diff --git a/Subsurface/Source/GUI/GUIButton.cs b/Subsurface/Source/GUI/GUIButton.cs index 0eacc727d..bb238ba04 100644 --- a/Subsurface/Source/GUI/GUIButton.cs +++ b/Subsurface/Source/GUI/GUIButton.cs @@ -60,7 +60,7 @@ namespace Subsurface public override void Draw(SpriteBatch spriteBatch) { - if (rect.Contains(PlayerInput.GetMouseState.Position) && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn))) + if (rect.Contains(PlayerInput.MousePosition) && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn))) { state = ComponentState.Hover; if (PlayerInput.GetMouseState.LeftButton == ButtonState.Pressed) diff --git a/Subsurface/Source/GUI/GUIListBox.cs b/Subsurface/Source/GUI/GUIListBox.cs index 97784086a..56e882201 100644 --- a/Subsurface/Source/GUI/GUIListBox.cs +++ b/Subsurface/Source/GUI/GUIListBox.cs @@ -284,7 +284,7 @@ namespace Subsurface if (CheckSelected() != selected.UserData) selected = null; } } - else if (enabled && (MouseOn == this || (MouseOn != null && this.IsParentOf(MouseOn))) && child.Rect.Contains(PlayerInput.GetMouseState.Position)) + else if (enabled && (MouseOn == this || (MouseOn != null && this.IsParentOf(MouseOn))) && child.Rect.Contains(PlayerInput.MousePosition)) { child.State = ComponentState.Hover; if (PlayerInput.LeftButtonClicked()) diff --git a/Subsurface/Source/GUI/GUIScrollBar.cs b/Subsurface/Source/GUI/GUIScrollBar.cs index 0ab9663d7..b654948c5 100644 --- a/Subsurface/Source/GUI/GUIScrollBar.cs +++ b/Subsurface/Source/GUI/GUIScrollBar.cs @@ -155,14 +155,14 @@ namespace Subsurface int moveAmount; if (isHorizontal) { - moveAmount = PlayerInput.GetMouseState.Position.X - PlayerInput.GetOldMouseState.Position.X; + moveAmount = (int)PlayerInput.MouseSpeed.X; newX = Math.Min(Math.Max(newX + moveAmount, 0), frame.Rect.Width - bar.Rect.Width); barScroll = (float)newX / ((float)frame.Rect.Width - (float)bar.Rect.Width); } else { - moveAmount = PlayerInput.GetMouseState.Position.Y - PlayerInput.GetOldMouseState.Position.Y; + moveAmount = (int)PlayerInput.MouseSpeed.Y; newY = Math.Min(Math.Max(newY+moveAmount, 0), frame.Rect.Height - bar.Rect.Height); barScroll = (float)newY / ((float)frame.Rect.Height - (float)bar.Rect.Height); diff --git a/Subsurface/Source/GUI/GUITextBox.cs b/Subsurface/Source/GUI/GUITextBox.cs index fcebd9b22..74a4ca8d3 100644 --- a/Subsurface/Source/GUI/GUITextBox.cs +++ b/Subsurface/Source/GUI/GUITextBox.cs @@ -158,7 +158,7 @@ namespace Subsurface caretTimer += deltaTime; caretVisible = ((caretTimer*1000.0f) % 1000) < 500; - if (rect.Contains(PlayerInput.GetMouseState.Position)) + if (rect.Contains(PlayerInput.MousePosition)) { state = ComponentState.Hover; if (PlayerInput.LeftButtonClicked()) Select(); diff --git a/Subsurface/Source/GUI/GUITickBox.cs b/Subsurface/Source/GUI/GUITickBox.cs index a36d02855..0564b2bb5 100644 --- a/Subsurface/Source/GUI/GUITickBox.cs +++ b/Subsurface/Source/GUI/GUITickBox.cs @@ -56,7 +56,7 @@ namespace Subsurface if (!Enabled) return; - if (box.Rect.Contains(PlayerInput.GetMouseState.Position)) + if (box.Rect.Contains(PlayerInput.MousePosition)) { diff --git a/Subsurface/Source/Game1.cs b/Subsurface/Source/Game1.cs index 1065441fc..d4bcb9aff 100644 --- a/Subsurface/Source/Game1.cs +++ b/Subsurface/Source/Game1.cs @@ -10,6 +10,7 @@ using Subsurface.Particles; using System.Collections; using System.Collections.Generic; using Microsoft.Xna.Framework.Input; +using System.Xml; namespace Subsurface { @@ -119,8 +120,8 @@ namespace Subsurface //TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 55); World = new World(new Vector2(0, -9.82f)); - Settings.VelocityIterations = 2; - Settings.PositionIterations = 1; + FarseerPhysics.Settings.VelocityIterations = 2; + FarseerPhysics.Settings.PositionIterations = 1; } /// @@ -324,4 +325,4 @@ namespace Subsurface } } -} +} \ No newline at end of file diff --git a/Subsurface/Source/Map/Hull.cs b/Subsurface/Source/Map/Hull.cs index 3c77ad2b6..f95c6a561 100644 --- a/Subsurface/Source/Map/Hull.cs +++ b/Subsurface/Source/Map/Hull.cs @@ -156,7 +156,7 @@ namespace Subsurface public int GetWaveIndex(Vector2 position) { int index = (int)(position.X - rect.X) / WaveWidth; - index = MathHelper.Clamp(index, 0, waveY.Length-1); + index = (int)MathHelper.Clamp(index, 0, waveY.Length-1); return index; } diff --git a/Subsurface/Source/Map/Lights/ConvexHull.cs b/Subsurface/Source/Map/Lights/ConvexHull.cs index 3de439e67..364b82ad3 100644 --- a/Subsurface/Source/Map/Lights/ConvexHull.cs +++ b/Subsurface/Source/Map/Lights/ConvexHull.cs @@ -245,7 +245,10 @@ namespace Subsurface.Lights * Matrix.CreateOrthographic(Game1.GraphicsWidth, Game1.GraphicsHeight, -1, 1) * 0.5f; shadowEffect.CurrentTechnique.Passes[0].Apply(); - graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, penumbraVertices, 0, 2, VertexPositionTexture.VertexDeclaration); +#if WINDOWS + graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, penumbraVertices, 0, 2, VertexPositionTexture.VertexDeclaration); + +#endif } } diff --git a/Subsurface/Source/Map/WaterRenderer.cs b/Subsurface/Source/Map/WaterRenderer.cs index 46da2f6d5..37ca73c25 100644 --- a/Subsurface/Source/Map/WaterRenderer.cs +++ b/Subsurface/Source/Map/WaterRenderer.cs @@ -109,7 +109,12 @@ namespace Subsurface { pass.Apply(); - graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verts, 0, verts.Length / 3, WaterVertex.VertexDeclaration); +#if WINDOWS + graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verts, 0, verts.Length / 3, WaterVertex.VertexDeclaration); +#endif +#if LINUX + //graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verts, 0, verts.Length / 3, WaterVertex.VertexDeclaration, ); +#endif } } @@ -130,7 +135,9 @@ namespace Subsurface { pass.Apply(); - graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3, WaterVertex.VertexDeclaration); +#if WINDOWS + graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3, WaterVertex.VertexDeclaration); +#endif } } diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index 4472cbf9a..889076d4b 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -710,7 +710,6 @@ namespace Subsurface.Networking { spriteBatch.DrawString(GUI.SmallFont, c.name + ":", new Vector2(x + 10, y), Color.White); spriteBatch.DrawString(GUI.SmallFont, "- avg roundtrip " + c.Connection.AverageRoundtripTime+" s", new Vector2(x + 20, y + 15), Color.White); - spriteBatch.DrawString(GUI.SmallFont, "- current MTU " + c.Connection.CurrentMTU, new Vector2(x + 20, y + 30), Color.White); y += 50; } diff --git a/Subsurface/Source/PlayerInput.cs b/Subsurface/Source/PlayerInput.cs index 642be272c..aa3663114 100644 --- a/Subsurface/Source/PlayerInput.cs +++ b/Subsurface/Source/PlayerInput.cs @@ -69,7 +69,12 @@ namespace Subsurface public static Vector2 MousePosition { +#if WINDOWS get { return new Vector2(mouseState.Position.X, mouseState.Position.Y); } +#endif +#if LINUX + get { return new Vector2(mouseState.Y, mouseState.X); } +#endif } public static MouseState GetMouseState @@ -85,8 +90,12 @@ namespace Subsurface { get { - Point speed = mouseState.Position - oldMouseState.Position; - return new Vector2(speed.X, speed.Y); +#if WINDOWS + return mouseState.Position - oldMouseState.Position; +#endif +#if LINUX + return MousePosition - new Vector2(oldMouseState.X, oldMouseState.Y); +#endif } } diff --git a/Subsurface/Source/Properties.cs b/Subsurface/Source/Properties.cs index 3b4d10fef..c985ce848 100644 --- a/Subsurface/Source/Properties.cs +++ b/Subsurface/Source/Properties.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.Linq; using System.Xml.Linq; + namespace Subsurface { [AttributeUsage(AttributeTargets.Property)] diff --git a/Subsurface/Source/Screens/GameScreen.cs b/Subsurface/Source/Screens/GameScreen.cs index 366965bb4..27af439f4 100644 --- a/Subsurface/Source/Screens/GameScreen.cs +++ b/Subsurface/Source/Screens/GameScreen.cs @@ -146,7 +146,7 @@ namespace Subsurface graphics.Clear(new Color(11, 18, 26, 255)); - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearWrap); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone); Vector2 backgroundPos = cam.Position; if (Level.Loaded != null) backgroundPos -= Level.Loaded.Position; diff --git a/Subsurface/Source/Screens/MainMenu.cs b/Subsurface/Source/Screens/MainMenu.cs index 237d7b75a..107da690a 100644 --- a/Subsurface/Source/Screens/MainMenu.cs +++ b/Subsurface/Source/Screens/MainMenu.cs @@ -191,7 +191,7 @@ namespace Subsurface int currMaxPlayers = 10; int.TryParse(maxPlayersBox.Text, out currMaxPlayers); - currMaxPlayers = MathHelper.Clamp(currMaxPlayers+(int)button.UserData, 1, 10); + currMaxPlayers = (int)MathHelper.Clamp(currMaxPlayers+(int)button.UserData, 1, 10); maxPlayersBox.Text = currMaxPlayers.ToString(); diff --git a/Subsurface/Source/Sounds/SoundManager.cs b/Subsurface/Source/Sounds/SoundManager.cs index ef705ba62..345e7009d 100644 --- a/Subsurface/Source/Sounds/SoundManager.cs +++ b/Subsurface/Source/Sounds/SoundManager.cs @@ -1,8 +1,11 @@ using System.Collections.Generic; using System.Diagnostics; using Microsoft.Xna.Framework; -using OpenTK.Audio; +#if WINDOWS using OpenTK.Audio.OpenAL; +#endif +using OpenTK.Audio; + using System; namespace Subsurface.Sounds @@ -34,14 +37,14 @@ namespace Subsurface.Sounds for (int i = 0 ; i < DefaultSourceCount; i++) { - alSources.Add(AL.GenSource()); + alSources.Add(OpenTK.Audio.OpenAL.AL.GenSource()); } if (ALHelper.Efx.IsInitialized) { lowpassFilterId = ALHelper.Efx.GenFilter(); //alFilters.Add(alFilterId); - ALHelper.Efx.Filter(lowpassFilterId, EfxFilteri.FilterType, (int)EfxFilterType.Lowpass); + ALHelper.Efx.Filter(lowpassFilterId, OpenTK.Audio.OpenAL.EfxFilteri.FilterType, (int)OpenTK.Audio.OpenAL.EfxFilterType.Lowpass); //LowPassHFGain = 1; } @@ -129,29 +132,29 @@ namespace Subsurface.Sounds for (int i = 1; i < DefaultSourceCount; i++) { //find a source that's free to use (not playing or paused) - if (AL.GetSourceState(alSources[i]) == ALSourceState.Playing - || AL.GetSourceState(alSources[i]) == ALSourceState.Paused) continue; + if (OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Playing + || OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Paused) continue; //if (position!=Vector2.Zero) // position /= 1000.0f; alBuffers[i] = sound.AlBufferId; - AL.Source(alSources[i], ALSourceb.Looping, false); + OpenTK.Audio.OpenAL.AL.Source(alSources[i], OpenTK.Audio.OpenAL.ALSourceb.Looping, false); position /= 1000.0f; //System.Diagnostics.Debug.WriteLine("updatesoundpos: "+offset); - AL.Source(alSources[i], ALSourcef.Gain, volume); - AL.Source(alSources[i], ALSource3f.Position, position.X, position.Y, 0.0f); + OpenTK.Audio.OpenAL.AL.Source(alSources[i], OpenTK.Audio.OpenAL.ALSourcef.Gain, volume); + OpenTK.Audio.OpenAL.AL.Source(alSources[i], OpenTK.Audio.OpenAL.ALSource3f.Position, position.X, position.Y, 0.0f); - AL.Source(alSources[i], ALSourcei.Buffer, sound.AlBufferId); + OpenTK.Audio.OpenAL.AL.Source(alSources[i], OpenTK.Audio.OpenAL.ALSourcei.Buffer, sound.AlBufferId); - ALHelper.Efx.Filter(lowpassFilterId, EfxFilterf.LowpassGainHF, lowPassHfGain = Math.Min(lowPassGain, overrideLowPassGain)); + ALHelper.Efx.Filter(lowpassFilterId, OpenTK.Audio.OpenAL.EfxFilterf.LowpassGainHF, lowPassHfGain = Math.Min(lowPassGain, overrideLowPassGain)); ALHelper.Efx.BindFilterToSource(alSources[i], lowpassFilterId); ALHelper.Check(); //AL.Source(alSources[i], ALSource3f.Position, position.X, position.Y, 0.0f); - AL.SourcePlay(alSources[i]); + OpenTK.Audio.OpenAL.AL.SourcePlay(alSources[i]); //sound.sourceIndex = i; @@ -261,10 +264,10 @@ namespace Subsurface.Sounds for (int i = 0; i < DefaultSourceCount; i++) { //find a source that's free to use (not playing or paused) - if (AL.GetSourceState(alSources[i]) != ALSourceState.Playing - && AL.GetSourceState(alSources[i])!= ALSourceState.Paused) continue; + if (OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) != OpenTK.Audio.OpenAL.ALSourceState.Playing + && OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i])!= OpenTK.Audio.OpenAL.ALSourceState.Paused) continue; - ALHelper.Efx.Filter(lowpassFilterId, EfxFilterf.LowpassGainHF, lowPassHfGain = value); + ALHelper.Efx.Filter(lowpassFilterId, OpenTK.Audio.OpenAL.EfxFilterf.LowpassGainHF, lowPassHfGain = value); ALHelper.Efx.BindFilterToSource(alSources[i], lowpassFilterId); ALHelper.Check(); } @@ -293,10 +296,10 @@ namespace Subsurface.Sounds position/= 1000.0f; //System.Diagnostics.Debug.WriteLine("updatesoundpos: "+offset); - AL.Source(alSources[sourceIndex], ALSourcef.Gain, baseVolume); - AL.Source(alSources[sourceIndex], ALSource3f.Position, position.X, position.Y, 0.0f); + OpenTK.Audio.OpenAL.AL.Source(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSourcef.Gain, baseVolume); + OpenTK.Audio.OpenAL.AL.Source(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSource3f.Position, position.X, position.Y, 0.0f); - ALHelper.Efx.Filter(lowpassFilterId, EfxFilterf.LowpassGainHF, lowPassHfGain = Math.Min(lowPassGain, overrideLowPassGain)); + ALHelper.Efx.Filter(lowpassFilterId, OpenTK.Audio.OpenAL.EfxFilterf.LowpassGainHF, lowPassHfGain = Math.Min(lowPassGain, overrideLowPassGain)); ALHelper.Efx.BindFilterToSource(alSources[sourceIndex], lowpassFilterId); ALHelper.Check(); } @@ -328,7 +331,7 @@ namespace Subsurface.Sounds { if (alBuffers[i] == bufferId) { - AL.Source(alSources[i], ALSourcei.Buffer, 0); + OpenTK.Audio.OpenAL.AL.Source(alSources[i], OpenTK.Audio.OpenAL.ALSourcei.Buffer, 0); } } @@ -343,11 +346,11 @@ namespace Subsurface.Sounds for (int i = 0; i < DefaultSourceCount; i++) { - var state = AL.GetSourceState(alSources[i]); - if (state == ALSourceState.Playing || state == ALSourceState.Paused) + var state = OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]); + if (state == OpenTK.Audio.OpenAL.ALSourceState.Playing || state == OpenTK.Audio.OpenAL.ALSourceState.Paused) Stop(i); - AL.DeleteSource(alSources[i]); + OpenTK.Audio.OpenAL.AL.DeleteSource(alSources[i]); ALHelper.Check(); } diff --git a/Subsurface/Source/Utils/TextureLoader.cs b/Subsurface/Source/Utils/TextureLoader.cs index a4853d54a..d224204e2 100644 --- a/Subsurface/Source/Utils/TextureLoader.cs +++ b/Subsurface/Source/Utils/TextureLoader.cs @@ -4,6 +4,7 @@ using System.IO; using Microsoft.Xna.Framework.Graphics; using Color = Microsoft.Xna.Framework.Color; using System; +using Microsoft.Xna.Framework; namespace Subsurface { @@ -44,8 +45,15 @@ namespace Subsurface { try { +#if WINDOWS using (Stream fileStream = File.OpenRead(path)) return FromStream(fileStream, preMultiplyAlpha); +#endif +#if LINUX + using (Stream fileStream = File.OpenRead(path)) + return Texture2D.FromFile(_graphicsDevice, fileStream);// .FromStream(fileStream, preMultiplyAlpha); +#endif + } catch (Exception e) { @@ -55,7 +63,8 @@ namespace Subsurface } - public Texture2D FromStream(Stream stream, bool preMultiplyAlpha = true) +#if WINDOWS + private Texture2D FromStream(Stream stream, bool preMultiplyAlpha = true) { Texture2D texture; @@ -114,6 +123,8 @@ namespace Subsurface return texture; } +#endif + private static readonly BlendState BlendColorBlendState; private static readonly BlendState BlendAlphaBlendState;