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