Tutorial: create and show sprite
================================

Create a sprite with a sunnyboy, which zoomes in and out,
and show it.


step 1
At first we have to initialize VgaGames
and open a window or switch to graphical screen.

==>
int main(int argc, char ** argv) {
  char * arg0;  // for the name of the program
  bitmap * sunboy;  // for the sunnyboy
  sprite * sunsprite;  // sunnyboy sprite
  double d1;

  /* 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);}
<== step 2 We create a yellow sunnyboy. ==>
  /* create a yellow sunnyboy */
  sunboy=create_sunnyboy(vg_color_index(CL_YELLOW,100));
  if (sunboy==NULL) {  // error
    vg_window_close();
    exit(1);
  }
<== step 3 We create a new empty sprite. ==>
  /* create a new new sprite */
  sunsprite=vg_sprite_createnew();
  if (sunsprite==NULL) {  // error
    vg_window_close();
    exit(1);
  }
<== step 4 We add the sunnyboy to the sprite at first becoming larger, then becoming smaller to its original size again. The size is changed via vg_bitmap_zoom(), which returns a static variable of the size-changed sunnyboy. This static variable is passed to vg_sprite_add(), which always makes a copy of the passed bitmap. Then free the sunnyboy-bitmap, which is not more used. ==>
  /* add zoomed sunnyboys to sprite */
  vg_sprite_add(sunsprite,vg_bitmap_zoom(sunboy,1.0,1.0),18,NULL,0);
  for (d1=1.1; d1<2.0; d1+=0.1) {  /* becoming larger */
    vg_sprite_add(sunsprite,vg_bitmap_zoom(sunboy,d1,d1),3,NULL,0);
  }
  vg_sprite_add(sunsprite,vg_bitmap_zoom(sunboy,d1,d1),18,NULL,0);
  for (; d1>1.0; d1-=0.1) {  /* becoming smaller */
    vg_sprite_add(sunsprite,vg_bitmap_zoom(sunboy,d1,d1),3,NULL,0);
  }
  vg_sprite_add(sunsprite,vg_bitmap_zoom(sunboy,1.0,1.0),18,NULL,0);

  /* free sunnyboy */
  vg_bitmap_free(sunboy);
<== step 5 Now give out the sprite one time, this means until vg_sprite_getnext() returns NULL. ==>
  /* give out sprite one time */
  while ((sunboy=vg_sprite_getnext(sunsprite))!=NULL) {
    vg_bitmap_clear(NULL,RGB_BLACK);
    vg_bitmap_copyto(NULL,SC_WIDTH/2,SC_HEIGHT/2,sunboy,0,0,0,0,RGB_FULL);
    vg_window_flush();
    vg_wait_time(50);
  }
<== step 6 We do the rest. ==>
  /* free sprite */
  vg_sprite_free(sunsprite);

  /* close window */
  vg_window_close();

  exit(0);
}
<== alternative If we would load the sprite from a file with vg_sprite_createfromfile() instead of creating it, the sprite file could be as follows (assuming the sunnyboy-bitmapfile were sunboy.vga in the same directory): ==>
  [SPRITE]
  sunboy.vga LOOP=18
  sunboy.vga LOOP=3 ZOOM=1.1,1.1
  sunboy.vga LOOP=3 ZOOM=1.2,1.2
  sunboy.vga LOOP=3 ZOOM=1.3,1.3
  sunboy.vga LOOP=3 ZOOM=1.4,1.4
  sunboy.vga LOOP=3 ZOOM=1.5,1.5
  sunboy.vga LOOP=3 ZOOM=1.6,1.6
  sunboy.vga LOOP=3 ZOOM=1.7,1.7
  sunboy.vga LOOP=3 ZOOM=1.8,1.8
  sunboy.vga LOOP=3 ZOOM=1.9,1.9
  sunboy.vga LOOP=18 ZOOM=2.0,2.0
  sunboy.vga LOOP=3 ZOOM=1.9,1.9
  sunboy.vga LOOP=3 ZOOM=1.8,1.8
  sunboy.vga LOOP=3 ZOOM=1.7,1.7
  sunboy.vga LOOP=3 ZOOM=1.6,1.6
  sunboy.vga LOOP=3 ZOOM=1.5,1.5
  sunboy.vga LOOP=3 ZOOM=1.4,1.4
  sunboy.vga LOOP=3 ZOOM=1.3,1.3
  sunboy.vga LOOP=3 ZOOM=1.2,1.2
  sunboy.vga LOOP=3 ZOOM=1.1,1.1
  sunboy.vga LOOP=18
<== The whole program tut-sprite1.c create_sunnyboy.c
[Previous] - [Index] - [Next]