Example #1
0
File: Input.cpp Project: 456z/gosu
 void pollGamepads()
 {
     // This gamepad is an OR-ed version of all the other gamepads. If button
     // 3 is pressed on any attached gamepad, down(gpButton3) will return
     // true. This is handy for singleplayer games.
     GamepadBuffer anyGamepad = { false };
     
     std::size_t availableGamepads = gameControllers.size() + joysticks.size();
     
     for (int i = 0; i < availableGamepads; ++i) {
         GamepadBuffer currentGamepad = { false };
         
         // Poll data from SDL, using either of two API interfaces.
         if (i < gameControllers.size()) {
             SDL_GameController *gameController = gameControllers[i];
             pollGameController(gameController, currentGamepad);
         }
         else {
             SDL_Joystick *joystick = joysticks[i - gameControllers.size()];
             pollJoystick(joystick, currentGamepad);
         }
         
         // Now at the same time, enqueue all events for this particular
         // gamepad, and OR the keyboard state into anyGamepad.
         int offset = gpRangeBegin + gpNumPerGamepad * (i + 1);
         for (int j = 0; j < currentGamepad.size(); ++j) {
             anyGamepad[j] = anyGamepad[j] || currentGamepad[j];
             
             if (currentGamepad[j] && !buttonStates[j + offset]) {
                 buttonStates[j + offset] = true;
                 enqueueEvent(j + offset, true);
             }
             else if (!currentGamepad[j] && buttonStates[j + offset]) {
                 buttonStates[j + offset] = false;
                 enqueueEvent(j + offset, false);
             }
         }
     }
     
     // And lastly, enqueue events for the virtual "any" gamepad.
     for (int j = 0; j < anyGamepad.size(); ++j) {
         if (anyGamepad[j] && !buttonStates[j + gpRangeBegin]) {
             buttonStates[j + gpRangeBegin] = true;
             enqueueEvent(j + gpRangeBegin, true);
         }
         else if (!anyGamepad[j] && buttonStates[j + gpRangeBegin]) {
             buttonStates[j + gpRangeBegin] = false;
             enqueueEvent(j + gpRangeBegin, false);
         }
     }
 }
Example #2
0
GamePadDialog::GamePadDialog(InputState* state, QWidget *parent) :
	QDialog(parent),
	ui(new Ui::GamePadDialog),
	m_inputState(state),
#if QT_HAS_SDL
	m_joystick(0),
