make tetris stones change color when they freeze
Stefan Schuermans

Stefan Schuermans commited on 2019-08-14 19:58:48
Showing 10 changed files, with 132 additions and 41 deletions.

... ...
@@ -62,27 +62,48 @@
62 62
         <tr><td><code>backgroundColor</code></td>
63 63
             <td></td>
64 64
             <td>background of the tetris field</td></tr>
65
+        <tr><td><code>fixedIColor</code></td>
66
+            <td></td>
67
+            <td>fixed stones of type "I"</td></tr>
68
+        <tr><td><code>fixedJColor</code></td>
69
+            <td></td>
70
+            <td>fixed stones of type "J"</td></tr>
71
+        <tr><td><code>fixedLColor</code></td>
72
+            <td></td>
73
+            <td>fixed stones of type "L"</td></tr>
74
+        <tr><td><code>fixedOColor</code></td>
75
+            <td></td>
76
+            <td>fixed stones of type "O"</td></tr>
77
+        <tr><td><code>fixedSColor</code></td>
78
+            <td></td>
79
+            <td>fixed stones of type "S"</td></tr>
80
+        <tr><td><code>fixedTColor</code></td>
81
+            <td></td>
82
+            <td>fixed stones of type "T"</td></tr>
83
+        <tr><td><code>fixedZColor</code></td>
84
+            <td></td>
85
+            <td>fixed stones of type "Z"</td></tr>
65 86
         <tr><td><code>stoneIColor</code></td>
66 87
             <td></td>
67
-            <td>stones of type "I" (falling and fixed)</td></tr>
88
+            <td>active stones of type "I"</td></tr>
68 89
         <tr><td><code>stoneJColor</code></td>
69 90
             <td></td>
70
-            <td>stones of type "J" (falling and fixed)</td></tr>
91
+            <td>active stones of type "J"</td></tr>
71 92
         <tr><td><code>stoneLColor</code></td>
72 93
             <td></td>
73
-            <td>stones of type "L" (falling and fixed)</td></tr>
94
+            <td>active stones of type "L"</td></tr>
74 95
         <tr><td><code>stoneOColor</code></td>
75 96
             <td></td>
76
-            <td>stones of type "O" (falling and fixed)</td></tr>
97
+            <td>active stones of type "O"</td></tr>
77 98
         <tr><td><code>stoneSColor</code></td>
78 99
             <td></td>
79
-            <td>stones of type "S" (falling and fixed)</td></tr>
100
+            <td>active stones of type "S"</td></tr>
80 101
         <tr><td><code>stoneTColor</code></td>
81 102
             <td></td>
82
-            <td>stones of type "T" (falling and fixed)</td></tr>
103
+            <td>active stones of type "T"</td></tr>
83 104
         <tr><td><code>stoneZColor</code></td>
84 105
             <td></td>
85
-            <td>stones of type "Z" (falling and fixed)</td></tr>
106
+            <td>active stones of type "Z"</td></tr>
86 107
       </table>
87 108
     </p>
88 109
     <h3>Delays</h3>
