Simplification in TextureAtlas2D, added overload to IGraphicsApi.LoadTexture that accepts IntPtr and specified size.

This commit is contained in:
UnknownShadow200 2015-05-27 16:34:21 +10:00
parent 0587600078
commit e2289b1a92
3 changed files with 26 additions and 36 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using OpenTK; using OpenTK;
@ -24,9 +25,23 @@ namespace ClassicalSharp.GraphicsAPI {
} }
} }
public abstract int LoadTexture( Bitmap bmp ); public virtual int LoadTexture( Bitmap bmp ) {
Rectangle rec = new Rectangle( 0, 0, bmp.Width, bmp.Height );
BitmapData data = bmp.LockBits( rec, ImageLockMode.ReadOnly, bmp.PixelFormat );
int texId = LoadTexture( data.Width, data.Height, data.Scan0 );
bmp.UnlockBits( data );
return texId;
}
public abstract int LoadTexture( FastBitmap bmp ); public virtual int LoadTexture( FastBitmap bmp ) {
if( !bmp.IsLocked )
bmp.LockBits();
int texId = LoadTexture( bmp.Width, bmp.Height, bmp.Scan0 );
bmp.UnlockBits();
return texId;
}
public abstract int LoadTexture( int width, int height, IntPtr scan0 );
public abstract void Bind2DTexture( int texId ); public abstract void Bind2DTexture( int texId );

View File

@ -105,22 +105,8 @@ namespace ClassicalSharp.GraphicsAPI {
#if TRACK_RESOURCES #if TRACK_RESOURCES
Dictionary<int, string> textures = new Dictionary<int, string>(); Dictionary<int, string> textures = new Dictionary<int, string>();
#endif #endif
public override int LoadTexture( Bitmap bmp ) {
Rectangle rec = new Rectangle( 0, 0, bmp.Width, bmp.Height ); public override int LoadTexture( int width, int height, IntPtr scan0 ) {
BitmapData data = bmp.LockBits( rec, ImageLockMode.ReadOnly, bmp.PixelFormat );
int texId = LoadTexture( data.Width, data.Height, data.Scan0 );
bmp.UnlockBits( data );
return texId;
}
public override int LoadTexture( FastBitmap bmp ) {
if( !bmp.IsLocked ) bmp.LockBits();
int texId = LoadTexture( bmp.Width, bmp.Height, bmp.Scan0 );
bmp.UnlockBits();
return texId;
}
static int LoadTexture( int width, int height, IntPtr scan0 ) {
if( !Utils.IsPowerOf2( width ) || !Utils.IsPowerOf2( height ) ) if( !Utils.IsPowerOf2( width ) || !Utils.IsPowerOf2( height ) )
Utils.LogWarning( "Creating a non power of two texture." ); Utils.LogWarning( "Creating a non power of two texture." );

View File

@ -75,13 +75,13 @@ namespace ClassicalSharp {
invVerElementSize = (float)verElementSize / bmp.Height; invVerElementSize = (float)verElementSize / bmp.Height;
} }
/// <summary> Loads the texture at the specified coordinates within the atlas. </summary>
/// <param name="x"> horizontal position. (i.e. 1st image = 0, 2nd image = 1, etc</param>
/// <param name="bottomY"> vertical position. (i.e. 1st row = 0, 2nd row = 1, etc)</param>
/// <returns> The ID of the loaded texture at the specified coordinates.</returns>
public int LoadTextureElement( int x, int y ) { public int LoadTextureElement( int x, int y ) {
using( Bitmap bmp = GetTextureElement( x, y ) ) { using( FastBitmap atlas = new FastBitmap( AtlasBitmap, true ) ) {
return GraphicsApi.LoadTexture( bmp ); Bitmap bmp = new Bitmap( horElementSize, verElementSize );
using( FastBitmap dest = new FastBitmap( bmp, true ) ) {
CopyPortion( x, y, 0, 0, atlas, dest );
return GraphicsApi.LoadTexture( dest );
}
} }
} }
@ -99,20 +99,9 @@ namespace ClassicalSharp {
public TextureRectangle GetTexRec( int index ) { public TextureRectangle GetTexRec( int index ) {
int x = 0, y = 0; int x = 0, y = 0;
GetCoords( index, ref x, ref y ); GetCoords( index, ref x, ref y );
return new TextureRectangle( x * invHorElementSize, y * invVerElementSize, return GetTexRec( x, y );
invHorElementSize, invVerElementSize );
} }
public Bitmap GetTextureElement( int x, int y ) {
using( FastBitmap atlas = new FastBitmap( AtlasBitmap, true ) ) {
Bitmap bmp = new Bitmap( horElementSize, verElementSize );
using( FastBitmap dest = new FastBitmap( bmp, true) ) {
CopyPortion( x, y, 0, 0, atlas, dest );
}
return bmp;
}
}
internal void GetCoords( int id, ref int x, ref int y ) { internal void GetCoords( int id, ref int x, ref int y ) {
x = id % ElementsPerRow; x = id % ElementsPerRow;
y = id / ElementsPerRow; y = id / ElementsPerRow;