test/testjoystick.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 19 Jan 2006 21:28:52 +0000
changeset 1257 448a9a64537b
parent 0 74212992fb08
child 1661 281d3f4870e5
permissions -rw-r--r--
[PATCH] SDL_GetVideoMode() does not find best mode, part 2

Following commit 1.51, I come accross a problem when SDL must choose between
several video modes that could suit the one asked.

If I ask 320x240 with this list:
768x480 768x240 640x400 640x200 384x480 384x240 320x400 320x200

The smallest selectables modes are 384x240 and 320x400. And SDL choose the later
in this list, but 384x240 is more suitable. So I added a check to compare
the pixel count (surface) of modes, and select the one which has the smallest
pixel count.

In my example, 384x240 has 92160 pixels, and 320x400 has 128000 pixels. So now
SDL will choose 384x240 for the asked 320x240 mode.
slouken@0
     1
slouken@0
     2
/* Simple program to test the SDL joystick routines */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
slouken@0
    10
#define SCREEN_WIDTH	640
slouken@0
    11
#define SCREEN_HEIGHT	480
slouken@0
    12
slouken@0
    13
void WatchJoystick(SDL_Joystick *joystick)
slouken@0
    14
{
slouken@0
    15
	SDL_Surface *screen;
slouken@0
    16
	const char *name;
slouken@0
    17
	int i, done;
slouken@0
    18
	SDL_Event event;
slouken@0
    19
	int x, y, draw;
slouken@0
    20
	SDL_Rect axis_area[2];
slouken@0
    21
slouken@0
    22
	/* Set a video mode to display joystick axis position */
slouken@0
    23
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
slouken@0
    24
	if ( screen == NULL ) {
slouken@0
    25
		fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
slouken@0
    26
		return;
slouken@0
    27
	}
slouken@0
    28
slouken@0
    29
	/* Print info about the joystick we are watching */
slouken@0
    30
	name = SDL_JoystickName(SDL_JoystickIndex(joystick));
slouken@0
    31
	printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
slouken@0
    32
	       name ? name : "Unknown Joystick");
slouken@0
    33
	printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
slouken@0
    34
	       SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
slouken@0
    35
	       SDL_JoystickNumBalls(joystick),SDL_JoystickNumButtons(joystick));
slouken@0
    36
slouken@0
    37
	/* Initialize drawing rectangles */
slouken@0
    38
	memset(axis_area, 0, (sizeof axis_area));
slouken@0
    39
	draw = 0;
slouken@0
    40
slouken@0
    41
	/* Loop, getting joystick events! */
slouken@0
    42
	done = 0;
slouken@0
    43
	while ( ! done ) {
slouken@0
    44
		while ( SDL_PollEvent(&event) ) {
slouken@0
    45
			switch (event.type) {
slouken@0
    46
			    case SDL_JOYAXISMOTION:
slouken@0
    47
				printf("Joystick %d axis %d value: %d\n",
slouken@0
    48
				       event.jaxis.which,
slouken@0
    49
				       event.jaxis.axis,
slouken@0
    50
				       event.jaxis.value);
slouken@0
    51
				break;
slouken@0
    52
			    case SDL_JOYHATMOTION:
slouken@0
    53
				printf("Joystick %d hat %d value:",
slouken@0
    54
				       event.jhat.which,
slouken@0
    55
				       event.jhat.hat);
slouken@0
    56
				if ( event.jhat.value == SDL_HAT_CENTERED )
slouken@0
    57
					printf(" centered");
slouken@0
    58
				if ( event.jhat.value & SDL_HAT_UP )
slouken@0
    59
					printf(" up");
slouken@0
    60
				if ( event.jhat.value & SDL_HAT_RIGHT )
slouken@0
    61
					printf(" right");
slouken@0
    62
				if ( event.jhat.value & SDL_HAT_DOWN )
slouken@0
    63
					printf(" down");
slouken@0
    64
				if ( event.jhat.value & SDL_HAT_LEFT )
slouken@0
    65
					printf(" left");
slouken@0
    66
				printf("\n");
slouken@0
    67
				break;
slouken@0
    68
			    case SDL_JOYBALLMOTION:
slouken@0
    69
				printf("Joystick %d ball %d delta: (%d,%d)\n",
slouken@0
    70
				       event.jball.which,
slouken@0
    71
				       event.jball.ball,
slouken@0
    72
				       event.jball.xrel,
slouken@0
    73
				       event.jball.yrel);
slouken@0
    74
				break;
slouken@0
    75
			    case SDL_JOYBUTTONDOWN:
slouken@0
    76
				printf("Joystick %d button %d down\n",
slouken@0
    77
				       event.jbutton.which,
slouken@0
    78
				       event.jbutton.button);
slouken@0
    79
				break;
slouken@0
    80
			    case SDL_JOYBUTTONUP:
slouken@0
    81
				printf("Joystick %d button %d up\n",
slouken@0
    82
				       event.jbutton.which,
slouken@0
    83
				       event.jbutton.button);
slouken@0
    84
				break;
slouken@0
    85
			    case SDL_KEYDOWN:
slouken@0
    86
				if ( event.key.keysym.sym != SDLK_ESCAPE ) {
slouken@0
    87
					break;
slouken@0
    88
				}
slouken@0
    89
				/* Fall through to signal quit */
slouken@0
    90
			    case SDL_QUIT:
slouken@0
    91
				done = 1;
slouken@0
    92
				break;
slouken@0
    93
			    default:
slouken@0
    94
				break;
slouken@0
    95
			}
slouken@0
    96
		}
slouken@0
    97
		/* Update visual joystick state */
slouken@0
    98
		for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) {
slouken@0
    99
			SDL_Rect area;
slouken@0
   100
slouken@0
   101
			area.x = i*34;
slouken@0
   102
			area.y = SCREEN_HEIGHT-34;
slouken@0
   103
			area.w = 32;
slouken@0
   104
			area.h = 32;
slouken@0
   105
			if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
slouken@0
   106
				SDL_FillRect(screen, &area, 0xFFFF);
slouken@0
   107
			} else {
slouken@0
   108
				SDL_FillRect(screen, &area, 0x0000);
slouken@0
   109
			}
