diff --git a/GraphicsAPI/IGraphicsApi.cs b/GraphicsAPI/IGraphicsApi.cs index 202b5b4e4..5583520ef 100644 --- a/GraphicsAPI/IGraphicsApi.cs +++ b/GraphicsAPI/IGraphicsApi.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; 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 ); diff --git a/GraphicsAPI/OpenGLApi.cs b/GraphicsAPI/OpenGLApi.cs index 0e71081ec..0df080625 100644 --- a/GraphicsAPI/OpenGLApi.cs +++ b/GraphicsAPI/OpenGLApi.cs @@ -105,22 +105,8 @@ namespace ClassicalSharp.GraphicsAPI { #if TRACK_RESOURCES Dictionary textures = new Dictionary(); #endif - public override 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 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 ) { + + public override int LoadTexture( int width, int height, IntPtr scan0 ) { if( !Utils.IsPowerOf2( width ) || !Utils.IsPowerOf2( height ) ) Utils.LogWarning( "Creating a non power of two texture." ); diff --git a/Utils/TextureAtlas2D.cs b/Utils/TextureAtlas2D.cs index 918ebffe0..281ef3ab3 100644 --- a/Utils/TextureAtlas2D.cs +++ b/Utils/TextureAtlas2D.cs @@ -75,13 +75,13 @@ namespace ClassicalSharp { invVerElementSize = (float)verElementSize / bmp.Height; } - /// Loads the texture at the specified coordinates within the atlas. - /// horizontal position. (i.e. 1st image = 0, 2nd image = 1, etc - /// vertical position. (i.e. 1st row = 0, 2nd row = 1, etc) - /// The ID of the loaded texture at the specified coordinates. public int LoadTextureElement( int x, int y ) { - using( Bitmap bmp = GetTextureElement( x, y ) ) { - return GraphicsApi.LoadTexture( bmp ); + 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 GraphicsApi.LoadTexture( dest ); + } } } @@ -99,20 +99,9 @@ namespace ClassicalSharp { public TextureRectangle GetTexRec( int index ) { int x = 0, y = 0; GetCoords( index, ref x, ref y ); - return new TextureRectangle( x * invHorElementSize, y * invVerElementSize, - invHorElementSize, invVerElementSize ); + return GetTexRec( x, y ); } - 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 ) { x = id % ElementsPerRow; y = id / ElementsPerRow;