Tutorial: saving and loading a bitmap
=====================================

If you don't want to create a bitmap on-the-fly in your program,
load it from a file with vg_bitmap_createfromfile().
This file may be in a VgaGames-bitmap-format, a PPM format or a Windows bmp.
VgaGames-bitmap-format is recommended because of performance.

To create a VgaGames-bitmap,
 - create a MS-Windows .bmp file
 - convert it into VgaGames-bitmap-format with the program vgag-bitmap
or
 - create a graphic file in another format
 - convert it into a PPM-file with the netpbm-package
 - convert this PPM-file into VgaGames-bitmap-format with vgag-bitmap

vgag-bitmap converts to and from
 - MS-Windows .bmp
 - PPM (netpbm)
 - VgaGames-bitmap-format


Now to the tutorial:
We want to create a bitmap with a sunnyboy,
save it as a VgaGames-bitmap-format file,
load and show it, then end the program.


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 created sunnyboy

  /* 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 an orange sunnyboy using the subfunction create_sunnyboy() ==>
  /* create an orange sunnyboy */
  sunboy=create_sunnyboy(vg_color_index(CL_ORANGE,100));
  if (sunboy==NULL) {  // error
    vg_window_close();
    exit(1);
  }
<== step 3 We save the created sunnyboy in a file. ==>
  /* save sunnyboy as a file in VgaGames-bitmap-format,
     we use directory share/ because it is writable
  */
  vg_bitmap_save(sunboy,"share/sunnyboy.vga",VGAFORMAT_VGA);
<== step 4 We load the saved file. ==>
  /* now we want to load it */
  vg_bitmap_free(sunboy);  // at first free bitmap
  sunboy=vg_bitmap_createfromfile("share/sunnyboy.vga");
  if (sunboy==NULL) {  // error
    fprintf(stderr,"Bad luck - could not load sunnyboy.vga\n");
  }
<== step 5 Now we give out the loaded bitmap and do the rest. ==>
  /* give it out */
  if (sunboy!=NULL) {
    int x,y;
    x=SC_WIDTH/2;
    y=SC_HEIGHT/2;
    vg_bitmap_copyto(NULL,x,y,sunboy,0,0,0,0,RGB_TRANS);

    /* free bitmap (and remove it) */
    vg_bitmap_free(sunboy);
    unlink("share/sunnyboy.vga");

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

    /* wait 3 seconds */
    sleep(3);
  }

  /* close window */
  vg_window_close();

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