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 "enumerate.hh"
7 #include "random.hh"
8 #include <cstring>
9 #include <cstdint>
10 
11 namespace openmsx {
12 
13 template<typename Pixel>
15  : Layer(COVER_FULL, Z_BACKGROUND)
16  , display(display_)
17 {
18  // Precalc gray values for noise
19  for (auto [i, g] : enumerate(gray)) {
20  g = output.mapRGB255(gl::ivec3(int(i)));
21  }
22 }
23 
24 template<typename Pixel>
26 {
27  auto& generator = global_urng(); // fast (non-cryptographic) random numbers
28  std::uniform_int_distribution<int> distribution(0, 255);
29 
30  auto& output = checked_cast<SDLOutputSurface&>(output_);
31  {
32  auto pixelAccess = output.getDirectPixelAccess();
33  auto [width, height] = output.getLogicalSize();
34  for (int y = 0; y < height; y += 2) {
35  auto* p0 = pixelAccess.getLinePtr<Pixel>(y + 0);
36  auto* p1 = pixelAccess.getLinePtr<Pixel>(y + 1);
37  for (int x = 0; x < width; x += 2) {
38  p0[x + 0] = p0[x + 1] = gray[distribution(generator)];
39  }
40  memcpy(p1, p0, width * sizeof(Pixel));
41  }
42  }
43  output.flushFrameBuffer();
44 
45  display.repaintDelayed(100 * 1000); // 10fps
46 }
47 
48 // Force template instantiation.
49 #if HAVE_16BPP
50 template class SDLSnow<uint16_t>;
51 #endif
52 #if HAVE_32BPP
53 template class SDLSnow<uint32_t>;
54 #endif
55 
56 } // namespace openmsx
int g
Represents the output window/screen of openMSX.
Definition: Display.hh:33
Interface for display layers.
Definition: Layer.hh:12
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].
Snow effect for background layer.
Definition: SDLSnow.hh:15
void paint(OutputSurface &output) override
Paint this layer.
Definition: SDLSnow.cc:25
SDLSnow(OutputSurface &output, Display &display)
Definition: SDLSnow.cc:14
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition: enumerate.hh:28
This file implemented 3 utility functions:
Definition: Autofire.cc:9
uint32_t Pixel
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:118
auto & global_urng()
Return reference to a (shared) global random number generator.
Definition: random.hh:8