Tutorial: changing colormap
===========================

Create a bitmap with a sunnyboy, fill it with the colorindex of yellow,
show it.

Load another colormap,
you see that the sunnyboy changed its color
according to the new color of its colorindex.

Create another sunnyboy, fill it with the new colorindex of yellow,
show both.

Load the previous colormap,
you see that the two sunnyboys have different colors
because of different colorindexes.


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 * sunboy1,* sunboy2;  // for the created sunnyboys
  char colm[128];  // save original colormap
  int i1,cl1,cl2;
  char buf[32];

  /* 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, using the yellowest color of the current colormap. ==>
  /* create a sunnyboy as yellow as possible and give it out */
  cl1=vg_color_index(CL_YELLOW,100);
  sunboy1=create_sunnyboy(cl1);
  if (sunboy1==NULL) {  // error
    vg_window_close();
    exit(1);
  }
  vg_bitmap_copyto(NULL,SC_WIDTH/4,SC_HEIGHT/2,sunboy1,0,0,0,0,RGB_FULL);
  snprintf(buf,sizeof(buf),"%d",cl1);
  vg_draw_text(NULL,RGB_WHITE,SC_WIDTH/4-strlen(buf)*vg_font_width(NULL)/2,SC_HEIGHT/2+vg_bitmap_width(sunboy1),buf,NULL,RGB_FULL);
  vg_window_flush();  // flush window
  sleep(3);
<== step 3 We load another colormap test1.clm. The colorindex (yellow) of sunboy1 keeps its value, but the color of this colorindex changes, therefore sunboy1 has another color. ==>
  /* load another colormap and save the current into colm */
  vg_load_colormap("test1.clm",colm,sizeof(colm));
  vg_window_flush();  // flush window
  sleep(3);
<== step 4 We create another sunnyboy, using the yellowest color of the new colormap (which is in fact not really yellow here). You see now 2 sunnyboys. ==>
  /* create another sunnyboy as yellow as possible and give it out */
  cl2=vg_color_index(CL_YELLOW,100);
  sunboy2=create_sunnyboy(cl2);
  if (sunboy2==NULL) {  // error
    vg_window_close();
    exit(1);
  }
  vg_bitmap_copyto(NULL,SC_WIDTH*3/4,SC_HEIGHT/2,sunboy2,0,0,0,0,RGB_FULL);
  snprintf(buf,sizeof(buf),"%d",cl2);
  vg_draw_text(NULL,RGB_WHITE,SC_WIDTH*3/4-strlen(buf)*vg_font_width(NULL)/2,SC_HEIGHT/2+vg_bitmap_width(sunboy2),buf,NULL,RGB_FULL);
  vg_window_flush();  // flush window
  sleep(3);
<== step 5 We load the original colormap. sunboy1 becomes yellow again, sunboy2 changes its color to another one. ==>
  /* load previous colormap */
  vg_load_colormap(colm,NULL,0);
  vg_window_flush();  // flush window
  sleep(3);
<== step 6 Become darker and darker. ==>
  /* become darker and darker, from current value to -63 */
  for (i1=vg_brightness(0); i1>=-63; i1--) {
    vg_brightness(i1);
    vg_window_flush();  // flush window
    vg_wait_time(30);
  }
<== step 7 We do the rest. ==>
  /* free bitmaps */
  vg_bitmap_free(sunboy1);
  vg_bitmap_free(sunboy2);

  /* close window */
  vg_window_close();

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