/* $Id: ps2padV.c,v 1.11 2002/09/21 13:03:49 slouken Exp $ */ #include #include #include #include "SDL.h" #include "SDL_image.h" #include "joypadlib.h" /* Draw a Gimpish background pattern to show transparency in the image */ void draw_background(SDL_Surface *screen) { Uint8 *dst = screen->pixels; int x, y; int bpp = screen->format->BytesPerPixel; Uint32 col[2]; col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66); col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99); for(y = 0; y < screen->h; y++) { for(x = 0; x < screen->w; x++) { /* use an 8x8 checkerboard pattern */ Uint32 c = col[((x ^ y) >> 3) & 1]; switch(bpp) { case 1: dst[x] = c; break; case 2: ((Uint16 *)dst)[x] = c; break; case 3: if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { dst[x * 3] = c; dst[x * 3 + 1] = c >> 8; dst[x * 3 + 2] = c >> 16; } else { dst[x * 3] = c >> 16; dst[x * 3 + 1] = c >> 8; dst[x * 3 + 2] = c; } break; case 4: ((Uint32 *)dst)[x] = c; break; } } dst += screen->pitch; } } void print_stats() { int i; int num_joypads; num_joypads = joypadlib_num_supported_joypads(); printf("Total joypads: %d\n", num_joypads); for (i = 0; i < num_joypads; i++) { switch (joypadlib_get_joypad_status(i)) { case PS2PAD_STAT_NOTCON: printf("Joystick %d: Not connected\n", i); break; case PS2PAD_STAT_READY: printf("Joystick %d: Ready\n", i); break; case PS2PAD_STAT_BUSY: printf("Joystick %d: Busy\n", i); break; case PS2PAD_STAT_ERROR: printf("Joystick %d: Error\n", i); break; } } printf("\n"); for (i = 0; i < num_joypads; i++) { switch (joypadlib_get_joypad_type(i)) { case PS2PAD_TYPE_NEJICON: printf("Joystick %d: Nejicon\n", i); break; case PS2PAD_TYPE_DIGITAL: printf("Joystick %d: Digital\n", i); break; case PS2PAD_TYPE_ANALOG: printf("Joystick %d: Analog\n", i); break; case PS2PAD_TYPE_DUALSHOCK: printf("Joystick %d: Dual shock\n", i); break; } } printf("\n"); for (i = 0; i < num_joypads; i++) { switch (joypadlib_joypad_supports_pressure(i)) { case 0: printf("Joystick %d: Doesn't support pressure sensitivity\n", i); break; case 1: printf("Joystick %d: Supports pressure sensitivity\n", i); break; } } printf("\n"); for (i = 0; i < num_joypads; i++) { int num_actuators = joypadlib_get_joypad_num_actuators(i); printf("Joystick %d num actuators: %d\n", i, num_actuators); } printf("\n"); } int main(void) { //Image vars Uint32 flags; SDL_Surface *screen, *image; int i, depth, done; SDL_Event event; char *pic; typedef struct{ int show; int x, y; SDL_Rect *srcRect; SDL_Rect dstRect; SDL_Surface *image; }moveableImage; moveableImage buttonImage[16]; //ps2pad vars unsigned long buttons; unsigned char button_pressures[PS2PAD_NUM_PRESSURE_BUTTONS]; int active_joypad; char *bi[] = {"RightArrow.png","LeftArrow.png","UpArrow.png","DownArrow.png","actionButton.png","actionButton.png","actionButton.png","actionButton.png","topButton.jpg","topButton.jpg","DownArrowBig.png","DownArrowBig.png","ControlButton.jpg","ControlButton.jpg","UpArrowBig.png","UpArrowBig.png"}; pic = "clear-black.jpg"; active_joypad = 0; /* Initialize the SDL library */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); return(255); } flags = SDL_SWSURFACE; image = IMG_Load(pic); if ( image == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n",pic,SDL_GetError()); //exit(1); } for(i=0; i<16; i++){ buttonImage[i].image = IMG_Load(bi[i]); if (buttonImage[i].image == NULL){ fprintf(stderr, "Couldn't load %s: %s\n",bi[i],SDL_GetError()); //exit(2); } buttonImage[i].dstRect.w = buttonImage[i].image->w; buttonImage[i].dstRect.h = buttonImage[i].image->h; } //Directional Pad Buttons' Alignments buttonImage[0].dstRect.x = 104; buttonImage[0].dstRect.y = 341; buttonImage[1].dstRect.x = 64; buttonImage[1].dstRect.y = 342; buttonImage[2].dstRect.x = 87; buttonImage[2].dstRect.y = 318; buttonImage[3].dstRect.x = 87; buttonImage[3].dstRect.y = 358; //Triange, Circle, X and Square Buttons' Alignments buttonImage[4].dstRect.x = 302; buttonImage[4].dstRect.y = 303; buttonImage[5].dstRect.x = 332; buttonImage[5].dstRect.y = 332; buttonImage[6].dstRect.x = 302; buttonImage[6].dstRect.y = 360; buttonImage[7].dstRect.x = 271; buttonImage[7].dstRect.y = 332; //L1 and R1 Buttons' Alignments buttonImage[8].dstRect.x = 73; buttonImage[8].dstRect.y = 257; buttonImage[9].dstRect.x = 292; buttonImage[9].dstRect.y = 257; //L2 and R2 Buttons' Alignments buttonImage[10].dstRect.x = 85; buttonImage[10].dstRect.y = 220; buttonImage[11].dstRect.x = 305; buttonImage[11].dstRect.y = 220; //Select and Start Buttons' Alignments buttonImage[12].dstRect.x = 160; buttonImage[12].dstRect.y = 339; buttonImage[13].dstRect.x = 226; buttonImage[13].dstRect.y = 339; //L3 and R3 Buttons' Alignments buttonImage[14].dstRect.x = 140; buttonImage[14].dstRect.y = 445; buttonImage[15].dstRect.x = 250; buttonImage[15].dstRect.y = 445; SDL_WM_SetCaption("ps2pad Viewer", "showimage"); /* Create a display for the image */ depth = SDL_VideoModeOK(image->w, image->h, 32, flags); /* Use the deepest native mode, except that we emulate 32bpp for viewing non-indexed images on 8bpp screens */ if ( depth == 0 ) { if ( image->format->BytesPerPixel > 1 ) depth = 32; else depth = 8; } else if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) depth = 32; if(depth == 8) flags |= SDL_HWPALETTE; screen = SDL_SetVideoMode(image->w, image->h, depth, flags); if ( screen == NULL ) fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",image->w, image->h, depth, SDL_GetError()); /* Set the palette, if one exists */ if ( image->format->palette ) SDL_SetColors(screen, image->format->palette->colors, 0, image->format->palette->ncolors); if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY)) draw_background(screen); /* Display the image */ SDL_BlitSurface(image, NULL, screen, NULL); SDL_UpdateRect(screen, 0, 0, 0, 0); /* initialize joypad */ joypadlib_initialize(); print_stats(); done = 0; while ( ! done ) { joypadlib_update(); if ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_KEYUP: switch (event.key.keysym.sym) { case SDLK_ESCAPE: case SDLK_SPACE: case SDLK_TAB: done = 1; break; default: break; } break; case SDL_MOUSEBUTTONDOWN: done = 1; break; case SDL_QUIT: done = 1; break; } }/* Check to see if the button selection has changed*/ else if(buttons != joypadlib_get_joypad_buttons(active_joypad, button_pressures, PRESSURE_TYPE_BYTE)){ /* If the button selection has changed clear the screen to just the bg image and save the selected buttons*/ SDL_BlitSurface(image, NULL, screen, NULL); buttons = joypadlib_get_joypad_buttons(active_joypad, button_pressures, PRESSURE_TYPE_BYTE); /* If Start and Select are both held down we are done*/ if((buttons & PS2PAD_BUTTON_START) && (buttons & PS2PAD_BUTTON_SELECT)) done = 1; else{/* Else find what buttons are down and update the screen */ if(buttons & PS2PAD_BUTTON_RIGHT) SDL_BlitSurface(buttonImage[0].image, NULL, screen, &buttonImage[0].dstRect); if(buttons & PS2PAD_BUTTON_LEFT) SDL_BlitSurface(buttonImage[1].image, NULL, screen, &buttonImage[1].dstRect); if(buttons & PS2PAD_BUTTON_UP) SDL_BlitSurface(buttonImage[2].image, NULL, screen, &buttonImage[2].dstRect); if(buttons & PS2PAD_BUTTON_DOWN) SDL_BlitSurface(buttonImage[3].image, NULL, screen, &buttonImage[3].dstRect); if(buttons & PS2PAD_BUTTON_TRIANGLE) SDL_BlitSurface(buttonImage[4].image, NULL, screen, &buttonImage[4].dstRect); if(buttons & PS2PAD_BUTTON_CIRCLE) SDL_BlitSurface(buttonImage[5].image, NULL, screen, &buttonImage[5].dstRect); if(buttons & PS2PAD_BUTTON_CROSS) SDL_BlitSurface(buttonImage[6].image, NULL, screen, &buttonImage[6].dstRect); if(buttons & PS2PAD_BUTTON_SQUARE) SDL_BlitSurface(buttonImage[7].image, NULL, screen, &buttonImage[7].dstRect); if(buttons & PS2PAD_BUTTON_L1) SDL_BlitSurface(buttonImage[8].image, NULL, screen, &buttonImage[8].dstRect); if(buttons & PS2PAD_BUTTON_R1) SDL_BlitSurface(buttonImage[9].image, NULL, screen, &buttonImage[9].dstRect); if(buttons & PS2PAD_BUTTON_L2) SDL_BlitSurface(buttonImage[10].image, NULL, screen, &buttonImage[10].dstRect); if(buttons & PS2PAD_BUTTON_R2) SDL_BlitSurface(buttonImage[11].image, NULL, screen, &buttonImage[11].dstRect); if(buttons & PS2PAD_BUTTON_SELECT) SDL_BlitSurface(buttonImage[12].image, NULL, screen, &buttonImage[12].dstRect); if(buttons & PS2PAD_BUTTON_START) SDL_BlitSurface(buttonImage[13].image, NULL, screen, &buttonImage[13].dstRect); if(buttons & PS2PAD_BUTTON_L3) SDL_BlitSurface(buttonImage[14].image, NULL, screen, &buttonImage[14].dstRect); if(buttons & PS2PAD_BUTTON_R3) SDL_BlitSurface(buttonImage[15].image, NULL, screen, &buttonImage[15].dstRect); } SDL_UpdateRect(screen, 0, 0, 0, 0); } else SDL_Delay(10); } /* We're done! */ SDL_FreeSurface(image); joypadlib_shutdown(); SDL_Quit(); return(0); }