openMSX
GLSnow.cc
Go to the documentation of this file.
1 #include "GLSnow.hh"
2 #include "Display.hh"
3 #include "openmsx.hh"
4 #include <cstdlib>
5 
6 namespace openmsx {
7 
8 GLSnow::GLSnow(Display& display_, unsigned width_, unsigned height_)
9  : Layer(COVER_FULL, Z_BACKGROUND)
10  , display(display_)
11  , width(width_), height(height_)
12 {
13  // Create noise texture.
14  byte buf[128 * 128];
15  for (auto& b : buf) {
16  b = byte(rand());
17  }
18  glGenTextures(1, &noiseTextureId);
19  glBindTexture(GL_TEXTURE_2D, noiseTextureId);
20  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
21  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
22  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, 128, 128, 0,
23  GL_LUMINANCE, GL_UNSIGNED_BYTE, buf);
24 }
25 
27 {
28  // Free texture.
29  glDeleteTextures(1, &noiseTextureId);
30 }
31 
32 void GLSnow::paint(OutputSurface& /*output*/)
33 {
34  // Rotate and mirror noise texture in consecutive frames to avoid
35  // seeing 'patterns' in the noise.
36  static const int coord[8][4][2] = {
37  { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } },
38  { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 0, 0 } },
39  { { 0, 1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } },
40  { { 1, 1 }, { 1, 0 }, { 0, 0 }, { 0, 1 } },
41  { { 1, 1 }, { 0, 1 }, { 0, 0 }, { 1, 0 } },
42  { { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 } },
43  { { 1, 0 }, { 1, 1 }, { 0, 1 }, { 0, 0 } },
44  { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 1, 0 } }
45  };
46  static unsigned cnt = 0;
47 
48  // Draw noise texture.
49  GLfloat x = GLfloat(rand()) / RAND_MAX;
50  GLfloat y = GLfloat(rand()) / RAND_MAX;
51  cnt = (cnt + 1) % 8;
52  glEnable(GL_TEXTURE_2D);
53  glBindTexture(GL_TEXTURE_2D, noiseTextureId);
54  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
55  glBegin(GL_QUADS);
56  glTexCoord2f(0.0f + x, 2.0f + y);
57  glVertex2i(coord[cnt][0][0] * width, coord[cnt][0][1] * height);
58  glTexCoord2f(2.0f + x, 2.0f + y);
59  glVertex2i(coord[cnt][1][0] * width, coord[cnt][1][1] * height);
60  glTexCoord2f(2.0f + x, 0.0f + y);
61  glVertex2i(coord[cnt][2][0] * width, coord[cnt][2][1] * height);
62  glTexCoord2f(0.0f + x, 0.0f + y);
63  glVertex2i(coord[cnt][3][0] * width, coord[cnt][3][1] * height);
64  glEnd();
65  glDisable(GL_TEXTURE_2D);
66 
67  display.repaintDelayed(100 * 1000); // 10fps
68 }
69 
70 } // namespace openmsx
Represents the output window/screen of openMSX.
Definition: Display.hh:33
unsigned char byte
8 bit unsigned integer
Definition: openmsx.hh:33
Interface for display layers.
Definition: Layer.hh:11
A frame buffer where pixels can be written to.
virtual ~GLSnow()
Definition: GLSnow.cc:26
void repaintDelayed(uint64_t delta)
Definition: Display.cc:406
GLSnow(Display &display, unsigned width, unsigned height)
Definition: GLSnow.cc:8
virtual void paint(OutputSurface &output)
Paint this layer.
Definition: GLSnow.cc:32