slouken@0
   110
			SDL_UpdateRects(screen, 1, &area);
slouken@0
   111
		}
slouken@0
   112
slouken@0
   113
		/* Erase previous axes */
slouken@0
   114
		SDL_FillRect(screen, &axis_area[draw], 0x0000);
slouken@0
   115
slouken@0
   116
		/* Draw the X/Y axis */
slouken@0
   117
		draw = !draw;
slouken@0
   118
		x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768);
slouken@0
   119
		x *= SCREEN_WIDTH;
slouken@0
   120
		x /= 65535;
slouken@0
   121
		if ( x < 0 ) {
slouken@0
   122
			x = 0;
slouken@0
   123
		} else
slouken@0
   124
		if ( x > (SCREEN_WIDTH-16) ) {
slouken@0
   125
			x = SCREEN_WIDTH-16;
slouken@0
   126
		}
slouken@0
   127
		y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768);
slouken@0
   128
		y *= SCREEN_HEIGHT;
slouken@0
   129
		y /= 65535;
slouken@0
   130
		if ( y < 0 ) {
slouken@0
   131
			y = 0;
slouken@0
   132
		} else
slouken@0
   133
		if ( y > (SCREEN_HEIGHT-16) ) {
slouken@0
   134
			y = SCREEN_HEIGHT-16;
slouken@0
   135
		}
slouken@0
   136
		axis_area[draw].x = (Sint16)x;
slouken@0
   137
		axis_area[draw].y = (Sint16)y;
slouken@0
   138
		axis_area[draw].w = 16;
slouken@0
   139
		axis_area[draw].h = 16;
slouken@0
   140
		SDL_FillRect(screen, &axis_area[draw], 0xFFFF);
slouken@0
   141
slouken@0
   142
		SDL_UpdateRects(screen, 2, axis_area);
slouken@0
   143
	}
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
int main(int argc, char *argv[])
slouken@0
   147
{
slouken@0
   148
	const char *name;
slouken@0
   149
	int i;
slouken@0
   150
	SDL_Joystick *joystick;
slouken@0
   151
slouken@0
   152
	/* Initialize SDL (Note: video is required to start event loop) */
slouken@0
   153
	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) {
slouken@0
   154
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   155
		exit(1);
slouken@0
   156
	}
slouken@0
   157
slouken@0
   158
	/* Print information about the joysticks */
slouken@0
   159
	printf("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@0
   160
	for ( i=0; i<SDL_NumJoysticks(); ++i ) {
slouken@0
   161
		name = SDL_JoystickName(i);
slouken@0
   162
		printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick");
slouken@0
   163
	}
slouken@0
   164
slouken@0
   165
	if ( argv[1] ) {
slouken@0
   166
		joystick = SDL_JoystickOpen(atoi(argv[1]));
slouken@0
   167
		if ( joystick == NULL ) {
slouken@0
   168
			printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
slouken@0
   169
			       SDL_GetError());
slouken@0
   170
		} else {
slouken@0
   171
			WatchJoystick(joystick);
slouken@0
   172
			SDL_JoystickClose(joystick);
slouken@0
   173
		}
slouken@0
   174
	}
slouken@0
   175
	SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
slouken@0
   176
slouken@0
   177
	return(0);
slouken@0
   178
}