begin of Pong game
Stefan Schuermans

Stefan Schuermans commited on 2019-06-10 11:34:12
Showing 10 changed files, with 216 additions and 28 deletions.

... ...
@@ -1 +1 @@
1
-syncfilt_off
1
+pong18x8
... ...
@@ -43,26 +43,46 @@ Game::~Game()
43 43
 /// check for update of configuration
44 44
 void Game::updateConfig()
45 45
 {
46
-  // format file was modified -> re-create canvas and redraw
46
+  bool doReinit = false;
47
+  bool doRedraw = false;
48
+
49
+  // format file was modified -> re-create canvas and schedule redraw
47 50
   if (m_fileFormat.checkModified()) {
51
+    m_fileFormat.update();
48 52
     createImgBuf();
53
+    doReinit = true;
49 54
   }
50 55
 
51
-  // color file was modified -> redraw image
52
-  if (m_fileBackgroundColor.checkModified()) {
53
-    redraw();
56
+  // color file was modified -> convert color, schedule redraw
57
+  if (colorUpdate(m_fileBackgroundColor, m_backgroundColor)) {
58
+    doRedraw = true;
54 59
   }
55 60
 
56 61
   // output stream name file was modified -> re-get output stream
57 62
   if (m_fileOutStream.checkModified()) {
58 63
     m_fileOutStream.update();
59 64
   }
65
+
66
+  // check config update of derived game
67
+  if (updateConfigGame()) {
68
+    doRedraw = true;
69
+  }
70
+
71
+  // re-initialize / redraw
72
+  if (doReinit) {
73
+    reinitialize();
74
+    doRedraw = true;
75
+  }
76
+  if (doRedraw) {
77
+    redraw();
78
+  }
60 79
 }
61 80
 
62 81
 /// activate game: set up image buffer, call redraw()
63 82
 void Game::activate()
64 83
 {
65 84
   createImgBuf();
85
+  reinitialize();
66 86
   redraw();
67 87
 }
68 88
 
... ...
@@ -146,31 +166,47 @@ void Game::rectFill(int y1, int y2, int x1, int x2, ColorData const &cd)
146 166
   }
147 167
 }
148 168
 
169
+/// process update of color file, return true on update
170
+bool Game::colorUpdate(ColorFile &colorFile, ColorData &data) const
171
+{
172
+  if (colorFile.checkModified()) {
173
+    colorFile.update();
174
+    color2data(colorFile, data);
175
+    return true;
176
+  } else {
177
+    return false;
178
+  }
179
+}
180
+
149 181
 /// convert color to raw color data
