From 4f6981592e29038ad9f818399c0d5a48750cf28a Mon Sep 17 00:00:00 2001
From: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
Date: Wed, 7 Mar 2012 16:57:18 +0200
Subject: [PATCH 07/10] Fix mouse polling

Don't poll for mouse motion excessive number of times (no need to poll it for
each bit!)
---
 snes/controller/mouse/mouse.cpp | 14 ++++++++++++--
 snes/controller/mouse/mouse.hpp |  2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/snes/controller/mouse/mouse.cpp b/snes/controller/mouse/mouse.cpp
index 6b26fae..1a066b9 100755
--- a/snes/controller/mouse/mouse.cpp
+++ b/snes/controller/mouse/mouse.cpp
@@ -3,9 +3,13 @@
 uint2 Mouse::data() {
   if(counter >= 32) return 1;
 
-  int position_x = interface->inputPoll(port, Input::Device::Mouse, 0, (unsigned)Input::MouseID::X);  //-n = left, 0 = center, +n = right
-  int position_y = interface->inputPoll(port, Input::Device::Mouse, 0, (unsigned)Input::MouseID::Y);  //-n = up,   0 = center, +n = down
+  if(counter == 0) {
+    _position_x = interface->inputPoll(port, Input::Device::Mouse, 0, (unsigned)Input::MouseID::X);  //-n = left, 0 = center, +n = right
+    _position_y = interface->inputPoll(port, Input::Device::Mouse, 0, (unsigned)Input::MouseID::Y);  //-n = up,   0 = center, +n = down
+  }
 
+  int position_x = _position_x;
+  int position_y = _position_y;
   bool direction_x = position_x < 0;  //0 = right, 1 = left
   bool direction_y = position_y < 0;  //0 = down,  1 = up
 
@@ -67,10 +71,16 @@ void Mouse::serialize(serializer& s) {
   unsigned char block[Controller::SaveSize] = {0};
   block[0] = latched ? 1 : 0;
   block[1] = counter;
+  block[2] = (unsigned short)_position_x >> 8;
+  block[3] = (unsigned short)_position_x;
+  block[4] = (unsigned short)_position_y >> 8;
+  block[5] = (unsigned short)_position_y;
   s.array(block, Controller::SaveSize);
   if(s.mode() == nall::serializer::Load) {
     latched = (block[0] != 0);
     counter = block[1];
+    _position_x = (short)(((unsigned short)block[2] << 8) | (unsigned short)block[3]);
+    _position_y = (short)(((unsigned short)block[4] << 8) | (unsigned short)block[5]);
   }
 }
 
diff --git a/snes/controller/mouse/mouse.hpp b/snes/controller/mouse/mouse.hpp
index b66ea51..b07c8ab 100755
--- a/snes/controller/mouse/mouse.hpp
+++ b/snes/controller/mouse/mouse.hpp
@@ -6,4 +6,6 @@ struct Mouse : Controller {
 private:
   bool latched;
   unsigned counter;
+  int _position_x;
+  int _position_y;
 };
-- 
1.8.4.4

