Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:31:17+00:00 2026-06-06T16:31:17+00:00

I’m new in Allegro 5, I’ve written some code with the few tutorials there

  • 0

I’m new in Allegro 5, I’ve written some code with the few tutorials there are so far, but I cannot get the joystick sticks input.

Here the code, it is just two bars moving perpendicularly(and a very lazy approach, just swapped the x an y coordinates on the second bar).

#pragma comment (lib,"allegro-5.0.2-monolith-md-debug.lib")
#include <stdio.h>
#include <string>
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>

#define FPS 0xFF
#define SCREEN_W 1000
#define SCREEN_H 750
#define BAR_W 75
#define BAR_H 10

enum KEYS
{
    KEY_LEFT, KEY_RIGHT
};

int main(int argc, char **argv)
{
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_JOYSTICK *joystick = NULL;
    ALLEGRO_TIMER *timer = NULL;
    ALLEGRO_BITMAP *bouncer = NULL;
    float bouncer_x = SCREEN_W / 2.0 - BAR_W / 2.0;
    float bouncer_y = SCREEN_H - BAR_H;
    bool key[2] = {false, false};
    bool redraw = true;
    bool doexit = false;

    if(!al_init())
    {
        fprintf(stderr, "failed to initialize allegro!\n");
        return -1;
    }

    if(!al_install_keyboard())
    {
        fprintf(stderr, "failed to initialize the keyboard!\n");
        return -1;
    }

    if(!al_install_joystick())
    {
        fprintf(stderr, "failed to initialize the joystick!\n");
    }
    al_reconfigure_joysticks();
    joystick=al_get_joystick(al_get_num_joysticks()-1);

    timer = al_create_timer(1.0 / FPS);
    if(!timer)
    {
        fprintf(stderr, "failed to create timer!\n");
        return -1;
    }

    display = al_create_display(SCREEN_W, SCREEN_H);
    if(!display)
    {
        fprintf(stderr, "failed to create display!\n");
        al_destroy_timer(timer);
        return -1;
    }

    bouncer = al_create_bitmap(BAR_W, BAR_H);
    if(!bouncer)
    {
        fprintf(stderr, "failed to create bouncer bitmap!\n");
        al_destroy_display(display);
        al_destroy_timer(timer);
        return -1;
    }

    al_set_target_bitmap(bouncer);
    al_clear_to_color(al_map_rgb(0, 0, 255));
    al_set_target_bitmap(al_get_backbuffer(display));

    event_queue = al_create_event_queue();
    if(!event_queue)
    {
        fprintf(stderr, "failed to create event_queue!\n");
        al_destroy_bitmap(bouncer);
        al_destroy_display(display);
        al_destroy_timer(timer);
        return -1;
    }

    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_joystick_event_source());
    al_clear_to_color(al_map_rgb(0,0,0));

    al_flip_display();
    al_start_timer(timer);

    while(!doexit)
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);


        if(ev.type == ALLEGRO_EVENT_TIMER)
        {

            if(key[KEY_LEFT] && bouncer_x >= 2.0)
                bouncer_x -= 2.0;

            if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BAR_W - 2.0)
                bouncer_x += 2.0;

            redraw = true;
        }

        else if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS
                && ev.joystick.stick == 0
                && ev.joystick.axis == 0)
        {        
            float joypos=ev.joystick.pos;

            if(joypos<0 && bouncer_x >= 2.0)
                bouncer_x-=joypos;

            if(joypos>0 && bouncer_x <= SCREEN_W - BAR_W - 2.0)
                bouncer_x+=joypos;

            if(joypos=0)
                bouncer_x=SCREEN_W/2;
        }

        else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
            break;

        else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            switch(ev.keyboard.keycode)
            {
            case ALLEGRO_KEY_LEFT:
                key[KEY_LEFT] = true;
                break;

            case ALLEGRO_KEY_RIGHT:
                key[KEY_RIGHT] = true;
                break;
            }
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch(ev.keyboard.keycode)
            {
            case ALLEGRO_KEY_LEFT: 
                key[KEY_LEFT] = false;
                break;

            case ALLEGRO_KEY_RIGHT:
                key[KEY_RIGHT] = false;
                break;

            case ALLEGRO_KEY_ESCAPE:
                doexit = true;
                break;
            }
        }

        if(redraw && al_is_event_queue_empty(event_queue))
        {
            std::string str=al_get_joystick_name(joystick);
            redraw = false;

            al_clear_to_color(al_map_rgb(0,0,0));

            al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
            al_draw_bitmap(bouncer, bouncer_y, bouncer_x, 0);

            std::cout << ev.joystick.pos << " ";
            std::cout << str << " ";
            std::cout << al_get_joystick_active(joystick) << std::endl;

            al_flip_display();
        }
    }

    al_destroy_bitmap(bouncer);
    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);

    return 0;
}

Now, the trouble: ev.joystick.pos has a relatively static value (viewed trough MSVC2010 debugger), no matter if I have all the axis of my joystick at the max position.

Also, I don’t know how to get the value of an specific axis of an specific stick. I managed to get the bar move only when an specific axis and stick changes, but not “how much” it changes.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:31:21+00:00Added an answer on June 6, 2026 at 4:31 pm

    I can’t see what you’re doing wrong, but I got the controller working in my game, so maybe my code will shed some insight. By the way, I’m using an Xbox 360 controller. Actually, I’m using a GameStop brand 360 controller, so it’s not technically official. Anyway, here’s my relevant code (the full source is a monster):

        if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS){
            if(ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS){
            if(ev.joystick.stick == 0){ //float joys[3][2]
                joys[ev.joystick.stick][ev.joystick.axis] = ev.joystick.pos;
            }
         }
    
    
        void SetPosition(){
    
        int leftStick = 0;
        int rightStick = 1;
        int dPad = 2;
    
        int x = 0;
        int y = 1;
        int z = 2;
    
        bitmapX += joys[leftStick][x];
        bitmapY += joys[leftStick][y];
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.