Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions source/LibRender2/Text/OpenGlFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ public Vector2 MeasureString(string text)
{
for (int i = 0; i < text.Length; i++)
{
// ReSharper disable once NotAccessedVariable
Texture texture;
OpenGlFontChar data;
i += GetCharacterData(text, i, out texture, out data) - 1;
i += GetCharacterData(text, i, out _, out data) - 1;
width += data.TypographicSize.X;

if (data.TypographicSize.Y > height)
Expand Down
26 changes: 14 additions & 12 deletions source/LibRender2/Text/OpenGlFontTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class OpenGlFontTable : IDisposable

// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
// Must remain, else will be disposed of by the GC as this is a separate assembly
private readonly Bitmap bitmap;
private readonly byte[] myBytes;
/// <summary>The border around glpyhs on the font bitmap</summary>
const int drawBorder = 20;
/// <summary>The border used when calculating texture co-ordinates</summary>
Expand All @@ -36,7 +36,7 @@ public OpenGlFontTable(Font font, int offset)
* */
Vector2[] physicalSizes = new Vector2[256];
Vector2[] typographicSizes = new Vector2[256];
bitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
Bitmap bitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
double lineHeight = 0;
Expand Down Expand Up @@ -120,7 +120,18 @@ public OpenGlFontTable(Font font, int offset)
Characters[i] = new OpenGlFontChar(new Vector4(x0, y0, x1 - x0, y1 - y0), new Vector2(physicalSizes[i].X + 2.0 * coordinateBorder, physicalSizes[i].Y + 2.0 * coordinateBorder), typographicSizes[i]);
}
graphics.Dispose();
Texture = new Texture(bitmap);
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
if (data.Stride == 4 * data.Width) {
/*
* Copy the data from the bitmap
* to the array in BGRA format.
*/
myBytes = new byte[data.Stride * data.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, myBytes, 0, data.Stride * data.Height);
bitmap.UnlockBits(data);
}
Texture = new Texture(bitmap.Width, bitmap.Height, 32, myBytes, null);
bitmap.Dispose();
}

/// <summary>Rounds the specified value to the next-highest power of two.</summary>
Expand All @@ -145,16 +156,7 @@ private static uint RoundToPowerOfTwo(uint value)

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool currentlyDisposing)
{
if (currentlyDisposing)
{
bitmap.Dispose();
}
}
}
}
48 changes: 0 additions & 48 deletions source/LibRender2/Textures/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,54 +146,6 @@ public Texture RegisterTexture(Texture texture)
return RegisteredTextures[idx];
}

/// <summary>Registers a texture and returns a handle to the texture.</summary>
/// <param name="bitmap">The bitmap that contains the texture.</param>
/// <param name="parameters">The parameters that specify how to process the texture.</param>
/// <returns>The handle to the texture.</returns>
/// <remarks>Be sure not to dispose of the bitmap after calling this function.</remarks>
public Texture RegisterTexture(Bitmap bitmap, TextureParameters parameters)
{
/*
* Register the texture and return the newly created handle.
* */
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(bitmap, parameters);
RegisteredTexturesCount++;
return RegisteredTextures[idx];
}

/// <summary>Registers a texture and returns a handle to the texture.</summary>
/// <param name="bitmap">The bitmap that contains the texture.</param>
/// <returns>The handle to the texture.</returns>
/// <remarks>Be sure not to dispose of the bitmap after calling this function.</remarks>
public Texture RegisterTexture(Bitmap bitmap)
{
/*
* Register the texture and return the newly created handle.
* */
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(bitmap);
RegisteredTexturesCount++;
return RegisteredTextures[idx];
}

/// <summary>Registers a texture and returns a handle to the texture.</summary>
/// <param name="bitmap">The bitmap that contains the texture.</param>
/// <param name="alpha">A second bitmap containing the alpha channel for this texture</param>
/// <returns>The handle to the texture.</returns>
/// <remarks>Be sure not to dispose of the bitmap after calling this function.</remarks>
public Texture RegisterTexture(Bitmap bitmap, Bitmap alpha)
{
/*
* Register the texture and return the newly created handle.
* */
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(bitmap);
RegisteredTexturesCount++;
return RegisteredTextures[idx];
}


// --- load texture ---

/// <summary>Loads the specified texture into OpenGL if not already loaded.</summary>
Expand Down
6 changes: 0 additions & 6 deletions source/ObjectViewer/Hosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,6 @@ public override bool RegisterTexture(string path, TextureParameters parameters,
return false;
}

