Tutorial: using mouse
=====================

We want to use the mouse. At the position of the mouse-pointer
a single white pixel is drawn.
When moving the mouse, the pixel also moves.
When moving the mouse with left mouse-button pressed, a line is drawn
from the position where the button was pressed to the actual position.


step 1
At first we have to initialize VgaGames
and open a window or switch to graphical screen,
then to check for the mouse.

==>
int main(int argc, char ** argv) {
  char * arg0;  // for the name of the program

  /* initialize vgagames, always pass argv[0] */
  if (vg_init_vgagames(argv[0],0,NULL) < 0) {exit(1);}

  /* open window */
  if ((arg0=strrchr(argv[0],'/'))==NULL) {arg0=argv[0];} else {arg0++;}
  if (vg_window_open(arg0,0,0) < 0) {exit(1);}

  /* check for mouse */
  if (!vg_mouse_found()) {
    fprintf(stderr,"Cannot find a mouse\n");
    vg_window_close();  // close window
    exit(1);
  }
<== step 2 We want to draw the white pixel at the mouse-position using a loop. It is important to check whether the mouse is inside the window. ==>
  /* program loop */
  for (;;) {

    /* update key and mouse events */
    vg_key_update();

    if (vg_mouse_x() >= 0) {  // mouse inside the window
      /* draw the pixel */
      int x,y;
      x=vg_mouse_x();  // get x-position of mouse
      y=vg_mouse_y();  // get y-position of mouse
      vg_draw_pixel(NULL,x,y,RGB_WHITE);

      /* now flush window out to visible screen */
      vg_window_flush();
    }

    /* wait a little time up to 70 milliseconds */
    vg_wait_time(70);
  }
<== step 3 Now we want to enhance the code of step 2 to draw the line when pressed the left mouse-button. ==>
  /* program loop */
  for (;;) {

    /* update key and mouse events */
    vg_key_update();

    /* clear the window (not visible unless called vg_window_flush()) */
    vg_bitmap_clear(NULL, RGB_BLACK);

    if (vg_mouse_x() >= 0) {  // mouse inside the window
      static int press_x=0, press_y=0;  // where mouse-button was pressed
      int x,y;
      x=vg_mouse_x();  // get x-position of mouse
      y=vg_mouse_y();  // get y-position of mouse

      /* check whether left mouse-button is pressed */
      if (vg_mouse_pressed(MOUSE_LEFT, LONGKEY)) {
        /* draw a line */
        vg_draw_line(NULL,press_x,press_y,x,y,RGB_WHITE);
      } else {
        /* draw the pixel */
        vg_draw_pixel(NULL,x,y,RGB_WHITE);
        press_x=x;  // remember mouse x-position
        press_y=y;  // remember mouse y-position
      }

      /* now flush window out to visible screen */
      vg_window_flush();
    }

    /* wait a little time up to 70 milliseconds */
    vg_wait_time(70);
  }
<== step 4 We are ready with our code, but still have to include an ending condition. We decide to end the program, when the right mouse-button is pressed. ==>
  /* program loop */
  for (;;) {

    /* update key and mouse events */
    vg_key_update();

    if (vg_mouse_x() >= 0) {  // mouse inside the window
      static int press_x=0, press_y=0;  // where mouse-button was pressed
      int x,y;
      x=vg_mouse_x();  // get x-position of mouse
      y=vg_mouse_y();  // get y-position of mouse

      /* check whether left mouse-button is pressed */
      if (vg_mouse_pressed(MOUSE_LEFT, LONGKEY)) {
        /* draw a line */
        vg_draw_line(NULL,press_x,press_y,x,y,RGB_WHITE);
      } else {
        /* draw the pixel */
        vg_draw_pixel(NULL,x,y,RGB_WHITE);
        press_x=x;  // remember mouse x-position
        press_y=y;  // remember mouse y-position
      }

      /* here comes an info text */

      /* now flush window out to visible screen */
      vg_window_flush();

      /* check for right mouse-button to end the program,
         instead of using SHORTKEY here we also could have used LONGKEY
      */
      if (vg_mouse_pressed(MOUSE_RIGHT, SHORTKEY)) {
        break;
      }
    }

    /* wait a little time up to 70 milliseconds */
    vg_wait_time(70);
  }

  /* close window */
  vg_window_close();

  exit(0);
}
<== The whole program tut-window2.c
[Previous] - [Index] - [Next]