本文整理汇总了C++中SDL_NumJoysticks函数的典型用法代码示例。如果您正苦于以下问题:C++ SDL_NumJoysticks函数的具体用法?C++ SDL_NumJoysticks怎么用?C++ SDL_NumJoysticks使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SDL_NumJoysticks函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: platform_event_rescan_idev
void platform_event_rescan_idev(arcan_evctx* ctx)
{
if (iodev.sticks_init)
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
SDL_Init(SDL_INIT_JOYSTICK);
SDL_JoystickEventState(SDL_ENABLE);
int n_joys = SDL_NumJoysticks();
iodev.sticks_init = true;
if (n_joys == 0){
drop_joytbl(ctx);
return;
}
/*
* (Re) scan/open all joysticks,
* look for matching / already present devices
* and copy their settings.
*/
size_t jsz = sizeof(struct arcan_stick) * n_joys;
struct arcan_stick* joys = malloc(jsz);
memset(joys, '\0', jsz);
for (int i = 0; i < n_joys; i++) {
struct arcan_stick* dj = &joys[i];
struct arcan_stick* sj = NULL;
unsigned long hashid = djb_hash(SDL_JoystickName(i));
/* find existing */
if (iodev.joys){
for (int j = 0; j < iodev.n_joy; j++){
if (iodev.joys[j].hashid == hashid){
sj = &iodev.joys[j];
break;
}
}
/* if found, copy to new table */
if (sj){
memcpy(dj, sj, sizeof(struct arcan_stick));
if (dj->hats){
dj->hattbls = malloc(dj->hats * sizeof(unsigned));
memcpy(dj->hattbls, sj->hattbls, sizeof(unsigned) * sj->hats);
}
if (dj->axis){
dj->adata = malloc(dj->axis * sizeof(struct axis_opts));
memcpy(dj->adata, sj->adata, sizeof(struct axis_opts) * sj->axis);
}
dj->handle = SDL_JoystickOpen(i);
continue;
}
}
/* otherwise add as new entry */
strncpy(dj->label, SDL_JoystickName(i), 255);
dj->hashid = djb_hash(SDL_JoystickName(i));
dj->handle = SDL_JoystickOpen(i);
dj->devnum = gen_devid(dj->hashid);
dj->axis = SDL_JoystickNumAxes(joys[i].handle);
dj->buttons = SDL_JoystickNumButtons(joys[i].handle);
dj->balls = SDL_JoystickNumBalls(joys[i].handle);
dj->hats = SDL_JoystickNumHats(joys[i].handle);
if (dj->hats > 0){
size_t dst_sz = joys[i].hats * sizeof(unsigned);
dj->hattbls = malloc(dst_sz);
memset(joys[i].hattbls, 0, dst_sz);
}
if (dj->axis > 0){
size_t ad_sz = sizeof(struct axis_opts) * dj->axis;
dj->adata = malloc(ad_sz);
memset(dj->adata, '\0', ad_sz);
for (int i = 0; i < dj->axis; i++){
dj->adata[i].mode = ARCAN_ANALOGFILTER_AVG;
/* these values are sortof set
* based on the SixAxis (common enough, and noisy enough) */
dj->adata[i].lower = -32765;
dj->adata[i].deadzone = 5000;
dj->adata[i].upper = 32768;
dj->adata[i].kernel_sz = 1;
}
}
/* notify the rest of the system about the added device */
struct arcan_event addev = {
.category = EVENT_IO,
.io.kind = EVENT_IO_STATUS,
.io.devkind = EVENT_IDEVKIND_STATUS,
.io.devid = dj->devnum,
.io.input.status.devkind = EVENT_IDEVKIND_GAMEDEV,
.io.input.status.action = EVENT_IDEV_ADDED
};
snprintf((char*) &addev.io.label, sizeof(addev.io.label) /
sizeof(addev.io.label[0]), "%s", dj->label);
//.........这里部分代码省略.........
示例2: closeJoystick
void SDLManagerPrivate::closeJoystick()
{
if ( SDL_NumJoysticks() > 0 && SDL_JoystickOpened(0) )
SDL_JoystickClose( joystick );
}
示例3: SDL_GameControllerOpen
/*
* Open a controller for use - the index passed as an argument refers to
* the N'th controller on the system. This index is the value which will
* identify this controller in future controller events.
*
* This function returns a controller identifier, or NULL if an error occurred.
*/
SDL_GameController *
SDL_GameControllerOpen(int device_index)
{
SDL_GameController *gamecontroller;
SDL_GameController *gamecontrollerlist;
ControllerMapping_t *pSupportedController = NULL;
if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
return (NULL);
}
gamecontrollerlist = SDL_gamecontrollers;
/* If the controller is already open, return it */
while (gamecontrollerlist) {
if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
gamecontroller = gamecontrollerlist;
++gamecontroller->ref_count;
return (gamecontroller);
}
gamecontrollerlist = gamecontrollerlist->next;
}
/* Find a controller mapping */
pSupportedController = SDL_PrivateGetControllerMapping(device_index);
if (!pSupportedController) {
SDL_SetError("Couldn't find mapping for device (%d)", device_index);
return (NULL);
}
/* Create and initialize the joystick */
gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
if (gamecontroller == NULL) {
SDL_OutOfMemory();
return NULL;
}
SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
gamecontroller->joystick = SDL_JoystickOpen(device_index);
if (!gamecontroller->joystick) {
SDL_free(gamecontroller);
return NULL;
}
SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
/* The triggers are mapped from -32768 to 32767, where -32768 is the 'unpressed' value */
{
int leftTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERLEFT];
int rightTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERRIGHT];
if (leftTriggerMapping >= 0) {
gamecontroller->joystick->axes[leftTriggerMapping] =
gamecontroller->joystick->axes_zero[leftTriggerMapping] = (Sint16)-32768;
}
if (rightTriggerMapping >= 0) {
gamecontroller->joystick->axes[rightTriggerMapping] =
gamecontroller->joystick->axes_zero[rightTriggerMapping] = (Sint16)-32768;
}
}
/* Add joystick to list */
++gamecontroller->ref_count;
/* Link the joystick in the list */
gamecontroller->next = SDL_gamecontrollers;
SDL_gamecontrollers = gamecontroller;
SDL_SYS_JoystickUpdate(gamecontroller->joystick);
return (gamecontroller);
}
示例4: joy_init
int joy_init()
{
int i,j,n;
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
return 0;
}
memset(&Joystick,0,sizeof(Joystick));
n = SDL_NumJoysticks();
con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
for (i = 0; i < n; i++) {
con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
if (SDL_Joysticks[num_joysticks].handle) {
joy_present = 1;
SDL_Joysticks[num_joysticks].n_axes
= SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
{
Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
}
SDL_Joysticks[num_joysticks].n_buttons
= SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
{
Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
}
SDL_Joysticks[num_joysticks].n_hats
= SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
{
Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
}
con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
{
SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
//a hat counts as four buttons
joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_U:TNUM_HAT_U;
joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_R:TNUM_HAT_R;
joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_D:TNUM_HAT_D;
joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_L:TNUM_HAT_L;
}
num_joysticks++;
}
else
con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
}
return joy_present;
}
示例5: JoyInit
int JoyInit()
{
int i, j, n;
tSdlJoystick *joyP = sdlJoysticks;
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
#if TRACE
console.printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
#endif
return 0;
}
memset (&joyInfo, 0, sizeof (joyInfo));
n = SDL_NumJoysticks();
#if TRACE
console.printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
#endif
for (i = 0; (i < n) && (gameStates.input.nJoysticks < MAX_JOYSTICKS); i++) {
#if TRACE
console.printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName (i));
#endif
joyP->handle = SDL_JoystickOpen (i);
if (joyP->handle) {
bJoyPresent = 1;
if((joyP->nAxes = SDL_JoystickNumAxes (joyP->handle)) > MAX_AXES_PER_JOYSTICK) {
Warning (TXT_JOY_AXESNO, joyP->nAxes, MAX_AXES_PER_JOYSTICK);
joyP->nAxes = MAX_AXES_PER_JOYSTICK;
}
if((joyP->nButtons = SDL_JoystickNumButtons (joyP->handle)) > MAX_BUTTONS_PER_JOYSTICK) {
Warning (TXT_JOY_BUTTONNO, joyP->nButtons, MAX_BUTTONS_PER_JOYSTICK);
joyP->nButtons = MAX_BUTTONS_PER_JOYSTICK;
}
if((joyP->nHats = SDL_JoystickNumHats (joyP->handle)) > MAX_HATS_PER_JOYSTICK) {
Warning (TXT_JOY_HATNO, joyP->nHats, MAX_HATS_PER_JOYSTICK);
joyP->nHats = MAX_HATS_PER_JOYSTICK;
}
#if TRACE
console.printf(CON_VERBOSE, "sdl-joystick: %d axes\n", joyP->nAxes);
console.printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", joyP->nButtons);
console.printf(CON_VERBOSE, "sdl-joystick: %d hats\n", joyP->nHats);
#endif
memset (&joyInfo, 0, sizeof (joyInfo));
for (j = 0; j < joyP->nAxes; j++)
joyP->axisMap [j] = joyInfo.nAxes++;
for (j = 0; j < joyP->nButtons; j++)
joyP->buttonMap [j] = joyInfo.nButtons++;
for (j = 0; j < joyP->nHats; j++) {
joyP->hatMap [j] = joyInfo.nButtons;
//a hat counts as four buttons
joybutton_text [joyInfo.nButtons++] = i ? TNUM_HAT2_U : TNUM_HAT_U;
joybutton_text [joyInfo.nButtons++] = i ? TNUM_HAT2_R : TNUM_HAT_R;
joybutton_text [joyInfo.nButtons++] = i ? TNUM_HAT2_D : TNUM_HAT_D;
joybutton_text [joyInfo.nButtons++] = i ? TNUM_HAT2_L : TNUM_HAT_L;
}
joyP++;
gameStates.input.nJoysticks++;
}
else {
#if TRACE
console.printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
#endif
}
#if TRACE
console.printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", joyInfo.nAxes);
console.printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", joyInfo.nButtons);
#endif
}
return bJoyPresent;
}
示例6: I_GetJoystickCount
//
// I_GetJoystickCount
//
int I_GetJoystickCount()
{
return SDL_NumJoysticks();
}
示例7: main
//.........这里部分代码省略.........
}
if (signal_len < 8) {
signal_len += rand() % (8 - signal_len);
} else {
signal_len = 0;
}
if (speed_len < 8) {
speed_len += rand() % (8 - speed_len);
} else {
speed_len = 0;
}
}
if(play_traffic) {
play_id = fork();
if((int)play_id == -1) {
printf("Error: Couldn't fork bg player\n");
exit(-1);
} else if (play_id == 0) {
play_can_traffic();
// Shouldn't return
exit(0);
}
atexit(kill_child);
}
// GUI Setup
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
if(SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0 ) {
printf("SDL Could not initializes\n");
exit(40);
}
if( SDL_NumJoysticks() < 1) {
printf(" Warning: No joysticks connected\n");
} else {
if(SDL_IsGameController(0)) {
gGameController = SDL_GameControllerOpen(0);
if(gGameController == NULL) {
printf(" Warning: Unable to open game controller. %s\n", SDL_GetError() );
} else {
gJoystick = SDL_GameControllerGetJoystick(gGameController);
gHaptic = SDL_HapticOpenFromJoystick(gJoystick);
print_joy_info();
}
} else {
gJoystick = SDL_JoystickOpen(0);
if(gJoystick == NULL) {
printf(" Warning: Could not open joystick\n");
} else {
gHaptic = SDL_HapticOpenFromJoystick(gJoystick);
if (gHaptic == NULL) printf("No Haptic support\n");
print_joy_info();
}
}
}
window = SDL_CreateWindow("CANBus Control Panel", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if(window == NULL) {
printf("Window could not be shown\n");
}
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface *image = IMG_Load(get_data("joypad.png"));
base_texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_RenderCopy(renderer, base_texture, NULL, NULL);
SDL_RenderPresent(renderer);
int button, axis; // Used for checking dynamic joystick mappings
示例8: if
void Jugador::manejarEntrada() {
if(!bMuerto) {
// manejar teclas
if(ManejadorDeEntrada::Instancia()->isKeyDown(SDL_SCANCODE_UP)
&& posicion.getY() > 0)
{
velocidad.setY(-velocidadMovimiento);
velocidad.setX(0);
}
else if(ManejadorDeEntrada::Instancia()->isKeyDown(SDL_SCANCODE_DOWN)
&& (posicion.getY() + altura) < ElJuego::Instancia()->getAlturaJuego() - 10)
{
velocidad.setY(velocidadMovimiento);
velocidad.setX(0);
}
if(ManejadorDeEntrada::Instancia()->isKeyDown(SDL_SCANCODE_LEFT) && posicion.getX() > 0)
{
velocidad.setX(-velocidadMovimiento);
velocidad.setY(0);
}
else if(ManejadorDeEntrada::Instancia()->isKeyDown(SDL_SCANCODE_RIGHT)
&& (posicion.getX() + ancho) < ElJuego::Instancia()->getAnchoJuego())
{
velocidad.setX(velocidadMovimiento);
velocidad.setY(0);
}
//
// if(ManejadorDeEntrada::Instancia()->isKeyDown(SDL_SCANCODE_SPACE)) {
// if(m_bulletCounter == m_bulletFiringSpeed) {
// TheSoundManager::Instance()->playSound("shoot", 0);
// TheBulletHandler::Instance()->addPlayerBullet(
// m_position.getX() + 90, m_position.getY() + 12, 11, 11, "bullet1", 1, Vector2D(10,0));
// m_bulletCounter = 0;
// }
//
// m_bulletCounter++;
// } else { m_bulletCounter = m_bulletFiringSpeed; }
// */
/* manejando joysticks */
//comprobarJoysticks
if( SDL_NumJoysticks() < 1 ) {
//printf( "Peligro: No hay Joysticks conectados!\n" );
} else {
if(ManejadorDeEntrada::Instancia()->joysticksInicializados()) {
// if(ManejadorDeEntrada::Instancia()->getButtonState(0, 2)) {
// if(m_bulletCounter == m_bulletFiringSpeed)
// {
// TheSoundManager::Instance()->playSound("shoot", 0);
// TheBulletHandler::Instance()->addPlayerBullet(
// m_position.getX() + 90, m_position.getY() + 12, 11, 11, "bullet1", 1, Vector2D(10,0));
// m_bulletCounter = 0;
// }
//
// m_bulletCounter++;
// } else { m_bulletCounter = m_bulletFiringSpeed; }
if((ManejadorDeEntrada::Instancia()->getAxisX(0, 1) > 0
&& (posicion.getX() + ancho) < ElJuego::Instancia()->getAnchoJuego())
|| (ManejadorDeEntrada::Instancia()->getAxisX(0, 1) < 0 && posicion.getX() > 0)) {
velocidad.setX(velocidadMovimiento * ManejadorDeEntrada::Instancia()->getAxisX(0, 1));
}
if((ManejadorDeEntrada::Instancia()->getAxisY(0, 1) > 0
&& (posicion.getY() + altura) < ElJuego::Instancia()->getAlturaJuego() - 10 )
|| (ManejadorDeEntrada::Instancia()->getAxisY(0, 1) < 0 && posicion.getY() > 0)) {
velocidad.setY(velocidadMovimiento * ManejadorDeEntrada::Instancia()->getAxisY(0, 1));
}
}
}
}
}
示例9: init_SDL
int init_SDL(void)
{
joys[0]=0;
joys[1]=0;
joys[2]=0;
joys[3]=0;
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
return(0);
}
sdlscreen = SDL_SetVideoMode(0,0, 16, SDL_SWSURFACE);
//We handle up to four joysticks
if(SDL_NumJoysticks())
{
int joystickIndex;
int configuratedJoysticks[4] = {-1,-1,-1,-1};
SDL_JoystickEventState(SDL_ENABLE);
// Try to configure joysticks from config indexes
for(int player = 0; player< 4; player++){
joystickIndex = joy_indexes[player];
if(joystickIndex >= 0){
if(SDL_NumJoysticks() > joystickIndex){
SDL_Joystick* joystick = SDL_JoystickOpen(joystickIndex);
//Check for valid joystick, some keyboards
//aren't SDL compatible
if(joystick)
{
if (SDL_JoystickNumAxes(myjoy[joystickIndex]) > 6)
{
SDL_JoystickClose(myjoy[joystickIndex]);
joystick=0;
logoutput("Error detected invalid joystick/keyboard\n");
break;
}
}
configuratedJoysticks[player] = joystickIndex;
joys[player] = joystick;
logoutput("Configured joystick %s at %d for player %d\n", SDL_JoystickName(joystickIndex), SDL_JoystickIndex(joystick), player+1);
joyCount++;
}
}
}
// Finish configuration
for(joystickIndex=0;joystickIndex<SDL_NumJoysticks();joystickIndex++) {
// If already configured skip
bool alreadyConfig = false;
for(int player = 0; player< 4; player++){
logoutput("Checking if joystick at %d is configured for player %d : %d\n", joystickIndex,player ,configuratedJoysticks[player]);
if(configuratedJoysticks[player] == joystickIndex){
alreadyConfig = true;
break;
}
}
if(alreadyConfig) continue;
SDL_Joystick* joystick = SDL_JoystickOpen(joystickIndex);
//Check for valid joystick, some keyboards
//aren't SDL compatible
if(joystick)
{
if (SDL_JoystickNumAxes(myjoy[joystickIndex]) > 6)
{
SDL_JoystickClose(myjoy[joystickIndex]);
joystick=0;
logoutput("Error detected invalid joystick/keyboard\n");
break;
}
// Set the first free joystick
for(int player = 0; player< 4; player++){
if(configuratedJoysticks[player] == -1){
joy_indexes[player] = joystickIndex;
joys[player] = joystick;
configuratedJoysticks[player] = joystickIndex;
logoutput("Default Configured joystick at %d for player %d\n", joystickIndex ,player+1);
joyCount++;
break;
}
}
}
}
if(joys[0])
logoutput("Found %d joysticks\n",joyCount);
}
else
joyCount=1;
//sq frig number of players for keyboard
//joyCount=2;
SDL_EventState(SDL_ACTIVEEVENT,SDL_IGNORE);
SDL_EventState(SDL_SYSWMEVENT,SDL_IGNORE);
SDL_EventState(SDL_VIDEORESIZE,SDL_IGNORE);
SDL_EventState(SDL_USEREVENT,SDL_IGNORE);
SDL_ShowCursor(SDL_DISABLE);
//.........这里部分代码省略.........
示例10: main
int main(int argc, char** argv)
{
if (argc == 1)
{
print_help(argv[0]);
exit(1);
}
// printf("argv: %s %s %s %s %s %i\n", argv[0], argv[1], argv[2], argv[3], argv[4], argc );
// FIXME: We don't need video, but without it SDL will fail to work in SDL_WaitEvent()
if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
else
{
atexit(SDL_Quit);
if (argc == 2 && (strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-h") == 0))
{
print_help(argv[0]);
}
if (argc == 2 && (strcmp(argv[1], "--version") == 0))
{
printf("sdl2osc 0.1.0\n");
exit(EXIT_SUCCESS);
}
else if (argc == 2 && (strcmp(argv[1], "--list") == 0 ||
(strcmp(argv[1], "-l") == 0)))
{
int num_joysticks = SDL_NumJoysticks();
if (num_joysticks == 0)
{
printf("No joysticks were found\n");
}
else
{
int joy_idx;
printf("Found %d joystick(s)\n\n", num_joysticks);
for(joy_idx = 0; joy_idx < num_joysticks; ++joy_idx)
{
SDL_Joystick* joy = SDL_JoystickOpen(joy_idx);
if (!joy)
{
fprintf(stderr, "Unable to open joystick %d\n", joy_idx);
}
else
{
print_joystick_info(joy_idx, joy);
SDL_JoystickClose(joy);
}
}
}
}
else if (argc == 3 && (strcmp(argv[1], "--event") == 0 ||
strcmp(argv[1], "-e") == 0))
{
int joy_idx;
if (!str2int(argv[2], &joy_idx))
{
fprintf(stderr, "Error: JOYSTICKNUM argument must be a number, but was '%s'\n", argv[2]);
exit(1);
}
SDL_Joystick* joy = SDL_JoystickOpen(joy_idx);
if (!joy)
{
fprintf(stderr, "Unable to open joystick %d\n", joy_idx);
}
else
{
print_joystick_info(joy_idx, joy);
printf("Entering joystick test loop, press Ctrl-c to exit\n");
int quit = 0;
SDL_Event event;
while(!quit && SDL_WaitEvent(&event))
{
switch(event.type)
{
case SDL_JOYAXISMOTION:
printf("SDL_JOYAXISMOTION: joystick: %d axis: %d value: %d\n",
event.jaxis.which, event.jaxis.axis, event.jaxis.value);
break;
case SDL_JOYBUTTONDOWN:
printf("SDL_JOYBUTTONUP: joystick: %d button: %d state: %d\n",
event.jbutton.which, event.jbutton.button, event.jbutton.state);
break;
case SDL_JOYBUTTONUP:
printf("SDL_JOYBUTTONDOWN: joystick: %d button: %d state: %d\n",
event.jbutton.which, event.jbutton.button, event.jbutton.state);
break;
case SDL_JOYHATMOTION:
//.........这里部分代码省略.........
示例11: SDL_EnableUNICODE
//------------------------------------------------------------
void ofxSDLAppWindow::runAppViaInfiniteLoop(ofBaseApp* appPtr) {
static ofEventArgs voidEventArgs;
// ------------------------------------------------------------
// enable keyboarding
SDL_EnableUNICODE(1);
// ------------------------------------------------------------
// connect controllers
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
numJoys = SDL_NumJoysticks();
for (int j = 0; j < numJoys; j++) {
joys[j] = SDL_JoystickOpen(j);
}
// ------------------------------------------------------------
// setup application
ofAppPtr = (ofxSDLApp*) appPtr;
if (ofAppPtr) {
ofAppPtr->setup();
ofAppPtr->update();
}
#ifdef OF_USING_POCO
ofNotifyEvent(ofEvents.setup, voidEventArgs);
ofNotifyEvent(ofEvents.update, voidEventArgs);
#endif
// ------------------------------------------------------------
// loop forever and ever and ever and ever (and ever)
while (true) {
// ------------------------------------------------------------
// check for events
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_JOYAXISMOTION:
this->joyMovedHandler(&event);
break;
case SDL_JOYBUTTONDOWN:
this->joyDownHandler(&event);
break;
case SDL_JOYBUTTONUP:
this->joyUpHandler(&event);
break;
case SDL_MOUSEMOTION:
// TODO
break;
case SDL_MOUSEBUTTONDOWN:
this->mouseDownHandler(&event);
break;
case SDL_MOUSEBUTTONUP:
this->mouseUpHandler(&event);
break;
case SDL_KEYDOWN:
this->keyDownHandler(&event);
break;
case SDL_KEYUP:
this->keyUpHandler(&event);
break;
case SDL_QUIT:
this->exitApp();
break;
default:
break;
}
}
// ------------------------------------------------------------
// set viewport, clear the screen
int width, height;
width = ofGetWidth();
height = ofGetHeight();
height = height > 0 ? height : 1;
glViewport(0, 0, width, height);
float* bgPtr = ofBgColorPtr();
bool bClearAuto = ofbClearBg();
#ifdef TARGET_WIN32
// TODO: unsure if this is required for SDL, copied from GLUT.
// to do non auto clear on PC for now - we do something like "single" buffering --
// it's not that pretty but it work for the most part
if (bClearAuto == false) {
glDrawBuffer(GL_FRONT);
}
#endif
//.........这里部分代码省略.........
示例12: main
//.........这里部分代码省略.........
}
// Init SDL2
unsigned int sdl_flags = SDL_INIT_TIMER;
#ifndef STANDALONE_SERVER
sdl_flags |= SDL_INIT_VIDEO;
#endif
if(SDL_Init(sdl_flags)) {
err_msgbox("SDL2 Initialization failed: %s", SDL_GetError());
goto exit_2;
}
SDL_version sdl_linked;
SDL_GetVersion(&sdl_linked);
INFO("Found SDL v%d.%d.%d", sdl_linked.major, sdl_linked.minor, sdl_linked.patch);
INFO("Running on platform: %s", SDL_GetPlatform());
#ifndef STANDALONE_SERVER
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK|SDL_INIT_GAMECONTROLLER|SDL_INIT_HAPTIC)) {
err_msgbox("SDL2 Initialization failed: %s", SDL_GetError());
goto exit_2;
}
// Attempt to find gamecontrollerdb.txt, either from resources or from
// built-in header
SDL_RWops *rw = SDL_RWFromConstMem(gamecontrollerdb, strlen(gamecontrollerdb));
SDL_GameControllerAddMappingsFromRW(rw, 1);
char *gamecontrollerdbpath = malloc(128);
snprintf(gamecontrollerdbpath, 128, "%s/gamecontrollerdb.txt", pm_get_local_path(RESOURCE_PATH));
int mappings_loaded = SDL_GameControllerAddMappingsFromFile(gamecontrollerdbpath);
if (mappings_loaded > 0) {
DEBUG("loaded %d mappings from %s", mappings_loaded, gamecontrollerdbpath);
}
free(gamecontrollerdbpath);
// Load up joysticks
INFO("Found %d joysticks attached", SDL_NumJoysticks());
SDL_Joystick *joy;
char guidstr[33];
for (int i = 0; i < SDL_NumJoysticks(); i++) {
joy = SDL_JoystickOpen(i);
if (joy) {
SDL_JoystickGUID guid = SDL_JoystickGetGUID(joy);
SDL_JoystickGetGUIDString(guid, guidstr, 33);
INFO("Opened Joystick %d", i);
INFO(" * Name: %s", SDL_JoystickNameForIndex(i));
INFO(" * Number of Axes: %d", SDL_JoystickNumAxes(joy));
INFO(" * Number of Buttons: %d", SDL_JoystickNumButtons(joy));
INFO(" * Number of Balls: %d", SDL_JoystickNumBalls(joy));
INFO(" * Number of Hats: %d", SDL_JoystickNumHats(joy));
INFO(" * GUID : %s", guidstr);
} else {
INFO("Joystick %d is unsupported", i);
}
if (SDL_JoystickGetAttached(joy)) {
SDL_JoystickClose(joy);
}
}
// Init libDumb
dumb_register_stdfiles();
#endif
// Init enet
if(enet_initialize() != 0) {
err_msgbox("Failed to initialize enet");
goto exit_3;
}
// Initialize engine
if(engine_init()) {
err_msgbox("Failed to initialize game engine.");
goto exit_4;
}
// Run
engine_run(net_mode);
// Close everything
engine_close();
exit_4:
enet_deinitialize();
exit_3:
SDL_Quit();
exit_2:
dumb_exit();
settings_save();
settings_free();
exit_1:
sd_stringparser_lib_deinit();
INFO("Exit.");
log_close();
exit_0:
if (ip) {
free(ip);
}
plugins_close();
pm_free();
return ret;
}
示例13: main
int
main(int argc, char *argv[])
{
SDL_Joystick *joystick = NULL;
SDL_Haptic *haptic = NULL;
SDL_JoystickID instance = -1;
SDL_bool keepGoing = SDL_TRUE;
int i;
SDL_bool enable_haptic = SDL_TRUE;
Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK;
for (i = 1; i < argc; ++i) {
if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) {
enable_haptic = SDL_FALSE;
}
}
if(enable_haptic) {
init_subsystems |= SDL_INIT_HAPTIC;
}
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
/* Initialize SDL (Note: video is required to start event loop) */
if (SDL_Init(init_subsystems) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
//SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
if (enable_haptic)
SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
while(keepGoing)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
keepGoing = SDL_FALSE;
break;
case SDL_JOYDEVICEADDED:
if (joystick != NULL)
{
SDL_Log("Only one joystick supported by this test\n");
}
else
{
joystick = SDL_JoystickOpen(event.jdevice.which);
instance = SDL_JoystickInstanceID(joystick);
SDL_Log("Joy Added : %d : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
if (enable_haptic)
{
if (SDL_JoystickIsHaptic(joystick))
{
haptic = SDL_HapticOpenFromJoystick(joystick);
if (haptic)
{
SDL_Log("Joy Haptic Opened\n");
if (SDL_HapticRumbleInit( haptic ) != 0)
{
SDL_Log("Could not init Rumble!: %s\n", SDL_GetError());
SDL_HapticClose(haptic);
haptic = NULL;
}
} else {
SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError());
}
}
else
{
SDL_Log("No haptic found\n");
}
}
}
break;
case SDL_JOYDEVICEREMOVED:
if (instance == event.jdevice.which)
{
SDL_Log("Joy Removed: %d\n", event.jdevice.which);
instance = -1;
if(enable_haptic && haptic)
{
SDL_HapticClose(haptic);
haptic = NULL;
}
SDL_JoystickClose(joystick);
joystick = NULL;
} else {
SDL_Log("Unknown joystick diconnected\n");
}
break;
case SDL_JOYAXISMOTION:
//.........这里部分代码省略.........
开发者ID:NightOwlsEntertainment,项目名称:PetBox_A_Journey_to_Conquer_Elementary_Algebra,代码行数:101,代码来源:testhotplug.c
示例14: if
void GameStateConfigDesktop::logicInput() {
if (mouse_move_cb->checkClick()) {
if (mouse_move_cb->isChecked()) {
MOUSE_MOVE=true;
no_mouse_cb->unCheck();
NO_MOUSE=false;
}
else MOUSE_MOVE=false;
}
else if (mouse_aim_cb->checkClick()) {
if (mouse_aim_cb->isChecked()) {
MOUSE_AIM=true;
no_mouse_cb->unCheck();
NO_MOUSE=false;
}
else MOUSE_AIM=false;
}
else if (no_mouse_cb->checkClick()) {
if (no_mouse_cb->isChecked()) {
NO_MOUSE=true;
mouse_aim_cb->unCheck();
MOUSE_AIM=false;
mouse_move_cb->unCheck();
MOUSE_MOVE=false;
}
else NO_MOUSE=false;
}
else if (enable_joystick_cb->checkClick()) {
if (enable_joystick_cb->isChecked()) {
ENABLE_JOYSTICK=true;
if (SDL_NumJoysticks() > 0) {
JOYSTICK_DEVICE = 0;
SDL_JoystickClose(joy);
joy = SDL_JoystickOpen(JOYSTICK_DEVICE);
joystick_device_lstb->selected[JOYSTICK_DEVICE] = true;
}
}
else {
ENABLE_JOYSTICK=false;
for (int i=0; i<joystick_device_lstb->getSize(); i++)
joystick_device_lstb->selected[i] = false;
}
if (SDL_NumJoysticks() > 0) joystick_device_lstb->refresh();
}
else if (joystick_deadzone_sl->checkClick()) {
JOY_DEADZONE = joystick_deadzone_sl->getValue();
}
else if (joystick_device_lstb->checkClick()) {
JOYSTICK_DEVICE = joystick_device_lstb->getSelected();
if (JOYSTICK_DEVICE != -1) {
enable_joystick_cb->Check();
ENABLE_JOYSTICK=true;
if (SDL_NumJoysticks() > 0) {
SDL_JoystickClose(joy);
joy = SDL_JoystickOpen(JOYSTICK_DEVICE);
}
}
else {
enable_joystick_cb->unCheck();
ENABLE_JOYSTICK = false;
}
}
}
示例15: eventCnt
JoystickInput::JoystickInput(bool sdlInitFlag)
: eventCnt(0),
eventIndex(0),
axisCnt(0),
buttonCnt(0),
hatCnt(0),
updateTimer(),
pwmTimer(),
autoFireTimer(),
mutex_(),
haveJoystick(false),
lockFlag(false),
sdlInitialized(false),
config()
{
sdlDevices[0] = (void *) 0;
sdlDevices[1] = (void *) 0;
#ifdef HAVE_SDL_H
if (sdlInitFlag) {
if (SDL_Init(SDL_INIT_JOYSTICK) != 0)
return;
sdlInitialized = true;
}
int nDevices = SDL_NumJoysticks();
int j = 0;
int prvDevNum = -1;
for (int i = 0; i < nDevices && j < 2; i++) {
SDL_Joystick *joy_ = SDL_JoystickOpen(i);
if (!joy_)
continue;
int axisCnt_ = SDL_JoystickNumAxes(joy_);
int buttonCnt_ = SDL_JoystickNumButtons(joy_);
int hatCnt_ = SDL_JoystickNumHats(joy_);
if (!(axisCnt_ >= 4 && buttonCnt_ >= 4 && hatCnt_ >= 1)) {
SDL_JoystickClose(joy_);
continue;
}
sdlDevices[j++] = joy_;
prvDevNum = i;
}
for (int i = 0; i < nDevices && j < 2; i++) {
if (i == prvDevNum)
continue;
SDL_Joystick *joy_ = SDL_JoystickOpen(i);
if (!joy_)
continue;
int axisCnt_ = SDL_JoystickNumAxes(joy_);
int buttonCnt_ = SDL_JoystickNumButtons(joy_);
int hatCnt_ = SDL_JoystickNumHats(joy_);
if (!(axisCnt_ >= 2 && buttonCnt_ >= 1 && hatCnt_ >= 1)) {
SDL_JoystickClose(joy_);
continue;
}
sdlDevices[j++] = joy_;
prvDevNum = i;
}
for (int i = 0; i < nDevices && j < 2; i++) {
if (i == prvDevNum)
continue;
SDL_Joystick *joy_ = SDL_JoystickOpen(i);
if (!joy_)
continue;
sdlDevices[j++] = joy_;
}
if (!j)
return;
nDevices = j;
for (j = 0; j < nDevices; j++) {
SDL_Joystick *joy_ = reinterpret_cast<SDL_Joystick *>(sdlDevices[j]);
int axisCnt_ = SDL_JoystickNumAxes(joy_);
int buttonCnt_ = SDL_JoystickNumButtons(joy_);
int hatCnt_ = SDL_JoystickNumHats(joy_);
if (j == 0 && nDevices > 1) {
axisCnt_ = (axisCnt_ < 4 ? axisCnt_ : 4);
buttonCnt_ = (buttonCnt_ < 8 ? buttonCnt_ : 8);
hatCnt_ = (hatCnt_ < 1 ? hatCnt_ : 1);
}
for (int i = 0; i < axisCnt_ && axisCnt < 8; i++) {
axes[axisCnt].devNum = j;
axes[axisCnt].axisNum = i;
axes[axisCnt].prvInputState = 0;
axes[axisCnt].prvOutputState = 0;
axisCnt++;
}
for (int i = 0; i < buttonCnt_ && buttonCnt < 16; i++) {
buttons[buttonCnt].devNum = j;
buttons[buttonCnt].buttonNum = i;
buttons[buttonCnt].prvInputState = false;
buttons[buttonCnt].prvOutputState = false;
buttonCnt++;
}
for (int i = 0; i < hatCnt_ && hatCnt < 2; i++) {
povHats[hatCnt].devNum = j;
povHats[hatCnt].hatNum = i;
povHats[hatCnt].prvState = SDL_HAT_CENTERED;
hatCnt++;
}
}
haveJoystick = (axisCnt > 0 || buttonCnt > 0 || hatCnt > 0);
#else
//.........这里部分代码省略.........