fixed fish returns
[shermanaquarium] / sherman-aquarium / shermans / screensaver.c
1
2 /* 
3
4    Sherman's aquarium - Screensaver part
5
6    Updated and partly rewritten for Sherman's aquarium v3.0.0 on
7    30th and 31st December 2003.
8
9    Jonas Aaberg <cja@gmx.net>
10
11 */
12
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <signal.h>
19 #include <time.h>
20 #include <glob.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <gdk/gdk.h>
23 #include <gdk/gdkx.h>
24 #include <SDL/SDL.h>
25 #include <X11/Xlib.h>
26
27
28
29 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
30 #define RMASK 0xff000000
31 #define GMASK 0x00ff0000
32 #define BMASK 0x0000ff00
33 #define AMASK 0x000000ff
34
35 #else
36
37 #define RMASK 0x000000ff
38 #define GMASK 0x0000ff00
39 #define BMASK 0x00ff0000
40 #define AMASK 0xff000000
41 #endif
42
43
44
45
46 typedef struct {
47     unsigned char r,g,b,alpha;
48 } GaiColor;
49
50
51 #include "aquarium.h"
52 #include "bottom.h"
53 #include "bubble.h"
54 #include "background.h"
55 #include "fish.h"
56 #include "draw.h"
57 #include "soundeffects.h"
58 #include "vroot.h"
59
60 #ifdef DARWIN
61 #include "getopt.h"
62 #else
63 #include <getopt.h>
64 #endif
65
66
67 #define ARG_SEAFLOOR 1
68 #define ARG_SOUND 2
69 #define ARG_PLANTS 3
70 #define ARG_PLANTSCALE 4
71 #define ARG_BOTTOMANIMALS 5
72 #define ARG_OGG 6
73 #define ARG_BG_SOLID 7
74 #define ARG_BG_SHADED 8
75 #define ARG_BG_WATERALIKE 9
76 #define ARG_BG_IMAGE 10
77 #define ARG_LCR 11
78 #define ARG_LCG 12
79 #define ARG_LCB 13
80 #define ARG_UCR 14
81 #define ARG_UCG 15
82 #define ARG_UCB 16
83 #define ARG_BG_IMAGE_FILE 17
84 #define ARG_RANDOM 18
85 #define ARG_SELECTED 19
86 #define ARG_RANDOM_POP 20
87 #define ARG_FISH1 21
88 #define ARG_FISH2 22
89 #define ARG_FISH3 23
90 #define ARG_FISH4 24
91 #define ARG_FISH5 25
92 #define ARG_FISH6 26
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
98 #define ARG_MEGAN 32
99 #define ARG_HUNTER 33
100 #define ARG_PREY 34
101 #define ARG_LORI 35
102 #define ARG_ERNEST 36
103 #define ARG_SQUID 37
104 #define ARG_HAWTHORNE 38
105 #define ARG_EAT 39
106 #define ARG_EXPLODE 40
107 #define ARG_REBIRTH 41
108 #define ARG_SCALEDIFF 42
109 #define ARG_SPEED 43
110 #define ARG_SCALE 44
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
121 #define ARG_VIEW 55
122
123 #define DEPTH 24
124
125 static int screen_width;
126 static int screen_height;
127 static gboolean comics = FALSE;
128 static char *comic_dirs[1024]; /* No more than 1024 comic dirs :-) Ugly, but... */
129 static int num_comic_dirs = 0;
130
131 static GdkWindow *window;
132 static GdkGC *gc;
133 static SDL_Surface *screen=NULL, *screen_image, *background, **thisfish;
134 static SDL_Rect *fish_dest, *fish_src, *clean_dest;
135 static int curr_dest, clean_count,  no_sdl_quit = 0, comics_delay = 50;
136 int window_id = -1, fullscreen = 1;
137 int view = 1;
138 static unsigned char *original_bg;
139 static AquariumData ad;
140
141
142
143 void screensaver_draw_image(int x, int y, int idx, int rev, SA_Image *image)
144 {
145
146     fish_dest[curr_dest].x=x;
147     fish_dest[curr_dest].y=y;
148     fish_dest[curr_dest].w=image->width;
149     fish_dest[curr_dest].h=image->height;
150
151     fish_src[curr_dest].x=0;
152     fish_src[curr_dest].y=(int)((float)idx*(float)image->full_height/(float)image->frames+0.5);
153     fish_src[curr_dest].w=image->width;
154     fish_src[curr_dest].h=image->height;
155
156     if(!rev)
157         thisfish[curr_dest] = SDL_CreateRGBSurfaceFrom(image->image,
158                                                        image->width,
159                                                        (int)((float)image->full_height*(float)(idx+1) / 
160                                                              (float)image->frames + 0.5),
161                                                        32,image->rowstride,
162                                                        RMASK, GMASK, BMASK, AMASK);
163     else
164         thisfish[curr_dest] = SDL_CreateRGBSurfaceFrom(image->rev,
165                                                        image->width,
166                                                        (int)((float)image->full_height*(float)(idx+1) / 
167                                                              (float)image->frames + 0.5),
168                                                        32,image->rowstride,
169                                                        RMASK, GMASK, BMASK, AMASK);
170
171     curr_dest++;
172 }
173
174
175
176
177 void screensaver_clean(int x,int y,int w,int h)
178 {
179
180     clean_dest[clean_count].x=x-5;
181     clean_dest[clean_count].y=y-5;
182     clean_dest[clean_count].w=w+10;
183     clean_dest[clean_count].h=h+10;
184
185
186     SDL_BlitSurface(background,&clean_dest[clean_count],screen,&clean_dest[clean_count]);
187
188     clean_count++;
189 }
190
191
192 void screensaver_quit()
193 {
194     /* Resetting the term signal to the orignal so we can quit nicely.*/
195     signal(SIGTERM, SIG_DFL);
196
197     if(no_sdl_quit)
198         kill(getpid(),SIGTERM);
199     else
200         exit(0);
201
202     /* In case something is really weird */
203     kill(getpid(),SIGKILL);
204 }
205
206 void comics_clean(void)
207 {
208     int i;
209     for(i=0;i<num_comic_dirs;i++)
210         g_free(comic_dirs[i]);
211     num_comic_dirs = 0;
212     comic_dirs[0] = NULL;
213 }
214
215 void comics_prepare(char *dir)
216 {
217     comic_dirs[num_comic_dirs] = g_strdup_printf("%s/*", dir);
218     num_comic_dirs++;
219
220     /* Make sure the final one is always followed by a NULL */
221     comic_dirs[num_comic_dirs] = NULL;
222 }
223
224 char *comics_pick(void)
225 {
226     int i, flags = GLOB_NOSORT;
227     glob_t comic_files;
228     char *the_comic = NULL;
229
230
231     for(i=0;i<num_comic_dirs;i++){
232         if(i)
233             flags |= GLOB_APPEND;
234         glob(comic_dirs[i], flags, NULL, &comic_files);
235     }
236
237     if(comic_files.gl_pathc != 0)
238         the_comic = g_strdup(comic_files.gl_pathv[g_rand_int_range(ad.rnd, 0, comic_files.gl_pathc)]);
239
240     globfree(&comic_files);
241
242
243     return the_comic;
244
245 }
246
247
248 void comics_load(void)
249 {
250     int i,j, srs, ys, xs, alpha, sy, dy;
251     unsigned char *src;
252     char *comic_file = NULL;
253     GError *ferror = NULL;
254     GdkPixbuf *comic_pic, *tmp_pic;
255
256     memcpy(ad.rgb, original_bg, ad.xmax*3*ad.ymax);
257     memcpy(ad.bgr, original_bg, ad.xmax*3*ad.ymax);
258
259     comic_file = comics_pick();
260
261     if(comic_file == NULL)
262         return;
263
264     comic_pic = gdk_pixbuf_new_from_file(comic_file, &ferror);
265
266     g_free(comic_file);
267
268     if(comic_pic == NULL)
269         return;
270
271     if(screen_height < gdk_pixbuf_get_height(comic_pic) || screen_width < gdk_pixbuf_get_width(comic_pic)){
272
273         tmp_pic = gdk_pixbuf_scale_simple(comic_pic,screen_width, screen_height, GDK_INTERP_BILINEAR);
274         g_object_unref(comic_pic);
275         comic_pic = tmp_pic;
276     }
277
278
279     ys = (screen_height - gdk_pixbuf_get_height(comic_pic))/2;
280     xs = (screen_width - gdk_pixbuf_get_width(comic_pic))/2;
281
282
283     srs = gdk_pixbuf_get_rowstride(comic_pic);
284     alpha = gdk_pixbuf_get_has_alpha(comic_pic);
285     src = gdk_pixbuf_get_pixels(comic_pic);
286
287     for(i=0;i<gdk_pixbuf_get_height(comic_pic);i++){
288         sy = i * srs;
289         dy = (i+ys) * ad.xmax + xs;
290         
291         for(j=0;j<gdk_pixbuf_get_width(comic_pic);j++){
292             ad.bgr[(dy+j)*3+0] = ad.rgb[(dy+j)*3+0] = src[sy + j*(3+alpha)+0];
293             ad.bgr[(dy+j)*3+1] = ad.rgb[(dy+j)*3+1] = src[sy + j*(3+alpha)+1];
294             ad.bgr[(dy+j)*3+2] = ad.rgb[(dy+j)*3+2] = src[sy + j*(3+alpha)+2];
295         }
296     }
297     g_object_unref(comic_pic);
298
299 }
300
301
302
303 void screensaver_main_sdl(void)
304 {
305     SDL_Event event;
306     clock_t totaltime1;
307 #ifdef PERFORMACE_CHECK
308     clock_t totaltime2;
309 #endif
310     clock_t cali1, cali2;
311     int totalframes=0;
312     int num_events=0;
313     int main_loop=0;
314     int frames=0;
315     int i;
316     int delay=0;
317     int counter = 0;
318
319
320     totaltime1 = clock();
321     cali1 = clock();
322
323     while(!main_loop){
324
325
326         if(counter == 0 && comics){
327             comics_load();
328             SDL_BlitSurface(screen_image, NULL, screen, NULL);
329             SDL_UpdateRect(screen,0,0,0,0);
330             counter = 25*comics_delay;
331         }
332         counter --;
333
334
335         curr_dest=0;
336         clean_count=0;
337
338         fish_update();
339         bubble_update();
340
341         for(i=0;i<curr_dest;i++)
342             SDL_BlitSurface(thisfish[i],&fish_src[i],screen,&fish_dest[i]);
343         
344
345         /* If we get a SIGTERM from screensaver in this loop, and we later do a SDL_Quit()
346            X will get problems. So we have to avoid calling SDL_Quit if a sigterm is caught
347            in this loop.
348
349            Otherwise we get:
350            Xlib: unexpected async reply (sequence 0xf03)!
351            And sherman's starts eating processor power like mad!
352         */
353         no_sdl_quit = 1;
354         for(i=0;i<curr_dest;i++){
355             SDL_UpdateRects(screen,1,&clean_dest[i]);
356             SDL_FreeSurface(thisfish[i]);
357         }
358         no_sdl_quit = 0;
359
360         if(window_id!=-1)
361             SDL_Flip(screen);
362
363         totalframes++;
364         frames++;
365
366         if(frames==10){
367             cali2=clock();
368
369             /* Check if we're going too fast! */
370             if((float)(cali2-cali1) < (float)(0.2*CLOCKS_PER_SEC)){
371                 delay=(int)(((float)((0.2*CLOCKS_PER_SEC)-(cali2-cali1))/CLOCKS_PER_SEC)*100000);
372             }
373             else{
374                 if(delay!=0){
375                     if(delay<((int)(((float)((0.2*CLOCKS_PER_SEC)-(cali2-cali1))/CLOCKS_PER_SEC)*100000))){
376                         delay-=(int)(((float)((0.2*CLOCKS_PER_SEC)-(cali2-cali1))/CLOCKS_PER_SEC)*100000);
377                     }
378                     else
379                         delay=0;
380                 }
381             }
382             cali1=clock();
383             frames=0;
384         }
385         usleep(delay);
386         
387
388         while(SDL_PollEvent(&event)){
389             switch(event.type){
390             case SDL_QUIT:
391                 main_loop=1;
392                 break;
393             case SDL_KEYDOWN:
394                 num_events++;
395                 if(num_events==2)
396                     main_loop=1;
397                 break;
398             case SDL_MOUSEMOTION:
399                 num_events++;
400                 if(num_events==2)
401                     main_loop=1;
402                 break;
403             case  SDL_MOUSEBUTTONDOWN:
404                 num_events++;
405                 if(num_events==2)
406                     main_loop=1;
407                 break;
408             }
409         }    
410     }
411
412 #ifdef PERFORMACE_CHECK
413     totaltime2=clock();
414     printf("Frames: %d\n",totalframes);
415     printf("Seconds: %f\n",(float)(totaltime2-totaltime1)/CLOCKS_PER_SEC);
416     printf("FPS: %f\n",(float)totalframes/((float)(totaltime2-totaltime1)/CLOCKS_PER_SEC));
417 #endif
418
419 }
420
421 void screensaver_main_gdk(void)
422 {
423     GdkEvent *event;
424     int counter = 0;
425
426     while(1){
427         while(gdk_events_pending()){
428             event = gdk_event_get();
429             if(event){
430                 if(event->type == GDK_DESTROY)
431                     exit(0);
432             }
433         }
434
435         if(counter == 0 && comics){
436             comics_load();
437             counter = 25*comics_delay;
438         }
439         counter --;
440
441         memcpy(ad.rgb, ad.bgr, ad.ymax * ad.xmax * 3);
442         fish_update();
443         bubble_update();
444         gdk_draw_rgb_image(window, gc, ad.xmin, ad.ymin, ad.xmax, ad.ymax,
445                            GDK_RGB_DITHER_NONE, ad.rgb, ad.xmax * 3);
446         gdk_flush();
447         /* 25 fps */
448         usleep(1000000/25);
449     }
450
451 }
452
453 void init_sdl(int sdl_flags)
454 {
455
456     if (SDL_Init(SDL_INIT_VIDEO) < 0){
457         printf("Can't init SDL: %s\n",SDL_GetError());
458         exit(1);
459     }
460
461
462     signal(SIGTERM, screensaver_quit);
463     atexit(SDL_Quit);
464     
465
466     if(!SDL_VideoModeOK(screen_width,screen_height, DEPTH, sdl_flags)){
467         printf("Sorry, video mode %dx%d in %d bits isn't supported by hardware\n",
468                screen_width,screen_height, DEPTH);
469         exit(2);
470     }
471
472
473     screen = SDL_SetVideoMode(screen_width, screen_height, DEPTH, sdl_flags);
474
475     if(screen == NULL){
476         printf("Unable to set video mode %dx%d in %d bits.\n",
477                screen_width,screen_height,DEPTH);
478         exit(3);
479     }
480
481     SDL_WM_SetCaption("Sherman's aquarium",NULL);
482
483
484     /* Hide the mouse cursor */
485     SDL_ShowCursor(0);
486
487     /* Start with all black */
488     SDL_FillRect(screen,NULL,0x000000);
489
490     screen_image = SDL_CreateRGBSurfaceFrom(ad.rgb, ad.xmax, ad.ymax, DEPTH, ad.xmax*3, 
491                                             RMASK, GMASK, BMASK, 0);
492     background = SDL_CreateRGBSurfaceFrom(ad.bgr, ad.xmax, ad.ymax, DEPTH, ad.xmax*3, 
493                                           RMASK, GMASK, BMASK, 0);
494
495
496     SDL_BlitSurface(screen_image, NULL, screen, NULL);
497     SDL_UpdateRect(screen,0,0,0,0);
498
499 }
500
501
502 void screensaver_init()
503 {
504     char *sdl_command;
505     XWindowAttributes win_attr;
506     Display *display;
507     Fish_settings *fish_settings;
508     Bubble_settings *bubble_settings;
509     int sdl_flags = SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT;
510
511     screen_height = 480;
512     screen_width = 800;
513     
514     if(window_id != -1){
515         display = XOpenDisplay(NULL);
516         XGetWindowAttributes(display, (Window)window_id, &win_attr);
517         screen_height = win_attr.height;
518         screen_width = win_attr.width;
519         fullscreen = 0;
520         //ad.proximity = 1;                     /* No sound effects */
521     ad.proximity = 0;
522         window = gdk_window_foreign_new((Window)window_id);
523         gdk_window_show(window);
524         gc = gdk_gc_new(window);
525     }
526
527     if(fullscreen){
528         screen_width = gdk_screen_width();
529         screen_height = gdk_screen_height();
530
531         display=XOpenDisplay(NULL);
532         sdl_command = g_strdup_printf("SDL_WINDOWID=%d",
533                                       (int)RootWindowOfScreen(ScreenOfDisplay(display, DefaultScreen(display))));
534         putenv(sdl_command);
535         ad.proximity = 0;
536         sdl_flags |= SDL_FULLSCREEN;
537     }
538
539     ad.xmax = screen_width;
540     ad.ymax = screen_height;
541   
542     ad.virtual_aquarium_x = ad.xmax + 2 * VIRTUAL_AQUARIUM_DX;
543     ad.virtual_aquarium_y = ad.ymax + 2 * VIRTUAL_AQUARIUM_DY;
544
545     ad.ymin = ad.xmin = ad.viewpoint_start_x = ad.viewpoint_start_y = 0;
546
547     ad.rgb = g_malloc0(ad.xmax*3*ad.ymax);
548     ad.bgr = g_malloc0(ad.xmax*3*ad.ymax);
549     original_bg = g_malloc0(ad.xmax*3*ad.ymax);
550
551     background_init();
552     fish_init();
553     fish_turn();
554     bottom_init();
555     bubble_init();
556
557     memcpy(ad.rgb, ad.bgr, ad.xmax*3*ad.ymax);
558     memcpy(original_bg, ad.bgr, ad.xmax*3*ad.ymax);
559
560
561     if(fullscreen || window_id == -1)
562         init_sdl(sdl_flags);
563
564
565     fish_settings = fish_get_settings_ptr();
566     bubble_settings = bubble_get_settings_ptr();
567
568     fish_dest = g_malloc0(sizeof(SDL_Rect)*(fish_settings->num_fish + 
569                                             bubble_settings->max_bubbles));
570     fish_src = g_malloc0(sizeof(SDL_Rect)*(fish_settings->num_fish +
571                                            bubble_settings->max_bubbles));
572     clean_dest = g_malloc0(sizeof(SDL_Rect)*(fish_settings->num_fish  + 
573                                              bubble_settings->max_bubbles));
574
575     thisfish = g_malloc0(sizeof(SDL_Surface*)*(fish_settings->num_fish + bubble_settings->max_bubbles));
576     
577 }
578
579 int htoi(char *str)
580 {
581   int i,sum=0,d;
582
583   if(str[0]!='0' || str[1]!='x') return -1;
584   for(i=2;i<strlen(str);i++){
585     d=0;
586     if(str[i]>='0' && str[i]<='9') d=(int)(str[i]-'0');
587     if(str[i]>='A' && str[i]<='F') d=(int)(str[i]-'A'+10);
588     if(str[i]>='a' && str[i]<='f') d=(int)(str[i]-'a'+10);
589
590     sum+=d;
591     sum=sum<<4;
592   }
593
594   return(sum>>4);
595 }
596
597 void screensaver_init_param(int argc, char **argv)
598 {
599     int i,c, numfish = 0, comic_start;
600     char *comic_buff;
601
602     Sound_settings *s, sound_settings = {0,TYPE_MP3, NULL};
603     Bubble_settings *bub, bubble_settings = {20};
604     Bottom_settings *b, bottom_settings = {1,5,1,75,3};
605     Background_settings *bg, background_settings = {NULL,NULL, 2,0, 
606                                                     (GaiColor){0, 100, 150, 0},
607                                                     (GaiColor){10,120, 250, 0},
608                                                     (GaiColor){0,0,0,0}};
609     Fish_settings *f, fish_settings = {1, 1, 75, 0, 100, 1, 15, 0, 75, 75,
610                                        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
611     struct option cmdline_options[] = {
612         {"sound", no_argument, NULL, ARG_SOUND},
613         {"soundprg", required_argument, NULL, ARG_SOUNDPRG},
614         {"ogg", no_argument, NULL, ARG_OGG},
615
616
617         {"seafloor", no_argument, NULL, ARG_SEAFLOOR},
618         {"plants", required_argument, NULL, ARG_PLANTS},
619         {"plantscale", required_argument, NULL, ARG_PLANTSCALE},
620         {"bottomanimals", required_argument, NULL, ARG_BOTTOMANIMALS},
621
622         {"bg_solid", no_argument, NULL, ARG_BG_SOLID},
623         {"bg_shaded", no_argument, NULL, ARG_BG_SHADED},
624         {"bg_wateralike", no_argument, NULL, ARG_BG_WATERALIKE},
625         {"bg_image", no_argument, NULL, ARG_BG_IMAGE},
626
627
628         {"lcr", required_argument, NULL, ARG_LCR},
629         {"lcg", required_argument, NULL, ARG_LCG},
630         {"lcb", required_argument, NULL, ARG_LCB},
631
632         {"ucr", required_argument, NULL, ARG_UCR},
633         {"ucg", required_argument, NULL, ARG_UCG},
634         {"ucb", required_argument, NULL, ARG_UCB},
635         
636         {"bg_image_file", required_argument, NULL, ARG_BG_IMAGE_FILE},
637
638         {"comics", no_argument, NULL, ARG_COMICS},
639         {"comics_dir", required_argument, NULL, ARG_COMICS_DIR},
640         {"comics_delay", required_argument, NULL, ARG_COMICS_DELAY},
641
642
643         {"random", no_argument, NULL, ARG_RANDOM},
644         {"selected", no_argument, NULL, ARG_SELECTED},
645         {"random_pop", required_argument, NULL, ARG_RANDOM_POP},
646
647         {"fish1", required_argument, NULL, ARG_FISH1},
648         {"fish2", required_argument, NULL, ARG_FISH2},
649         {"fish3", required_argument, NULL, ARG_FISH3},
650         {"fish4", required_argument, NULL, ARG_FISH4},
651         {"fish5", required_argument, NULL, ARG_FISH5},
652         {"fish6", required_argument, NULL, ARG_FISH6},
653         {"swordfish", required_argument, NULL, ARG_SWORDFISH},
654         {"blowfish", required_argument, NULL, ARG_BLOWFISH},
655         {"bdweller", required_argument, NULL, ARG_BDWELLER},
656         {"fillmore", required_argument, NULL, ARG_FILLMORE},
657         {"sherman", required_argument, NULL, ARG_SHERMAN},
658         {"megan", required_argument, NULL, ARG_MEGAN},
659         {"hunter", required_argument, NULL, ARG_HUNTER},
660         {"prey", required_argument, NULL, ARG_PREY},
661         {"lori", required_argument, NULL, ARG_LORI},
662         {"ernest", required_argument, NULL, ARG_ERNEST},
663         {"squid", required_argument, NULL, ARG_SQUID},
664         {"hawthorne", required_argument, NULL, ARG_HAWTHORNE},
665
666         {"eat", no_argument, NULL, ARG_EAT},
667         {"explode", no_argument, NULL, ARG_EXPLODE},
668         {"rebirth", no_argument, NULL, ARG_REBIRTH},
669         {"scalediff", no_argument, NULL, ARG_SCALEDIFF},
670         {"speed", required_argument, NULL, ARG_SPEED},
671         {"scale", required_argument, NULL, ARG_SCALE},
672         {"huntera", required_argument, NULL, ARG_HUNTERA},
673         {"sworda", required_argument, NULL, ARG_SWORDA},
674         {"window-id", required_argument, NULL, ARG_WINDOW_ID},
675         {"root", no_argument, NULL, ARG_FULLSCREEN},
676         {"bubble", required_argument, NULL, ARG_BUBBLE},
677         {"desktop", no_argument, NULL, ARG_DESKTOP},
678         {"view", required_argument, NULL, ARG_VIEW},
679         {0,0,0,0}};
680
681
682     b = bottom_get_settings_ptr();
683     bub = bubble_get_settings_ptr();
684     bg = background_get_settings_ptr();
685     f = fish_get_settings_ptr();
686     s = sound_get_settings_ptr();
687
688     memcpy(f,&fish_settings, sizeof(Fish_settings));
689     memcpy(bg,&background_settings, sizeof(Background_settings));
690     memcpy(b,&bottom_settings, sizeof(Bottom_settings));
691     memcpy(bub, &bubble_settings, sizeof(Bubble_settings));
692     memcpy(s, &sound_settings, sizeof(Sound_settings));
693 fprintf(stderr, "1BG %i\n",bg->desktop);
694
695     while ((c =
696             getopt_long_only(argc, argv, "", cmdline_options, NULL)) != -1){
697
698         switch(c){
699
700         case ARG_SEAFLOOR:
701             b->have_sea_floor = 1;
702             break;
703         case ARG_SOUND:
704             s->on = 1;
705             break;
706         case ARG_PLANTS:
707             b->max_plants = atoi(optarg);
708             break;
709         case ARG_PLANTSCALE:
710             b->scale = atoi(optarg);
711             break;
712         case ARG_BOTTOMANIMALS:
713             b->num_bottom_animals = atoi(optarg);
714             break;
715         case ARG_OGG:
716             s->type = TYPE_OGG;
717             break;
718         case ARG_BG_SOLID:
719             bg->type = BG_SOLID;
720             bg->desktop = 0;
721             break;
722         case ARG_BG_SHADED:
723             bg->type = BG_SHADED;
724             bg->desktop = 0;
725             break;
726         case ARG_BG_WATERALIKE:
727             bg->type = BG_WATER;
728             bg->desktop = 0;
729             break;
730         case ARG_BG_IMAGE:
731             bg->type = BG_IMAGE;
732             bg->desktop = 0;
733             break;
734         case ARG_LCR:
735             bg->solid_c.r = bg->shaded_bot_c.r = atoi(optarg);
736             break;
737         case ARG_LCG:
738             bg->solid_c.g = bg->shaded_bot_c.g = atoi(optarg);
739             break;
740         case ARG_LCB:
741             bg->solid_c.b = bg->shaded_bot_c.b = atoi(optarg);
742             break;
743         case ARG_UCR:
744             bg->shaded_top_c.r = atoi(optarg);
745             break;
746         case ARG_UCG:
747             bg->shaded_top_c.g = atoi(optarg);
748             break;
749         case ARG_UCB:
750             bg->shaded_top_c.b = atoi(optarg);
751             break;
752         case ARG_BG_IMAGE_FILE:
753             bg->imagename = g_strdup_printf(optarg);
754             break;
755         case ARG_RANDOM:
756             f->type = RANDOM_FISH;
757             break;
758         case ARG_SELECTED:
759             f->type = SELECTION_FISH;
760             break;
761         case ARG_RANDOM_POP:
762             f->num_fish = atoi(optarg);
763             break;
764         case ARG_FISH1:
765             f->fish1 = atoi(optarg);
766             break;
767         case ARG_FISH2:
768             f->fish2 = atoi(optarg);
769             break;
770         case ARG_FISH3:
771             f->fish3 = atoi(optarg);
772             break;
773         case ARG_FISH4:
774             f->fish4 = atoi(optarg);
775             break;
776         case ARG_FISH5:
777             f->fish5 = atoi(optarg);
778             break;
779         case ARG_FISH6:
780             f->fish6 = atoi(optarg);
781             break;
782         case ARG_BLOWFISH:
783             f->blowfish = atoi(optarg);
784             break;
785         case ARG_SWORDFISH:
786             f->swordfish = atoi(optarg);
787             break;
788         case ARG_BDWELLER:
789             f->bdweller = atoi(optarg);
790             break;
791         case ARG_FILLMORE:
792             f->fillmore = atoi(optarg);
793             break;
794         case ARG_SHERMAN:
795             f->sherman = atoi(optarg);
796             break;
797         case ARG_MEGAN:
798             f->megan = atoi(optarg);
799             break;
800         case ARG_HUNTER:
801             f->hunter = atoi(optarg);
802             break;
803         case ARG_PREY:
804             f->prey = atoi(optarg);
805             break;
806         case ARG_LORI:
807             f->lori = atoi(optarg);
808             break;
809         case ARG_ERNEST:
810             f->ernest = atoi(optarg);
811             break;
812         case ARG_SQUID:
813             f->squid = atoi(optarg);
814             break;
815         case ARG_HAWTHORNE:
816             f->hawthorne = atoi(optarg);
817             break;
818         case ARG_EAT:
819             f->eat = 1;
820             break;
821         case ARG_EXPLODE:
822             f->explode = 1;
823             break;
824         case ARG_REBIRTH:
825             f->rebirth = 1;
826             break;
827         case ARG_SCALEDIFF:
828             f->scale_diff = 1;
829             break;
830         case ARG_SPEED:
831             f->speed = atoi(optarg);
832             break;
833         case ARG_SCALE:
834             f->scale = atoi(optarg);
835         case ARG_HUNTERA:
836             f->hunter_agr = atoi(optarg);
837             break;
838         case ARG_SWORDA:
839             f->swordfish_agr = atoi(optarg);
840             break;
841         case ARG_SOUNDPRG:
842             s->prg = g_strdup_printf(optarg);
843             break;
844         case ARG_WINDOW_ID:
845             window_id = atoi(optarg);
846             break;
847         case ARG_FULLSCREEN:
848             fullscreen = 1;
849             break;
850         case ARG_BUBBLE:
851             bub->max_bubbles = atoi(optarg);
852             break;
853         case ARG_VIEW:
854             view = atoi(optarg);
855             break;
856         case ARG_DESKTOP:
857             bg->type = 0;
858             bg->desktop = 1;
859             break;
860         case ARG_COMICS:
861             comics = TRUE;
862             comics_prepare(IMAGE_PATH "/strips");
863             break;
864         case ARG_COMICS_DELAY:
865             comics_delay = atoi(optarg);
866             break;
867         case ARG_COMICS_DIR:
868             comics_clean();
869             comic_start = 0;
870             comic_buff = g_strdup(optarg);
871             for(i=0;i<strlen(optarg);i++){
872                 if(comic_buff[i] == ';'){
873                     comic_buff[i] = '\0';
874                     comics_prepare(comic_buff+comic_start);
875                     comic_start = i+1;
876                 }
877             }
878             comics_prepare(comic_buff+comic_start);
879             g_free(comic_buff);
880
881             break;
882         default:
883             break;
884         }
885     }
886
887     numfish += f->fish1;
888     numfish += f->fish2;
889     numfish += f->fish3;
890     numfish += f->fish4;
891     numfish += f->fish5;
892     numfish += f->fish6;
893     numfish += f->swordfish;
894     numfish += f->blowfish;
895     numfish += f->fillmore;
896     numfish += f->sherman;
897     numfish += f->prey;
898     numfish += f->hunter;
899     numfish += f->lori;
900     numfish += f->ernest;
901     numfish += f->squid;
902     numfish += f->megan;
903     numfish += f->bdweller;
904     numfish += f->hawthorne;
905
906     if(f->type == SELECTION_FISH)
907         f->num_fish = numfish;
908
909 }
910
911 int main(int argc, char **argv)
912 {
913     gdk_init(&argc, &argv);
914
915     ad.rnd = g_rand_new();
916
917     screensaver_init_param(argc, argv);
918     screensaver_init(0);
919
920     if(window_id==-1){
921             screensaver_main_sdl();
922     }
923     else{
924             screensaver_main_gdk();
925     }
926     return 0;
927 }
928
929
930 AquariumData *aquarium_get_settings_ptr(void)
931 {
932     return &ad;
933 }
934
935 unsigned char *aquarium_install_path(void)
936 {
937     return IMAGE_PATH;
938 }
939
940
941 void aquarium_draw_image(int x, int y, int idx, int rev, SA_Image *image)
942 {
943     if(window_id==-1)
944         screensaver_draw_image(x, y, idx, rev, image);
945     else
946         draw_image(x, y, idx, rev, image);
947 }
948
949 void aquarium_draw_pic_alpha(SA_Image *image, int w, int h, int x, int y, int idx, int alpha)
950 {
951     if(window_id==-1)
952         screensaver_draw_image(x, y, idx, 0, image);
953     else
954         draw_pic_alpha(image->image, w, h, x, y, idx,alpha);
955
956
957
958 void aquarium_clean_image(int x, int y, int w, int h)
959 {
960     if(window_id==-1)
961         screensaver_clean(x, y, w, h);
962 }
963
964 GdkPixbuf *gai_load_image(char *fname)
965 {
966     GError *msg = NULL;
967     GdkPixbuf *pix;
968     char *full_name;
969
970     full_name = g_strdup_printf("%s/%s",IMAGE_PATH,fname);
971     pix = gdk_pixbuf_new_from_file(full_name, &msg);
972     if(!pix){
973         printf("%s\n",msg->message);
974         exit(1);
975     }
976     g_free(full_name);
977
978     return pix;
979     
980 }
981 void gai_display_error_continue(char *msg)
982 {
983     printf(" *** Error: %s\n",msg);
984 }