[SOLVED]Move graphics object on the screen

General FreeBASIC programming questions.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Move graphics object on the screen

Post by fxm »

More exactly with compile option '-exx':
On my (Windows) PC, in windowed mode (not full-screen), the program crashes (by run-time error) when trying to quickly move the mouse out of the window.
Mouse clipping mode alone is not enough.

Improved code:

Code: Select all

' *** CODE 4.3: PUT with background saving

#DEFINE PI 3.141592653589793
SCREENRES 300, 200, 32                ' Graphic screen with 32-bit color depth
DIM AS ANY PTR image, background
DIM AS ULONG light = RGB(255, 64, 64) ' lighter color value of the stone
DIM AS ULONG dark = RGB(192, 0, 0)    ' dark color value of the stone

' Write image to buffer
image = IMAGECREATE(40, 40)
background = IMAGECREATE(40, 40)
CIRCLE image, (20, 25), 15, dark, PI, 0, .6
LINE image, (5, 20)-STEP (0, 5), dark
LINE image, (35, 20)-STEP (0, 5), dark
CIRCLE image, (20, 20), 15, dark, , , .6
PAINT image, (20, 30), dark, dark
PAINT image, (20, 20), light, dark

' create background
LINE (50, 50)-(250, 150), RGB(0,255,0), BF ' green rectangle ...
LINE (80, 80)-(220, 120), RGB(0,0,255), BF ' ... and a blue one inside

DIM AS INTEGER mx = 0, my = 0, mb ' Mouse position and button status
DIM AS INTEGER oldX = 0, oldY = 0          ' last noted mouse position
SETMOUSE mx, my, 0, 1                      ' Limit mouse to window
GET (oldX, oldY)-STEP(39, 39), background  ' save background
PUT (mx, my), image, TRANS
DO
  IF GETMOUSE(mx, my, ,mb) = 0 THEN          ' determine new position ...
    IF mx > 260 THEN mx = 260          ' ... and adjust to the limits
    IF my > 160 THEN my = 160
    IF mx <> oldX OR my <> oldY THEN   ' mouse was moved
      SCREENLOCK
      PUT (oldX, oldY), background, PSET       ' restore old position
      GET (mx, my)-STEP(39, 39), background    ' save background
      PUT (mx, my), image, TRANS               ' draw new position
      SCREENUNLOCK
      oldX = mx                           ' remember new position
      oldY = my
    END IF
  END IF
  SLEEP 1
LOOP UNTIL mb > 0 OR INKEY = CHR(27)    ' exit with mouse click or ESC
IMAGEDESTROY image                      ' Release image buffer
IMAGEDESTROY background
Post Reply