Tutorial: create a bitmap with a sunnyboy ========================================= Create a bitmap with a sunnyboy in a subfunction, show it and 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);} |
/* create a green sunnyboy */ sunboy=create_sunnyboy(vg_color_index(CL_GREEN,100)); if (sunboy==NULL) { // error vg_window_close(); exit(1); } |
/* copy sunnyboy to window */ {int x,y; // copy it to the middle of the window, // vg_bitmap_copyto() copies the middle of the sunnyboy // to the destination coordinates. x=SC_WIDTH/2; y=SC_HEIGHT/2; // we use RGB_TRANS to copy only the face, not the edges vg_bitmap_copyto(NULL,x,y,sunboy,0,0,0,0,RGB_TRANS); } |
/* now flush window out to visible screen */ vg_window_flush(); /* wait 3 seconds */ sleep(3); /* free bitmap */ vg_bitmap_free(sunboy); /* close window */ vg_window_close(); exit(0); } |
/* function to return a created sunnyboy bitmap */ bitmap * create_sunnyboy(int color) { /* give back a bitmap with a sunnyboy */ int x,y,r; bitmap * sunboy; r=10; // radius of face of Sunnyboy /* create empty bitmap, large enough for sunnyboy */ sunboy=vg_bitmap_createnew(r*2+1,r*2+1); if (sunboy==NULL) {return(NULL);} // error /* get middle of bitmap */ x=vg_bitmap_width(sunboy)/2; y=vg_bitmap_height(sunboy)/2; /* draw sunnyboy into the middle of the bitmap */ vg_draw_circle(sunboy,x,y,r,RGB_WHITE,0); vg_draw_fillout(sunboy,x,y,color); // draw eyes, nose and mouth. // We use RGB_DARK instead of RGB_BLACK, because RGB_BLACK is not only // black but may also be considered as transparent pixel, which is not // drawn, if the bitmap is copied in transparent mode. // RGB_DARK is the next darkest pixel, which is always drawn. vg_draw_box(sunboy,x-4,y-4,2,2,RGB_DARK,0); vg_draw_box(sunboy,x+4,y-4,2,2,RGB_DARK,0); vg_draw_pixel(sunboy,x-1,y,RGB_DARK); vg_draw_pixel(sunboy,x+1,y,RGB_DARK); vg_draw_pixel(sunboy,x-4,y+3,RGB_DARK); vg_draw_pixel(sunboy,x+4,y+3,RGB_DARK); vg_draw_line(sunboy,x-3,y+4,x-2,y+4,RGB_DARK); vg_draw_line(sunboy,x+3,y+4,x+2,y+4,RGB_DARK); vg_draw_line(sunboy,x-1,y+5,x+1,y+5,RGB_DARK); return(sunboy); } |