前提・実現したいこと
SDL2を使ってゲームを作っています。
キー入力での移動を実装したのですが、長押しすると一瞬遅れて動いてしまいます。
その遅れを直すにはどうすれば良いのでしょうか?
発生している問題・エラーメッセージ
該当のソースコード
C
1#include "SDL2/SDL.h" 2#include "SDL2/SDL_image.h" 3#include "SDL2/SDL_joystick.h" 4 5int Object[20][15] = { 6 {0, 0}, 7 {0, 0}}; 8 9int GetPos(SDL_Rect *from, SDL_Rect *to) 10{ 11 to->x = from->x * 32; 12 to->y = from->y * 32; 13 return 0; 14} 15 16int main(int argc, const char *argv[]) 17{ 18 SDL_Window *Window; 19 SDL_Renderer *renderer; 20 SDL_Surface *image; 21 SDL_Rect rect, rect_pos, rect_tmp; 22 SDL_Texture *texture; 23 SDL_Joystick *joy; 24 int isFinish = 0; 25 26 SDL_Init(SDL_INIT_EVERYTHING); 27 28 Window = SDL_CreateWindow("Key", 0, 0, 640, 480, SDL_WINDOW_SHOWN); 29 30 renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED); 31 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 32 33 image = IMG_Load("char/front.png"); 34 35 texture = SDL_CreateTextureFromSurface(renderer, image); 36 rect.x = 0; 37 rect.y = 0; 38 rect.w = image->w; 39 rect.h = image->h; 40 41 rect_pos.w = image->w; 42 rect_pos.h = image->h; 43 rect_pos.x = 0; 44 rect_pos.y = 0; 45 SDL_FreeSurface(image); 46 47 Object[1][1] = 1; 48 49 rect_tmp = rect; 50 while (isFinish == 0) 51 { 52 53 SDL_Event event; 54 const Uint8 *state = SDL_GetKeyboardState(NULL); 55 56 while (SDL_PollEvent(&event)) 57 { 58 if (state[SDL_SCANCODE_DOWN]) 59 { 60 rect_tmp.y++; 61 } 62 if (state[SDL_SCANCODE_UP]) 63 { 64 rect_tmp.y--; 65 } 66 if (state[SDL_SCANCODE_RIGHT]) 67 { 68 rect_tmp.x++; 69 } 70 if (state[SDL_SCANCODE_LEFT]) 71 { 72 rect_tmp.x--; 73 } 74 if (state[SDL_SCANCODE_ESCAPE]) 75 { 76 isFinish = 1; 77 } 78 79 if (rect_tmp.y < 0 || 80 rect_tmp.y > 14 || 81 rect_tmp.x < 0 || 82 rect_tmp.x > 19 || 83 Object[rect_tmp.y][rect_tmp.x] == 1) 84 { 85 rect_tmp = rect; 86 } 87 else 88 { 89 rect = rect_tmp; 90 } 91 92 GetPos(&rect, &rect_pos); 93 } 94 95 SDL_RenderClear(renderer); 96 SDL_RenderCopy(renderer, texture, NULL, &rect_pos); 97 SDL_RenderPresent(renderer); 98 } 99 100 SDL_DestroyRenderer(renderer); 101 SDL_DestroyTexture(texture); 102 SDL_DestroyWindow(Window); 103 104 SDL_Quit(); 105 106 return 0; 107}
追記
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/07/10 03:45
2019/07/10 09:58
退会済みユーザー
2019/07/17 10:02
2019/07/17 10:35 編集
退会済みユーザー
2019/07/17 10:35