0

I am trying to make my object move for my project, it works fine as for single tap, but for continuous press it has some kind of input lag, I do not know how to fix it. I tried to put it in switch case, it did not work. I tried to use KEYUP with KEYDOWN and put my operation outside of construction that is reading my keys, but it still is not working.

I know there is a way to do that, but I cannot find it. Here is full code with lib if something, I am using Windows10:

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <stdbool.h>
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2main.lib")
#pragma comment(lib, "SDL2test.lib")

static const int width = 1280;
static const int height = 720;


int main()
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Screen", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);


    SDL_Surface* surf4;
    surf4 = SDL_LoadBMP("sample.bmp");
    SDL_Texture* charr = SDL_CreateTextureFromSurface(renderer, surf4);
    SDL_FreeSurface(surf4);
    SDL_Rect rectc;
    rectc.h = 140;
    rectc.w = 110;
    rectc.x = 585;
    rectc.y = 500;

    SDL_QueryTexture(charr, NULL, NULL, &rectc.h, &rectc.w);

    SDL_Event event;
    bool r = true;
    while (r)
    {
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, charr, NULL, &rectc);
        SDL_RenderPresent(renderer);
        bool wkey = false;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_KEYDOWN)
            {
                if (event.key.keysym.sym == SDLK_w)
                {
                    wkey = true;
                }
                if (event.key.keysym.sym == SDLK_d)
                {
                    rectc.x = rectc.x + 15;
                }
            }
            if (event.type == SDL_KEYUP)
            {
                if (event.key.keysym.sym == SDLK_w) wkey = false;
            }

            if (event.type == SDL_QUIT)
            {
                r = false;
            }
        }
        if (wkey) rectc.y = rectc.y - 15;
    }

    SDL_DestroyTexture(charr);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
desuragi
  • 9
  • 5
  • @AndreasWenzel I am using SDL2, windows10 – desuragi May 29 '22 at 12:44
  • You submitted your question for review to be reopened. If you want your question to be reopened, you may want to state why you believe that the associated duplicate question is actually not a duplicate of your question. – Andreas Wenzel May 29 '22 at 12:56
  • 1
    Also, prefer scancodes to keycodes. The linked thread explains this too. – HolyBlackCat May 29 '22 at 14:11
  • @HolyBlackCat yes, thank you, I have changed that, but delay is still there – desuragi May 29 '22 at 14:21
  • 2
    Got it to work: 1. Move `bool wkey = false;` outside of the loop. 2. Ignore events with `.repeat != 0`. 3. Remove `SDL_WINDOW_OPENGL` flag, since you're using the builtin renderer and not GL. It hanged the program on my machine. – HolyBlackCat May 29 '22 at 14:44
  • 1
    You have now removed the dependancy on `SDL_image.h`, which is good, because it was not necessary. However, your question still does not contain a [mre], because it does not contain all parts necessary to reproduce the problem. In order to run your posted code, an image file is required, which you have not provided. If you want your question to be reopened, it would probably be best to draw an untextured rectangle instead of an image, for example using the function [`SDL_RenderFillRect`](http://wiki.libsdl.org/SDL_RenderFillRect), if you can reproduce the problem that way. – Andreas Wenzel May 29 '22 at 15:16

0 Answers0