openMSX
OutputSurface.hh
Go to the documentation of this file.
1 #ifndef OUTPUTSURFACE_HH
2 #define OUTPUTSURFACE_HH
3 
4 #include "PixelFormat.hh"
5 #include "gl_vec.hh"
6 #include <string>
7 #include <cassert>
8 #include <cstdint>
9 
10 namespace openmsx {
11 
20 {
21 public:
22  OutputSurface(const OutputSurface&) = delete;
23  OutputSurface& operator=(const OutputSurface&) = delete;
24 
25  virtual ~OutputSurface() = default;
26 
27  int getLogicalWidth() const { return m_logicalSize[0]; }
28  int getLogicalHeight() const { return m_logicalSize[1]; }
29  gl::ivec2 getLogicalSize() const { return m_logicalSize; }
30  gl::ivec2 getPhysicalSize() const { return m_physSize; }
31 
32  gl::ivec2 getViewOffset() const { return m_viewOffset; }
33  gl::ivec2 getViewSize() const { return m_viewSize; }
34  gl::vec2 getViewScale() const { return m_viewScale; }
35  bool isViewScaled() const { return m_viewScale != gl::vec2(1.0f); }
36 
37  const PixelFormat& getPixelFormat() const { return pixelFormat; }
38 
43  uint32_t mapRGB(gl::vec3 rgb) const
44  {
45  return mapRGB255(gl::ivec3(rgb * 255.0f));
46  }
47 
50  uint32_t mapRGB255(gl::ivec3 rgb) const
51  {
52  auto [r, g, b] = rgb;
53  return getPixelFormat().map(r, g, b);
54  }
55 
58  template<typename Pixel> inline Pixel getKeyColor() const
59  {
60  return sizeof(Pixel) == 2
61  ? 0x0001 // lowest bit of 'some' color component is set
62  : 0x00000000; // alpha = 0
63  }
64 
69  template<typename Pixel> inline Pixel getKeyColorClash() const
70  {
71  assert(sizeof(Pixel) != 4); // shouldn't get clashes in 32bpp
72  return 0; // is visually very close, practically
73  // indistinguishable, from the actual KeyColor
74  }
75 
80  template<typename Pixel> Pixel mapKeyedRGB255(gl::ivec3 rgb)
81  {
82  Pixel p = mapRGB255(rgb);
83  if (sizeof(Pixel) == 2) {
84  return (p != getKeyColor<Pixel>())
85  ? p
86  : getKeyColorClash<Pixel>();
87  } else {
88  assert(p != getKeyColor<Pixel>());
89  return p;
90  }
91  }
92 
97  template<typename Pixel> Pixel mapKeyedRGB(gl::vec3 rgb)
98  {
99  return mapKeyedRGB255<Pixel>(gl::ivec3(rgb * 255.0f));
100  }
101 
105  virtual void saveScreenshot(const std::string& filename) = 0;
106 
107 protected:
108  OutputSurface() = default;
109 
110  // These two _must_ be called from (each) subclass constructor.
111  void calculateViewPort(gl::ivec2 logSize, gl::ivec2 physSize);
112  void setPixelFormat(const PixelFormat& format) { pixelFormat = format; }
113  void setOpenGlPixelFormat();
114 
115 private:
116  PixelFormat pixelFormat;
117  gl::ivec2 m_logicalSize;
118  gl::ivec2 m_physSize;
119  gl::ivec2 m_viewOffset;
120  gl::ivec2 m_viewSize;
121  gl::vec2 m_viewScale{1.0f};
122 };
123 
124 } // namespace openmsx
125 
126 #endif
openmsx::OutputSurface::operator=
OutputSurface & operator=(const OutputSurface &)=delete
openmsx::DiskImageUtils::format
void format(SectorAccessibleDisk &disk, bool dos1)
Format the given disk (= a single partition).
Definition: DiskImageUtils.cc:182
openmsx::OutputSurface::mapRGB
uint32_t mapRGB(gl::vec3 rgb) const
Returns the pixel value for the given RGB color.
Definition: OutputSurface.hh:43
openmsx::OutputSurface::saveScreenshot
virtual void saveScreenshot(const std::string &filename)=0
Save the content of this OutputSurface to a PNG file.
openmsx::OutputSurface::calculateViewPort
void calculateViewPort(gl::ivec2 logSize, gl::ivec2 physSize)
Definition: OutputSurface.cc:6
openmsx::OutputSurface::setPixelFormat
void setPixelFormat(const PixelFormat &format)
Definition: OutputSurface.hh:112
gl_vec.hh
openmsx::OutputSurface::isViewScaled
bool isViewScaled() const
Definition: OutputSurface.hh:35
openmsx::OutputSurface::getViewScale
gl::vec2 getViewScale() const
Definition: OutputSurface.hh:34
openmsx::OutputSurface::getLogicalHeight
int getLogicalHeight() const
Definition: OutputSurface.hh:28
openmsx::OutputSurface::getViewOffset
gl::ivec2 getViewOffset() const
Definition: OutputSurface.hh:32
openmsx::PixelFormat
Definition: PixelFormat.hh:8
openmsx::OutputSurface::setOpenGlPixelFormat
void setOpenGlPixelFormat()
Definition: OutputSurface.cc:24
gl::vecN
Definition: gl_vec.hh:35
openmsx::OutputSurface::getPixelFormat
const PixelFormat & getPixelFormat() const
Definition: OutputSurface.hh:37
openmsx::OutputSurface::mapRGB255
uint32_t mapRGB255(gl::ivec3 rgb) const
Same as mapRGB, but RGB components are in range [0..255].
Definition: OutputSurface.hh:50
openmsx::OutputSurface::mapKeyedRGB255
Pixel mapKeyedRGB255(gl::ivec3 rgb)
Returns the pixel value for the given RGB color.
Definition: OutputSurface.hh:80
openmsx::Pixel
uint32_t Pixel
Definition: GLHQLiteScaler.cc:93
openmsx::OutputSurface::getLogicalWidth
int getLogicalWidth() const
Definition: OutputSurface.hh:27
openmsx::PixelFormat::map
unsigned map(unsigned r, unsigned g, unsigned b) const
Definition: PixelFormat.hh:40
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::OutputSurface::~OutputSurface
virtual ~OutputSurface()=default
g
int g
Definition: ScopedAssign_test.cc:20
openmsx::OutputSurface::getPhysicalSize
gl::ivec2 getPhysicalSize() const
Definition: OutputSurface.hh:30
openmsx::OutputSurface::getKeyColorClash
Pixel getKeyColorClash() const
Returns a color that is visually very close to the key color.
Definition: OutputSurface.hh:69
openmsx::OutputSurface
A frame buffer where pixels can be written to.
Definition: OutputSurface.hh:19
gl::vec2
vecN< 2, float > vec2
Definition: gl_vec.hh:144
openmsx::OutputSurface::getKeyColor
Pixel getKeyColor() const
Returns the color key for this output surface.
Definition: OutputSurface.hh:58
PixelFormat.hh
openmsx::OutputSurface::mapKeyedRGB
Pixel mapKeyedRGB(gl::vec3 rgb)
Returns the pixel value for the given RGB color.
Definition: OutputSurface.hh:97
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::OutputSurface::getLogicalSize
gl::ivec2 getLogicalSize() const
Definition: OutputSurface.hh:29
openmsx::OutputSurface::getViewSize
gl::ivec2 getViewSize() const
Definition: OutputSurface.hh:33
openmsx::OutputSurface::OutputSurface
OutputSurface()=default