gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:22:19 25 4
gpt4 key购买 nike

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

这是我的事件文件

事件.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

事件.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);
}
}

最佳答案

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

如果超过了时间限制,则保存当前时间以供下次检查。

关于c - 我的事件手势 SDL C 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com