]> git.localhorst.tv Git - ffmpeg-test.git/commitdiff
improved numbers in drawing game
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 3 Nov 2024 13:33:07 +0000 (14:33 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 3 Nov 2024 13:33:07 +0000 (14:33 +0100)
src/app/DrawingGame.h

index 1256a4ed8ea088258c37867698576e3db5b21fc4..b162822cf84ed20d4008171b877b0bd933187b7a 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef TEST_APP_DRAWINGGAME_H_
 #define TEST_APP_DRAWINGGAME_H_
 
+#include <cmath>
 #include <sstream>
 #include <vector>
 
@@ -18,8 +19,7 @@ public:
        DrawingGame(cairo::Context &ctx, int w, int h, const gfx::ColorRGB &base_color)
        : Game()
        , w(w), h(h), x(0), y(0)
-       , step_size(1)
-       , color_intensity(9)
+       , number(0)
        , brush{ 0, 0, 0 }
        , pos{ 760, 100 }
        , font_color{ 1, 1, 1 }
@@ -119,11 +119,7 @@ public:
                switch (c) {
                        case '0': case '1': case '2': case '3': case '4':
                        case '5': case '6': case '7': case '8': case '9':
-                               if (IsMovementCharacter(next)) {
-                                       SetStepSize(c - '0');
-                               } else if (IsColorCharacter(next)) {
-                                       SetColorIntensity(c - '0');
-                               }
+                               AddDigit(c - '0');
                                break;
                        case 'w': case 'W':
                        case 'i': case 'I':
@@ -212,31 +208,31 @@ public:
 
 public:
        void MoveUp() {
-               y = (y - step_size) % h;
+               y = (y - GetAbsoluteNumber(1, h)) % h;
                while (y < 0) y += h;
                text_dirty = true;
-               SetStepSize(1);
+               ConsumeNumber();
        }
 
        void MoveDown() {
-               y = (y + step_size) % h;
+               y = (y + GetAbsoluteNumber(1, h)) % h;
                while (y < 0) y += h;
                text_dirty = true;
-               SetStepSize(1);
+               ConsumeNumber();
        }
 
        void MoveLeft() {
-               x = (x - step_size) % w;
+               x = (x - GetAbsoluteNumber(1, w)) % w;
                while (x < 0) x += w;
                text_dirty = true;
-               SetStepSize(1);
+               ConsumeNumber();
        }
 
        void MoveRight() {
-               x = (x + step_size) % w;
+               x = (x + GetAbsoluteNumber(1, w)) % w;
                while (x < 0) x += w;
                text_dirty = true;
-               SetStepSize(1);
+               ConsumeNumber();
        }
 
        void SetColor(const gfx::ColorRGB &c) {
@@ -244,33 +240,33 @@ public:
        }
 
        void SetRed() {
-               brush.r = double(color_intensity) / 9.0;
-               SetColorIntensity(9);
+               brush.r = GetColorIntensity();
+               ConsumeNumber();
        }
 
        void SetGreen() {
-               brush.g = double(color_intensity) / 9.0;
-               SetColorIntensity(9);
+               brush.g = GetColorIntensity();
+               ConsumeNumber();
        }
 
        void SetBlue() {
-               brush.b = double(color_intensity) / 9.0;
-               SetColorIntensity(9);
+               brush.b = GetColorIntensity();
+               ConsumeNumber();
        }
 
        void SetCyan() {
-               brush.r = 1.0 - double(color_intensity) / 9.0;
-               SetColorIntensity(9);
+               brush.r = 1.0 - GetColorIntensity();
+               ConsumeNumber();
        }
 
        void SetMagenta() {
-               brush.g = 1.0 - double(color_intensity) / 9.0;
-               SetColorIntensity(9);
+               brush.g = 1.0 - GetColorIntensity();
+               ConsumeNumber();
        }
 
        void SetYellow() {
-               brush.b = 1.0 - double(color_intensity) / 9.0;
-               SetColorIntensity(9);
+               brush.b = 1.0 - GetColorIntensity();
+               ConsumeNumber();
        }
 
        void Pick() {
@@ -287,12 +283,29 @@ public:
                brush.b = 1.0 - brush.b;
        }
 
-       void SetColorIntensity(int m) {
-               color_intensity = std::max(0, std::min(9, m));
+       void AddDigit(int d) {
+               number = number * 10 + d;
+               ++digits;
        }
 
-       void SetStepSize(int m) {
-               step_size = std::max(0, std::min(9, m));
+       int GetAbsoluteNumber(int min, int max) const {
+               return std::max(min, std::min(max, number));
+       }
+
+       double GetRelativeNumber() const {
+               if (digits == 0) return 0.0;
+               int max = std::pow(10.0, digits) - 1.0;
+               return double(number) / max;
+       }
+
+       double GetColorIntensity() const {
+               if (digits == 0) return 1.0;
+               return GetRelativeNumber();
+       }
+
+       void ConsumeNumber() {
+               number = 0;
+               digits = 0;
        }
 
        gfx::ColorRGB &CurrentCell() {
@@ -312,8 +325,8 @@ private:
 private:
        std::vector<gfx::ColorRGB> cells;
        int w, h, x, y;
-       int step_size;
-       int color_intensity;
+       int number;
+       int digits;
        gfx::ColorRGB brush;
 
        std::string command_queue;