4 Sherman's aquarium - Screensaver part
6 Updated and partly rewritten for Sherman's aquarium v3.0.0 on
7 30th and 31st December 2003.
9 Jonas Aaberg <cja@gmx.net>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
30 #define RMASK 0xff000000
31 #define GMASK 0x00ff0000
32 #define BMASK 0x0000ff00
33 #define AMASK 0x000000ff
37 #define RMASK 0x000000ff
38 #define GMASK 0x0000ff00
39 #define BMASK 0x00ff0000
40 #define AMASK 0xff000000
47 unsigned char r,g,b,alpha;
54 #include "background.h"
57 #include "soundeffects.h"
67 #define ARG_SEAFLOOR 1
70 #define ARG_PLANTSCALE 4
71 #define ARG_BOTTOMANIMALS 5
73 #define ARG_BG_SOLID 7
74 #define ARG_BG_SHADED 8
75 #define ARG_BG_WATERALIKE 9
76 #define ARG_BG_IMAGE 10
83 #define ARG_BG_IMAGE_FILE 17
85 #define ARG_SELECTED 19
86 #define ARG_RANDOM_POP 20
93 #define ARG_BLOWFISH 27
94 #define ARG_SWORDFISH 28
95 #define ARG_BDWELLER 29
96 #define ARG_FILLMORE 30
97 #define ARG_SHERMAN 31
102 #define ARG_ERNEST 36
104 #define ARG_HAWTHORNE 38
106 #define ARG_EXPLODE 40
107 #define ARG_REBIRTH 41
108 #define ARG_SCALEDIFF 42
111 #define ARG_HUNTERA 45
112 #define ARG_SWORDA 46
113 #define ARG_SOUNDPRG 47
114 #define ARG_WINDOW_ID 48
115 #define ARG_FULLSCREEN 49
116 #define ARG_BUBBLE 50
117 #define ARG_DESKTOP 51
118 #define ARG_COMICS 52
119 #define ARG_COMICS_DIR 53
120 #define ARG_COMICS_DELAY 54
124 static int screen_width;
125 static int screen_height;
126 static gboolean comics = FALSE;
127 static char *comic_dirs[1024]; /* No more than 1024 comic dirs :-) Ugly, but... */
128 static int num_comic_dirs = 0;
130 static GdkWindow *window;
132 static SDL_Surface *screen=NULL, *screen_image, *background, **thisfish;
133 static SDL_Rect *fish_dest, *fish_src, *clean_dest;
134 static int curr_dest, clean_count, no_sdl_quit = 0, comics_delay = 50;
135 int window_id = -1, fullscreen = 0;
136 static unsigned char *original_bg;
137 static AquariumData ad;
141 void screensaver_draw_image(int x, int y, int idx, int rev, SA_Image *image)
144 fish_dest[curr_dest].x=x;
145 fish_dest[curr_dest].y=y;
146 fish_dest[curr_dest].w=image->width;
147 fish_dest[curr_dest].h=image->height;
149 fish_src[curr_dest].x=0;
150 fish_src[curr_dest].y=(int)((float)idx*(float)image->full_height/(float)image->frames+0.5);
151 fish_src[curr_dest].w=image->width;
152 fish_src[curr_dest].h=image->height;
155 thisfish[curr_dest] = SDL_CreateRGBSurfaceFrom(image->image,
157 (int)((float)image->full_height*(float)(idx+1) /
158 (float)image->frames + 0.5),
160 RMASK, GMASK, BMASK, AMASK);
162 thisfish[curr_dest] = SDL_CreateRGBSurfaceFrom(image->rev,
164 (int)((float)image->full_height*(float)(idx+1) /
165 (float)image->frames + 0.5),
167 RMASK, GMASK, BMASK, AMASK);
175 void screensaver_clean(int x,int y,int w,int h)
178 clean_dest[clean_count].x=x-5;
179 clean_dest[clean_count].y=y-5;
180 clean_dest[clean_count].w=w+10;
181 clean_dest[clean_count].h=h+10;
184 SDL_BlitSurface(background,&clean_dest[clean_count],screen,&clean_dest[clean_count]);
190 void screensaver_quit()
192 /* Resetting the term signal to the orignal so we can quit nicely.*/
193 signal(SIGTERM, SIG_DFL);
196 kill(getpid(),SIGTERM);
200 /* In case something is really weird */
201 kill(getpid(),SIGKILL);
204 void comics_clean(void)
207 for(i=0;i<num_comic_dirs;i++)
208 g_free(comic_dirs[i]);
210 comic_dirs[0] = NULL;
213 void comics_prepare(char *dir)
215 comic_dirs[num_comic_dirs] = g_strdup_printf("%s/*", dir);
218 /* Make sure the final one is always followed by a NULL */
219 comic_dirs[num_comic_dirs] = NULL;
222 char *comics_pick(void)
224 int i, flags = GLOB_NOSORT;
226 char *the_comic = NULL;
229 for(i=0;i<num_comic_dirs;i++){
231 flags |= GLOB_APPEND;
232 glob(comic_dirs[i], flags, NULL, &comic_files);
235 if(comic_files.gl_pathc != 0)
236 the_comic = g_strdup(comic_files.gl_pathv[g_rand_int_range(ad.rnd, 0, comic_files.gl_pathc)]);
238 globfree(&comic_files);
246 void comics_load(void)
248 int i,j, srs, ys, xs, alpha, sy, dy;
250 char *comic_file = NULL;
251 GError *ferror = NULL;
252 GdkPixbuf *comic_pic, *tmp_pic;
254 memcpy(ad.rgb, original_bg, ad.xmax*3*ad.ymax);
255 memcpy(ad.bgr, original_bg, ad.xmax*3*ad.ymax);
257 comic_file = comics_pick();
259 if(comic_file == NULL)
262 comic_pic = gdk_pixbuf_new_from_file(comic_file, &ferror);
266 if(comic_pic == NULL)
269 if(screen_height < gdk_pixbuf_get_height(comic_pic) || screen_width < gdk_pixbuf_get_width(comic_pic)){
271 tmp_pic = gdk_pixbuf_scale_simple(comic_pic,screen_width, screen_height, GDK_INTERP_BILINEAR);
272 g_object_unref(comic_pic);
277 ys = (screen_height - gdk_pixbuf_get_height(comic_pic))/2;
278 xs = (screen_width - gdk_pixbuf_get_width(comic_pic))/2;
281 srs = gdk_pixbuf_get_rowstride(comic_pic);
282 alpha = gdk_pixbuf_get_has_alpha(comic_pic);
283 src = gdk_pixbuf_get_pixels(comic_pic);
285 for(i=0;i<gdk_pixbuf_get_height(comic_pic);i++){
287 dy = (i+ys) * ad.xmax + xs;
289 for(j=0;j<gdk_pixbuf_get_width(comic_pic);j++){
290 ad.bgr[(dy+j)*3+0] = ad.rgb[(dy+j)*3+0] = src[sy + j*(3+alpha)+0];
291 ad.bgr[(dy+j)*3+1] = ad.rgb[(dy+j)*3+1] = src[sy + j*(3+alpha)+1];
292 ad.bgr[(dy+j)*3+2] = ad.rgb[(dy+j)*3+2] = src[sy + j*(3+alpha)+2];
295 g_object_unref(comic_pic);
301 void screensaver_main_sdl(void)
305 #ifdef PERFORMACE_CHECK
308 clock_t cali1, cali2;
318 totaltime1 = clock();
324 if(counter == 0 && comics){
326 SDL_BlitSurface(screen_image, NULL, screen, NULL);
327 SDL_UpdateRect(screen,0,0,0,0);
328 counter = 25*comics_delay;
339 for(i=0;i<curr_dest;i++)
340 SDL_BlitSurface(thisfish[i],&fish_src[i],screen,&fish_dest[i]);
343 /* If we get a SIGTERM from screensaver in this loop, and we later do a SDL_Quit()
344 X will get problems. So we have to avoid calling SDL_Quit if a sigterm is caught
348 Xlib: unexpected async reply (sequence 0xf03)!
349 And sherman's starts eating processor power like mad!
352 for(i=0;i<curr_dest;i++){
353 SDL_UpdateRects(screen,1,&clean_dest[i]);
354 SDL_FreeSurface(thisfish[i]);
367 /* Check if we're going too fast! */
368 if((float)(cali2-cali1) < (float)(0.2*CLOCKS_PER_SEC)){
369 delay=(int)(((float)((0.2*CLOCKS_PER_SEC)-(cali2-cali1))/CLOCKS_PER_SEC)*100000);
373 if(delay<((int)(((float)((0.2*CLOCKS_PER_SEC)-(cali2-cali1))/CLOCKS_PER_SEC)*100000))){
374 delay-=(int)(((float)((0.2*CLOCKS_PER_SEC)-(cali2-cali1))/CLOCKS_PER_SEC)*100000);
386 while(SDL_PollEvent(&event)){
396 case SDL_MOUSEMOTION:
401 case SDL_MOUSEBUTTONDOWN:
410 #ifdef PERFORMACE_CHECK
412 printf("Frames: %d\n",totalframes);
413 printf("Seconds: %f\n",(float)(totaltime2-totaltime1)/CLOCKS_PER_SEC);
414 printf("FPS: %f\n",(float)totalframes/((float)(totaltime2-totaltime1)/CLOCKS_PER_SEC));
419 void screensaver_main_gdk(void)
425 while(gdk_events_pending()){
426 event = gdk_event_get();
428 if(event->type == GDK_DESTROY)
433 if(counter == 0 && comics){
435 counter = 25*comics_delay;
439 memcpy(ad.rgb, ad.bgr, ad.ymax * ad.xmax * 3);
442 gdk_draw_rgb_image(window, gc, ad.xmin, ad.ymin, ad.xmax, ad.ymax,
443 GDK_RGB_DITHER_NONE, ad.rgb, ad.xmax * 3);
451 void init_sdl(int sdl_flags)
454 if (SDL_Init(SDL_INIT_VIDEO) < 0){
455 printf("Can't init SDL: %s\n",SDL_GetError());
460 signal(SIGTERM, screensaver_quit);
464 if(!SDL_VideoModeOK(screen_width,screen_height, DEPTH, sdl_flags)){
465 printf("Sorry, video mode %dx%d in %d bits isn't supported by hardware\n",
466 screen_width,screen_height, DEPTH);
471 screen = SDL_SetVideoMode(screen_width, screen_height, DEPTH, sdl_flags);
474 printf("Unable to set video mode %dx%d in %d bits.\n",
475 screen_width,screen_height,DEPTH);
479 SDL_WM_SetCaption("Sherman's aquarium",NULL);
482 /* Hide the mouse cursor */
485 /* Start with all black */
486 SDL_FillRect(screen,NULL,0x000000);
488 screen_image = SDL_CreateRGBSurfaceFrom(ad.rgb, ad.xmax, ad.ymax, DEPTH, ad.xmax*3,
489 RMASK, GMASK, BMASK, 0);
490 background = SDL_CreateRGBSurfaceFrom(ad.bgr, ad.xmax, ad.ymax, DEPTH, ad.xmax*3,
491 RMASK, GMASK, BMASK, 0);
494 SDL_BlitSurface(screen_image, NULL, screen, NULL);
495 SDL_UpdateRect(screen,0,0,0,0);
500 void screensaver_init()
503 XWindowAttributes win_attr;
505 Fish_settings *fish_settings;
506 Bubble_settings *bubble_settings;
507 int sdl_flags = SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT;
513 display = XOpenDisplay(NULL);
514 XGetWindowAttributes(display, (Window)window_id, &win_attr);
515 screen_height = win_attr.height;
516 screen_width = win_attr.width;
518 ad.proximity = 1; /* No sound effects */
519 window = gdk_window_foreign_new((Window)window_id);
520 gdk_window_show(window);
521 gc = gdk_gc_new(window);
525 screen_width = gdk_screen_width();
526 screen_height = gdk_screen_height();
528 display=XOpenDisplay(NULL);
529 sdl_command = g_strdup_printf("SDL_WINDOWID=%d",
530 (int)RootWindowOfScreen(ScreenOfDisplay(display, DefaultScreen(display))));
533 sdl_flags |= SDL_FULLSCREEN;
536 ad.xmax = screen_width;
537 ad.ymax = screen_height;
539 ad.virtual_aquarium_x = ad.xmax + 2 * VIRTUAL_AQUARIUM_DX;
540 ad.virtual_aquarium_y = ad.ymax + 2 * VIRTUAL_AQUARIUM_DY;
542 ad.ymin = ad.xmin = ad.viewpoint_start_x = ad.viewpoint_start_y = 0;
544 ad.rgb = g_malloc0(ad.xmax*3*ad.ymax);
545 ad.bgr = g_malloc0(ad.xmax*3*ad.ymax);
546 original_bg = g_malloc0(ad.xmax*3*ad.ymax);
554 memcpy(ad.rgb, ad.bgr, ad.xmax*3*ad.ymax);
555 memcpy(original_bg, ad.bgr, ad.xmax*3*ad.ymax);
558 if(fullscreen || window_id == -1)
562 fish_settings = fish_get_settings_ptr();
563 bubble_settings = bubble_get_settings_ptr();
565 fish_dest = g_malloc0(sizeof(SDL_Rect)*(fish_settings->num_fish +
566 bubble_settings->max_bubbles));
567 fish_src = g_malloc0(sizeof(SDL_Rect)*(fish_settings->num_fish +
568 bubble_settings->max_bubbles));
569 clean_dest = g_malloc0(sizeof(SDL_Rect)*(fish_settings->num_fish +
570 bubble_settings->max_bubbles));
572 thisfish = g_malloc0(sizeof(SDL_Surface*)*(fish_settings->num_fish + bubble_settings->max_bubbles));
580 if(str[0]!='0' || str[1]!='x') return -1;
581 for(i=2;i<strlen(str);i++){
583 if(str[i]>='0' && str[i]<='9') d=(int)(str[i]-'0');
584 if(str[i]>='A' && str[i]<='F') d=(int)(str[i]-'A'+10);
585 if(str[i]>='a' && str[i]<='f') d=(int)(str[i]-'a'+10);
594 void screensaver_init_param(int argc, char **argv)
596 int i,c, numfish = 0, comic_start;
599 Sound_settings *s, sound_settings = {0,TYPE_MP3, NULL};
600 Bubble_settings *bub, bubble_settings = {20};
601 Bottom_settings *b, bottom_settings = {0,5,1,75,2};
602 Background_settings *bg, background_settings = {NULL,NULL, 0,1,
603 (GaiColor){0, 100, 150, 0},
604 (GaiColor){10,120, 250, 0},
605 (GaiColor){0,0,0,0}};
606 Fish_settings *f, fish_settings = {0, 0, 75, 0, 100, 0, 15, 0, 75, 75,
607 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
608 struct option cmdline_options[] = {
609 {"sound", no_argument, NULL, ARG_SOUND},
610 {"soundprg", required_argument, NULL, ARG_SOUNDPRG},
611 {"ogg", no_argument, NULL, ARG_OGG},
614 {"seafloor", no_argument, NULL, ARG_SEAFLOOR},
615 {"plants", required_argument, NULL, ARG_PLANTS},
616 {"plantscale", required_argument, NULL, ARG_PLANTSCALE},
617 {"bottomanimals", required_argument, NULL, ARG_BOTTOMANIMALS},
619 {"bg_solid", no_argument, NULL, ARG_BG_SOLID},
620 {"bg_shaded", no_argument, NULL, ARG_BG_SHADED},
621 {"bg_wateralike", no_argument, NULL, ARG_BG_WATERALIKE},
622 {"bg_image", no_argument, NULL, ARG_BG_IMAGE},
625 {"lcr", required_argument, NULL, ARG_LCR},
626 {"lcg", required_argument, NULL, ARG_LCG},
627 {"lcb", required_argument, NULL, ARG_LCB},
629 {"ucr", required_argument, NULL, ARG_UCR},
630 {"ucg", required_argument, NULL, ARG_UCG},
631 {"ucb", required_argument, NULL, ARG_UCB},
633 {"bg_image_file", required_argument, NULL, ARG_BG_IMAGE_FILE},
635 {"comics", no_argument, NULL, ARG_COMICS},
636 {"comics_dir", required_argument, NULL, ARG_COMICS_DIR},
637 {"comics_delay", required_argument, NULL, ARG_COMICS_DELAY},
640 {"random", no_argument, NULL, ARG_RANDOM},
641 {"selected", no_argument, NULL, ARG_SELECTED},
642 {"random_pop", required_argument, NULL, ARG_RANDOM_POP},
644 {"fish1", required_argument, NULL, ARG_FISH1},
645 {"fish2", required_argument, NULL, ARG_FISH2},
646 {"fish3", required_argument, NULL, ARG_FISH3},
647 {"fish4", required_argument, NULL, ARG_FISH4},
648 {"fish5", required_argument, NULL, ARG_FISH5},
649 {"fish6", required_argument, NULL, ARG_FISH6},
650 {"swordfish", required_argument, NULL, ARG_SWORDFISH},
651 {"blowfish", required_argument, NULL, ARG_BLOWFISH},
652 {"bdweller", required_argument, NULL, ARG_BDWELLER},
653 {"fillmore", required_argument, NULL, ARG_FILLMORE},
654 {"sherman", required_argument, NULL, ARG_SHERMAN},
655 {"megan", required_argument, NULL, ARG_MEGAN},
656 {"hunter", required_argument, NULL, ARG_HUNTER},
657 {"prey", required_argument, NULL, ARG_PREY},
658 {"lori", required_argument, NULL, ARG_LORI},
659 {"ernest", required_argument, NULL, ARG_ERNEST},
660 {"squid", required_argument, NULL, ARG_SQUID},
661 {"hawthorne", required_argument, NULL, ARG_HAWTHORNE},
663 {"eat", no_argument, NULL, ARG_EAT},
664 {"explode", no_argument, NULL, ARG_EXPLODE},
665 {"rebirth", no_argument, NULL, ARG_REBIRTH},
666 {"scalediff", no_argument, NULL, ARG_SCALEDIFF},
667 {"speed", required_argument, NULL, ARG_SPEED},
668 {"scale", required_argument, NULL, ARG_SCALE},
669 {"huntera", required_argument, NULL, ARG_HUNTERA},
670 {"sworda", required_argument, NULL, ARG_SWORDA},
671 {"window-id", required_argument, NULL, ARG_WINDOW_ID},
672 {"root", no_argument, NULL, ARG_FULLSCREEN},
673 {"bubble", required_argument, NULL, ARG_BUBBLE},
674 {"desktop", no_argument, NULL, ARG_DESKTOP},
678 b = bottom_get_settings_ptr();
679 bub = bubble_get_settings_ptr();
680 bg = background_get_settings_ptr();
681 f = fish_get_settings_ptr();
682 s = sound_get_settings_ptr();
684 memcpy(f,&fish_settings, sizeof(Fish_settings));
685 memcpy(bg,&background_settings, sizeof(Background_settings));
686 memcpy(b,&bottom_settings, sizeof(Bottom_settings));
687 memcpy(bub, &bubble_settings, sizeof(Bubble_settings));
688 memcpy(s, &sound_settings, sizeof(Sound_settings));
691 getopt_long_only(argc, argv, "", cmdline_options, NULL)) != -1){
696 b->have_sea_floor = 1;
702 b->max_plants = atoi(optarg);
705 b->scale = atoi(optarg);
707 case ARG_BOTTOMANIMALS:
708 b->num_bottom_animals = atoi(optarg);
718 bg->type = BG_SHADED;
721 case ARG_BG_WATERALIKE:
730 bg->solid_c.r = bg->shaded_bot_c.r = atoi(optarg);
733 bg->solid_c.g = bg->shaded_bot_c.g = atoi(optarg);
736 bg->solid_c.b = bg->shaded_bot_c.b = atoi(optarg);
739 bg->shaded_top_c.r = atoi(optarg);
742 bg->shaded_top_c.g = atoi(optarg);
745 bg->shaded_top_c.b = atoi(optarg);
747 case ARG_BG_IMAGE_FILE:
748 bg->imagename = g_strdup_printf(optarg);
751 f->type = RANDOM_FISH;
754 f->type = SELECTION_FISH;
757 f->num_fish = atoi(optarg);
760 f->fish1 = atoi(optarg);
763 f->fish2 = atoi(optarg);
766 f->fish3 = atoi(optarg);
769 f->fish4 = atoi(optarg);
772 f->fish5 = atoi(optarg);
775 f->fish6 = atoi(optarg);
778 f->blowfish = atoi(optarg);
781 f->swordfish = atoi(optarg);
784 f->bdweller = atoi(optarg);
787 f->fillmore = atoi(optarg);
790 f->sherman = atoi(optarg);
793 f->megan = atoi(optarg);
796 f->hunter = atoi(optarg);
799 f->prey = atoi(optarg);
802 f->lori = atoi(optarg);
805 f->ernest = atoi(optarg);
808 f->squid = atoi(optarg);
811 f->hawthorne = atoi(optarg);
826 f->speed = atoi(optarg);
829 f->scale = atoi(optarg);
831 f->hunter_agr = atoi(optarg);
834 f->swordfish_agr = atoi(optarg);
837 s->prg = g_strdup_printf(optarg);
840 window_id = htoi(optarg);
846 bub->max_bubbles = atoi(optarg);
854 comics_prepare(IMAGE_PATH "/strips");
856 case ARG_COMICS_DELAY:
857 comics_delay = atoi(optarg);
862 comic_buff = g_strdup(optarg);
863 for(i=0;i<strlen(optarg);i++){
864 if(comic_buff[i] == ';'){
865 comic_buff[i] = '\0';
866 comics_prepare(comic_buff+comic_start);
870 comics_prepare(comic_buff+comic_start);
885 numfish += f->swordfish;
886 numfish += f->blowfish;
887 numfish += f->fillmore;
888 numfish += f->sherman;
890 numfish += f->hunter;
892 numfish += f->ernest;
895 numfish += f->bdweller;
896 numfish += f->hawthorne;
898 if(f->type == SELECTION_FISH)
899 f->num_fish = numfish;
903 int main(int argc, char **argv)
905 gdk_init(&argc, &argv);
907 ad.rnd = g_rand_new();
909 screensaver_init_param(argc, argv);
913 screensaver_main_sdl();
915 screensaver_main_gdk();
920 AquariumData *aquarium_get_settings_ptr(void)
925 unsigned char *aquarium_install_path(void)
931 void aquarium_draw_image(int x, int y, int idx, int rev, SA_Image *image)
934 screensaver_draw_image(x, y, idx, rev, image);
936 draw_image(x, y, idx, rev, image);
939 void aquarium_draw_pic_alpha(SA_Image *image, int w, int h, int x, int y, int idx, int alpha)
942 screensaver_draw_image(x, y, idx, 0, image);
944 draw_pic_alpha(image->image, w, h, x, y, idx,alpha);
948 void aquarium_clean_image(int x, int y, int w, int h)
951 screensaver_clean(x, y, w, h);
954 GdkPixbuf *gai_load_image(char *fname)
960 full_name = g_strdup_printf("%s/%s",IMAGE_PATH,fname);
961 pix = gdk_pixbuf_new_from_file(full_name, &msg);
963 printf("%s\n",msg->message);
971 void gai_display_error_continue(char *msg)
973 printf(" *** Error: %s\n",msg);