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();
}