150
-void Game::color2data(Format const &format, Color const &color,
151
-                      ColorData &data)
182
+void Game::color2data(ColorFile const &colorFile, ColorData &data) const
152 183
 {
153
-  data.resize(format.m_channels);
154
-  if (format.m_channels == 1) {
184
+  if (! m_fileFormat.m_valid) {
185
+    data.clear();
186
+  } else {
187
+    unsigned int channels = m_fileFormat.m_obj.m_channels;
188
+    unsigned int maxval = m_fileFormat.m_obj.m_maxval;
189
+    data.resize(m_fileFormat.m_obj.m_channels);
190
+    Color const &color = colorFile.m_obj;
191
+    if (channels == 1) {
155 192
       // single channel
156 193
       // convert to monochrome according to CIE XYZ
157 194
       double val = 0.2125 * color.m_red + 0.7154 * color.m_green
158 195
                                         + 0.0721 * color.m_blue;
159
-    // adapt to maxval
160
-    val = val * format.m_maxval / 255.0;
161
-    // round
162
-    data.at(0) = (unsigned char)(val + 0.5);
163
-  } else if (format.m_channels == 2) {
196
+      // adapt to maxval and round
197
+      data.at(0) = (unsigned char)(val * maxval / 255.0 + 0.5);
198
+    } else if (channels == 2) {
164 199
       // two channels
165 200
       // adapt to maxval and round, ignore blue
166
-    data.at(0) = (unsigned char)(color.m_red * format.m_maxval / 255.0 + 0.5);
167
-    data.at(1) = (unsigned char)(color.m_green * format.m_maxval / 255.0 + 0.5);
168
-  } else if (format.m_channels >= 3) {
201
+      data.at(0) = (unsigned char)(color.m_red * maxval / 255.0 + 0.5);
202
+      data.at(1) = (unsigned char)(color.m_green * maxval / 255.0 + 0.5);
203
+    } else if (channels >= 3) {
169 204
       // three channels (more than three channels: further channels are dark)
170 205
       // adapt to maxval and round
171
-    data.at(0) = (unsigned char)(color.m_red * format.m_maxval / 255.0 + 0.5);
172
-    data.at(1) = (unsigned char)(color.m_green * format.m_maxval / 255.0 + 0.5);
173
-    data.at(2) = (unsigned char)(color.m_blue * format.m_maxval / 255.0 + 0.5);
206
+      data.at(0) = (unsigned char)(color.m_red * maxval / 255.0 + 0.5);
207
+      data.at(1) = (unsigned char)(color.m_green * maxval / 255.0 + 0.5);
208
+      data.at(2) = (unsigned char)(color.m_blue * maxval / 255.0 + 0.5);
209
+    }
174 210
   }
175 211
 }
176 212
 
... ...
@@ -218,11 +254,7 @@ void Game::createImgBuf()
218 254
   m_imgBuf.resize(m_height * m_width * m_channels);
219 255
 
220 256
   // convert background color
221
-  color2data(m_fileFormat.m_obj, m_fileBackgroundColor.m_obj,
222
-             m_backgroundColor);
223
-
224
-  // set image buffer to background color
225
-  rectFill(0, m_height, 0, m_width, m_backgroundColor);
257
+  color2data(m_fileBackgroundColor, m_backgroundColor);
226 258
 }
227 259
 
228 260
 /// tear down image buffer
... ...
@@ -50,7 +50,10 @@ private:
50 50
 
51 51
 public:
52 52
   /// check for update of configuration
53
-  virtual void updateConfig();
53
+  virtual void updateConfig() final;
54
+
55
+  /// check for update of configuration (derived game), return true on update
56
+  virtual bool updateConfigGame() = 0;
54 57
 
55 58
 protected:
56 59
   /// re-initialize game (e.g. due to config change)
... ...
@@ -108,9 +111,11 @@ protected:
108 111
   /// draw filled rectangle to image buffer
109 112
   void rectFill(int y1, int y2, int x1, int x2, ColorData const &cd);
110 113
 
114
+  /// process update of color file, return true on update
115
+  bool colorUpdate(ColorFile &colorFile, ColorData &data) const;
116
+
111 117
   /// convert color to raw color data
112
-  static void color2data(Format const &format, Color const &color,
113
-                         ColorData &data);
118
+  void color2data(ColorFile const &colorFile, ColorData &data) const;
114 119
 
115 120
   /// send current image buffer as frame to output stream
116 121
   void sendFrame();
