Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    2
  • comments
    0
  • views
    220

Still coding in I/O for C-Lesh

Sign in to follow this  
francoisdiy

458 views

You can take a look at this here but basically I'm converting keyboard, mouse, and gamepad input into signals and then buffering them and removing one signal per frame. I think this is ingenious!

void Cio::Rprocess() {
sf::Clock clock;
// Check for gamepad connection.
if (sf::Joystick::isConnected(0)) {
if (sf::Joystick::getButtonCount(0) == BUTTON_COUNT) {
this->gamepad_connected = true;
}
}
// Try to load gamepad map.
std::ifstream config_file("gamepad.cfg");
if (config_file) {
for (int button_index = 0; button_index < BUTTON_COUNT; button_index++) {
config_file >> this->button_map[button_index];
}
}
else {
this->Rmap_gamepad(); // Do a mapping.
}
// Do window processing here and block.
bool paused = false;
while (!this->window->isOpen()) {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window->close();
}
if (event.type == sf::Event::LostFocus) {
paused = true;
}
if (event.type == sf::Event::GainedFocus) {
paused = true;
}
if (!paused) {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
// Process mouse and keyboard events.
this->Rprocess_keys();
this->Rprocess_mouse();
// Process mouse movement.
sf::Vector2i coords = sf::Mouse::getPosition();
if (event.type == sf::Event::MouseMoved) {
signal.code = SIGNAL_MOVE;
signal.x = coords.x;
signal.y = coords.y;
this->sig_buffer.push_back(signal);
}
else if (event.type == sf::Event::MouseEntered) {
signal.code = SIGNAL_ENTERED;
signal.x = coords.x;
signal.y = coords.y;
this->sig_buffer.push_back(signal);
}
else if (event.type == sf::Event::MouseLeft) {
signal.code = SIGNAL_LEAVE;
signal.x = coords.x;
signal.y = coords.y;
this->sig_buffer.push_back(signal);
}
// Process the gamepad.
if (event.type == sf::Event::JoystickConnected) {
this->gamepad_connected = true;
}
if (event.type == sf::Event::JoystickDisconnected) {
this->gamepad_connected = false;
}
this->Rprocess_gamepad();
// Generate a blanking signal.
sf::Time elapsed = clock.getElapsedTime();
if (elapsed.asMilliseconds() >= (1000 / FPS)) {
clock.restart();
// Grab a signal and store it in the register.
if (this->sig_buffer.size() > 0) {
this->signal_reg = this->sig_buffer.front();
this->sig_buffer.erase(this->sig_buffer.begin());
}
// Clear back buffer.
this->back_buffer->clear();
// Run code here.
}
}
}
// Do buffer rendering.
this->back_buffer->display();
sf::Texture& texture = this->back_buffer->getTexture();
sf::Sprite buffer_sprite(texture);
window->draw(buffer_sprite);
// Do window rendering.
window->clear();
window->display();
}
}
 
void Cio::Rprocess_keys() {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
signal.code = SIGNAL_LEFT;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
signal.code = SIGNAL_RIGHT;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
signal.code = SIGNAL_UP;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
signal.code = SIGNAL_DOWN;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_Y;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::X)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_B;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_A;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_X;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_START;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_SELECT;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_A;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_L;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_R;
this->sig_buffer.push_back(signal);
}
}
 
void Cio::Rprocess_mouse() {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
signal.code = SIGNAL_MOUSE;
sf::Vector2i position = sf::Mouse::getPosition();
signal.x = position.x;
signal.y = position.y;
signal.button = BUTTON_LEFT;
this->sig_buffer.push_back(signal);
}
else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
signal.code = SIGNAL_MOUSE;
sf::Vector2i position = sf::Mouse::getPosition();
signal.x = position.x;
signal.y = position.y;
signal.button = BUTTON_RIGHT;
this->sig_buffer.push_back(signal);
}
}
 
void Cio::Rprocess_gamepad() {
if (this->gamepad_connected) {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
// Poll the gamepad to update positions.
sf::Joystick::update();
float x_pos = sf::Joystick::getAxisPosition(0, sf::X);
float y_pos = sf::Joystick::getAxisPosition(0, sf::Y);
if (x_pos == -100.0) { // Values should be standardized.
signal.code = SIGNAL_LEFT;
this->sig_buffer.push_back(signal);
}
else if (x_pos == 100.0) {
signal.code = SIGNAL_RIGHT;
this->sig_buffer.push_back(signal);
}
if (y_pos == -100.0) {
signal.code = SIGNAL_UP;
this->sig_buffer.push_back(signal);
}
else if (y_pos == 100.0) {
signal.code = SIGNAL_DOWN;
this->sig_buffer.push_back(signal);
}
// Check for button presses using button map.
for (int button_index = 0; button_index < BUTTON_COUNT; button_index++) {
int button = this->button_map[button_index];
if (sf::Joystick::isButtonPressed(0, button)) {
signal.button = this->button_signal_map[button_index]; // Assign the button signal.
this->sig_buffer.push_back(signal);
break;
}
}
}
}
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!