openMSX
SDLSnow.cc
Go to the documentation of this file.
1 #include "SDLSnow.hh"
2 #include "SDLOutputSurface.hh"
3 #include "Display.hh"
4 #include "build-info.hh"
5 #include "checked_cast.hh"
6 #include "random.hh"
7 #include <cstring>
8 #include <cstdint>
9 
10 namespace openmsx {
11 
12 template <class Pixel>
14  : Layer(COVER_FULL, Z_BACKGROUND)
15  , display(display_)
16 {
17  // Precalc gray values for noise
18  for (int i = 0; i < 256; ++i) {
19  gray[i] = output.mapRGB255(gl::ivec3(i));
20  }
21 }
22 
23 template <class Pixel>
25 {
26  auto& generator = global_urng(); // fast (non-cryptographic) random numbers
27  std::uniform_int_distribution<int> distribution(0, 255);
28 
29  auto& output = checked_cast<SDLOutputSurface&>(output_);
30  {
31  auto pixelAccess = output.getDirectPixelAccess();
32  auto [width, height] = output.getLogicalSize();
33  for (int y = 0; y < height; y += 2) {
34  auto* p0 = pixelAccess.getLinePtr<Pixel>(y + 0);
35  auto* p1 = pixelAccess.getLinePtr<Pixel>(y + 1);
36  for (int x = 0; x < width; x += 2) {
37  p0[x + 0] = p0[x + 1] = gray[distribution(generator)];
38  }
39  memcpy(p1, p0, width * sizeof(Pixel));
40  }
41  }
42  output.flushFrameBuffer();
43 
44  display.repaintDelayed(100 * 1000); // 10fps
45 }
46 
47 // Force template instantiation.
48 #if HAVE_16BPP
49 template class SDLSnow<uint16_t>;
50 #endif
51 #if HAVE_32BPP
52 template class SDLSnow<uint32_t>;
53 #endif
54 
55 } // namespace openmsx
SDLSnow.hh
Display.hh
random.hh
openmsx::SDLSnow::SDLSnow
SDLSnow(OutputSurface &output, Display &display)
Definition: SDLSnow.cc:13
gl::vecN
Definition: gl_vec.hh:35
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::Pixel
uint32_t Pixel
Definition: GLHQLiteScaler.cc:93
SDLOutputSurface.hh
openmsx::Layer
Interface for display layers.
Definition: Layer.hh:11
build-info.hh
openmsx::SDLSnow::paint
void paint(OutputSurface &output) override
Paint this layer.
Definition: SDLSnow.cc:24
openmsx::x
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1419
checked_cast.hh
openmsx::Display
Represents the output window/screen of openMSX.
Definition: Display.hh:31
openmsx::OutputSurface
A frame buffer where pixels can be written to.
Definition: OutputSurface.hh:19
openmsx::SDLSnow
Snow effect for background layer.
Definition: SDLSnow.hh:14
global_urng
auto & global_urng()
Return reference to a (shared) global random number generator.
Definition: random.hh:8
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5