]> git.localhorst.tv Git - ffmpeg-test.git/commitdiff
improved(?) drawing game
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 13 Oct 2024 22:10:43 +0000 (00:10 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 13 Oct 2024 22:10:43 +0000 (00:10 +0200)
src/app/Application.h
src/app/DrawingGame.h

index aa2d06c70c236d28c64f25cef3f3c1dee6316e7a..01e5f833f71baa26668d2fc1ce48d0f4e796aedc 100644 (file)
@@ -133,86 +133,7 @@ private:
                if (msg.nick == "horstiebot") return;
                std::string text = msg.GetText();
                if (text.empty()) return;
-               for (auto i = text.begin(); i != text.end(); ++i) {
-                       switch (*i) {
-                               case '0': case '1': case '2': case '3': case '4':
-                               case '5': case '6': case '7': case '8': case '9':
-                                       drawing_game.SetModifier(*i - '0');
-                                       break;
-                               case 'u': case 'U':
-                               case 'n': case 'N':
-                                       drawing_game.MoveUp();
-                                       break;
-                               case 'd': case 'D':
-                               case 's': case 'S':
-                                       drawing_game.MoveDown();
-                                       break;
-                               case 'l': case 'L':
-                               case 'w': case 'W':
-                                       drawing_game.MoveLeft();
-                                       break;
-                               case 'r': case 'R':
-                               case 'e': case 'E':
-                                       drawing_game.MoveRight();
-                                       break;
-                               case 'a': case 'A':
-                                       drawing_game.SetRed();
-                                       break;
-                               case 'b': case 'B':
-                                       drawing_game.SetGreen();
-                                       break;
-                               case 'c': case 'C':
-                                       drawing_game.SetBlue();
-                                       break;
-                               case 'f': case 'F':
-                                       drawing_game.ClearRed();
-                                       break;
-                               case 'g': case 'G':
-                                       drawing_game.ClearGreen();
-                                       break;
-                               case 'h': case 'H':
-                                       drawing_game.ClearBlue();
-                                       break;
-                               case 'i': case 'I':
-                                       drawing_game.SetYellow();
-                                       break;
-                               case 'j': case 'J':
-                                       drawing_game.SetCyan();
-                                       break;
-                               case 'k': case 'K':
-                                       drawing_game.SetMagenta();
-                                       break;
-                               case 'm': case 'M':
-                                       drawing_game.ClearYellow();
-                                       break;
-                               case 'o': case 'O':
-                                       drawing_game.ClearCyan();
-                                       break;
-                               case 'p': case 'P':
-                                       drawing_game.ClearMagenta();
-                                       break;
-                               case 'q': case 'Q':
-                                       drawing_game.InvertRed();
-                                       break;
-                               case 't': case 'T':
-                                       drawing_game.InvertGreen();
-                                       break;
-                               case 'v': case 'V':
-                                       drawing_game.InvertBlue();
-                                       break;
-                               case 'x': case 'X':
-                                       drawing_game.InvertColor();
-                                       break;
-                               case 'y': case 'Y':
-                                       drawing_game.SetColor({ 1, 1, 1 });
-                                       break;
-                               case 'z': case 'Z':
-                                       drawing_game.SetColor({ 0, 0, 0 });
-                                       break;
-                               default:
-                                       break;
-                       }
-               }
+               drawing_game.QueueCommands(text);
        }
 
 private:
index 6f9b629b1d3f27338d18b7adf75be22310086262..f42499aaa06ebca8579df0855455033756055590 100644 (file)
@@ -16,7 +16,8 @@ public:
        DrawingGame(cairo::Context &ctx, int w, int h, const gfx::ColorRGB &base_color)
        : Game()
        , w(w), h(h), x(0), y(0)
-       , modifier(1)
+       , step_size(1)
+       , color_intensity(9)
        , pos{ 760, 100 }
        , font_color{ 1, 1, 1 }
        , cursor_font("DejaVu Sans 16px")
