From f839e07d4d2e94070129129c669072145235cf4f Mon Sep 17 00:00:00 2001
From: Star Rauchenberger <fefferburbia@gmail.com>
Date: Sat, 13 Mar 2021 09:29:47 -0500
Subject: Added cursor to pause menu

#7
---
 res/feather_pen.png | Bin 0 -> 190 bytes
 src/menu_system.cpp |  17 +++++++++++++++++
 src/menu_system.h   |   9 +++++++++
 src/renderer.cpp    |  12 ++++++++++++
 4 files changed, 38 insertions(+)
 create mode 100644 res/feather_pen.png

diff --git a/res/feather_pen.png b/res/feather_pen.png
new file mode 100644
index 0000000..281f4d3
Binary files /dev/null and b/res/feather_pen.png differ
diff --git a/src/menu_system.cpp b/src/menu_system.cpp
index 00c4849..acbfd76 100644
--- a/src/menu_system.cpp
+++ b/src/menu_system.cpp
@@ -12,6 +12,23 @@ void MenuSystem::tick(double dt) {
     } else if (pauseAnimation_.getProgress() == 1.0) {
       openState_ = OpenState::Open;
     }
+  } else if (openState_ == OpenState::Open) {
+    cursorBobTimer_.accumulate(dt);
+    while (cursorBobTimer_.step()) {
+      if (cursorBobDown_) {
+        cursorBob_++;
+
+        if (cursorBob_ >= 4) {
+          cursorBobDown_ = false;
+        }
+      } else {
+        cursorBob_--;
+
+        if (cursorBob_ <= 0) {
+          cursorBobDown_ = true;
+        }
+      }
+    }
   }
 }
 
diff --git a/src/menu_system.h b/src/menu_system.h
index 56f84d5..9cbd1f0 100644
--- a/src/menu_system.h
+++ b/src/menu_system.h
@@ -5,6 +5,7 @@
 #include "interpolation.h"
 #include "menu.h"
 #include "system.h"
+#include "timer.h"
 
 class Game;
 
@@ -31,6 +32,10 @@ public:
 
   const std::vector<MenuItem>& getMenu() const { return menu_; }
 
+  int getCursorPosition() const { return cursor_; }
+
+  int getCursorBob() const { return cursorBob_; }
+
 private:
 
   enum class OpenState {
@@ -43,6 +48,10 @@ private:
   Interpolation pauseAnimation_;
   OpenState openState_ = OpenState::Closed;
   std::vector<MenuItem> menu_;
+  int cursor_ = 0;
+  int cursorBob_ = 0;
+  bool cursorBobDown_ = true;
+  Timer cursorBobTimer_ { 125 };
 };
 
 #endif /* end of include guard: MENU_SYSTEM_H_205861EC */
diff --git a/src/renderer.cpp b/src/renderer.cpp
index 14eefea..fc16e20 100644
--- a/src/renderer.cpp
+++ b/src/renderer.cpp
@@ -559,6 +559,7 @@ void Renderer::renderMenu(Game& game) {
 
   const int lineHeight = 16;
   int totalHeight = menus.getMenu().size() * lineHeight;
+  std::vector<vec2i> positions;
 
   int index = 0;
   for (const MenuItem& menuItem : menus.getMenu()) {
@@ -577,11 +578,22 @@ void Renderer::renderMenu(Game& game) {
         SDL_SetRenderTarget(ren_.get(), menuTex_.get());
         SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest);
 
+        positions.emplace_back(dest.x, dest.y);
+
         break;
       }
     }
+
     index++;
   }
+
+  int cursorTexId = loadImageFromFile("../res/feather_pen.png");
+  const SDL_Rect cursorDest {
+    positions[menus.getCursorPosition()].x() - 2 - 16 - menus.getCursorBob(),
+    positions[menus.getCursorPosition()].y() - 8 - menus.getCursorBob(),
+    16,
+    16 };
+  SDL_RenderCopy(ren_.get(), textures_.at(cursorTexId).get(), nullptr, &cursorDest);
 }
 
 int Renderer::loadImageFromFile(std::string filename) {
-- 
cgit 1.4.1