Bonjour,
j'ai trouver sur github un programme en console pour récupérer les valeurs d'un joystick, mais au début il y a une instruction #pragma, mais même avec le commentaire qui lui est associée je ne comprends pas à quoi cette instruction peut elle servir et si elle est utile dans mon programme en console, voici mon programme :
#include <stdio.h>
#ifdef __linux
#include <unistd.h>
#else
#include <windows.h>
#endif
#include "libenjoy.h"
// This tels msvc to link agains winmm.lib. Pretty nasty though.
#pragma comment(lib, "winmm.lib")
int main()
{
int left_right = 0,
back_front = 0,
counterClockwise_clockwise = 0,
down_up = 0,
start = 0,
takeoff = 0,
land = 0;
libenjoy_context *ctx = libenjoy_init(); // initialize the library
libenjoy_joy_info_list *info;
// Updates internal list of joysticks. If you want auto-reconnect
// after re-plugging the joystick, you should call this every 1s or so
libenjoy_enumerate(ctx);
// get list with available joysticks. structs are
// typedef struct libenjoy_joy_info_list {
// uint32_t count;
// libenjoy_joy_info **list;
// } libenjoy_joy_info_list;
//
// typedef struct libenjoy_joy_info {
// char *name;
// uint32_t id;
// char opened;
// } libenjoy_joy_info;
//
// id is not linear (eg. you should not use vector or array),
// and if you disconnect joystick and then plug it in again,
// it should have the same ID
info = libenjoy_get_info_list(ctx);
if(info->count != 0) // just get the first joystick
{
libenjoy_joystick *joy;
printf("Opening joystick %s...", info->list[0]->name);
joy = libenjoy_open_joystick(ctx, info->list[0]->id);
if(joy)
{
int counter = 0;
libenjoy_event ev;
/*
Parce que y a pas de \n, et quand il n y a pas de \n l'OS croit que le buffer
est en cours de chargement, alors il attend pour tout vider d'un coup
(c'est ce qui se passe également pour l'écriture des fichiers). fflush()
force l'OS à vider le buffer, et transférer son contenu (soit vers l'affichage
s'il s'agit de stdout, soit vers un fichier s'il s'agit d'une écriture fichier)
*/
fflush(stdout);
sleep(1);
printf ("\033[32mSuccess!\033[0m");
fflush(stdout);
sleep(1);
printf(" 3..");
fflush(stdout);
sleep(1);
printf("2..");
fflush(stdout);
sleep(1);
printf("1..");
fflush(stdout);
sleep(1);
printf(" let's go !\n");
fflush(stdout);
sleep(2);
while(1)
{
// Value data are not stored in library. if you want to use
// them, you have to store them
// That's right, only polling available
while(libenjoy_poll(ctx, &ev))
{
if(ev.type == LIBENJOY_EV_AXIS)
{
switch(ev.part_id)
{
case 0:
left_right = ev.data;
break;
case 1:
back_front = -ev.data;
break;
case 2:
counterClockwise_clockwise = ev.data;
break;
case 3:
down_up = -ev.data;
break;
}
}
if(ev.type == LIBENJOY_EV_BUTTON)
{
switch(ev.part_id)
{
case 3:
start = ev.data;
break;
case 12:
takeoff = ev.data;
break;
case 14:
land = ev.data;
break;
}
}
printf("left_right : %6d | back_front : %6d | counterClockwise_clockwise : %6d | down_up : %6d | start : %1d | takeoff : %1d | land %1d \n", left_right, back_front, counterClockwise_clockwise, down_up, start, takeoff, land);
}
#ifdef __linux
usleep(50000);
#else
Sleep(50);
#endif
counter += 50;
if(counter >= 1000)
{
libenjoy_enumerate(ctx);
counter = 0;
}
}
// Joystick is really closed in libenjoy_poll or libenjoy_close,
// because closing it while libenjoy_poll is in process in another thread
// could cause crash. Be sure to call libenjoy_poll(ctx, NULL); (yes,
// you can use NULL as event) if you will not poll nor libenjoy_close
// anytime soon.
libenjoy_close_joystick(joy);
}
else
printf("\033[31mFailed!\033[0m");
}
else
printf("No joystick available\n");
// Frees memory allocated by that joystick list. Do not forget it!
libenjoy_free_info_list(info);
// deallocates all memory used by lib. Do not forget this!
// libenjoy_poll must not be called during or after this call
libenjoy_close(ctx);
return 0;
}
Merci d'avance à ceux qui me répondront
C'est un truc spécifique au compilo microsoft, ici ça permet de linker directement une lib.
Ce que tu peux faire aussi via les paramètres de l'IDE normalement.
Merci
Mais est-ce vraiment utile de laisser cette instruction si je veux juste faire un programme en console ? Merci d'avance
personne pour m'aider ?
si tu ajoute les options de compilations qui vont bien, cette directive est inutile.
Merci godrik, quels sont ces options ? Quand je compile je fais ceci : "gcc *.c -o nomduprogramme" (sous linux et windows)
en l'occurence ce pragma n'est pas compris par gcc. ajoute un -l winmm
ca ne marchera pas sous linux evidement.