Vram.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. u32* data = static_cast<u32*>(pb.data);
  34. for( int i = 0, size = m_Width * m_Height / 2; i < size; ++i )
  35. {
  36. u16 col = ( m_Vram[ i * 2 + 1 ] << 8) | m_Vram[ i * 2 + 0 ];
  37. data[ i ] = ( ( ( col & 0x1f ) * 255 / 31 ) << 0x18 ) | ( ( ( ( col >> 5 ) & 0x1f ) * 255 / 31 ) << 0x10 ) | ( ( ( ( col >> 10 ) & 0x1f ) * 255 / 31 ) << 0x8 ) | 0xff;
  38. }
  39. Ogre::Image image;
  40. image.loadDynamicImage( (Ogre::uchar*)pb.data, m_Width / 2, m_Height, Ogre::PF_R8G8B8A8 );
  41. image.save( file + ".png" );
  42. buffer->unlock();
  43. }
  44. void
  45. Vram::PutU8(u16 x, u16 y, u8 byte)
  46. {
  47. if( x > m_Width || y > m_Height )
  48. {
  49. LOGGER->Log( "Vram::PutU8: Error: Tried to put byte in Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n" );
  50. return;
  51. }
  52. m_Vram[ y * m_Width + x ] = byte;
  53. }
  54. u8
  55. Vram::GetU8( u16 x, u16 y ) const
  56. {
  57. if( x > m_Width || y > m_Height )
  58. {
  59. LOGGER->Log( "Vram::GetU8: Error: Tried to get byte from Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n");
  60. return 0;
  61. }
  62. return m_Vram[ y * m_Width + x ];
  63. }
  64. void
  65. Vram::PutU16( u16 x, u16 y, u16 bytes )
  66. {
  67. if( x + 1 > m_Width || y > m_Height )
  68. {
  69. LOGGER->Log( "Vram::PutU16: Error: Tried to put pixel in Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n" );
  70. return;
  71. }
  72. // this byte order use to make posible store everything to vram as U8 but get as U16LE
  73. m_Vram[ y * m_Width + x + 1 ] = bytes >> 8;
  74. m_Vram[ y * m_Width + x + 0 ] = bytes & 0x00ff;
  75. }
  76. u16
  77. Vram::GetU16( u16 x, u16 y ) const
  78. {
  79. if( x + 1 > m_Width || y > m_Height )
  80. {
  81. LOGGER->Log( "Vram::GetU16: Error: Tried to get pixel from Vram x(" + Ogre::StringConverter::toString( x ) + ") y(" + Ogre::StringConverter::toString( y ) + ")\n" );
  82. return 0;
  83. }
  84. // this byte order use to make posible store everything to vram as U8 but get as U16LE
  85. return ( m_Vram[ y * m_Width + x + 1 ] << 8) | m_Vram[ y * m_Width + x + 0 ];
  86. }