mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-16 19:15:14 -04:00
Simplification in TextureAtlas2D, added overload to IGraphicsApi.LoadTexture that accepts IntPtr and specified size.
This commit is contained in:
parent
0587600078
commit
e2289b1a92
@ -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 );
|
||||
|
||||
|
@ -105,22 +105,8 @@ namespace ClassicalSharp.GraphicsAPI {
|
||||
#if TRACK_RESOURCES
|
||||
Dictionary<int, string> textures = new Dictionary<int, string>();
|
||||
#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." );
|
||||
|
||||
|
@ -75,13 +75,13 @@ namespace ClassicalSharp {
|
||||
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 ) {
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user