Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Mouse clipping

This thread is locked; no one can reply to it. rss feed Print
[A5] Mouse clipping
Neil Roy
Member #2,229
April 2002
avatar

Edit: using Allegro 5.0.7. I would try a newer version but I would need a precompiled binary for MingW-TDM 4.6.1 :(

I have been doing more work on my Deluxe Pacman 2 level editor and I decided to code it so it loads in the same config file the main game uses. This way you can edit your levels in fullscreen mode etc as you prefer.

Anyhow, I use ALLEGRO_FULLSCREEN_WINDOW and transforms/clipping to scale the screen size up with a black border.

I just realized today that when my editor uses the same function to scale the screen up, that although the drawing is clipped, the mouse isn't. This has led my mouse code to not work, I have to click off to the left of where the buttons really are on screen to get them to work.

I can add code to adjust the mouse position if full screen, but... you never know what the resolution is that the person will be using. I am wondering if you have considered adding in mouse clipping in the same way there is screen clipping? I can move the mouse pointer into the black areas which looks a bit odd when the images displayed are clipped. I'm not entirely certain how to safe go about programming the mouse code for my buttons. It currently checks the region the buttons were drawn in, but the mouse doesn't match the drawing co-ordinates and as I said, you never know what the resolution will be that the person will run this on. ???

Any ideas, suggestion are welcome, thanks.

This isn't too important, but just so you know, I use the following code to fit my screen to the fullscreen mode mentioned;

#SelectExpand
1// Scale Screen: 2// Sets up transforms in order to fit any resolution 3// onto an ALLEGRO_FULLSCREEN_WINDOW. 4// 5// (Special thanks to Matthew Leverton for help with this!) 6 7#include <allegro5/allegro.h> 8 9// usage: a5_scale_screen(BUFFER_WIDTH, BUFFER_HEIGHT, display_width, display_height); 10void a5_scale_screen(int bw, int bh, int dw, int dh) 11{ 12 ALLEGRO_TRANSFORM t; 13 14 // Calculate the horizontal and vertial aspect ratios 15 const float HAR = dw/(float)bw; 16 const float VAR = dh/(float)bh; 17 18 // The aspect ratio, x-offset and y-offset (in pixels) 19 float ar, ox, oy; 20 21 22 if (bw == dw && bh == dh) 23 { 24 // 1:1, just reset everything 25 al_identity_transform(&t); 26 al_use_transform(&t); 27 al_set_clipping_rectangle(0, 0, bw, bh); 28 } 29 else 30 { 31 // Choose the smaller aspect ratio 32 if (HAR < VAR) 33 { 34 // horizontal bars on the top and bottom 35 ar = HAR; 36 ox = 0; 37 oy = (dh - (ar * bh)) / 2.0; 38 } 39 else 40 { 41 // vertical bars on the left and right 42 ar = VAR; 43 ox = (dw - (ar * bw)) / 2.0; 44 oy = 0; 45 } 46 47 // set up the transformation to scale and translate 48 al_build_transform(&t, ox, oy, ar, ar, 0); 49 al_use_transform(&t); 50 51 // make sure nothing is drawn into the black bars 52 al_set_clipping_rectangle(ox, oy, ar * bw, ar * bh); 53 } 54}

---
“I love you too.” - last words of Wanda Roy

Matthew Leverton
Supreme Loser
January 1999
avatar

You need to apply the inverse transformation to the mouse coordinates. For example:

https://bitbucket.org/leverton/tsb-nes-helmet-editor/src/23c42e735f14/src/gfx.c#cl-103

https://bitbucket.org/leverton/tsb-nes-helmet-editor/src/23c42e735f14/src/gfx.c#cl-311

This is the important bit:

mouse_x = (event.mouse.x - offset_x) / scale_x;
mouse_y = (event.mouse.y - offset_y) / scale_y;

Neil Roy
Member #2,229
April 2002
avatar

Very nice, thanks for that. I was sitting on the can thinking about how to do this, ;D... great place to think the bathroom is and was thinking along these lines but still wasn't 100% certain how I was going to go about it.

while (status != 666)

:o EVIL!!! :P

---
“I love you too.” - last words of Wanda Roy

Matthew Leverton
Supreme Loser
January 1999
avatar

I wouldn't learn too much from the code. I wrote it in 8-bit Allegro 4 and converted it to Allegro 5. :'(

Neil Roy
Member #2,229
April 2002
avatar

I like looking over how other people do things, there's always something I can learn. :)

---
“I love you too.” - last words of Wanda Roy

Go to: