SDL Image Browsing in Linux
# Include "SDL. H "// # include" sdl_ttf.h "# include" sdl_rotozoom.h "# include" sdl_image.h "# include" math. H "# include" string. H "# define Max 16 # define BMP _name" icon.bmp "# define icon" sample.bmp "# define title_name" testsdl "# define icon_name" My winframe "// minimize the name of the form // # define png_name "bliss.png" // background image # define text "welcome" // text setting # define text_size 50 // set the font size # define width 640 # define heigth 480 char * picture [] = {"Plugin," 2.jpg", "3.jpg"," 4.jpg", "5.jpg"," 6.jpg", "7.jpg"," plugin, "plugin," 10.jpg", "11.jpg"," 12.jpg", "13.png ", "14.jpg"," 15.jpg", "16.jpg"}; static sdl_surface * screen = NULL; int Init () // initialize SDL {If (sdl_init (sdl_init_video) & img_init (img_init_png )) =-1) {fprintf (stderr, "SDL init error: % s", sdl_geterror (); Return-1 ;}return 0 ;} sdl_surface * loadbmp (char * filename) // load BMP image {s Dl_surface * BMP; BMP = sdl_loadbmp (filename); If (BMP = NULL) {fprintf (stderr, "cocould not load % s: % s", filename, sdl_geterror (); exit (1);} return BMP;} void creatscreen (INT width, int height, int bpp, uint32 flags) // create a videomode {screen = sdl_setvideomode (width, height, bpp, flags); If (screen = NULL) {fprintf (stderr, "cocould not creat a screen!: % S ", sdl_geterror (); exit (1) ;}return ;}int destory (sdl_surface * file) // release space {sdl_freesurface (File); Return 0 ;} void show_pic (sdl_surface * BMP, sdl_rect * rect) // display BMP images {sdl_blitsurface (BMP, null, screen, rect); sdl_updaterect (screen, 0, 0, 0, 0); return;} int getrightpic (int I) {I ++; if (I % 15 = 0) {I = 0;} return I ;} int getleftpic (int I) {I --; if (I <0) {I = 15;} return I;} void zoombig (double * ){* A-= 0.25; If (* A <= 1.5) {* A = 1.5 ;}} void zoomsmall (double * A) {* A + = 0.25; if (* A> = 5) {* A = 5 ;}} void draw_button (char * STR, int flag) {If (strcmp (STR, "Left ") = 0) {boxcolor (screen, 160,420,200,460, 0xf3f5ffff); If (flag = 1) {hlinecolor (screen, 160,200,460, 0x000000ff); vlinecolor (screen, 200,420,460, 0x000000ff);} show_pic (screen, & (screen-> clip_rect);} If (strcmp (STR, "right") = 0 ){ Boxcolor (screen, 438,420,478,460, 0xf3f5ffff); If (flag = 1) {hlinecolor (screen, 438,478,460, 0x000000ff); vlinecolor (screen, 478,420,460, 0x000000ff);} show_pic (screen, & (screen-> clip_rect);} return;} void button (char * Str) {sdl_rect rect; int X, Y; If (strcmp (STR, "Left ") = 0) {boxcolor (screen, 160,420,200,460, 0xf3f5ffff); sdl_updaterect (screen, 0, 0, 0, 0);} If (strcmp (STR, "R Ight ") = 0) {boxcolor (screen, 438,420,478,460, 0xf3f5ffff); sdl_updaterect (screen, 0, 0, 0, 0) ;}} int main (INT argc, char ** argv) {const sdl_videoinfo * info = NULL; int flag = 1; int I = 0; int width = width; int heigth = heigth; int bpp = 0; double zoom_x, zoom_y; sdl_surface * backpng = NULL; Init (); creatscreen (width, heigth, bpp, sdl_swsurface); Info = sdl_getvideoinfo (); If (Info = NULL) {fprintf (S Tderr, "video query failed: % s \ n", sdl_geterror (); exit (1) ;}bpp = Info-> vfmt-> bitsperpixel; // obtain the videomode color bppbackpng = img_load (picture [I]); If (! Backpng) {fprintf (stderr, "cocould not load % s: % s \ n", picture [I], sdl_geterror (); exit (1 );} zoom_x = (double) (screen-> W)/(double) (backpng-> W); zoom_y = (double) (screen-> H)/(double) (backpng-> H); backpng = zoomsurface (backpng, zoom_x, zoom_y, 1); show_pic (backpng, & (screen-> clip_rect); sdl_rect fillrect; fillrect. X = 107; fillrect. y = 80; fillrect. W = 426; fillrect. h= 320; sdl_wm_setcaption (title_name, Icon_name); // set the window title sdl_wm_seticon (loadbmp (icon), null); // set the icon static double zoomxy = 1.5; sdl_event event; while (FLAG) {While (sdl_pollevent (& event) // Add the event to the event queue {Switch (event. type) {Case sdl_keydown: printf ("key down ...... \ n "); If (event. key. keysym. sym = sdlk_escape) {flag = 0;} If (event. key. keysym. sym = sdlk_space) {backpng = img_load (picture [0]); If (! Backpng) {fprintf (stderr, "cocould not load % s: % s \ n", picture [I], sdl_geterror (); exit (1 );} zoom_x = (double) (screen-> W)/(double) (backpng-> W); zoom_y = (double) (screen-> H)/(double) (backpng-> H); backpng = zoomsurface (backpng, zoom_x, zoom_y, 1); show_pic (backpng, & (screen-> clip_rect); destory (backpng ); backpng = NULL;} break; Case sdl_keyup:/** keyborad test */printf ("key up ...... \ n "); printf (" key: % d \ N ", event. key. keysym. sym); If (event. key. keysym. sym = 269) {zoomsmall (& zoomxy);} If (event. key. keysym. sym = 270) {zoombig (& zoomxy);} break; Case sdl_mousemotion:/** mouse Motton test */printf ("Mouse Motton: x = % d, y = % d \ n ", event. button. x, event. button. y); break; Case sdl_mousebuttondown: draw_button ("Left", 0); draw_button ("right", 0); If (event. button. x> = 160) & (event. button. x <= 200 ))&&(( Event. button. y> = 420) & (event. button. Y <= 460) {draw_button ("Left", 1); I = getleftpic (I); backpng = img_load (picture [I]); If (! Backpng) {fprintf (stderr, "cocould not load % s: % s \ n", picture [I], sdl_geterror (); exit (1 );} zoom_x = (double) (screen-> W)/(double) (backpng-> W); zoom_y = (double) (screen-> H)/(double) (backpng-> H); backpng = zoomsurface (backpng, (zoom_x/zoomxy), (zoom_y/zoomxy), 1); sdl_rect rect; rect. X = (screen-> W-backpng-> W)/2; rect. y = (screen-> H-backpng-> H)/2; rect. W = backpng-> W; rect. H = backpng-> H; P Rintf ("% d, % d \ n", rect. x, rect. y, rect. w, rect. h); sdl_fillrect (screen, & fillrect, 0 xffffffff); sdl_updaterect (screen, 0, 0, 0); show_pic (backpng, & rect); destory (backpng ); backpng = NULL;} If (event. button. x> = 438 & event. button. x <= 478) & (event. button. y> = 420 & event. button. Y <= 460) {draw_button ("right", 1); I = getrightpic (I); backpng = img_load (picture [I]); If (! Backpng) {fprintf (stderr, "cocould not load % s: % s \ n", picture [I], sdl_geterror (); exit (1 );} zoom_x = (double) (screen-> W)/(double) (backpng-> W); zoom_y = (double) (screen-> H)/(double) (backpng-> H); backpng = zoomsurface (backpng, (zoom_x/zoomxy), (zoom_y/zoomxy), 1); sdl_rect rect; rect. X = (screen-> W-backpng-> W)/2; rect. y = (screen-> H-backpng-> H)/2; rect. W = backpng-> W; rect. H = backpng-> H; printf ("% d, % d \ n", rect. x, rect. y, rect. w, rect. h); sdl_fillrect (screen, & fillrect, 0 xffffffff); sdl_updaterect (screen, 0, 0, 0); show_pic (backpng, & rect); destory (backpng ); backpng = NULL;} break; Case sdl_mousebuttonup: draw_button ("Left", 0); draw_button ("right", 0); break; Case sdl_quit: printf ("Quit \ n"); flag = 0; break ;}}destory (backpng); destory (screen); // ttf_quit (); sdl_quit (); return 0 ;}