@@ -27,6 +28,15 @@ public:
 
 public:
        virtual void Update(cairo::Context &ctx, const Clock &now) {
+               if (!command_queue.empty()) {
+                       const char c = command_queue.back();
+                       command_queue.pop_back();
+                       if (!command_queue.empty()) {
+                               InterpretCharacter(c, command_queue.back());
+                       } else {
+                               InterpretCharacter(c);
+                       }
+               }
                if (text_dirty) {
                        UpdateText(ctx);
                }
@@ -50,7 +60,7 @@ public:
                r.x = pos.x + x * r.w;
                r.y = pos.y + y * r.h + 30;
                ctx.SetSourceColor({ 0.5, 0.5, 0.5 });
-               ctx.SetLineWidth(1);
+               ctx.SetLineWidth(2);
                ctx.Rectangle(r);
                ctx.Stroke();
        }
@@ -58,29 +68,148 @@ public:
        virtual void Mix(const Clock &, float *plane, int channels, int frame_size) const {
        }
 
+public:
+       void QueueCommands(const std::string &s) {
+               command_queue.insert(command_queue.begin(), s.rbegin(), s.rend());
+       }
+
+       void RunCommands(const std::string &s) {
+               for (auto i = s.begin(), end = s.end(); i != end; ++i) {
+                       if (i + 1 != end) {
+                               InterpretCharacter(*i, *(i + 1));
+                       } else {
+                               InterpretCharacter(*i);
+                       }
+               }
+       }
+
+       void InterpretCharacter(char c, char next = 0) {
+               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 {
+                                       SetColorIntensity(c - '0');
+                               }
+                               break;
+                       case 'u': case 'U':
+                       case 'n': case 'N':
+                               MoveUp();
+                               break;
+                       case 'd': case 'D':
+                       case 's': case 'S':
+                               MoveDown();
+                               break;
+                       case 'l': case 'L':
+                       case 'w': case 'W':
+                               MoveLeft();
+                               break;
+                       case 'r': case 'R':
+                       case 'e': case 'E':
+                               MoveRight();
+                               break;
+                       case 'a': case 'A':
+                               SetRed();
+                               break;
+                       case 'b': case 'B':
+                               SetGreen();
+                               break;
+                       case 'c': case 'C':
+                               SetBlue();
+                               break;
+                       case 'f': case 'F':
+                               ClearRed();
+                               break;
+                       case 'g': case 'G':
+                               ClearGreen();
+                               break;
+                       case 'h': case 'H':
+                               ClearBlue();
+                               break;
+                       case 'i': case 'I':
+                               SetYellow();
+                               break;
+                       case 'j': case 'J':
+                               SetCyan();
+                               break;
+                       case 'k': case 'K':
+                               SetMagenta();
+                               break;
+                       case 'm': case 'M':
+                               ClearYellow();
+                               break;
+                       case 'o': case 'O':
+                               ClearCyan();
+                               break;
+                       case 'p': case 'P':
+                               ClearMagenta();
+                               break;
+                       case 'q': case 'Q':
+                               InvertRed();
+                               break;
+                       case 't': case 'T':
+                               InvertGreen();
+                               break;
+                       case 'v': case 'V':
+                               InvertBlue();
+                               break;
+                       case 'x': case 'X':
+                               InvertColor();
+                               break;
+                       case 'y': case 'Y':
+                               SetColor({ 1, 1, 1 });
+                               break;
+                       case 'z': case 'Z':
+                               SetColor({ 0, 0, 0 });
+                               break;
+                       default:
+                               break;
+               }
+       }
+
+       bool IsMovementCharacter(char c) const {
+               switch (c) {
+                       case 'u': case 'U':
+                       case 'n': case 'N':
+                       case 'd': case 'D':
+                       case 's': case 'S':
+                       case 'l': case 'L':
+                       case 'w': case 'W':
+                       case 'r': case 'R':
+                       case 'e': case 'E':
+                               return true;
+               }
+               return false;
+       }
+
 public:
        void MoveUp() {
-               y = (y - modifier) % h;
+               y = (y - step_size) % h;
                while (y < 0) y += h;
                text_dirty = true;
+               SetStepSize(1);
        }
 
        void MoveDown() {
-               y = (y + modifier) % h;
+               y = (y + step_size) % h;
                while (y < 0) y += h;
                text_dirty = true;
+               SetStepSize(1);
        }
 
        void MoveLeft() {
-               x = (x - modifier) % w;
+               x = (x - step_size) % w;
                while (x < 0) x += w;
                text_dirty = true;
+               SetStepSize(1);
        }
 
        void MoveRight() {
-               x = (x + modifier) % w;
+               x = (x + step_size) % w;
                while (x < 0) x += w;
                text_dirty = true;
+               SetStepSize(1);
        }
 
        void SetColor(const gfx::ColorRGB &c) {
@@ -88,15 +217,15 @@ public:
        }
 
        void SetRed() {
-               cells[y * w + x].r = double(modifier) / 9.0;
+               cells[y * w + x].r = double(color_intensity) / 9.0;
        }
 
        void SetGreen() {
-               cells[y * w + x].g = double(modifier) / 9.0;
+               cells[y * w + x].g = double(color_intensity) / 9.0;
        }
 
        void SetBlue() {
-               cells[y * w + x].b = double(modifier) / 9.0;
+               cells[y * w + x].b = double(color_intensity) / 9.0;
        }
 
        void ClearRed() {
@@ -112,18 +241,18 @@ public:
        }
 
        void SetYellow() {
-               cells[y * w + x].r = double(modifier) / 9.0;
-               cells[y * w + x].g = double(modifier) / 9.0;
+               cells[y * w + x].r = double(color_intensity) / 9.0;
+               cells[y * w + x].g = double(color_intensity) / 9.0;
        }
 
        void SetCyan() {
-               cells[y * w + x].g = double(modifier) / 9.0;
-               cells[y * w + x].b = double(modifier) / 9.0;
+               cells[y * w + x].g = double(color_intensity) / 9.0;
+               cells[y * w + x].b = double(color_intensity) / 9.0;
        }
 
        void SetMagenta() {
-               cells[y * w + x].r = double(modifier) / 9.0;
-               cells[y * w + x].b = double(modifier) / 9.0;
+               cells[y * w + x].r = double(color_intensity) / 9.0;
+               cells[y * w + x].b = double(color_intensity) / 9.0;
        }
 
        void ClearYellow() {
@@ -159,9 +288,16 @@ public:
                cells[y * w + x].b = 1.0 - cells[y * w + x].b;
        }
 
-       void SetModifier(int m) {
-               modifier = std::max(0, std::min(9, m));
-               text_dirty = true;
+       void SetColorIntensity(int m) {
+               int new_intensity = std::max(0, std::min(9, m));
+               text_dirty = new_intensity != color_intensity;
+               color_intensity = new_intensity;
+       }
+
+       void SetStepSize(int m) {
+               int new_size = std::max(0, std::min(9, m));
+               text_dirty = new_size != step_size;
+               step_size = new_size;
        }
 
 private:
@@ -169,7 +305,8 @@ private:
                if (!text_dirty) return;
                std::stringstream out;
                out << "Cursor: " << x << ", " << y;
-               out << "  Modifier: " << modifier;
+               out << "  Schrittgröße: " << step_size;
+               out << "  Deckkraft: " << color_intensity;
                cursor_layout.SetText(out.str());
                ctx.UpdateLayout(cursor_layout);
                text_dirty = false;
@@ -178,7 +315,10 @@ private:
 private:
        std::vector<gfx::ColorRGB> cells;
        int w, h, x, y;
-       int modifier;
+       int step_size;
+       int color_intensity;
+
+       std::string command_queue;
 
        gfx::Position pos;