... ...
@@ -0,0 +1,78 @@
1
+/* Blinker
2
+   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <string>
7
+#include <vector>
8
+
9
+#include <BlinkenLib/BlinkenFrame.h>
10
+
11
+#include "File.h"
12
+#include "Format.h"
13
+#include "FormatFile.h"
14
+#include "Game.h"
15
+#include "Mgrs.h"
16
+#include "Module.h"
17
+#include "OutStreamFile.h"
18
+#include "Pong.h"
19
+
20
+namespace Blinker {
21
+
22
+/**
23
+ * @brief constructor
24
+ * @param[in] name module name
25
+ * @param[in] mgrs managers
26
+ * @param[in] dirBase base directory
27
+ */
28
+Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
29
+  Game(name, mgrs, dirBase),
30
+  m_fileBallColor(dirBase.getFile("ballColor")),
31
+  m_ballColor()
32
+{
33
+  // FIXME: activate at begin for initial development only
34
+  activate();
35
+}
36
+
37
+/// virtual destructor
38
+Pong::~Pong()
39
+{
40
+}
41
+
42
+/// check for update of configuration (derived game), return true on update
43
+bool Pong::updateConfigGame()
44
+{
45
+  bool ret = false;
46
+
47
+  // color file was modified -> convert color, return true for update
48
+  if (colorUpdate(m_fileBallColor, m_ballColor)) {
49
+    ret = true;
50
+  }
51
+
52
+  return ret;
53
+}
54
+
55
+/// re-initialize game (e.g. due to config change)
56
+void Pong::reinitialize()
57
+{
58
+  // TODO
59
+
60
+  // convert colors
61
+  color2data(m_fileBallColor, m_ballColor);
62
+}
63
+
64
+/// redraw current game image, expected to call sendFrame() at end
65
+void Pong::redraw()
66
+{
67
+  // set image buffer to background color
68
+  rectFill(0, m_height, 0, m_width, m_backgroundColor);
69
+
70
+  // FIXME: draw ball
71
+  pixel(m_height / 2, m_width / 2, m_ballColor);
72
+
73
+  // send updated image buffer as frame
74
+  sendFrame();
75
+}
76
+
77
+} // namespace Blinker
78
+
... ...
@@ -0,0 +1,67 @@
1
+/* Blinker
2
+   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef BLINKER_PONG_H
7
+#define BLINKER_PONG_H
8
+
9
+#include <string>
10
+#include <vector>
11
+
12
+#include <BlinkenLib/BlinkenFrame.h>
13
+
14
+#include "Color.h"
15
+#include "ColorFile.h"
16
+#include "File.h"
17
+#include "Format.h"
18
+#include "FormatFile.h"
19
+#include "Game.h"
20
+#include "Mgrs.h"
21
+#include "Module.h"
22
+#include "OutStreamFile.h"
23
+
24
+namespace Blinker {
25
+
26
+/// pong game
27
+class Pong: public Game
28
+{
29
+public:
30
+  /**
31
+   * @brief constructor
32
+   * @param[in] name module name
33
+   * @param[in] mgrs managers
34
+   * @param[in] dirBase base directory
35
+   */
36
+  Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase);
37
+
38
+  /// virtual destructor
39
+  virtual ~Pong();
40
+
41
+private:
42
+  /// copy constructor disabled
43
+  Pong(const Pong &that);
44
+
45
+  /// assignment operator disabled
46
+  const Pong & operator=(const Pong &that);
47
+
48
+public:
49
+  /// check for update of configuration (derived game), return true on update
50
+  virtual bool updateConfigGame();
51
+
52
+protected:
53
+  /// re-initialize game (e.g. due to config change)
54
+  virtual void reinitialize();
55
+
56
+  /// redraw current game image, expected to call sendFrame() at end
57
+  virtual void redraw();
58
+
59
+protected:
60
+  ColorFile     m_fileBallColor; ///< color file for ball color
61
+  ColorData     m_ballColor;     ///< ball color
62
+}; // class Canvas
63
+
64
+} // namespace Blinker
65
+
66
+#endif // #ifndef BLINKER_PONG_H
67
+
... ...
@@ -19,6 +19,7 @@
19 19
 #include "Output.h"
20 20
 #include "PlatformInit.h"
21 21
 #include "Player.h"
22
+#include "Pong.h"
22 23
 #include "Printer.h"
23 24
 #include "Priority.h"
24 25
 #include "RateLimiter.h"
... ...
@@ -58,6 +59,7 @@ void run(const std::string &dirConfig)
58 59
   MODULEMGR(OpSplitter,       opsplitters);
59 60
   MODULEMGR(Output,           outputs);
60 61
   MODULEMGR(Player,           players);
62
+  MODULEMGR(Pong,             pongs);
61 63
   MODULEMGR(Printer,          printers);
62 64
   MODULEMGR(Priority,         priorities);
63 65
   MODULEMGR(Resizer,          resizers);
64 66