joyrider3774
Posts: 35
Joined: Sun Mar 13, 2016 12:21 pm

Anyone using SDL2 with official raspberry pi touchscreen ?

Tue Feb 11, 2020 7:33 pm

Hi,

I'm creating a little game to be used on the raspberry pi using mouse and keyboard, but i also own an official raspberry pi touchscreen and noticed when using the touchscreen that sdl did not generate mousebutton down events. instead it seems i need to use SDL_TouchFingerEvent for it to work. I implemented it in my game but i'm having problems when letterboxing occurs. I'm using a logical size of 320x240 for my game but a (default) window size of 640x480 so sdl scales the rendering. I also let my window be resizeable. Unlike the mousebuttondown events where the x and y position is relative to the window and it adheres to letterboxing occuring and scaling (so i can use calculations based on 320x240 perspective) SDL_Touchfingerevent seems to be using a normalized value for the positions between 0 and 1. When i don't resize my window i can get the the 320x240 coordinates by multiplying the x & y values with the window size however this starts to fail when letterboxing (to keep aspect ratio during scaling) occurs when i have resized my window and i can not seem to calculate the values for 320x240 perspective anymore. I also seem to be getting negative values while the docs said it was normalized between 0 & 1 when letterboxing happens.

i mean this with letterboxing (the black borders getting added above and under left and right but in screenshot i only resized on x axis)
Image

to me it seems i need to substract the black sizes from the calculations but i can't seem to be able to calculate this and it probably won't not work as the touchfingerevent values don't start from 0 anymore when letterboxing occurs. Does anyone know how to correctly calculate the position relative to 320x240 when this occurs ?

Maybe someone already used sdl2 and the official touchscreen and came accros a similar problem ?

Thanks

joyrider3774
Posts: 35
Joined: Sun Mar 13, 2016 12:21 pm

Re: Anyone using SDL2 with official raspberry pi touchscreen ?

Fri Feb 14, 2020 7:30 pm

ok i managed to fix it.

For some reason if the rendertarget in SDL is not the screen i was getting other values. Setting the rendertarget to the screen with using `SDL_SetRenderTarget(Renderer, NULL)` before calling pollevent or just making sure the render target was my screen before calling it fixed it. Then all i had to do was multiply the x & y values with logical size (in my case 320x240) if you don't use a logical size different from window size you probably just have to multiply with your screen size

Return to “General programming discussion”