Splitted the mouse cursor into three separate images to avoid blending artifacts
authorIngo Ruhnke <grumbel@gmail.com>
Tue, 5 Aug 2014 00:15:04 +0000 (02:15 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Tue, 5 Aug 2014 00:15:04 +0000 (02:15 +0200)
data/images/engine/menu/mousecursor-click.png [new file with mode: 0644]
data/images/engine/menu/mousecursor-link.png [new file with mode: 0644]
data/images/engine/menu/mousecursor.png
src/gui/mousecursor.cpp
src/gui/mousecursor.hpp
src/supertux/resources.cpp

diff --git a/data/images/engine/menu/mousecursor-click.png b/data/images/engine/menu/mousecursor-click.png
new file mode 100644 (file)
index 0000000..7f2852f
Binary files /dev/null and b/data/images/engine/menu/mousecursor-click.png differ
diff --git a/data/images/engine/menu/mousecursor-link.png b/data/images/engine/menu/mousecursor-link.png
new file mode 100644 (file)
index 0000000..4ca6839
Binary files /dev/null and b/data/images/engine/menu/mousecursor-link.png differ
index 888152f..e846b9e 100644 (file)
Binary files a/data/images/engine/menu/mousecursor.png and b/data/images/engine/menu/mousecursor.png differ
index 101cf1f..f03dd1c 100644 (file)
 
 MouseCursor* MouseCursor::current_ = 0;
 
-MouseCursor::MouseCursor(std::string cursor_file) : 
-  mid_x(0), 
-  mid_y(0),
-  state_before_click(),
-  cur_state(),
-  cursor()
+MouseCursor::MouseCursor(const std::string& cursor_file,
+                         const std::string& cursor_click_file,
+                         const std::string& cursor_link_file) :
+  m_mid_x(0), 
+  m_mid_y(0),
+  m_state(MC_NORMAL),
+  m_cursor()
 {
-  cursor = Surface::create(cursor_file);
-
-  cur_state = MC_NORMAL;
+  m_cursor.push_back(Surface::create(cursor_file));
+  m_cursor.push_back(Surface::create(cursor_click_file));
+  m_cursor.push_back(Surface::create(cursor_link_file));
 }
 
 MouseCursor::~MouseCursor()
 {
 }
 
-int MouseCursor::state()
-{
-  return cur_state;
-}
-
-void MouseCursor::set_state(int nstate)
+void MouseCursor::set_state(MouseCursorState nstate)
 {
-  cur_state = nstate;
+  m_state = nstate;
 }
 
 void MouseCursor::set_mid(int x, int y)
 {
-  mid_x = x;
-  mid_y = y;
+  m_mid_x = x;
+  m_mid_y = y;
 }
 
 void MouseCursor::draw(DrawingContext& context)
 {
-  if(cur_state == MC_HIDE)
-    return;
+  if (m_state != MC_HIDE)
+  {
+    int x;
+    int y;
+    Uint8 ispressed = SDL_GetMouseState(&x, &y);
 
-  int x,y,w,h;
-  Uint8 ispressed = SDL_GetMouseState(&x,&y);
+    Vector mouse_pos = Renderer::instance()->to_logical(x, y);
 
-  Vector mouse_pos = Renderer::instance()->to_logical(x, y);
+    x = int(mouse_pos.x);
+    y = int(mouse_pos.y);
 
-  x = int(mouse_pos.x);
-  y = int(mouse_pos.y);
-
-  w = (int) cursor->get_width();
-  h = (int) (cursor->get_height() / MC_STATES_NB);
-  if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2)) {
-    if(cur_state != MC_CLICK) {
-      state_before_click = cur_state;
-      cur_state = MC_CLICK;
+    int tmp_state = m_state;
+    if (ispressed & SDL_BUTTON(1) || ispressed & SDL_BUTTON(2))
+    {
+      tmp_state = MC_CLICK;
     }
-  } else {
-    if(cur_state == MC_CLICK)
-      cur_state = state_before_click;
-  }
 
-  context.draw_surface_part(cursor, Vector(0, h*cur_state),
-                            Vector(w, h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
+    context.draw_surface(m_cursor[static_cast<int>(tmp_state)],
+                         Vector(x - m_mid_x, y - m_mid_y),
+                         LAYER_GUI + 100);
+  }
 }
 
 /* EOF */
index 30afd56..3ecd84e 100644 (file)
@@ -23,7 +23,8 @@
 
 #define MC_STATES_NB 3
 
-enum {
+enum MouseCursorState
+{
   MC_NORMAL = 0,
   MC_CLICK,
   MC_LINK,
@@ -40,16 +41,15 @@ class DrawingContext;
 class MouseCursor
 {
 public:
-  /// Constructor of MouseCursor.
-  /** Expects an imagefile for the cursor and  the number of animation frames it contains. */
-  MouseCursor(std::string cursor_file);
+  MouseCursor(const std::string& cursor_file,
+              const std::string& cursor_click_file,
+              const std::string& cursor_link_file);
   ~MouseCursor();
-  /// Get MouseCursor state.
-  /** (MC_NORMAL, MC_CLICK, MC_LINK or MC_HIDE) */
-  int state();
+
   /// Set MouseCursor state.
   /** (MC_NORMAL, MC_CLICK, MC_LINK or MC_HIDE) */
-  void set_state(int nstate);
+  void set_state(MouseCursorState nstate);
+
   /// Define the middle of a MouseCursor.
   /** Useful for cross mouse cursor images in example. */
   void set_mid(int x, int y);
@@ -64,14 +64,11 @@ public:
   static void set_current(MouseCursor* pcursor)
   {        current_ = pcursor;      };
   
-  friend class Resources;
-
 private:
-  int mid_x;
-  int mid_y;
-  int state_before_click;
-  int cur_state;
-  SurfacePtr cursor;
+  int m_mid_x;
+  int m_mid_y;
+  MouseCursorState m_state;
+  std::vector<SurfacePtr> m_cursor;
 
 private:
   static MouseCursor* current_;
index 011393c..2eac6a6 100644 (file)
@@ -41,7 +41,9 @@ void
 Resources::load_shared()
 {
   /* Load the mouse-cursor */
-  mouse_cursor = new MouseCursor("images/engine/menu/mousecursor.png");
+  mouse_cursor = new MouseCursor("images/engine/menu/mousecursor.png",
+                                 "images/engine/menu/mousecursor-click.png",
+                                 "images/engine/menu/mousecursor-link.png");
   MouseCursor::set_current(mouse_cursor);
 
   /* Load global images: */
@@ -106,7 +108,6 @@ Resources::unload_shared()
   /* Free mouse-cursor */
   if(mouse_cursor != NULL)
   {
-    mouse_cursor->cursor.reset();
     delete mouse_cursor;
   }
 }