openMSX
SDLSnow.cc
Go to the documentation of this file.
1 #include "SDLSnow.hh"
2 #include "OutputSurface.hh"
3 #include "Display.hh"
4 #include "build-info.hh"
5 #include "random.hh"
6 #include <cstring>
7 #include <cstdint>
8 
9 namespace openmsx {
10 
11 template <class Pixel>
13  : Layer(COVER_FULL, Z_BACKGROUND)
14  , display(display_)
15 {
16  // Precalc gray values for noise
17  for (int i = 0; i < 256; ++i) {
18  gray[i] = output.mapRGB255(gl::ivec3(i));
19  }
20 }
21 
22 template <class Pixel>
24 {
25  auto& generator = global_urng(); // fast (non-cryptographic) random numbers
26  std::uniform_int_distribution<int> distribution(0, 255);
27 
28  output.lock();
29  const unsigned width = output.getWidth();
30  const unsigned height = output.getHeight();
31  for (unsigned y = 0; y < height; y += 2) {
32  auto* p0 = output.getLinePtrDirect<Pixel>(y + 0);
33  auto* p1 = output.getLinePtrDirect<Pixel>(y + 1);
34  for (unsigned x = 0; x < width; x += 2) {
35  p0[x + 0] = p0[x + 1] = gray[distribution(generator)];
36  }
37  memcpy(p1, p0, width * sizeof(Pixel));
38  }
39  output.flushFrameBuffer();
40 
41  display.repaintDelayed(100 * 1000); // 10fps
42 }
43 
44 // Force template instantiation.
45 #if HAVE_16BPP
46 template class SDLSnow<uint16_t>;
47 #endif
48 #if HAVE_32BPP
49 template class SDLSnow<uint32_t>;
50 #endif
51 
52 } // namespace openmsx
void lock()
Lock this OutputSurface.
Definition: OutputSurface.cc:7
unsigned mapRGB255(gl::ivec3 rgb)
Same as mapRGB, but RGB components are in range [0..255].
Represents the output window/screen of openMSX.
Definition: Display.hh:31
Interface for display layers.
Definition: Layer.hh:11
A frame buffer where pixels can be written to.
uint32_t Pixel
void paint(OutputSurface &output) override
Paint this layer.
Definition: SDLSnow.cc:23
virtual void flushFrameBuffer()
For SDLGL-FB-nn, copy frame buffer to OpenGL display.
void repaintDelayed(uint64_t delta)
Definition: Display.cc:367
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
Snow effect for background layer.
Definition: SDLSnow.hh:14
SDLSnow(OutputSurface &output, Display &display)
Definition: SDLSnow.cc:12
Pixel * getLinePtrDirect(unsigned y)
Returns a pointer to the requested line in the pixel buffer.
auto & global_urng()
Return reference to a (shared) global random number generator.
Definition: random.hh:8