Silverfrost Forums

Welcome to our forums

Best way to catch mouse cursor leaving %gr?

30 Jul 2012 11:48 #10526

Hi all,

I am writing a minesweeper game using clearwin+. I currently use %gr with full mouse input to get mouse state and for drawing the game board. Game board is drawn by drawing buffer to %gr and then board tile below mouse cursor is highlighted by drawing correct highlighted board tile image on top of it.

I would like to remove tile highlighting from game board when mouse cursor leaves %gr. What is the best way to catch when that happens?

Some simple example code would be nice... 😃

30 Jul 2012 1:58 #10527

Just stop highlighting when the cursor co-ordinates are outside the graphics windows? Here is some example code:

winapp 
program my_game 
implicit none
include <windows.ins> 

external    display
integer*4   j,ptr

j = winio@('%`^gr[user_surface,full_mouse_input]',200L,200L,ptr,1L,display)
end 

integer function display()
implicit none
include <windows.ins> 

integer*4   mouse_x,mouse_y,x1,x2,y1,y2

mouse_x = CLEARWIN_INFO@('GRAPHICS_MOUSE_X')
mouse_y = CLEARWIN_INFO@('GRAPHICS_MOUSE_Y')

x1 = 20*int(mouse_x/20.)
x2 = x1+19
y1 = 20*int(mouse_y/20.)
y2 = y1+19

call draw_filled_rectangle@(0,0,200,200,255)
if (mouse_x > 5 .and. mouse_x < 195 .and. mouse_y > 5 .and. mouse_y < 195) then
  call draw_filled_rectangle@(x1,y1,x2,y2,10)
end if

display = 1
end

Regards - Wilfried

30 Jul 2012 6:33 #10528

Quoted from Wilfried Linder Just stop highlighting when the cursor co-ordinates are outside the graphics windows?

Thanks,

I used cursor_monitor@() for this purpose. One problem however remains: it's possible to move mouse cursor outside the program window so fast that %gr or cursor monitor callbacks don't get launched. Any way for fixing this behaviour?

My original minesweeper game written in Hollywood is available here There are sources and binaries for some operating systems.

30 Jul 2012 7:49 #10529

If I understand Wilfried correctly, he has a border round the gameplay (board) area that the mouse pointer has to cross to exit. You could put something in this area so it wasn't obvious that the board didn't fill all the %gr area - something like a 3D or decorative border. If the mouse coordinates were in the border then you could un-highlight all the gameplay squares.

You may need to experiment on how big the border has to be to let you catch the mouse on its way off the play area. You could do all your highlighting etc in the graphics callback without needing to use ADD_CURSOR_MONITOR@.

I do something similar in one of my programs, and I use IMPORT_IMAGE@ a lot. I never buffer the graphics, but just keep overlaying the squares with new graphics. The screen is the buffer. You just need to draw bitmaps for each of the possible states of a square to use with IMPORT_IMAGE@. If you are doing it for your own entertainment, you can grab images from other programs using SHIFT-PrtSc and then paste the picture into paint. Then you can cut out and use the bit(s) you want. (My application isn't a game, it is a repositionable toolbar - I have bitmaps for each state each button can be in. I solved the problem of detecting mouse exit from a button by only selecting the middle 10x10 of a 16x16 button as its active area, in other words a 3 pixel border was enough to catch the mouse on its way out).

Eddie

31 Jul 2012 6:36 #10535

According to Eddie's remarks I added a 20-pixels border, then it runs better:

winapp 
program my_game 
implicit none
include <windows.ins> 

external    display
integer*4   j,ptr

j = winio@('%ww[no_border]&')  ! added
j = winio@('%`^gr[user_surface,full_mouse_input]',240L,240L,ptr,1L,display)  ! expanded to 240x240 pixels
end 

integer function display()
implicit none
include <windows.ins> 

integer*4   mouse_x,mouse_y,x1,y1

mouse_x = CLEARWIN_INFO@('GRAPHICS_MOUSE_X')
mouse_y = CLEARWIN_INFO@('GRAPHICS_MOUSE_Y')

call draw_filled_rectangle@(0,0,240,240,255)
if (mouse_x > 25 .and. mouse_x < 215 .and. mouse_y > 25 .and. mouse_y < 215) then
  x1 = 20*int(mouse_x/20.)
  y1 = 20*int(mouse_y/20.)
  call draw_filled_rectangle@(x1,y1,x1+19,y1+19,10)
end if

display = 1
end

Regards - Wilfried

31 Jul 2012 7:06 #10536

Ok, I added a small border around the %gr area and things works quite well now.

Thanks for help, I will probably return with more questions later.

EDIT:

Ended up using timer callback for monitoring mouse cursor position using: GetCursorPos() - Get mouse cursor position relative to screen coordinates ScreenToClient() - Convert mouse cursor coordinates relative to client window

Please login to reply.