Epple-II/src/paddles.cpp

103 lines
2.9 KiB
C++

/*
epple2
Copyright (C) 2008 by Christopher A. Mosher <cmosher01@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY, without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "e2const.h"
#include "paddles.h"
#include "screenimage.h"
#include <wx/gdicmn.h>
#include <wx/window.h>
#include <boost/log/trivial.hpp>
#include <iostream>
#include <ostream>
Paddles::Paddles(ScreenImage* gui) : scrn(gui), rTick(PADDLE_COUNT) {
}
Paddles::~Paddles() {
}
void Paddles::tick() {
for (int paddle = 0; paddle < PADDLE_COUNT; ++paddle) {
if (this->rTick[paddle] > 0) {
--this->rTick[paddle];
}
}
}
void Paddles::startTimers() {
try {
tryStartPaddleTimers();
} catch (...) {
BOOST_LOG_TRIVIAL(error) << "Warning: cannot start paddle timers; mouse will not function as paddles.";
}
}
wxPoint Paddles::current_mouse_position() {
const auto p = ::wxGetMousePosition();
int x,y;
this->scrn->getPos(&x,&y);
return p-wxPoint(x,y);
}
void Paddles::tryStartPaddleTimers() {
// wxWindow *pwin = ::wxGetActiveWindow();
// const wxPoint w = pwin->GetPosition(); // crash
// BOOST_LOG_TRIVIAL(info) << "x: " << w.x << ", y: " << w.y;
// constwxPoint m = ::wxGetMousePosition();
// const wxPoint p = w-m;
const wxPoint p = current_mouse_position();
double pMin = 0;
double pMax = 500;
int x = (int) ((p.x - pMin) / (pMax - pMin) * PADDLE_CYCLES + .5);
int y = (int) ((p.y - pMin) / (pMax - pMin) * PADDLE_CYCLES + .5);
if (isTimedOut(0)) {
this->rTick[0] = x;
}
if (isTimedOut(1)) {
this->rTick[1] = y;
}
/*
Here we emulate having 4700 ohm across pins 7 and 1
of the game controller, and a 47K ohm resistor across
pins 11 and 1, to give cheap real-time clocks at
paddles 2 and 3. Paddle 2 is the 100 microsecond reference,
and paddle 3 is the 1 millisecond reference. This is
described in U.A.2, p. 7-33.
*/
if (isTimedOut(2)) {
this->rTick[2] = E2Const::AVG_CPU_HZ / 10000; // was 90, but why?
}
if (isTimedOut(3)) {
this->rTick[3] = E2Const::AVG_CPU_HZ / 1000;
}
}
bool Paddles::isTimedOut(const int paddle) {
if (paddle < 0 || PADDLE_COUNT <= paddle) {
return false;
}
return this->rTick[paddle] <= 0;
}