Category: touchscreen

Working with Mice in SDL

Working with Mice in SDL

Computer mouse
Image by OpenClipart-Vectors from Pixabay

On the Match three game, I’m at the point where I need to be able to click on a piece and drag it to the next piece (horizontal or vertical) and then start the two animating to swap positions.

In SDL, I’ve got a simple mouseclick function that is called from ProcessEvents (my function for handling all input) with the x,y coordinates on the screen where clicked. These have to beĀ  converted from screen coordinates to board coordinates (or ignored if not on the board) to identify the piece clicked. However the mouse now needs to drag this piece to an adjacent piece.

As usual I’m not the first person to ask this and there’s a StackOverflow Question with an answer. So I’ll be following this answer and hopefully get it working. This is possibly the key mechanic in a MatchThree game so it’s important to get it right. After that I’ll get teh touchscreen working again which should in theory use the same code apart from reading the game controller..

My raspberry Pi touchscreen arrived but it wasn’t smooth going

My raspberry Pi touchscreen arrived but it wasn’t smooth going

I bought the Longruner (no, not a typo!) 7″ touch screen and it turned up after a few days. It’s nicely made and similar to this, though it looks slightly different. That hand must be a dolls hand!

It doesn’t have any documentation but there was a DVD with three MS Word documents which I opened on my Windows PC (It comes in useful sometimes!). One of them the “7inch HDMI Display user Manual(En).docx” has a photo of the three ports.

There’s a full size HDMI socket and a USB to Android (Micro USB-B I believe) cable is included that plugs into a USB port and either of the Android type ports. You can either have power and touch screen with the port nearest the HDMI as I did or just power alone, if you want to use it as a monitor. The two green squares show the USB and touch/power cables.

When I switched it on, it just sat there saying no HDMI signal. That MS Word document included instructions to edit the config.txt in \boot. Of course I had no display but the Pi was plugged into the network and I ran WinSCP which logged in and let me view files. I don’t think you can edit files as root with WinSCP, and that config.txt file needs root access to edit it.

However I am a long time user of Putty. (You can also get putty from putty.org but I don’t know if that is also an official one). After that I could ssh into the pi, do a sudo -s command to switch in root and then nano /boot/config.txt to edit config.txt.

longruner touchscreen being touchedHere’s a photo of my fat finger moving the cursor round the screen. It works surprisingly well, and the display is lovely though my photos probably do not do it justice.

These are the lines you paste into the end of the config.txt, save it out and reboot.

max_usb_curren=1
hdmi_force_hotplug=1
config_hdmi_boost=7
hdmi_group=2
hdmi_mode=87
hdmi_drive=1
display_rotate=0
hdmi_cvt 1024 600 60 6 0 0 0

Now I just have to figure out how to use it in my games. The display is 1024 x 600 btw.

Plugging in a second monitor worked fine as this photo shows. The blacks are darker on the touchscreen but that’s just because the gammas don’t match and I was too lazy to change them!

Touchscreen and 2nd monitor on Raspberry Pi