Vram.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreStringConverter.h>
  16. #include "Vram.h"
  17. #include "Logger.h"
  18. #include <string.h>
  19. Vram::Vram(): width_(2048), height_(512){memset(vram_, 0, 2048 * 512);}
  20. Vram::~Vram(){}
  21. u16 Vram::GetWidth() const{return width_;}
  22. u16 Vram::GetHeight() const{return height_;}
  23. void Vram::Save(const Ogre::String& file){
  24. Ogre::TexturePtr ptex;
  25. Ogre::HardwarePixelBufferSharedPtr buffer;
  26. ptex = Ogre::TextureManager::getSingleton().createManual(
  27. "DynaTex", "General", Ogre::TEX_TYPE_2D, width_ / 2, height_,
  28. 0, Ogre::PF_R8G8B8A8, Ogre::TU_STATIC
  29. );
  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 = width_ * height_ / 2; i < size; ++ i){
  36. u16 col = (vram_[i * 2 + 1] << 8) | vram_[i * 2 + 0];
  37. data[i]
  38. = (((col & 0x1f) * 255 / 31) << 0x18)
  39. | ((((col >> 5) & 0x1f) * 255 / 31) << 0x10)
  40. | ((((col >> 10) & 0x1f) * 255 / 31) << 0x8)
  41. | 0xff;
  42. }
  43. Ogre::Image image;
  44. image.loadDynamicImage(
  45. (Ogre::uchar*) pb.data, width_ / 2, height_, Ogre::PF_R8G8B8A8
  46. );
  47. image.save(file + ".png");
  48. buffer->unlock();
  49. }
  50. void Vram::PutU8(u16 x, u16 y, u8 byte){
  51. if (x > width_ || y > height_){
  52. LOGGER->Log(
  53. "Vram::PutU8: Error: Tried to put byte in Vram x("
  54. + Ogre::StringConverter::toString(x) + ") y("
  55. + Ogre::StringConverter::toString(y) + ")\n"
  56. );
  57. return;
  58. }
  59. vram_[y * width_ + x] = byte;
  60. }
  61. u8 Vram::GetU8(u16 x, u16 y) const{
  62. if (x > width_ || y > height_){
  63. LOGGER->Log(
  64. "Vram::GetU8: Error: Tried to get byte from Vram x("
  65. + Ogre::StringConverter::toString(x) + ") y("
  66. + Ogre::StringConverter::toString(y) + ")\n"
  67. );
  68. return 0;
  69. }
  70. return vram_[y * width_ + x];
  71. }
  72. void Vram::PutU16(u16 x, u16 y, u16 bytes){
  73. if (x + 1 > width_ || y > height_){
  74. LOGGER->Log(
  75. "Vram::PutU16: Error: Tried to put pixel in Vram x("
  76. + Ogre::StringConverter::toString(x) + ") y("
  77. + Ogre::StringConverter::toString(y) + ")\n"
  78. );
  79. return;
  80. }
  81. // This byte order is used to make possible store everything in VRAM
  82. // as U8 but get as U16LE.
  83. vram_[y * width_ + x + 1] = bytes >> 8;
  84. vram_[y * width_ + x + 0] = bytes & 0x00ff;
  85. }
  86. u16 Vram::GetU16(u16 x, u16 y) const{
  87. if (x + 1 > width_ || y > height_){
  88. LOGGER->Log(
  89. "Vram::GetU16: Error: Tried to get pixel from Vram x("
  90. + Ogre::StringConverter::toString(x) + ") y("
  91. + Ogre::StringConverter::toString(y) + ")\n"
  92. );
  93. return 0;
  94. }
  95. // This byte order is used to make possible store everything in VRAM
  96. // as U8 but get as U16LE.
  97. return (vram_[y * width_ + x + 1] << 8) | vram_[y * width_ + x + 0];
  98. }