#include #include #include #include #include using std::cout; using std::endl; using sf::Text; int main () { // Create the main window sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Joystick Use", sf::Style::Default); sf::Event e; int buttonCount; int turbo = 1; // this can be changed bool connected; connected = sf::Joystick::isConnected(0); if (connected) { std::cout << "Joystick 0 is connected "; buttonCount = sf::Joystick::getButtonCount(0); cout << " button count = " << buttonCount << endl; } else { std::cout << "No game controller connected" << endl; } sf::RectangleShape square; square.setFillColor(sf::Color(255, 0, 0, 255)); square.setPosition(static_cast(window.getSize().x / 2), static_cast(window.getSize().y / 2)); square.setOutlineColor(sf::Color(0, 0, 0, 255)); square.setSize(sf::Vector2f(50.f, 50.f)); sf::Joystick::Identification id = sf::Joystick::getIdentification(0); std::cout << "\nVendor ID: " << id.vendorId << "\nProduct ID: " << id.productId << std::endl; sf::String controller("Joystick Use: " + id.name); window.setTitle(controller);//easily tells us what controller is connected bool hasAxis; hasAxis = sf::Joystick::hasAxis(0, sf::Joystick::X); if (hasAxis) cout << "Has X axis\n"; hasAxis = sf::Joystick::hasAxis(0, sf::Joystick::Y); if (hasAxis) cout << "Has Y axis\n"; hasAxis = sf::Joystick::hasAxis(0, sf::Joystick::Z); if (hasAxis) cout << "Has Z axis\n"; // Load font sf::Font myFont; if (!myFont.loadFromFile("steelfish_rg.ttf")) { cout << "Error loading font file!" << endl; return 1; } Text buttonText ("", myFont); //for movement sf::Vector2f speed = sf::Vector2f(0.f,0.f); // time sf::Clock tickClock; sf::Time timeSinceLastUpdate = sf::Time::Zero; sf::Time TimePerFrame = sf::seconds(1.f / 60.f); // Start the game loop while (window.isOpen()) { if (connected) buttonText.setString(std::string("Press any button\nEsc to exit ")); else { buttonText.setString(std::string("Please connect a game controller")); connected = sf::Joystick::isConnected(0); } // Process events bool running = true; while (running) { while (window.pollEvent(e)) { if (e.type == sf::Event::KeyPressed) { switch (e.key.code) { case sf::Keyboard::Escape: { window.close(); return 0; } break; default: break; } } if (e.type == sf::Event::JoystickMoved) { std::cout << "X axis: " << speed.x << std::endl; std::cout << "Y axis: " << speed.y << std::endl; } if (sf::Joystick::isButtonPressed(0, 0)) //"A" button on the XBox 360 controller { turbo = 2; } if (!sf::Joystick::isButtonPressed(0, 0)) { turbo = 1; } if(sf::Joystick::isButtonPressed(0,1)) //"B" button on the XBox 360 controller { window.close(); return 0; } } speed = sf::Vector2f(sf::Joystick::getAxisPosition(0, sf::Joystick::X), sf::Joystick::getAxisPosition(0, sf::Joystick::Y)); timeSinceLastUpdate += tickClock.restart(); while (timeSinceLastUpdate > TimePerFrame) { timeSinceLastUpdate -= TimePerFrame; //update the position of the square according to input from joystick //CHECK DEAD ZONES - OTHERWISE INPUT WILL RESULT IN JITTERY MOVEMENTS WHEN NO INPUT IS PROVIDED //INPUT RANGES FROM -100 TO 100 //A 15% DEAD ZONE SEEMS TO WORK FOR ME - GIVE THAT A SHOT if (speed.x > 15.f || speed.x < -15.f || speed.y > 15.f || speed.y < -15.f) square.move(turbo*speed.x*TimePerFrame.asSeconds(), turbo*speed.y*TimePerFrame.asSeconds()); } // Check for buttons for (unsigned int i = 0; i < 10; i++) if (sf::Joystick::isButtonPressed (0, i)) std::cout << "Button " << i << " pressed\n"; // Clear screen window.clear(); window.draw(buttonText); window.draw(square); // Update the window window.display(); } system("pause"); return 0; } }