#endif
	m_isInit(false)
{
	ui->setupUi(this);
	SetViewMode();

#if QT_HAS_SDL
	SDL_Init(SDL_INIT_JOYSTICK);
#endif
	m_isInit = true;

	data_timer = new QTimer();
	data_timer->setInterval(50);
	connect(data_timer,SIGNAL(timeout()),this,SLOT(pollJoystick()));
	data_timer->start();

	for(int i=0;i<18;i++)
	{
		QLabel* labelPreview = findChild<QLabel*>(GamepadPadMapping[i].ViewLabelName);
		if(labelPreview)
		{
			labelPreview->setVisible(false);
		}

		if(g_Config.iMappingMap.find(i+configOffset) != g_Config.iMappingMap.end())
		{
			GetMappingFromInt(g_Config.iMappingMap[i+configOffset],
							  GamepadPadMapping[i].mapping_in,
							  GamepadPadMapping[i].mapping_type,
							  GamepadPadMapping[i].mapping_sign);
		}
	}

	on_refreshListBtn_clicked();
}
Example #3
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow), sentBytes(NULL)
{
    ui->setupUi(this);

    //---- Menu Configuration ----
    machineGroup    = new QActionGroup(this);
    machineGroup->addAction(ui->actionScorpio);
    machineGroup->addAction(ui->actionQuadcopter);
    ui->actionScorpio->setChecked(true);
    viewGroup       =   new QActionGroup(this);
    viewGroup->addAction(ui->actionTracking);
    viewGroup->addAction(ui->actionTracking);
    viewGroup->addAction(ui->actionManual_Control);
    viewGroup->addAction(ui->actionCommand_Center);
    ui->actionCommand_Center->setChecked(true);
    //---- ----

    //---- Statusbar ----
    serialConnected=false;
    gpsFix=false;
    joystickConnected=false;
    gpsFooterStat       = new QLabel(this);
    connectedFooterStat = new QLabel(this);
    joystickFooterStat  = new QLabel(this);
    aliveFooterState    = new QLabel(this);
    connectedGPSFooterStat = new QLabel(this);
    ui->statusBar->addPermanentWidget(gpsFooterStat);
    ui->statusBar->addPermanentWidget(connectedFooterStat);
    ui->statusBar->addPermanentWidget(aliveFooterState);
    ui->statusBar->addPermanentWidget(joystickFooterStat);
    ui->statusBar->addPermanentWidget(connectedGPSFooterStat);
    //----  ----

    //---- Compass Graphics View ----
    compassPixmap = new QPixmap(":/img/CompassSquare150.png");
    ui->compassViewLabel->setPixmap(rotatePixmap(compassPixmap,0));
    //----  ----
    //---- Speedometer Graphics View ----
    ui->speedometerViewLabel->setPixmap(updateSpeedIndicator(0));
    //----  ----
    //---- Steer Graphics View ----
    ui->steerGraphicsView->setStyleSheet("background: transparent");
    steerGraphicsScene = new QGraphicsScene(this);
    QBrush  brush(QColor::fromRgb(59,174,227));
    steerRailIndicator = steerGraphicsScene->addRect(0,22.5,148,15,Qt::NoPen,brush);
    brush.setColor(Qt::red);
    QPen    pen(Qt::black);
    pen.setWidth(2);
    steerIndicator = steerGraphicsScene->addRect(67.5,5,15,50,Qt::NoPen,brush);
    updateSteerIndicator(-100);
    ui->steerGraphicsView->setScene(steerGraphicsScene);
    //----  ----

    //---- Tracker View ----
    ui->trackerGraphicsView->
            setBackgroundBrush(QBrush(QColor::fromRgb(237,200,90),Qt::Dense4Pattern));
    trackerScene = new QGraphicsScene(this);
    ui->trackerGraphicsView->setScene(trackerScene);
    QPolygonF NorthArrow;
    NorthArrow.append(QPointF(-5.,0)); NorthArrow.append(QPointF(0.,-20)); NorthArrow.append(QPointF(5.,0));
    NorthArrow.append(QPointF(0.,-5.)); NorthArrow.append(QPointF(-5.,0));
    brush.setColor(Qt::black);
    northArrowGraphicsItem = trackerScene->addPolygon(NorthArrow,Qt::NoPen,brush);
    northArrowGraphicsItem->setPos(800,10);
    northArrowGraphicsItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
    //Scorpio Graphics Item
    scorpio2DGraphicsItem = new GraphicsItemScorpio;
    trackerScene->addItem(scorpio2DGraphicsItem);
    scorpio2DGraphicsItem->setPos(500,150);
    wgs2utm(BASE_LATITUDE, BASE_LONGITUDE,&baseEasting,&baseNorthing, &baseUTMZone);
    lastPoKnown=false;
    //----  ----
    brush.setColor(QColor(255, 0, 0, 180));
    targetMarker    =   trackerScene->addEllipse(400,100,10,10,pen,brush);
    brush.setColor(QColor(255, 255, 0, 180));
    baseMarker      =   trackerScene->addRect(0,0,10,10,pen,brush);
    //----  ----
    connect(&timerTracker,SIGNAL(timeout()),this,SLOT(updateTrackerGraphics()));
    //TODO: another sig-slot for updating scene to zoom to fit
    timerTracker.start(100);
    //---- ---- ---- ----

    //---- Joystick Initialization ----
    myJoystick = new RR_SDLJoystick;
    myJoystickData = new RR_SDLJoystickData_t;
    QString *joystickInitError;

    if(!myJoystick->initJoystick(myJoystickData, joystickInitError)){
    ui->plainTextEdit->insertPlainText("Error Connecting Joystick");
    //TODO: Fix so that the insertPlainText takes the error code from Joystick Object.
    }
    else{
       ui->plainTextEdit->insertPlainText("Joystick Connected!");
       joystickConnected=true;
       connect(&timerJoystick,SIGNAL(timeout()),myJoystick ,SLOT(pollJoystick()));
       timerJoystick.start(150);
    }
    connect(myJoystick,SIGNAL(gotJoystick()),this,SLOT(updateGaugeIndicators()));
    connect(myJoystick,SIGNAL(gotJoystick()),this,SLOT(updateJoystickTelemetry()));
    //---- ----

    //---- Serial ----
    serialPort = new RR_QtSerial(57600,"COM26");

    connect(&timerSerial,SIGNAL(timeout()), serialPort,SLOT(receiveBytes()));
    connect(ui->actionConnect,SIGNAL(triggered(bool)),serialPort,SLOT(switchSerial(bool)));
    connect(serialPort,SIGNAL(gotConnected(bool)),this,SLOT(updateSerialStatusBar(bool)));
    connect(serialPort,SIGNAL(gotConnected(bool)),ui->actionConnect,SLOT(setChecked(bool)));
    timerSerial.start(150);
    //----- GPS ------
    serialGPSPort = new RR_QtSerial(57600,"COM19");
    connect(&timerGPSSerial, SIGNAL(timeout()),serialGPSPort, SLOT(receiveBytes()));
    connect(ui->actionConnect_GPS, SIGNAL(triggered(bool)), serialGPSPort, SLOT(switchSerial(bool)));
    connect(serialGPSPort,SIGNAL(gotConnected(bool)),this,SLOT(updateGPSSerialStatusBar(bool)));
    connect(serialGPSPort,SIGNAL(gotConnected(bool)),ui->actionConnect_GPS,SLOT(setChecked(bool)));
    timerGPSSerial.start(150);

    //-------
    //---- GPS Data Serialization ----
    receivedGPSData   = new RR_QtTelemetryReceivedData_t;
    sentGPSData       = new RR_QtTelemetrySentData_t;
    gps               = new RR_QtTelemetry(receivedGPSData,sentGPSData);
    receivedGPSData->fix=false;
    connect(serialGPSPort,SIGNAL(gotBytes(QByteArray*)),gps,SLOT(decodeBytes(QByteArray*)));
    connect(gps,SIGNAL(gotDecodedMessage()),this,SLOT(updateGPSData()));
    //----  ----
    //---- Telemetry ----
    receivedTelemetryData   = new RR_QtTelemetryReceivedData_t;
    sentTelemetryData       = new RR_QtTelemetrySentData_t;
    telemetry               = new RR_QtTelemetry(receivedTelemetryData,sentTelemetryData);
    receivedTelemetryData->fix=false;
    connect(this,SIGNAL(gotJoystickMessage()),telemetry,SLOT(sendMessage()));
    connect(telemetry,SIGNAL(gotEncodedBytes(QByteArray*)),serialPort,SLOT(sendBytes(QByteArray*)));
    connect(serialPort,SIGNAL(gotBytes(QByteArray*)),telemetry,SLOT(decodeBytes(QByteArray*)));
    connect(telemetry,SIGNAL(gotDecodedMessage()),this,SLOT(updateTelemetryGraphics()));
    //----  ----
    //---- Text Display ----
    connect(&timerSerialPrint,SIGNAL(timeout()),this, SLOT(printStuff()));
    timerSerialPrint.start(ui->printfreSlider->value());
    connect(ui->printfreSlider, SIGNAL(sliderMoved(int)),&timerSerialPrint,SLOT(start(int)));
    connect(ui->clearPushButton,SIGNAL(clicked()),ui->plainTextEdit,SLOT(clear()));
    connect(ui->printfreSlider,SIGNAL(sliderMoved(int)),this,SLOT(setPrintFrequencyText()));
    setPrintFrequencyText();
    //----  ----
    //---- Test and Debug ----
    connect(&timerTestDebug,SIGNAL(timeout()),this,SLOT(testdebug()));

    timerTestDebug.start(1000);
    //----  ----

    connect(&timerStatusBarUpdate, SIGNAL(timeout()), this, SLOT(updateGPSStatbar()));
    timerStatusBarUpdate.start(500);

    //Pulse Check
    connect(&timerPulseCheck,SIGNAL(timeout()), this, SLOT(checkPulse()));
    pulseSamplingTime = 5000;
    timerPulseCheck.start(pulseSamplingTime);
}
Example #4
0
void playGame(void)
{
    bool gameLoaded = false;
    uint8_t ch;
    
    gScoreBar = 0;
    gShouldSave = false;
    
    printf("\n\nChecking for a saved game...");
    
    if (loadGame()) {
        bool gotAnswer = false;
        
        printf("\n\nYou have a saved game!\n    Would you like to continue it (Y/N)");
        
        while (!gotAnswer) {
            ch = cgetc();
            switch (ch) {
                case 'y':
                case 'Y':
                    printf("\n\nLoading your saved puzzle");
                    gotAnswer = true;
                    gShouldSave = true;
                    gameLoaded = true;
                    break;
                    
                case 'n':
                case 'N':
                    gotAnswer = true;
                    break;
                    
                default:
                    badThingHappened();
                    break;
            }
        }
    }
    
    showAndClearDblLoRes();
    if (!gameLoaded) {
        startNewGame();
    }
    drawBoard();
    speakGo();
    while (true) {
        resetStarAnim();
        
        while (true) {
            doStarAnim();
            
            if (pollKeyboard()) {
                break;
            }
            
            if ((gGameOptions.enableJoystick) &&
                (pollJoystick())) {
                break;
            }
            
            if ((gGameOptions.enableMouse) &&
                (pollMouse())) {
                break;
            }
        }
        
        if (gameIsOver()) {
            endGame();
            showAndClearDblLoRes();
            refreshScore(0);
            startNewGame();
            gShouldSave = false;
        }
    }
}