... ...
@@ -43,6 +43,13 @@ Tetris::Tetris(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
43 43
   m_fileStoneOColor(dirBase.getFile("stoneOColor")),
44 44
   m_fileStoneZColor(dirBase.getFile("stoneZColor")),
45 45
   m_fileStoneSColor(dirBase.getFile("stoneSColor")),
46
+  m_fileFixedIColor(dirBase.getFile("fixedIColor")),
47
+  m_fileFixedLColor(dirBase.getFile("fixedLColor")),
48
+  m_fileFixedJColor(dirBase.getFile("fixedJColor")),
49
+  m_fileFixedTColor(dirBase.getFile("fixedTColor")),
50
+  m_fileFixedOColor(dirBase.getFile("fixedOColor")),
51
+  m_fileFixedZColor(dirBase.getFile("fixedZColor")),
52
+  m_fileFixedSColor(dirBase.getFile("fixedSColor")),
46 53
   m_fileDelay(dirBase.getFile("delay")),
47 54
   m_fileDropDelay(dirBase.getFile("dropDelay")),
48 55
   m_fileBlinkDelay(dirBase.getFile("blinkDelay")),
... ...
@@ -86,13 +93,48 @@ void Tetris::updateConfigGame(bool &doReinit, bool &doRedraw)
86 93
   (void)doReinit;
87 94
 
88 95
   // color file was modified -> convert color, ask for redraw
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
+  if (colorUpdate(m_fileStoneIColor, 255, m_stoneColors[Active][StoneI])) {
97
+    doRedraw = true;
98
+  }
99
+  if (colorUpdate(m_fileStoneLColor, 255, m_stoneColors[Active][StoneL])) {
100
+    doRedraw = true;
101
+  }
102
+  if (colorUpdate(m_fileStoneJColor, 255, m_stoneColors[Active][StoneJ])) {
103
+    doRedraw = true;
104
+  }
105
+  if (colorUpdate(m_fileStoneTColor, 255, m_stoneColors[Active][StoneT])) {
106
+    doRedraw = true;
107
+  }
108
+  if (colorUpdate(m_fileStoneOColor, 255, m_stoneColors[Active][StoneO])) {
109
+    doRedraw = true;
110
+  }
111
+  if (colorUpdate(m_fileStoneZColor, 255, m_stoneColors[Active][StoneZ])) {
112
+    doRedraw = true;
113
+  }
114
+  if (colorUpdate(m_fileStoneSColor, 255, m_stoneColors[Active][StoneS])) {
115
+    doRedraw = true;
116
+  }
117
+  if (colorUpdate(m_fileFixedIColor, 192, m_stoneColors[Fixed][StoneI])) {
118
+    doRedraw = true;
119
+  }
120
+  if (colorUpdate(m_fileFixedLColor, 192, m_stoneColors[Fixed][StoneL])) {
121
+    doRedraw = true;
122
+  }
123
+  if (colorUpdate(m_fileFixedJColor, 192, m_stoneColors[Fixed][StoneJ])) {
124
+    doRedraw = true;
125
+  }
126
+  if (colorUpdate(m_fileFixedTColor, 192, m_stoneColors[Fixed][StoneT])) {
127
+    doRedraw = true;
128
+  }
129
+  if (colorUpdate(m_fileFixedOColor, 192, m_stoneColors[Fixed][StoneO])) {
130
+    doRedraw = true;
131
+  }
132
+  if (colorUpdate(m_fileFixedZColor, 192, m_stoneColors[Fixed][StoneZ])) {
133
+    doRedraw = true;
134
+  }
135
+  if (colorUpdate(m_fileFixedSColor, 192, m_stoneColors[Fixed][StoneS])) {
136
+    doRedraw = true;
137
+  }
96 138
 
97 139
   // delay cfg value file was updated -> read new delay cfg value, no re-*
98 140
   valueUpdate(m_fileDelay, c_delayDescr, m_delay);
... ...
@@ -177,7 +219,7 @@ void Tetris::opConnRecvKey(OpConn *pConn, char key)
177 219
     if (checkStoneFit(m_stone, m_rot, m_posY, m_posX - 1)) {
178 220
       wipeStone(m_stone, m_rot, m_posY, m_posX);
179 221
       m_posX -= 1;
180
-      drawStone(m_stone, m_rot, m_posY, m_posX); // TODO: active color
222
+      drawStone(m_stone, m_rot, m_posY, m_posX, Active);
181 223
       sendFrame();
182 224
     }
183 225
     return;
... ...
@@ -188,7 +230,7 @@ void Tetris::opConnRecvKey(OpConn *pConn, char key)
188 230
     if (checkStoneFit(m_stone, m_rot, m_posY, m_posX + 1)) {
189 231
       wipeStone(m_stone, m_rot, m_posY, m_posX);
190 232
       m_posX += 1;
191
-      drawStone(m_stone, m_rot, m_posY, m_posX); // TODO: active color
233
+      drawStone(m_stone, m_rot, m_posY, m_posX, Active);
192 234
       sendFrame();
193 235
     }
194 236
     return;
... ...
@@ -203,7 +245,7 @@ void Tetris::opConnRecvKey(OpConn *pConn, char key)
203 245
     if (checkStoneFit(m_stone, new_rot, m_posY, m_posX)) {
204 246
       wipeStone(m_stone, m_rot, m_posY, m_posX);
205 247
       m_rot = new_rot;
206
-      drawStone(m_stone, m_rot, m_posY, m_posX); // TODO: active color
248
+      drawStone(m_stone, m_rot, m_posY, m_posX, Active);
207 249
       sendFrame();
208 250
     }
209 251
     return;
... ...
@@ -218,7 +260,7 @@ void Tetris::opConnRecvKey(OpConn *pConn, char key)
218 260
     if (checkStoneFit(m_stone, new_rot, m_posY, m_posX)) {
219 261
       wipeStone(m_stone, m_rot, m_posY, m_posX);
220 262
       m_rot = new_rot;
221
-      drawStone(m_stone, m_rot, m_posY, m_posX); // TODO: active color
263
+      drawStone(m_stone, m_rot, m_posY, m_posX, Active);
222 264
       sendFrame();
223 265
     }
224 266
     return;
... ...
@@ -265,13 +307,20 @@ void Tetris::opConnClose(OpConn *pConn)
265 307
 void Tetris::reinitialize()
266 308
 {
267 309
   // convert colors
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]);
310
+  color2data(m_fileStoneIColor, 255, m_stoneColors[Active][StoneI]);
311
+  color2data(m_fileStoneLColor, 255, m_stoneColors[Active][StoneL]);
312
+  color2data(m_fileStoneJColor, 255, m_stoneColors[Active][StoneJ]);
313
+  color2data(m_fileStoneTColor, 255, m_stoneColors[Active][StoneT]);
314
+  color2data(m_fileStoneOColor, 255, m_stoneColors[Active][StoneO]);
315
+  color2data(m_fileStoneZColor, 255, m_stoneColors[Active][StoneZ]);
316
+  color2data(m_fileStoneSColor, 255, m_stoneColors[Active][StoneS]);
317
+  color2data(m_fileFixedIColor, 255, m_stoneColors[Fixed][StoneI]);
318
+  color2data(m_fileFixedLColor, 255, m_stoneColors[Fixed][StoneL]);
319
+  color2data(m_fileFixedJColor, 255, m_stoneColors[Fixed][StoneJ]);
320
+  color2data(m_fileFixedTColor, 255, m_stoneColors[Fixed][StoneT]);
321
+  color2data(m_fileFixedOColor, 255, m_stoneColors[Fixed][StoneO]);
322
+  color2data(m_fileFixedZColor, 255, m_stoneColors[Fixed][StoneZ]);
323
+  color2data(m_fileFixedSColor, 255, m_stoneColors[Fixed][StoneS]);
275 324
 
