Salut !
Je me demandais comment, en C, il était possible de gérer l'appui de plusieurs touches simultanément.
En C++, on peut procéder comme ça :
const sf::Input& Input = App.GetInput();
bool RightKeyDown = Input.IsKeyDown(sf::Key::Right);
bool LeftKeyDown = Input.IsKeyDown(sf::Key::Left);
bool RightButtonDown = Input.IsMouseButtonDown(sf::Mouse::Right);
bool Joy0Button1Down = Input.IsJoystickButtonDown(0, 1);
unsigned int MouseX = Input.GetMouseX();
unsigned int MouseY = Input.GetMouseY();
float Joystick1X = Input.GetJoystickAxis(1, sf::Joy::AxisX);
float Joystick1Y = Input.GetJoystickAxis(1, sf::Joy::AxisY);
float Joystick1POV = Input.GetJoystickAxis(1, sf::Joy::AxisPOV);
Cependant, la ligne :sf::Input& Input = App.GetInput();
pose problème du fait de la référence, non ? Ou je suis passé à côté de quelque chose ? ^^
Merci d'avance !
J'ai regardé il y a un moment, sans succès (sans m'être vraiment plongé dedans, je l'admets ::) ), mais il m'est depuis sorti de la tête de poser cette question sur le forum, et n'ai pas eu la présence d'esprit de re-regarder.
Donc, si je ne me trompe pas, on aura le code suivant :
const sfInput *Input = Window_GetInput(App);
C'est bien ça ?
Je reprends ce topic suite à un autre problème (en C) :
sfInput *input = sfWindow_GetInput(*app);
if (sfInput_IsKeyDown(input, sfKeyRight) == sfTrue)
{
...
}
Il semblerait que cette condition n'est jamais validée dans le sens où son contenu n'est jamais effectué quand j'appuie sur la touche droite.
(le contenu est valide par contre, il s'exécute si je retire la condition)
Je ne comprends pas d'où le problème vient.
Je crois avoir compris mais un point reste assez... obscur, dirons-nous.
J'envoie à sfWindow_GetInput un pointeur de type sfRenderWindow (qui en C n'hérite pas de sfWindow, d'où mon erreur >_<), c'est peut-être pour ça que ça ne marche pas, non ?
Je te donne le code complet afin de mieux visualiser la chose (pour la petite histoire, on doit programmer un jeu du type Choplifter dans le cadre de nos études) :
Main :
#include <stdio.h>
#include <stdlib.h>
#include <SFML/Graphics.h>
#include "map.h"
#include "helico.h"
int main()
{
sfWindowSettings Settings = {24, 8, 0};
sfVideoMode Mode = {544, 416, 32};
sfRenderWindow* App;
sfEvent Event;
// Début test
Map map;
map.app = &App;
initMapArrayTo(&map, 1);
map.tab[0][0] = 2;
map.tab[16][12] = 2;
Helico helico;
helico.app = &App;
helico.texture = sfSprite_Create();
helico.coord.x = 0;
helico.coord.y = 208;
helico.speed = 1;
// Fin test
/* Create the main window */
App = sfRenderWindow_Create(Mode, "SFML window", sfClose, Settings);
/* Start the game loop */
while (sfRenderWindow_IsOpened(App))
{
/* Process events */
while (sfRenderWindow_GetEvent(App, &Event))
{
/* Close window : exit */
if (Event.Type == sfEvtClosed)
sfRenderWindow_Close(App);
}
/* Clear the screen */
sfRenderWindow_Clear(App, sfBlack);
drawMap(&map);
drawHelico(&helico, &map);
/* Update the window */
sfRenderWindow_Display(App);
}
/* Cleanup resources */
sfRenderWindow_Destroy(App);
return EXIT_SUCCESS;
}
Helico.c :
#include "helico.h"
void drawHelico(Helico *helico, Map *map)
{
sfImage *img;
sfInput *input = sfWindow_GetInput(*(helico->app));
helico->direction = 'd';
sfBool rightKeyDown = sfInput_IsKeyDown(input, sfKeyRight),
leftKeyDown = sfInput_IsKeyDown(input, sfKeyLeft),
upKeyDown = sfInput_IsKeyDown(input, sfKeyUp),
downKeyDown = sfInput_IsKeyDown(input, sfKeyDown);
if (upKeyDown == sfTrue)
helico->coord.y -= helico->speed;
if (downKeyDown == sfTrue)
{
// Si pas en contact avec le sol
helico->coord.y += helico->speed;
}
if (sfInput_IsKeyDown(input, sfKeyRight) == sfTrue)
{
helico->direction = 'd';
//if (helico->coord.x <= MIDDLESCREEN - MIDDLEWIDTH)
helico->coord.x += helico->speed;
//else map->scrolling--;
}
if (leftKeyDown == sfTrue)
{
helico->direction = 'g';
//if (helico->coord.x >= MIDDLESCREEN - MIDDLEWIDTH)
helico->coord.x -= helico->speed;
//else map->scrolling++;
}
if (helico->direction == 'd')
img = sfImage_CreateFromFile("helico_d.png");
else if (helico->direction == 'g')
img = sfImage_CreateFromFile("helico_g.png");
sfSprite_SetImage(helico->texture, img);
sfSprite_SetPosition(helico->texture, helico->coord.x, helico->coord.y);
sfRenderWindow_DrawSprite(*(helico->app), helico->texture);
}
void shoot(Helico *helico)
{
}
void update(Helico *helico)
{
}
Helico.h :
#ifndef HELICO_H_INCLUDED
#define HELICO_H_INCLUDED
#include "struct/coord.h"
#include "map.h"
#include <SFML/Graphics.h>
#define MAX_SPEED 3
#define MAX_LIFE 3
#define MIDDLESCREEN 272
#define MIDDLEWIDTH 32
typedef struct
{
Coord coord;
int speed, life;
char direction;
sfSprite *texture;
sfRenderWindow **app;
} Helico;
void drawHelico(Helico *helico, Map *map);
void shoot(Helico *helico);
void update(Helico *helico);
#endif // HELICO_H_INCLUDED
Une idée pour reprendre la chose sans se prendre la tête entre Window et RenderWindow ?
EDIT - Résolu !
=> sfRenderWindow_GetInput
Que je suis bête parfois u_u