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 <cstdint>
9
10namespace openmsx {
11
12template<std::unsigned_integral Pixel>
14 : Layer(COVER_FULL, Z_BACKGROUND)
15 , display(display_)
16{
17 // Precalc gray values for noise
18 for (auto [i, g] : enumerate(gray)) {
19 g = Pixel(output.mapRGB255(gl::ivec3(int(i))));
20 }
21}
22
23template<std::unsigned_integral 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.getLine<Pixel>(y + 0).subspan(0, width);
35 for (int x = 0; x < width; x += 2) {
36 p0[x + 0] = p0[x + 1] = gray[distribution(generator)];
37 }
38 auto p1 = pixelAccess.getLine<Pixel>(y + 1).subspan(0, width);
39 ranges::copy(p0, p1);
40 }
41 }
42 output.flushFrameBuffer();
43
44 display.repaintDelayed(100 * 1000); // 10fps
45}
46
47// Force template instantiation.
48#if HAVE_16BPP
49template class SDLSnow<uint16_t>;
50#endif
51#if HAVE_32BPP
52template class SDLSnow<uint32_t>;
53#endif
54
55} // 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:17
void paint(OutputSurface &output) override
Paint this layer.
Definition: SDLSnow.cc:24
SDLSnow(OutputSurface &output, Display &display)
Definition: SDLSnow.cc:13
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
auto copy(InputRange &&range, OutputIter out)
Definition: ranges.hh:232
auto & global_urng()
Return reference to a (shared) global random number generator.
Definition: random.hh:8
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition: ranges.hh:446