276 325
   // get values
277 326
   valueFromFile(m_fileDelay, c_delayDescr, m_delay);
... ...
@@ -313,14 +362,14 @@ void Tetris::redraw()
313 362
     if (! (m_blinking & 1) || ! m_rowsBlink.at(y)) {
314 363
       for (int x = 0; x < m_width; ++x, ++i) {
315 364
         if (m_field.at(i) >= 0) {
316
-          pixel(y, x, m_stoneColors[m_field.at(i)]);
365
+          pixel(y, x, m_stoneColors[Fixed][m_field.at(i)]);
317 366
         }
318 367
       }
319 368
     }
320 369
   }
321 370
 
322 371
   // draw current stone
323
-  drawStone(m_stone, m_rot, m_posY, m_posX);
372
+  drawStone(m_stone, m_rot, m_posY, m_posX, Active);
324 373
 
325 374
   // send updated image buffer as frame
326 375
   sendFrame();
... ...
@@ -401,14 +450,14 @@ void Tetris::timeStone()
401 450
     // move stone down by one pixel
402 451
     wipeStone(m_stone, m_rot, m_posY, m_posX);
403 452
     m_posY += 1;
404
-    drawStone(m_stone, m_rot, m_posY, m_posX); // TODO: active color
453
+    drawStone(m_stone, m_rot, m_posY, m_posX, Active);
405 454
   }
406 455
 
407 456
   // stone cannot move down by one pixel
