Re: [AD] scare_mouse_area

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Sun, Dec 19, 1999 at 04:27:24PM +0100, Vincent Penquerc'h wrote:
> Here is a scare_mouse_area routine, which scares the mouse only if it is
> in the specified bounds.

Sorry, I forgot to attach it ....
Here it is.

-- 
Vincent Penquerc'h
Windows NT - New Trial

--- allegro/src/mouse.c.original	Sun Dec 19 16:22:44 1999
+++ allegro/src/mouse.c	Sun Dec 19 16:08:47 1999
@@ -507,8 +507,50 @@
 
 
 
+/* scare_mouse_area:
+ *  Removes the mouse pointer prior to a drawing operation, if that is
+ *  required (ie. noop if the mouse is on a memory bitmap, or a hardware
+ *  cursor is in use, or the mouse lies outside of the specified bounds
+ *  (in this last case, the mouse is frozen)). This operation can later
+ *  be reversed by calling unscare_mouse().
+ *  To remember that the mouse was only frozen and not hidden, we store
+ *  0xffffffff as the pointer to the mouse BITMAP, assuming this is an
+ *  invalid pointer and thus can be used as a flag
+ */
+void scare_mouse_area(int x, int y, int w, int h)
+{
+   if (!mouse_driver)
+      return;
+
+   if ((is_same_bitmap(screen, _mouse_screen)) && (!(gfx_capabilities & GFX_HW_CURSOR))) {
+      if ((mouse_x - _mouse_x_focus < x + w) &&
+          (mouse_x - _mouse_x_focus + _mouse_sprite->w >= x) &&
+          (mouse_y - _mouse_y_focus < y + h) &&
+          (mouse_y - _mouse_y_focus + _mouse_sprite->h >= y)
+      ) {
+         if (scared_size < SCARED_SIZE)
+	    scared_screen[scared_size] = _mouse_screen;
+         show_mouse(NULL);
+      }
+      else {
+         if (scared_size < SCARED_SIZE)
+	    scared_screen[scared_size] = (BITMAP*)0xffffffff;
+         freeze_mouse_flag++;
+      }
+   }
+   else {
+      if (scared_size < SCARED_SIZE)
+	 scared_screen[scared_size] = NULL;
+   }
+
+   scared_size++;
+}
+
+
+
 /* unscare_mouse:
- *  Restores the original mouse state, after a call to scare_mouse().
+ *  Restores the original mouse state, after a call to scare_mouse() or
+ *  scare_mouse_area.
  */
 void unscare_mouse()
 {
@@ -520,7 +562,10 @@
 
    if (scared_size < SCARED_SIZE) {
       if (scared_screen[scared_size]) {
-	 show_mouse(scared_screen[scared_size]);
+         if (scared_screen[scared_size]==(BITMAP*)0xffffffff)
+            freeze_mouse_flag--;
+         else
+	    show_mouse(scared_screen[scared_size]);
       }
 
       scared_screen[scared_size] = NULL;
#include <allegro.h>

volatile int ticks;
void ticker()
{
  ticks++;
}

void main(int argc,char**argv)
{
  BITMAP *sprite;
  int i,j;
  PALETTE pal;
  int n;
  int t[2];

  install_allegro(SYSTEM_AUTODETECT,&errno,atexit);
  install_timer();
  install_keyboard();
  install_mouse();
  set_gfx_mode(GFX_AUTODETECT,640,460,0,0);
  show_mouse(screen);
  sprite=load_bitmap("sma.pcx",pal);
  if (!sprite) {
    set_gfx_mode(GFX_TEXT,80,25,0,0);
    allegro_message("Sprite\n");
    exit(1);
  }
  set_palette(pal);

  set_mouse_sprite_focus(0,0);
  install_int_ex(ticker,BPS_TO_TIMER(100));
  for (n=0;n<2;n++) {
    ticks=0;
    i=j=0;
    do {
      if (n || argc!=1) scare_mouse_area(i,j,sprite->w,sprite->h);
      if (!n && argc==1) scare_mouse();
      draw_sprite(screen,sprite,i,j);
      textprintf(screen,font,0,0,makecol(255,0,0),
         "%4d %4d %1d            ",i,j,freeze_mouse_flag);
      unscare_mouse();
/*
      j++;
      if (j==480) {
        vsync();
        j=0;
        i++;
        if (i==640) break;
      }
*/
      i++;
      if (i==640) {
        vsync();
        i=0;
        j++;
        if (j==480) break;
      }
    } while (!keypressed());
    t[n]=ticks;
  }
  set_gfx_mode(GFX_TEXT,0,0,0,0);
  printf("scare_mouse: %d    scare_mouse_area: %d\n",t[0],t[1]);
}
END_OF_MAIN()




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/