我的事件手势SDL C出现问题

时间:2013-06-02 15:23:44

标签: c events sdl

我开发了一个用c语言编写并使用SDL的游戏。 在我的主循环中,当按下一个键时,它处理多次,我想在我打字时准时处理它。 我不知道如何解决这个问题 理想情况下,我想将此事件限制为每次5次,例如。我怎么能这样做?

有我的活动档案

event.h

    #ifndef __EVENT_H__
    #define __EVENT_H__

#include <SDL/SDL.h>

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

EVENT.C

#include "event.h"

void    loop_events(t_event *input_manager)
{
    SDL_Event   event;

    while(SDL_PollEvent(&event))
    {
        switcher(&event, input_manager);
    }
}

void    switcher(SDL_Event *event, t_event *input_manager)
{
        switch (event->type)
        {
        case SDL_KEYDOWN:
            input_manager->key[event->key.keysym.sym]=1;
            break;
        case SDL_KEYUP:
            input_manager->key[event->key.keysym.sym]=0;
            break;
        case SDL_MOUSEMOTION:
            input_manager->mouse_x=event->motion.x;
            input_manager->mouse_y=event->motion.y;
            break;
        case SDL_MOUSEBUTTONDOWN:
            input_manager->mouse_buttons[event->button.button]=1;
            break;
        case SDL_MOUSEBUTTONUP:
            if (event->button.button!=SDL_BUTTON_WHEELUP && event->button.button!=SDL_BUTTON_WHEELDOWN)
                input_manager->mouse_buttons[event->button.button]=0;
            break;
        case SDL_QUIT:
            input_manager->quit = 1;
            break;
        default:
            break;
        }
}

void    event_initialize(t_event *input_manager)
{
    memset(input_manager, 0, sizeof(t_event));
    input_manager->mouse_buttons[SDL_BUTTON_WHEELUP] = 0;
    input_manager->mouse_buttons[SDL_BUTTON_WHEELDOWN] = 0;
}

我的游戏循环

while(!event.key[SDLK_ESCAPE] && !event.quit)
  {
   loop_events(&event);
   move_player(player, &event, world);
   display_world(world, screen);
   fire(player, &event, screen);

   display_player(screen, player, world);
   SDL_Delay(5);
   SDL_Flip(screen);
  }

和我想要处理我的事件的功能只有一次我输入

void  fire(t_player *player, t_event *event,  SDL_Surface *screen)
{

  if (event->key[SDLK_SPACE])
  {
    if (count_list(player->weapon_data) == 0)
       weapon_id_counter = 0; 
      t_weapon *new;
       new = malloc(sizeof(t_weapon));
       new->on_launch = bitmap_loader("resources/kirby.bmp");
       new->shape.x = player->shape.x;
       new->shape.y = player->shape.y - player->shape.h;
       new->shape.w = 32;
       new->shape.h = 32;
       printf("FIRRRE");
       new->is_launch = 1;
       new->id =weapon_id_counter++;
       player->weapon_data = add_weapon(new, player->weapon_data);
  }
}

1 个答案:

答案 0 :(得分:1)

第一次按下该键时,保存当前时间。然后,如果在时间限制之前再次按下它,则要么提出错误消息,要么忽略该键。

如果时间限制已过,则保存下次检查的当前时间。