2014-01-28 04:39:27 +00:00
using System ;
2014-01-30 04:46:59 +00:00
using System.Collections.Generic ;
2014-01-28 04:39:27 +00:00
using BizHawk.Common ;
using BizHawk.Emulation.Common ;
using BizHawk.Client.Common ;
using BizHawk.Bizware.BizwareGL ;
namespace BizHawk.Client.EmuHawk
{
/// <summary>
2014-01-30 04:46:59 +00:00
/// Recycles a pair of temporary textures (in case double-buffering helps any) to contain a BitmapBuffer's or DisplaySurface's contents, as long as the dimensions match.
2014-01-28 04:39:27 +00:00
/// When the dimensions dont match, a new one will be allocated
/// </summary>
class TextureFrugalizer : IDisposable
{
public TextureFrugalizer ( IGL gl )
{
GL = gl ;
2014-01-30 04:46:59 +00:00
ResetList ( ) ;
2014-01-28 04:39:27 +00:00
}
public void Dispose ( )
{
2014-01-30 04:46:59 +00:00
foreach ( var ct in CurrentTextures )
if ( ct ! = null )
ct . Dispose ( ) ;
ResetList ( ) ;
}
void ResetList ( )
{
CurrentTextures = new List < Texture2d > ( ) ;
CurrentTextures . Add ( null ) ;
CurrentTextures . Add ( null ) ;
2014-01-28 04:39:27 +00:00
}
IGL GL ;
2014-01-30 04:46:59 +00:00
List < Texture2d > CurrentTextures ;
2014-01-28 04:39:27 +00:00
public Texture2d Get ( DisplaySurface ds )
{
using ( var bb = new BitmapBuffer ( ds . PeekBitmap ( ) , new BitmapLoadOptions ( ) ) )
{
return Get ( bb ) ;
}
}
public Texture2d Get ( BitmapBuffer bb )
{
2014-01-30 04:46:59 +00:00
//get the current entry
Texture2d CurrentTexture = CurrentTextures [ 0 ] ;
//check if its rotten and needs recreating
2014-01-28 04:39:27 +00:00
if ( CurrentTexture = = null | | CurrentTexture . IntWidth ! = bb . Width | | CurrentTexture . IntHeight ! = bb . Height )
{
2014-01-30 04:46:59 +00:00
//needs recreating. be sure to kill the old one...
2014-01-28 04:39:27 +00:00
if ( CurrentTexture ! = null )
CurrentTexture . Dispose ( ) ;
2014-01-30 04:46:59 +00:00
//and make a new one
2014-01-28 04:39:27 +00:00
CurrentTexture = GL . LoadTexture ( bb ) ;
}
else
{
2014-01-30 04:46:59 +00:00
//its good! just load in the data
2014-01-28 04:39:27 +00:00
GL . LoadTextureData ( CurrentTexture , bb ) ;
}
2014-01-30 04:46:59 +00:00
//now shuffle the buffers
2014-04-15 21:46:18 +00:00
CurrentTextures [ 0 ] = CurrentTextures [ 1 ] ;
2014-01-30 04:46:59 +00:00
CurrentTextures [ 1 ] = CurrentTexture ;
2014-06-12 07:57:01 +00:00
//deterministic state, i guess
CurrentTexture . SetFilterNearest ( ) ;
2014-01-28 04:39:27 +00:00
return CurrentTexture ;
}
}
}