diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index 08aade138..cc0d7157b 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -873,7 +873,7 @@ unique_ptr Console::getControllerPort(const Controller::Type type, break; case Controller::Type::Lightgun: - controller = make_unique(port, myEvent, *mySystem); + controller = make_unique(port, myEvent, *mySystem, myOSystem.frameBuffer()); break; default: diff --git a/src/emucore/Lightgun.cxx b/src/emucore/Lightgun.cxx index 2b116a0ac..8f23f34d3 100644 --- a/src/emucore/Lightgun.cxx +++ b/src/emucore/Lightgun.cxx @@ -18,12 +18,14 @@ #include "TIA.hxx" #include "System.hxx" +#include "FrameBuffer.hxx" #include "Lightgun.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Lightgun::Lightgun(Jack jack, const Event& event, const System& system) - : Controller(jack, event, system, Controller::Type::Lightgun) +Lightgun::Lightgun(Jack jack, const Event& event, const System& system, const FrameBuffer& frameBuffer) + : Controller(jack, event, system, Controller::Type::Lightgun), + myFrameBuffer(frameBuffer) { } @@ -39,22 +41,16 @@ bool Lightgun::read(DigitalPin pin) // Pin 6: INPT4/5 case DigitalPin::Six: { - // TODO: scale correctly, current code assumes 2x zoom and no vertical scaling - Int32 xpos = myMouseX / 4; - Int32 ypos = myMouseY / 2; - uInt32 ux; - uInt32 uy; - - mySystem.tia().electronBeamPos(ux, uy); + const Common::Rect& rect = myFrameBuffer.imageRect(); + // scale mouse coordinates into TIA coordinates + Int32 xpos = (myMouseX - rect.x()) * TIAConstants::H_PIXEL / rect.w(); + Int32 ypos = (myMouseY - rect.y()) * 210 / rect.h(); // TODO: replace "magic number" + // get adjusted TIA coordinates Int32 x = mySystem.tia().clocksThisLine() - TIAConstants::H_BLANK_CLOCKS + X_OFS; + Int32 y = mySystem.tia().scanlines() - mySystem.tia().startLine() + Y_OFS; if (x < 0) x += TIAConstants::H_CLOCKS; - Int32 y = uy + Y_OFS; - - //cerr << "mouse:" << xpos << " " << ypos << endl; - //cerr << " beam:" << x << " " << y << endl; - bool enable = !((x - xpos) >= 0 && (x - xpos) < 15 && (y - ypos) >= 0); diff --git a/src/emucore/Lightgun.hxx b/src/emucore/Lightgun.hxx index 34de0f7a6..739dcd22a 100644 --- a/src/emucore/Lightgun.hxx +++ b/src/emucore/Lightgun.hxx @@ -38,7 +38,7 @@ public: @param event The event object to use for events @param system The system using this controller */ - Lightgun(Jack jack, const Event& event, const System& system); + Lightgun(Jack jack, const Event& event, const System& system, const FrameBuffer& frameBuffer); virtual ~Lightgun() = default; public: @@ -69,10 +69,11 @@ public: bool isAnalog() const override { return true; } private: + const FrameBuffer& myFrameBuffer; Int32 myMouseX{0}, myMouseY{0}; static constexpr Int32 X_OFS = -21; - static constexpr Int32 Y_OFS = 10; + static constexpr Int32 Y_OFS = 5; // Lookup table for associating paddle buttons with controller pins static const Controller::DigitalPin ourButtonPin;