Jump to content
  • Advertisement
Sign in to follow this  
daviangel

GPAIO 2nd edition

This topic is 5233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Has anyone been able to get scanjoystick and testjoystick programs from chapter 5 to run correctly? I get a crash on both macosx and windows using my logitech attack 3 joystick. here's the code using allegro and c:

#include <stdio.h>
#include "allegro.h"

#define WHITE makecol(255,255,255)
#define LTGREEN makecol(192,255,192)
#define LTRED makecol(255,192,192)
#define LTBLUE makecol(192,192,255)
int curline = 1;

void print(char *s, int color)
{
    //print text with automatic linefeed
    textout(screen, font, s, 10, (curline++) * 12, color);
}

void printjoyinfo()
{
    char *s;
    int n, ax;
    
    //display joystick information
    sprintf(s, "Number of Joysticks: %d", num_joysticks);
    print(s, WHITE);
    print("",0);
    
    //display stick information
    sprintf(s, "Number of Sticks: %d", joy[0].num_sticks);
    print(s, LTGREEN);
    for (n=0; n<joy[0].num_sticks; n++)
    {
        sprintf(s, "Stick %d: %s", n, joy[0].stick[n].name);
        print(s, LTGREEN);
        sprintf(s, "  Number of Axes: %d", joy[0].stick[n].num_axis);
        print(s, LTBLUE);
        for (ax=0; ax<joy[0].stick[n].num_axis; ax++)
        {
            sprintf(s,"    Axis %d: %s", ax, 
                joy[0].stick[n].axis[ax].name);
            print(s, LTRED);
        }
    }
    
    //display button information
    print("",0);
    sprintf(s, "Number of Buttons: %d", joy[0].num_buttons);
    print(s, LTGREEN);
    for (n=0; n<joy[0].num_buttons; n++)
    {
        sprintf(s,"  Button %d: %s", n, joy[0].button[n].name);
        print(s, LTBLUE);
    }
}

void main(void)
{
    int n, color, value;

    //initialize program    
    allegro_init();
    set_color_depth(16); // use 15,24,32 on macosx 
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    install_keyboard();

    //install the joystick handler
    install_joystick(JOY_TYPE_AUTODETECT);
    poll_joystick();

    //display title
    print("ScanJoystick Program", WHITE);
    print("---------------------------------------", WHITE);

    //look for a joystick
    if (num_joysticks > 0)
        printjoyinfo();
    else
        print("No joystick could be found", WHITE);
    
    //pause and exit
    print("",0);
    print("Press any key...", WHITE);
    while (!keypressed()) { }
    allegro_exit();
}

END_OF_MAIN();



any ideas?

Share this post


Link to post
Share on other sites
Advertisement
The thing i see is that:

Don't use "char* s". That's a pointer to ONE character, and the next sprintf will write the string to a random address this pointer points to.

Use "char s[260]".

Share this post


Link to post
Share on other sites
ok that worked thanks!
I figured that was the problem since on the code on the cd the author uses char* s="" instead of char* s?

Share this post


Link to post
Share on other sites
char* s="" might have been working if you're lucky. In that case s points to exactly one char (the 0 terminator). But if you write more than one char in there it's random memory again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!