Texture drawn at wrong coordinates?

I tried drawing a ‘1’ texture at mouse coordinates when I press the 1 key:

switch (e.type)
{
    case SDL_QUIT:
    {
        quit = true;
        break;
    }
    case SDL_KEYDOWN:
    {
        switch (e.key.keysym.sym)
        {
            case SDLK_1:
            {
                SDL_Rect rect = {e.motion.x - 8, e.motion.y - 8, 16, 16};
                SDL_RenderCopy(gRenderer, gT[3], NULL, &rect);
                printf("1\n");
                break;
            }
        }
        break;
    }
}

I cannot comprehend why this doesn’t work.

gT[3] is the texture of the ‘1’.

I thought maybe it is because its e.key.keysym.sym but I’m not sure.

>Solution :

SDL_Event::motion is only valid when SDL_Event::type is SDL_MOUSEMOTION.

Stop trying to use SDL_Event::motion when SDL_Event::type is SDL_KEYDOWN, perhaps by recording the X and Y coordinates of the most recent SDL_MOUSEMOTION event and using those instead.

Leave a Reply