public override bool RegisterTexture(Bitmap texture, TextureParameters parameters, out Texture handle)
{
handle = new Texture(texture, parameters);
return true;
}

/// <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>
Expand Down
11 changes: 10 additions & 1 deletion source/OpenBVE/Game/Menu/Menu.Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using LibRender2.Primitives;
Expand All @@ -13,6 +14,7 @@
using OpenBveApi.Textures;
using RouteManager2;
using Path = OpenBveApi.Path;
using Rectangle = System.Drawing.Rectangle;

namespace OpenBve
{
Expand Down Expand Up @@ -62,7 +64,14 @@ private static void packageWorkerThread_completed(object sender, RunWorkerComple
{
if (currentPackage != null)
{
routePictureBox.Texture = new Texture(new Bitmap(currentPackage.PackageImage));
if (currentPackage.PackageImage is Bitmap bitmap)
{
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
byte[] bytes = new byte[data.Stride * data.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, data.Stride * data.Height);
bitmap.UnlockBits(data);
routePictureBox.Texture = new Texture(bitmap.Width, bitmap.Height, 32, bytes, null);
}
routeDescriptionBox.Text = currentPackage.Description;
packagePreview = false;
}
Expand Down
8 changes: 7 additions & 1 deletion source/OpenBVE/Game/Menu/Menu.SingleMenu.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using DavyKager;
using OpenBveApi.Graphics;
Expand Down Expand Up @@ -221,7 +222,12 @@ public SingleMenu(MenuType menuType, int data = 0, double MaxWidth = 0)
if (icon != null)
{
Texture t;
Program.CurrentHost.RegisterTexture(icon.ToBitmap(), new TextureParameters(null, null), out t);
Bitmap b = icon.ToBitmap();
BitmapData bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
byte[] bytes = new byte[bd.Stride * bd.Height];
System.Runtime.InteropServices.Marshal.Copy(bd.Scan0, bytes, 0, bd.Stride * bd.Height);
b.UnlockBits(bd);
t = new Texture(b.Width, b.Height, 32, bytes, null);
iconCache.Add(ext, t);
Items[totalEntries].Icon = t;
}
Expand Down
10 changes: 8 additions & 2 deletions source/OpenBVE/Game/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OpenBveApi.Interface;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;
Expand All @@ -17,6 +18,7 @@
using OpenTK;
using TrainManager;
using Path = OpenBveApi.Path;
using Rectangle = System.Drawing.Rectangle;
using Vector2 = OpenBveApi.Math.Vector2;

namespace OpenBve
Expand Down Expand Up @@ -627,9 +629,13 @@ internal void ProcessCommand(Translations.Command cmd, double timeElapsed)
return;
}
routeDescriptionBox.Text = currentPackage.Description;
if (currentPackage.PackageImage != null)
if (currentPackage.PackageImage is Bitmap bitmap)
{
routePictureBox.Texture = new Texture(currentPackage.PackageImage as Bitmap);
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
byte[] bytes = new byte[data.Stride * data.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, data.Stride * data.Height);
bitmap.UnlockBits(data);
routePictureBox.Texture = new Texture(bitmap.Width, bitmap.Height, 32, bytes, null);
}
else
{
Expand Down
16 changes: 13 additions & 3 deletions source/OpenBVE/Game/RouteInfoOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using OpenBveApi.Colors;
using System.Drawing;
using System.Drawing.Imaging;
using OpenBveApi.Colors;
using OpenTK.Graphics.OpenGL;
using OpenBveApi.Textures;
using OpenBveApi.Interface;
Expand Down Expand Up @@ -110,14 +112,22 @@ private void setState(state newState)
case state.map:
if (mapImage == null)
{
mapImage = new Texture(Program.CurrentRoute.Information.RouteMap);
BitmapData data = Program.CurrentRoute.Information.RouteMap.LockBits(new Rectangle(0, 0, Program.CurrentRoute.Information.RouteMap.Width, Program.CurrentRoute.Information.RouteMap.Height), ImageLockMode.ReadOnly, Program.CurrentRoute.Information.RouteMap.PixelFormat);
byte[] bytes = new byte[data.Stride * data.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, data.Stride * data.Height);
Program.CurrentRoute.Information.RouteMap.UnlockBits(data);
mapImage = new Texture(Program.CurrentRoute.Information.RouteMap.Width, Program.CurrentRoute.Information.RouteMap.Height, 32, bytes, null);
mapSize = new Vector2(Program.CurrentRoute.Information.RouteMap.Width, Program.CurrentRoute.Information.RouteMap.Height);
}
break;
case state.gradient:
if (gradientImage == null)
{
gradientImage = new Texture(Program.CurrentRoute.Information.GradientProfile);
BitmapData data = Program.CurrentRoute.Information.GradientProfile.LockBits(new Rectangle(0, 0, Program.CurrentRoute.Information.GradientProfile.Width, Program.CurrentRoute.Information.GradientProfile.Height), ImageLockMode.ReadOnly, Program.CurrentRoute.Information.GradientProfile.PixelFormat);
byte[] bytes = new byte[data.Stride * data.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, data.Stride * data.Height);
Program.CurrentRoute.Information.GradientProfile.UnlockBits(data);
gradientImage = new Texture(Program.CurrentRoute.Information.GradientProfile.Width, Program.CurrentRoute.Information.GradientProfile.Height, 32, bytes, null);
gradientSize = new Vector2(Program.CurrentRoute.Information.GradientProfile.Width, Program.CurrentRoute.Information.GradientProfile.Height);
}
break;
Expand Down
7 changes: 6 additions & 1 deletion source/OpenBVE/Game/Timetable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using OpenBveApi;
using OpenBveApi.Runtime;
using OpenBveApi.Textures;
Expand Down Expand Up @@ -494,7 +495,11 @@ internal void RenderData(ref Texture timetableTexture)
{
// create texture
g.Dispose();
timetableTexture = Program.Renderer.TextureManager.RegisterTexture(b);
BitmapData data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
byte[] bytes = new byte[data.Stride * data.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, data.Stride * data.Height);
b.UnlockBits(data);
timetableTexture = new Texture(b.Width, b.Height, 32, bytes, null);
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions source/OpenBVE/System/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ public override bool RegisterTexture(Texture texture, TextureParameters paramete
return true;
}

public override bool RegisterTexture(Bitmap texture, TextureParameters parameters, out Texture handle)
{
handle = new Texture(texture, parameters);
return true;
}

// --- sound ---

/// <summary>Loads a sound and returns the sound data.</summary>
Expand Down
4 changes: 2 additions & 2 deletions source/OpenBveApi/Objects/Helpers/MeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void Apply(ref StaticObject Object, bool EnableHacks = false, bool Ignore
Object.Mesh.Materials[mm + i].TransparentColor = Materials[i].TransparentColor;
if (Materials[i].DaytimeTexture != null || Materials[i].Text != null)
{
Textures.Texture tday;
Textures.Texture tday = null;
if (Materials[i].Text != null)
{
Bitmap bitmap = null;
Expand All @@ -100,7 +100,7 @@ public void Apply(ref StaticObject Object, bool EnableHacks = false, bool Ignore
}

Bitmap texture = TextOverlay.AddTextToBitmap(bitmap, Materials[i].Text, Materials[i].Font, 12, Materials[i].BackgroundColor, Materials[i].TextColor, Materials[i].TextPadding);
currentHost.RegisterTexture(texture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tday);
//currentHost.RegisterTexture(texture, new TextureParameters(null, new Color24(Materials[i].TransparentColor.R, Materials[i].TransparentColor.G, Materials[i].TransparentColor.B)), out tday);
}
else
{
Expand Down
1 change: 0 additions & 1 deletion source/OpenBveApi/OpenBveApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@
<DependentUpon>Textures.cs</DependentUpon>
</Compile>
<Compile Include="Textures\Textures.OpenGLWrapMode.cs" />
<Compile Include="Textures\Textures.BitmapOrigin.cs" />
<Compile Include="Textures\Textures.TextureInterface.cs" />
<Compile Include="Textures\Textures.TextureOrigin.cs" />
<Compile Include="Textures\Textures.TextureParameters.cs" />
Expand Down
12 changes: 1 addition & 11 deletions source/OpenBveApi/System/Hosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,7 @@ public virtual bool RegisterTexture(Textures.Texture texture, TextureParameters
handle = null;
return false;
}

/// <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 virtual bool RegisterTexture(Bitmap texture, TextureParameters parameters, out Textures.Texture handle) {
handle = null;
return false;
}


/// <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>
Expand Down
Loading