/// <summary>Loads a texture and returns the texture data.</summary> /// <param name="path">The path to the file or folder that contains the texture.</param> /// <param name="parameters">The parameters that specify how to process the texture.</param> /// <param name="texture">Receives the texture.</param> /// <returns>Whether loading the texture was successful.</returns> public override bool LoadTexture(string path, OpenBveApi.Textures.TextureParameters parameters, out OpenBveApi.Textures.Texture texture) { if (System.IO.File.Exists(path) || System.IO.Directory.Exists(path)) { for (int i = 0; i < Plugins.LoadedPlugins.Length; i++) { if (Plugins.LoadedPlugins[i].Texture != null) { try { if (Plugins.LoadedPlugins[i].Texture.CanLoadTexture(path)) { try { if (!Plugins.LoadedPlugins[i].Texture.LoadTexture(path, out texture)) { texture = null; return false; } texture = texture.ApplyParameters(parameters); return true; } catch (Exception ex) { Program.AppendToLogFile("Plugin " + Plugins.LoadedPlugins[i].Title + " raised the following exception at LoadTexture:" + ex.Message); } } } catch (Exception ex) { Program.AppendToLogFile("Plugin " + Plugins.LoadedPlugins[i].Title + " raised the following exception at CanLoadTexture:" + ex.Message); } } } Program.AppendToLogFile("No plugin found that is capable of loading texture " + path.ToString()); } else { ReportProblem(OpenBveApi.Hosts.ProblemType.PathNotFound, path.ToString()); } texture = null; return false; }
// --- sound --- /// <summary>Loads a sound and returns the sound data.</summary> /// <param name="path">The path to the file or folder that contains the sound.</param> /// <param name="sound">Receives the sound.</param> /// <returns>Whether loading the sound was successful.</returns> public override bool LoadSound(string path, out OpenBveApi.Sounds.Sound sound) { if (System.IO.File.Exists(path) || System.IO.Directory.Exists(path)) { for (int i = 0; i < Plugins.LoadedPlugins.Length; i++) { if (Plugins.LoadedPlugins[i].Sound != null) { try { if (Plugins.LoadedPlugins[i].Sound.CanLoadSound(path)) { try { if (!Plugins.LoadedPlugins[i].Sound.LoadSound(path, out sound)) { sound = null; return false; } return true; } catch (Exception ex) { Program.AppendToLogFile("Plugin " + Plugins.LoadedPlugins[i].Title + " raised the following exception at LoadSound:" + ex.Message); } } } catch (Exception ex) { Program.AppendToLogFile("Plugin " + Plugins.LoadedPlugins[i].Title + " raised the following exception at CanLoadSound:" + ex.Message); } } } Program.AppendToLogFile("No plugin found that is capable of loading sound " + path.ToString()); } else { ReportProblem(OpenBveApi.Hosts.ProblemType.PathNotFound, path.ToString()); } sound = null; return false; }
// --- functions --- /// <summary>Gets the texture from this origin.</summary> /// <param name="texture">Receives the texture.</param> /// <returns>Whether the texture could be obtained successfully.</returns> internal override bool GetTexture(out OpenBveApi.Textures.Texture texture){ if (!Program.CurrentHost.LoadTexture(this.Path, this.Parameters, out texture)) { texture = null; return false; } return true; }
// --- functions --- /// <summary>Gets the sound from this origin.</summary> /// <param name="sound">Receives the sound.</param> /// <returns>Whether the sound could be obtained successfully.</returns> internal override bool GetSound(out OpenBveApi.Sounds.Sound sound) { if (!Program.CurrentHost.LoadSound(this.Path, out sound)) { sound = null; return false; } return true; }
public override void elapse(OpenBveApi.Runtime.ElapseData data) { globalTime = data.TotalTime; if (closedSignal && (globalTime.Milliseconds - time.Milliseconds) > tempo) handleManager.applyEmergencyBrake(); }
private static void Shear(MeshBuilder builder, OpenBveApi.Math.Vector3 direction, OpenBveApi.Math.Vector3 shift, double ratio) { for (int i = 0; i < builder.Vertices.Length; i++) { double factor = ratio * OpenBveApi.Math.Vector3.Dot(builder.Vertices[i].SpatialCoordinates, direction); builder.Vertices[i].SpatialCoordinates += shift * factor; if (!builder.Vertices[i].Normal.IsNullVector()) { factor = ratio * OpenBveApi.Math.Vector3.Dot(builder.Vertices[i].Normal, shift); builder.Vertices[i].Normal -= direction * factor; if (!builder.Vertices[i].Normal.IsNullVector()) { builder.Vertices[i].Normal.Normalize(); } } } for (int i = 0; i < builder.FaceCount; i++) { for (int j = 0; j < builder.Faces[i].Vertices.Length; j++) { if (!builder.Faces[i].Vertices[j].Normal.IsNullVector()) { double factor = ratio * OpenBveApi.Math.Vector3.Dot(builder.Faces[i].Vertices[j].Normal, shift); builder.Faces[i].Vertices[j].Normal -= direction * factor; if (!builder.Faces[i].Vertices[j].Normal.IsNullVector()) { builder.Faces[i].Vertices[j].Normal.Normalize(); } } } } }
internal SoundBuffer(OpenBveApi.Sounds.Sound sound, double radius) { this.Origin = new RawOrigin(sound); this.Radius = radius; this.Loaded = false; this.OpenAlBufferName = 0; this.Duration = 0.0; this.Ignore = false; }
public void elapse(ref OpenBveApi.Runtime.ElapseData data) { if (applyHandles || emergencyBrake) { data.Handles = this.handles; applyHandles = false; } }
public TrainControlManager(ref OpenBveApi.Runtime.LoadProperties properties) { controlBlinking = new List<int>(); lastTime = new OpenBveApi.Runtime.Time(0); panel = new int[cabControls.count]; for (int i=0; i<cabControls.count; i++) panel[i] = 0; properties.Panel = panel; }
/// <summary>Registeres a texture and returns a handle to the texture.</summary> /// <param name="texture">The texture data.</param> /// <returns>The handle to the texture.</returns> internal static Texture RegisterTexture(OpenBveApi.Textures.Texture texture) { /* * Register the texture and return the newly created handle. * */ if (RegisteredTextures.Length == RegisteredTexturesCount) { Array.Resize<Texture>(ref RegisteredTextures, RegisteredTextures.Length << 1); } RegisteredTextures[RegisteredTexturesCount] = new Texture(texture); RegisteredTexturesCount++; return RegisteredTextures[RegisteredTexturesCount - 1]; }
/// <summary>Creates a new instance of an object at a specified position with a specified orietation.</summary> /// <param name="handle">The handle to the object.</param> /// <param name="position">The position of the object.</param> /// <param name="orientation">The orientation of the object.</param> /// <returns>The success of the operation.</returns> public General.Result CreateObject(Geometry.ObjectHandle handle, OpenBveApi.Math.Vector3 position, OpenBveApi.Math.Orientation3 orientation) { // TODO: This operation should not be allowed at runtime. ObjectLibrary.ApiHandle apiHandle = handle as ObjectLibrary.ApiHandle; if (apiHandle != null) { ObjectGrid.Grid.Add(apiHandle.LibraryIndex, position, orientation); return General.Result.Successful; } else { return General.Result.InvalidArgument; } }
// --- constructors --- /// <summary>Creates a new sound source.</summary> /// <param name="buffer">The sound buffer.</param> /// <param name="radius">The effective sound radius.</param> /// <param name="pitch">The pitch change factor.</param> /// <param name="volume">The volume change factor.</param> /// <param name="position">The position. If a train and car are specified, the position is relative to the car, otherwise absolute.</param> /// <param name="train">The train this sound source is attached to, or a null reference.</param> /// <param name="car">The car this sound source is attached to, or a null reference.</param> /// <param name="looped">Whether this sound source plays in a loop.</param> internal SoundSource(SoundBuffer buffer, double radius, double pitch, double volume, OpenBveApi.Math.Vector3D position, TrainManager.Train train, int car, bool looped) { this.Buffer = buffer; this.Radius = radius; this.Pitch = pitch; this.Volume = volume; this.Position = position; this.Train = train; this.Car = car; this.Looped = looped; this.State = SoundSourceState.PlayPending; this.OpenAlSourceName = 0; }
public void elapse(OpenBveApi.Runtime.ElapseData data) { globalTime = data.TotalTime; if (globalTime.Milliseconds - lastTime.Milliseconds > halfBlinkingPeriod) { controlBlinking.ForEach(delegate (int control) { if (getState(control) == 1) setState(control, 0); else setState(control, 1); }); lastTime = globalTime; } }
// extract clip region private static void ExtractClipRegion(ref int width, ref int height, ref byte[] raw, OpenBveApi.Texture.TextureClipRegion region) { if (region != null) { int clipLeft = region.Left; int clipTop = region.Top; int clipWidth = region.Width; int clipHeight = region.Height; if (clipLeft < 0) { clipLeft = 0; } if (clipTop < 0) { clipTop = 0; } if (clipWidth < 0) { clipWidth = 0; } else if (clipWidth > width - clipLeft) { clipWidth = width - clipLeft; } if (clipHeight < 0) { clipHeight = 0; } else if (clipHeight > height - clipTop) { clipHeight = height - clipTop; } if ( region.Left != 0 | region.Top != 0 | region.Width != width | region.Height != height ) { byte[] clip = new byte[4 * clipWidth * clipHeight]; int i = 0; int j = 4 * clipTop * width + 4 * clipLeft; int jSkip = 4 * (width - clipWidth); for (int y = 0; y < clipHeight; y++) { for (int x = 0; x < clipWidth; x++) { clip[i + 0] = raw[j + 0]; clip[i + 1] = raw[j + 1]; clip[i + 2] = raw[j + 2]; clip[i + 3] = raw[j + 3]; i += 4; j += 4; } j += jSkip; } raw = clip; width = clipWidth; height = clipHeight; } } }
private static void Shear(OpenBveApi.Geometry.FaceVertexMesh mesh, OpenBveApi.Math.Vector3 direction, OpenBveApi.Math.Vector3 shift, double ratio) { for (int i = 0; i < mesh.Vertices.Length; i++) { double factor = ratio * OpenBveApi.Math.Vector3.Dot(mesh.Vertices[i].SpatialCoordinates, direction); mesh.Vertices[i].SpatialCoordinates += shift * factor; if (!mesh.Vertices[i].Normal.IsNullVector()) { factor = ratio * OpenBveApi.Math.Vector3.Dot(mesh.Vertices[i].Normal, shift); mesh.Vertices[i].Normal -= direction * factor; if (mesh.Vertices[i].Normal.IsNullVector()) { mesh.Vertices[i].Normal = OpenBveApi.Math.Vector3.Up; } else { mesh.Vertices[i].Normal.Normalize(); } } } }
// eliminate transparent color private static void EliminateTransparentColor(int width, int height, ref byte[] raw, OpenBveApi.Color.TransparentColor color) { if (color.Assigned) { // TODO: Replace this algorithm by a higher quality one. for (int i = 0; i < raw.Length; i += 4) { if ( raw[i + 0] == color.R & raw[i + 1] == color.G & raw[i + 2] == color.B ) { raw[i + 0] = 128; raw[i + 1] = 128; raw[i + 2] = 128; raw[i + 3] = 0; } } } }
/* * Helper * ------ * * This file contains helper functions that are called from Textures.cs. * * TODO: Not all functions in this file have been completely implemented yet. * TODO: The XML documentation in this file is still incomplete. * * */ // convert to 8 bits per channel private static void ConvertTo8BitsPerChannel(OpenBveApi.Texture.TextureData texture, out byte[] raw) { switch(texture.Format.BitsPerChannel) { case 8: raw = new byte[texture.Bytes.Length]; for (int i = 0; i < raw.Length; i++) { raw[i] = texture.Bytes[i]; } break; case 16: raw = new byte[texture.Bytes.Length >> 1]; for (int i = 0; i < raw.Length; i++) { raw[i] = texture.Bytes[i << 1]; } break; default: throw new InvalidOperationException(); } }
public override void elapse(OpenBveApi.Runtime.ElapseData data) { if (enabled) { currentSpeed = data.Vehicle.Speed; location = (int) Math.Round(data.Vehicle.Location); Speed limit = speedLimiter.getCurrentSpeedLimit(data); if (data.Vehicle.Speed.KilometersPerHour > limit.KilometersPerHour + firstMargin) { soundManager.startSound(ref beepBeep, SoundIndex.KvbOverSpeed); if (data.Vehicle.Speed.KilometersPerHour > limit.KilometersPerHour + secondMargin) stopTrain(); } else soundManager.stopSound(ref beepBeep); } }
// --- static functions --- /// <summary>Transforms a FormCL, FormCR, RoofCL, RoofCR or Crack object.</summary> /// <param name="obj">The object to transform.</param> /// <param name="nearDistance">The signed distance from the first rail to the second rail at the beginning of the block.</param> /// <param name="farDistance">The signed distance from the first rail to the second rail at the end of the block.</param> /// <returns>The transformed object.</returns> /// <remarks>Only objects of type OpenBveApi.Geometry.FaceVertexMesh are supported. If other objects are passed, they are returned unmodified.</remarks> private static OpenBveApi.Geometry.GenericObject GetTransformedObject(OpenBveApi.Geometry.GenericObject obj, double nearDistance, double farDistance) { if (obj is OpenBveApi.Geometry.FaceVertexMesh) { OpenBveApi.Geometry.FaceVertexMesh mesh = (OpenBveApi.Geometry.FaceVertexMesh)obj.Clone(); double x2 = 0.0, x3 = 0.0, x6 = 0.0, x7 = 0.0; for (int i = 0; i < mesh.Vertices.Length; i++) { switch (mesh.Vertices[i].Tag) { case 2: x2 = mesh.Vertices[i].SpatialCoordinates.X; break; case 3: x3 = mesh.Vertices[i].SpatialCoordinates.X; break; case 6: x6 = mesh.Vertices[i].SpatialCoordinates.X; break; case 7: x7 = mesh.Vertices[i].SpatialCoordinates.X; break; } } for (int i = 0; i < mesh.Vertices.Length; i++) { switch (mesh.Vertices[i].Tag) { case 0: mesh.Vertices[i].SpatialCoordinates.X = nearDistance - x3; break; case 1: mesh.Vertices[i].SpatialCoordinates.X = farDistance - x2; break; case 4: mesh.Vertices[i].SpatialCoordinates.X = nearDistance - x7; break; case 5: mesh.Vertices[i].SpatialCoordinates.X = nearDistance - x6; break; } } return (OpenBveApi.Geometry.GenericObject)mesh; } else { return obj; } }
// instance functions /// <summary>Adds an object to the library and returns the index at which the object is stored in the library.</summary> /// <param name="obj">The object to add to the library.</param> /// <returns>The index at which the object is stored in the library.</returns> internal int Add(OpenBveApi.Geometry.GenericObject obj) { if (obj is OpenBveApi.Geometry.FaceVertexMesh) { OpenBveApi.Geometry.FaceVertexMesh mesh = (OpenBveApi.Geometry.FaceVertexMesh)obj; if (Program.CurrentOptions.ObjectOptimization == 1) { mesh.Optimize(false); } else if (Program.CurrentOptions.ObjectOptimization == 2) { mesh.Optimize(true); } for (int i = 0; i < mesh.Faces.Length; i++) { switch (mesh.Faces[i].Type) { case OpenBveApi.Geometry.FaceType.Triangles: Triangles++; break; case OpenBveApi.Geometry.FaceType.TriangleStrip: TriangleStrips++; break; case OpenBveApi.Geometry.FaceType.TriangleFan: TriangleFans++; break; case OpenBveApi.Geometry.FaceType.Quads: Quads++; break; case OpenBveApi.Geometry.FaceType.QuadStrip: QuadStrips += (mesh.Faces[i].Vertices.Length - 2) / 2; break; case OpenBveApi.Geometry.FaceType.Polygon: Polygons++; break; } } } if (this.Objects.Length == this.ObjectCount) { Array.Resize<OpenBveApi.Geometry.GenericObject>(ref this.Objects, this.Objects.Length << 1); } int n = this.ObjectCount; this.Objects[n] = obj; this.ObjectCount++; return n; }
// --- constructors --- /// <summary>Creates a new sound source.</summary> /// <param name="buffer">The sound buffer.</param> /// <param name="radius">The effective sound radius.</param> /// <param name="pitch">The pitch change factor.</param> /// <param name="volume">The volume change factor.</param> /// <param name="position">The position. If a train and car are specified, the position is relative to the car, otherwise absolute.</param> /// <param name="train">The train this sound source is attached to, or a null reference.</param> /// <param name="car">The car this sound source is attached to, or a null reference.</param> /// <param name="looped">Whether this sound source plays in a loop.</param> internal SoundSource(SoundBuffer buffer, double radius, double pitch, double volume, OpenBveApi.Math.Vector3 position, TrainManager.Train train, int car, bool looped) { this.Buffer = buffer; this.Radius = radius; this.Pitch = pitch; this.Volume = volume; this.Position = position; this.Train = train; this.Car = car; this.Looped = looped; this.State = SoundSourceState.PlayPending; this.OpenAlSourceName = 0; //Set the sound type to undefined to use Michelle's original processing if (train != null) { this.Type = SoundType.TrainCar; } else { this.Type = SoundType.Undefined; } }
/// <summary>Registeres a texture and returns a handle to the texture.</summary> /// <param name="path">The path to the texture.</param> /// <param name="parameters">The parameters that specify how to process the texture.</param> /// <param name="handle">Receives a handle to the texture.</param> /// <returns>Whether registering the texture was successful.</returns> internal static bool RegisterTexture(string path, OpenBveApi.Textures.TextureParameters parameters, out Texture handle) { /* * Check if the texture is already registered. * If so, return the existing handle. * */ for (int i = 0; i < RegisteredTexturesCount; i++) { PathOrigin source = RegisteredTextures[i].Origin as PathOrigin; if (source != null && source.Path == path && source.Parameters == parameters) { handle = RegisteredTextures[i]; return true; } } /* * Register the texture and return the newly created handle. * */ if (RegisteredTextures.Length == RegisteredTexturesCount) { Array.Resize<Texture>(ref RegisteredTextures, RegisteredTextures.Length << 1); } RegisteredTextures[RegisteredTexturesCount] = new Texture(path, parameters); RegisteredTexturesCount++; handle = RegisteredTextures[RegisteredTexturesCount - 1]; return true; }
/// <summary>Loads a texture and returns the texture data.</summary> /// <param name="path">The path to the file or folder that contains the texture.</param> /// <param name="parameters">The parameters that specify how to process the texture.</param> /// <param name="texture">Receives the texture.</param> /// <returns>Whether loading the texture was successful.</returns> public override bool LoadTexture(string path, OpenBveApi.Textures.TextureParameters parameters, out OpenBveApi.Textures.Texture texture) { if (System.IO.File.Exists(path) || System.IO.Directory.Exists(path)) { for (int i = 0; i < Plugins.LoadedPlugins.Length; i++) { if (Plugins.LoadedPlugins[i].TextureLoaders.Length > 0) { try { for(int l = 0; l < Plugins.LoadedPlugins[i].TextureLoaders.Length; l++){ if (Plugins.LoadedPlugins[i].TextureLoaders[l].CanLoadTexture(path)) { try { if (Plugins.LoadedPlugins[i].TextureLoaders[l].LoadTexture(path, out texture)) { texture = texture.ApplyParameters(parameters); return true; } Debug.AddMessage(Debug.MessageType.Error, false, "Plugin " + Plugins.LoadedPlugins[i].Title + " returned unsuccessfully at LoadTexture" ); } catch (Exception ex) { Debug.AddMessage(Debug.MessageType.Error, false, "Plugin " + Plugins.LoadedPlugins[i].Title + " raised the following exception at LoadTexture:" + ex.Message ); } } } } catch (Exception ex) { Debug.AddMessage(Debug.MessageType.Error, false, "Plugin " + Plugins.LoadedPlugins[i].Title + " raised the following exception at CanLoadTexture:" + ex.Message ); } } } Debug.AddMessage(Debug.MessageType.Error, false, "No plugin found that is capable of loading texture " + path); } else { ReportProblem(OpenBveApi.Hosts.ProblemType.PathNotFound, path); } texture = null; return false; }
public TrainHandleManager(OpenBveApi.Runtime.VehicleSpecs specs) { this.specs = specs; handles = new OpenBveApi.Runtime.Handles(0,0,0,false); applyHandles = false; }
/// <summary>Registers a sound buffer and returns a handle to the buffer.</summary> /// <param name="data">The raw sound data.</param> /// <param name="radius">The default effective radius.</param> /// <returns>The handle to the sound buffer.</returns> internal static SoundBuffer RegisterBuffer(OpenBveApi.Sounds.Sound data, double radius) { if (Buffers.Length == BufferCount) { Array.Resize<SoundBuffer>(ref Buffers, Buffers.Length << 1); } Buffers[BufferCount] = new SoundBuffer(data, radius); BufferCount++; return Buffers[BufferCount - 1]; }
/// <summary>Plays a sound.</summary> /// <param name="buffer">The sound buffer.</param> /// <param name="pitch">The pitch change factor.</param> /// <param name="volume">The volume change factor.</param> /// <param name="position">The position. If a train and car are specified, the position is relative to the car, otherwise absolute.</param> /// <param name="train">The train the sound is attached to, or a null reference.</param> /// <param name="car">The car in the train the sound is attached to.</param> /// <param name="looped">Whether to play the sound in a loop.</param> /// <returns>The sound source.</returns> internal static SoundSource PlaySound(SoundBuffer buffer, double pitch, double volume, OpenBveApi.Math.Vector3 position, TrainManager.Train train, int car, bool looped) { if (Sources.Length == SourceCount) { Array.Resize<SoundSource>(ref Sources, Sources.Length << 1); } Sources[SourceCount] = new SoundSource(buffer, buffer.Radius, pitch, volume, position, train, car, looped); SourceCount++; return Sources[SourceCount - 1]; }
internal void SetEmissiveColor(OpenBveApi.Color.ColorRGB color) { if (color != this.EmissiveColor) { Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_EMISSION, new float[] { color.R, color.G, color.B, 1.0f }); this.EmissiveColor = color; } }
// --- functions --- /// <summary>Reports a problem to the host application.</summary> /// <param name="type">The type of problem that is reported.</param> /// <param name="text">The textual message that describes the problem.</param> public override void ReportProblem(OpenBveApi.Hosts.ProblemType type, string text) { Interface.AddMessage(Interface.MessageType.Error, false, text); }
/// <summary>Registers a texture and returns a handle to the texture.</summary> /// <param name="texture">The texture data.</param> /// <param name="parameters">The parameters that specify how to process the texture.</param> /// <param name="handle">Receives the handle to the texture.</param> /// <returns>Whether loading the texture was successful.</returns> public override bool RegisterTexture(OpenBveApi.Textures.Texture texture, OpenBveApi.Textures.TextureParameters parameters, out OpenBveApi.Textures.TextureHandle handle) { texture = texture.ApplyParameters(parameters); handle = Textures.RegisterTexture(texture); return true; }
/// <summary>Registers a texture and returns a handle to the texture.</summary> /// <param name="path">The path to the file or folder that contains the texture.</param> /// <param name="parameters">The parameters that specify how to process the texture.</param> /// <param name="handle">Receives the handle to the texture.</param> /// <returns>Whether loading the texture was successful.</returns> public override bool RegisterTexture(string path, OpenBveApi.Textures.TextureParameters parameters, out OpenBveApi.Textures.TextureHandle handle) { if (System.IO.File.Exists(path) || System.IO.Directory.Exists(path)) { Textures.Texture data; if (Textures.RegisterTexture(path, parameters, out data)) { handle = data; return true; } } else { ReportProblem(OpenBveApi.Hosts.ProblemType.PathNotFound, path.ToString()); } handle = null; return false; }