use default colors when missing game color config
Stefan Schuermans

Stefan Schuermans commited on 2019-08-14 19:27:45
Showing 4 changed files, with 67 additions and 40 deletions.

... ...
@@ -65,7 +65,7 @@ void Game::updateConfig()
65 65
   }
66 66
 
67 67
   // color file was modified -> convert color, schedule redraw
68
-  if (colorUpdate(m_fileBackgroundColor, m_backgroundColor)) {
68
+  if (colorUpdate(m_fileBackgroundColor, 0, m_backgroundColor)) {
69 69
     doRedraw = true;
70 70
   }
71 71
 
... ...
@@ -319,26 +319,40 @@ void Game::number3x5(int anchorY, int anchorX, int alignY, int alignX,
319 319
   digits3x5(topY, leftX, digits, cd);
320 320
 }
321 321
 
322
-/// process update of color file, return true on update
323
-bool Game::colorUpdate(ColorFile &colorFile, ColorData &data) const
322
+/**
323
+ * @brief process update of color file
324
+ * @param[in] colorFile file containing color as string
325
+ * @param[in] def default color (grayscale) in case file is invalid (0..255)
326
+ * @pram[out] data color data
327
+ * @return true on update
328
+ */
329
+bool Game::colorUpdate(ColorFile &colorFile, unsigned char def,
330
+                       ColorData &data) const
324 331
 {
325 332
   if (colorFile.checkModified()) {
326 333
     colorFile.update();
327
-    color2data(colorFile, data);
334
+    color2data(colorFile, def, data);
328 335
     return true;
329 336
   } else {
330 337
     return false;
331 338
   }
332 339
 }
333 340
 
334
-/// convert color to raw color data
335
-void Game::color2data(ColorFile const &colorFile, ColorData &data) const
341
+/**
342
+ * @brief convert color to raw color data
343
+ * @param[in] colorFile file containing color as string
344
+ * @param[in] def default color (grayscale) in case file is invalid (0..255)
345
+ * @pram[out] data color data
346
+ */
347
+void Game::color2data(ColorFile const &colorFile, unsigned char def,
348
+                      ColorData &data) const
336 349
 {
337
-  if (! m_fileFormat.m_valid || ! colorFile.m_valid) {
338
-    data.clear();
339
-  } else {
340 350
   unsigned int channels = m_fileFormat.m_obj.m_channels;
341 351
   unsigned int maxval = m_fileFormat.m_obj.m_maxval;
352
+  if (! m_fileFormat.m_valid || ! colorFile.m_valid) {
353
+    data.resize(m_fileFormat.m_obj.m_channels,
354
+                (unsigned char)(def * maxval / 255.0 + 0.5));
355
+  } else {
342 356
     data.resize(m_fileFormat.m_obj.m_channels);
343 357
     Color const &color = colorFile.m_obj;
344 358
     if (channels == 1) {
... ...
@@ -494,7 +508,7 @@ void Game::createImgBuf()
494 508
   m_imgBuf.resize(m_height * m_width * m_channels);
495 509
 
496 510
   // convert background color
497
-  color2data(m_fileBackgroundColor, m_backgroundColor);
511
+  color2data(m_fileBackgroundColor, 0, m_backgroundColor);
498 512
 }
499 513
 
500 514
 /// tear down image buffer
... ...
@@ -190,11 +190,24 @@ protected:
190 190
   void number3x5(int anchorY, int anchorX, int alignY, int alignX,
191 191
                  int number, ColorData const &cd);
192 192
 
193
-  /// process update of color file, return true on update
194
-  bool colorUpdate(ColorFile &colorFile, ColorData &data) const;
193
+  /**
194
+   * @brief process update of color file
195
+   * @param[in] colorFile file containing color as string
196
+   * @param[in] def default color (grayscale) in case file is invalid (0..255)
197
+   * @pram[out] data color data
198
+   * @return true on update
199
+   */
200
+  bool colorUpdate(ColorFile &colorFile, unsigned char def,
201
+                   ColorData &data) const;
195 202
 
196
-  /// convert color to raw color data
197
-  void color2data(ColorFile const &colorFile, ColorData &data) const;
203
+  /**
204
+   * @brief convert color to raw color data
205
+   * @param[in] colorFile file containing color as string
206
+   * @param[in] def default color (grayscale) in case file is invalid (0..255)
207
+   * @pram[out] data color data
208
+   */
209
+  void color2data(ColorFile const &colorFile, unsigned char def,
210
+                  ColorData &data) const;
198 211
 
199 212
   /// process update of value file, return true on update
200 213
   static bool valueUpdate(UIntFile &valueFile, ValueDescr const &descr,
... ...
@@ -91,12 +91,12 @@ void Pong::updateConfigGame(bool &doReinit, bool &doRedraw)
91 91
   (void)doReinit;
92 92
 
93 93
   // color file was modified -> convert color, ask for redraw
94
-  if (colorUpdate(m_fileBallColor, m_ballColor)) { doRedraw = true; }
95
-  if (colorUpdate(m_fileLineColor, m_lineColor)) { doRedraw = true; }
96
-  if (colorUpdate(m_filePadColor, m_padColor)) { doRedraw = true; }
97
-  if (colorUpdate(m_fileComputerColor, m_computerColor)) { doRedraw = true; }
98
-  if (colorUpdate(m_fileScoreColor, m_scoreColor)) { doRedraw = true; }
99
-  if (colorUpdate(m_fileGoalColor, m_goalColor)) { doRedraw = true; }
94
+  if (colorUpdate(m_fileBallColor, 255, m_ballColor)) { doRedraw = true; }
95
+  if (colorUpdate(m_fileLineColor, 128, m_lineColor)) { doRedraw = true; }
96
+  if (colorUpdate(m_filePadColor, 255, m_padColor)) { doRedraw = true; }
97
+  if (colorUpdate(m_fileComputerColor, 192, m_computerColor)) { doRedraw = true; }
98
+  if (colorUpdate(m_fileScoreColor, 128, m_scoreColor)) { doRedraw = true; }
99
+  if (colorUpdate(m_fileGoalColor, 255, m_goalColor)) { doRedraw = true; }
100 100
 
101 101
   // cfg value file was updated -> read new cfg value, no reinit/redraw
102 102
   valueUpdate(m_fileDelay, c_delayDescr, m_delay);
... ...
@@ -266,12 +266,12 @@ void Pong::reinitialize()
266 266
   m_rightDelay = 0;
267 267
 
268 268
   // convert colors
269
-  color2data(m_fileBallColor, m_ballColor);
270
-  color2data(m_fileLineColor, m_lineColor);
271
-  color2data(m_filePadColor, m_padColor);
272
-  color2data(m_fileComputerColor, m_computerColor);
273
-  color2data(m_fileScoreColor, m_scoreColor);
274
-  color2data(m_fileGoalColor, m_goalColor);
269
+  color2data(m_fileBallColor, 255, m_ballColor);
270
+  color2data(m_fileLineColor, 128, m_lineColor);
271
+  color2data(m_filePadColor, 255, m_padColor);
272
+  color2data(m_fileComputerColor, 192, m_computerColor);
273
+  color2data(m_fileScoreColor, 128, m_scoreColor);
274
+  color2data(m_fileGoalColor, 255, m_goalColor);
275 275
   // get values
276 276
   valueFromFile(m_fileDelay, c_delayDescr, m_delay);
277 277
   valueFromFile(m_fileMaxScore, c_maxScoreDescr, m_maxScore);
... ...
@@ -86,13 +86,13 @@ void Tetris::updateConfigGame(bool &doReinit, bool &doRedraw)
86 86
   (void)doReinit;
87 87
 
88 88
   // color file was modified -> convert color, ask for redraw
89
-  if (colorUpdate(m_fileStoneIColor, m_stoneColors[StoneI])) { doRedraw = true; }
90
-  if (colorUpdate(m_fileStoneLColor, m_stoneColors[StoneL])) { doRedraw = true; }
91
-  if (colorUpdate(m_fileStoneJColor, m_stoneColors[StoneJ])) { doRedraw = true; }
92
-  if (colorUpdate(m_fileStoneTColor, m_stoneColors[StoneT])) { doRedraw = true; }
93
-  if (colorUpdate(m_fileStoneOColor, m_stoneColors[StoneO])) { doRedraw = true; }
94
-  if (colorUpdate(m_fileStoneZColor, m_stoneColors[StoneZ])) { doRedraw = true; }
95
-  if (colorUpdate(m_fileStoneSColor, m_stoneColors[StoneS])) { doRedraw = true; }
89
+  if (colorUpdate(m_fileStoneIColor, 255, m_stoneColors[StoneI])) { doRedraw = true; }
90
+  if (colorUpdate(m_fileStoneLColor, 255, m_stoneColors[StoneL])) { doRedraw = true; }
91
+  if (colorUpdate(m_fileStoneJColor, 255, m_stoneColors[StoneJ])) { doRedraw = true; }
92
+  if (colorUpdate(m_fileStoneTColor, 255, m_stoneColors[StoneT])) { doRedraw = true; }
93
+  if (colorUpdate(m_fileStoneOColor, 255, m_stoneColors[StoneO])) { doRedraw = true; }
94
+  if (colorUpdate(m_fileStoneZColor, 255, m_stoneColors[StoneZ])) { doRedraw = true; }
95
+  if (colorUpdate(m_fileStoneSColor, 255, m_stoneColors[StoneS])) { doRedraw = true; }
96 96
 
97 97
   // delay cfg value file was updated -> read new delay cfg value, no re-*
98 98
   valueUpdate(m_fileDelay, c_delayDescr, m_delay);
... ...
@@ -265,13 +265,13 @@ void Tetris::opConnClose(OpConn *pConn)
265 265
 void Tetris::reinitialize()
266 266
 {
267 267
   // convert colors
268
-  color2data(m_fileStoneIColor, m_stoneColors[StoneI]);
269
-  color2data(m_fileStoneLColor, m_stoneColors[StoneL]);
270
-  color2data(m_fileStoneJColor, m_stoneColors[StoneJ]);
271
-  color2data(m_fileStoneTColor, m_stoneColors[StoneT]);
272
-  color2data(m_fileStoneOColor, m_stoneColors[StoneO]);
273
-  color2data(m_fileStoneZColor, m_stoneColors[StoneZ]);
274
-  color2data(m_fileStoneSColor, m_stoneColors[StoneS]);
268
+  color2data(m_fileStoneIColor, 255, m_stoneColors[StoneI]);
269
+  color2data(m_fileStoneLColor, 255, m_stoneColors[StoneL]);
270
+  color2data(m_fileStoneJColor, 255, m_stoneColors[StoneJ]);
271
+  color2data(m_fileStoneTColor, 255, m_stoneColors[StoneT]);
272
+  color2data(m_fileStoneOColor, 255, m_stoneColors[StoneO]);
273
+  color2data(m_fileStoneZColor, 255, m_stoneColors[StoneZ]);
274
+  color2data(m_fileStoneSColor, 255, m_stoneColors[StoneS]);
275 275
 
276 276
   // get values
277 277
   valueFromFile(m_fileDelay, c_delayDescr, m_delay);
278 278