VgaGames 3 - Miscellaneous man-pages

[.. upper level ..]

VG3_get_windowpos_on_bgimage()

Positioning the window on a larger background-image: Get x-/y-position of the background-image
SYNTAX
void VG3_get_windowpos_on_bgimage(struct vg3_windowpos_bgimage *wbpos, int xobj, int yobj, int *bgpos_x, int *bgpos_y, int at_once)

FUNCTION PARAMETERS
wbpos Window-positioning structure
xobj Central x-position of the player-object-image which shall be centered
yobj Central y-position of the player-object-image which shall be centered
bgpos_x For returning x-position of the background on the window
bgpos_y For returning y-position of the background on the window
at_once If player-object-image is not in the focus:
0 = move window steadily, 1 = move window immediately to the image

DESCRIPTION
If the game field, presented by a background-image, is larger than the window, only a section shall be shown, which moves according to the position of the player-object on the background-image. This section is generally the window. With this function the position of the background-image is returned. This position, returned in bgpos_x and bgpos_y must be added when copying the background-image or any other image to the window.

EXAMPLE

Positioning the window on a larger background-image according to the image of the player-object:
  void *wstruct;
  void *bg_image, *player_image;
  int win_w, win_h, bg_w, bg_h;
  struct vg3_rect player_rect;
  struct vg3_windowpos_bgimage wbpos;
  int bgpos_x, bgpos_y;

  [... opening window (wstruct), loading bg_image and player_image ...]

  /* getting window-size */
  VG3_window_getsize(wstruct, &win_w, &win_h);

  /* getting size of the background-image */
  VG3_image_getsize(wstruct, bg_image, NULL, &bg_w, &bg_h);

  /* setting position of the player-object into player-rectangle */
  player_rect.x = player_rect.y = 0;
  VG3_image_getsize(wstruct, player_image, NULL, &player_rect.w, &player_rect.h);

  /* initalize window-positioning structure */
  VG3_init_windowpos_on_bgimage(&wbpos, bg_w, bg_h, win_w, win_h, 2, 60);

  /* game loop */
  for (;;) {
    /* get background-image position according to player_rect into bgpos_x and bgpos_y */
    VG3_get_windowpos_on_bgimage(&wbpos,
                                 player_rect.x + player_rect.w / 2,
                                 player_rect.y + player_rect.h / 2,
                                 &bgpos_x, &bgpos_y, 0);

    /* copy background-image to window using position bgpos_x and bgpos_y */
    VG3_image_copy(wstruct, NULL, bg_image, bgpos_x + bg_w / 2, bgpos_y + bg_w / 2, NULL, 0);

    /* copy player-image to window adding bgpos_x and bgpos_y */
    VG3_image_copy(wstruct, NULL, player_image,
                   player_rect.x + player_rect.w / 2 + bgpos_x,
                   player_rect.y + player_rect.h / 2 + bgpos_y,
                   NULL, 0);

    [...]
  }

SEE ALSO
VG3_init_windowpos_on_bgimage()