408 457
   else {
409 458
     // add stone permanently to field at current position
410 459
     freezeStone(m_stone, m_rot, m_posY, m_posX);
411
-    drawStone(m_stone, m_rot, m_posY, m_posX); // TODO: frozen color
460
+    drawStone(m_stone, m_rot, m_posY, m_posX, Fixed);
412 461
 
413 462
     // overflow of game field -> game over
414 463
     if (checkStoneOverflow(m_stone, m_rot, m_posY, m_posX)) {
... ...
@@ -589,9 +638,9 @@ void Tetris::freezeStone(int stone, int rot, int y, int x)
589 638
 }
590 639
 
591 640
 /// draw a stone to image buffer
592
-void Tetris::drawStone(int stone, int rot, int y, int x)
641
+void Tetris::drawStone(int stone, int rot, int y, int x, State state)
593 642
 {
594
-  colorStone(stone, rot, y, x, m_stoneColors[stone]);
643
+  colorStone(stone, rot, y, x, m_stoneColors[state][stone]);
595 644
 }
596 645
 
597 646
 /// wipe a stone from image buffer (i.e. replace it with background color)
... ...
@@ -43,7 +43,14 @@ protected:
43 43
     StoneO,
44 44
     StoneZ,
45 45
     StoneS,
46
-    StoneCnt // not an index, but number of stones
46
+    StoneCnt ///< not an index, but number of stones
47
+  };
48
+
49
+  /// stone state
50
+  enum State {
51
+    Active, ///< stone is falling
52
+    Fixed, ///< stone is fixed part of field
53
+    StateCnt ///< not an index, but number of stone states
47 54
   };
48 55
 
49 56
   /// coordinates of a pixel part of a stone
... ...
@@ -168,7 +175,7 @@ protected:
168 175
   void freezeStone(int stone, int rot, int y, int x);
169 176
 
170 177
   /// draw a stone to image buffer
171
-  void drawStone(int stone, int rot, int y, int x);
178
+  void drawStone(int stone, int rot, int y, int x, State state);
172 179
 
173 180
   /// wipe a stone from image buffer (i.e. replace it with background color)
174 181
   void wipeStone(int stone, int rot, int y, int x);
... ...
@@ -196,13 +203,20 @@ protected:
196 203
   /// descriptor for delay value at end of game
197 204
   static ValueDescr const c_gameOverDelayDescr;
198 205
 
199
-  ColorFile m_fileStoneIColor;   ///< color file for I stone color
200
-  ColorFile m_fileStoneLColor;   ///< color file for L stone color
201
-  ColorFile m_fileStoneJColor;   ///< color file for J stone color
202
-  ColorFile m_fileStoneTColor;   ///< color file for T stone color
203
-  ColorFile m_fileStoneOColor;   ///< color file for O stone color
204
-  ColorFile m_fileStoneZColor;   ///< color file for Z stone color
205
-  ColorFile m_fileStoneSColor;   ///< color file for S stone color
206
+  ColorFile m_fileStoneIColor;   ///< color file for active I stone color
207
+  ColorFile m_fileStoneLColor;   ///< color file for active L stone color
208
+  ColorFile m_fileStoneJColor;   ///< color file for active J stone color
209
+  ColorFile m_fileStoneTColor;   ///< color file for active T stone color
210
+  ColorFile m_fileStoneOColor;   ///< color file for active O stone color
211
+  ColorFile m_fileStoneZColor;   ///< color file for active Z stone color
212
+  ColorFile m_fileStoneSColor;   ///< color file for active S stone color
213
+  ColorFile m_fileFixedIColor;   ///< color file for fixed I stone color
214
+  ColorFile m_fileFixedLColor;   ///< color file for fixed L stone color
215
+  ColorFile m_fileFixedJColor;   ///< color file for fixed J stone color
216
+  ColorFile m_fileFixedTColor;   ///< color file for fixed T stone color
217
+  ColorFile m_fileFixedOColor;   ///< color file for fixed O stone color
218
+  ColorFile m_fileFixedZColor;   ///< color file for fixed Z stone color
219
+  ColorFile m_fileFixedSColor;   ///< color file for fixed S stone color
206 220
   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
207 221
   UIntFile  m_fileDropDelay;     ///< file for delay while dropping (ms/frame)
208 222
   UIntFile  m_fileBlinkDelay;    ///< file for delay while blinking (ms/frame)
... ...
@@ -212,7 +226,7 @@ protected:
212 226
   NameFile m_fileRowCompleteSound; ///< "row complete" sound name file
213 227
   NameFile m_fileGameOverSound;    ///< "game over" sound name file
214 228
 
215
-  ColorData m_stoneColors[StoneCnt]; ///< stone colors
229
+  ColorData m_stoneColors[StateCnt][StoneCnt]; ///< stone colors
216 230
 
217 231
   unsigned int m_delay;         ///< initial delay in ms per frame
218 232
   unsigned int m_dropDelay;     ///< delay while dropping in ms per frame
219 233