Vram.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "Vram.h"
  2. #include <OgreStringConverter.h>
  3. #include "Logger.h"
  4. #include <string.h>
  5. Vram::Vram():
  6. m_Width( 2048 ),
  7. m_Height(512)
  8. {
  9. memset( m_Vram, 0, 2048 * 512 );
  10. }
  11. Vram::~Vram()
  12. {
  13. }
  14. u16
  15. Vram::GetWidth() const
  16. {
  17. return m_Width;
  18. }
  19. u16
  20. Vram::GetHeight() const
  21. {
  22. return m_Height;
  23. }
  24. void
  25. Vram::Save(const Ogre::String& file)
  26. {
  27. Ogre::TexturePtr ptex;
  28. Ogre::HardwarePixelBufferSharedPtr buffer;
  29. ptex = Ogre::TextureManager::getSingleton().createManual("DynaTex", "General", Ogre::TEX_TYPE_2D, m_Width / 2, m_Height, 0, Ogre::PF_R8G8B8A8, Ogre::TU_STATIC);
  30. buffer = ptex->getBuffer(0, 0);
  31. buffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
  32. const Ogre::PixelBox& pb = buffer->getCurrentLock();
  33. // IVV type cast
  34. u32* data = static_cast<u32*>((unsigned int *) pb.data);
  35. for( int i = 0, size = m_Width * m_Height / 2; i < size; ++i )
  36. {
  37. u16 col = ( m_Vram[ i * 2 + 1 ] << 8) | m_Vram[ i * 2 + 0 ];
  38. data[ i ] = ( ( ( col & 0x1f ) * 255 / 31 ) << 0x18 ) | ( ( ( ( col >> 5 ) & 0x1f ) * 255 / 31 ) << 0x10 ) | ( ( ( ( col >> 10 ) & 0x1f ) * 255 / 31 ) << 0x8 ) | 0xff;
  39. }
  40. Ogre::Image image;
  41. image.loadDynamicImage( (Ogre::uchar*)pb.data, m_Width / 2, m_Height, Ogre::PF_R8G8B8A8 );
  42. image.save( file + ".png" );
  43. buffer->unlock();
  44. }
  45. void
  46. Vram::PutU8(u16 x, u16 y, u8 byte)
  47. {
  48. if( x > m_Width || y > m_Height )
  49. {
  50. LOGGER->Log( "Vram::PutU8: Error: Tried to put byte in Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n" );
  51. return;
  52. }
  53. m_Vram[ y * m_Width + x ] = byte;
  54. }
  55. u8
  56. Vram::GetU8( u16 x, u16 y ) const
  57. {
  58. if( x > m_Width || y > m_Height )
  59. {
  60. LOGGER->Log( "Vram::GetU8: Error: Tried to get byte from Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n");
  61. return 0;
  62. }
  63. return m_Vram[ y * m_Width + x ];
  64. }
  65. void
  66. Vram::PutU16( u16 x, u16 y, u16 bytes )
  67. {
  68. if( x + 1 > m_Width || y > m_Height )
  69. {
  70. LOGGER->Log( "Vram::PutU16: Error: Tried to put pixel in Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n" );
  71. return;
  72. }
  73. // this byte order use to make posible store everything to vram as U8 but get as U16LE
  74. m_Vram[ y * m_Width + x + 1 ] = bytes >> 8;
  75. m_Vram[ y * m_Width + x + 0 ] = bytes & 0x00ff;
  76. }
  77. u16
  78. Vram::GetU16( u16 x, u16 y ) const
  79. {
  80. if( x + 1 > m_Width || y > m_Height )
  81. {
  82. LOGGER->Log( "Vram::GetU16: Error: Tried to get pixel from Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n" );
  83. return 0;
  84. }
  85. // this byte order use to make posible store everything to vram as U8 but get as U16LE
  86. return ( m_Vram[ y * m_Width + x + 1 ] << 8) | m_Vram[ y * m_Width + x + 0 ];
  87. }