Note that there are some explanatory texts on larger screens.

plurals
  1. POProbleme with my event gesture SDL C
    primarykey
    data
    text
    <p>I develop a game written in c and using SDL. in my main loop when a press a key, it handle more than one time, and I want handle it just on time when I type on it. I don't know how to fix this problem Idealy I want to limit this event one time each 5 secondes for example. How I can do this ?</p> <p>There is my event file</p> <p>event.h </p> <pre><code> #ifndef __EVENT_H__ #define __EVENT_H__ #include &lt;SDL/SDL.h&gt; typedef struct s_event { int mouse_x; int mouse_y; char key[SDLK_LAST]; char mouse_buttons[8]; int quit; } t_event; void loop_events(t_event *input_manager); void switcher(SDL_Event *event, t_event *input_manager); void event_initialize(t_event *input_manager); #endif </code></pre> <p>event.c</p> <pre><code>#include "event.h" void loop_events(t_event *input_manager) { SDL_Event event; while(SDL_PollEvent(&amp;event)) { switcher(&amp;event, input_manager); } } void switcher(SDL_Event *event, t_event *input_manager) { switch (event-&gt;type) { case SDL_KEYDOWN: input_manager-&gt;key[event-&gt;key.keysym.sym]=1; break; case SDL_KEYUP: input_manager-&gt;key[event-&gt;key.keysym.sym]=0; break; case SDL_MOUSEMOTION: input_manager-&gt;mouse_x=event-&gt;motion.x; input_manager-&gt;mouse_y=event-&gt;motion.y; break; case SDL_MOUSEBUTTONDOWN: input_manager-&gt;mouse_buttons[event-&gt;button.button]=1; break; case SDL_MOUSEBUTTONUP: if (event-&gt;button.button!=SDL_BUTTON_WHEELUP &amp;&amp; event-&gt;button.button!=SDL_BUTTON_WHEELDOWN) input_manager-&gt;mouse_buttons[event-&gt;button.button]=0; break; case SDL_QUIT: input_manager-&gt;quit = 1; break; default: break; } } void event_initialize(t_event *input_manager) { memset(input_manager, 0, sizeof(t_event)); input_manager-&gt;mouse_buttons[SDL_BUTTON_WHEELUP] = 0; input_manager-&gt;mouse_buttons[SDL_BUTTON_WHEELDOWN] = 0; } </code></pre> <p>my game loop</p> <pre><code>while(!event.key[SDLK_ESCAPE] &amp;&amp; !event.quit) { loop_events(&amp;event); move_player(player, &amp;event, world); display_world(world, screen); fire(player, &amp;event, screen); display_player(screen, player, world); SDL_Delay(5); SDL_Flip(screen); } </code></pre> <p>and the function where I want handle my event just one time when I type</p> <pre><code>void fire(t_player *player, t_event *event, SDL_Surface *screen) { if (event-&gt;key[SDLK_SPACE]) { if (count_list(player-&gt;weapon_data) == 0) weapon_id_counter = 0; t_weapon *new; new = malloc(sizeof(t_weapon)); new-&gt;on_launch = bitmap_loader("resources/kirby.bmp"); new-&gt;shape.x = player-&gt;shape.x; new-&gt;shape.y = player-&gt;shape.y - player-&gt;shape.h; new-&gt;shape.w = 32; new-&gt;shape.h = 32; printf("FIRRRE"); new-&gt;is_launch = 1; new-&gt;id =weapon_id_counter++; player-&gt;weapon_data = add_weapon(new, player-&gt;weapon_data); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload