Bunch of gcc 4.0 warning fixes.
[blender.git] / source / creator / creator.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32 #include <stdlib.h>
33 #include <string.h>
34
35 /* This little block needed for linking to Blender... */
36
37 #include "MEM_guardedalloc.h"
38
39 #ifdef WIN32
40 #include "BLI_winstuff.h"
41 #endif
42
43 #include "GEN_messaging.h"
44
45 #include "DNA_ID.h"
46
47 #include "BLI_blenlib.h"
48
49 #include "BKE_utildefines.h"
50 #include "BKE_blender.h"
51 #include "BKE_font.h"
52 #include "BKE_global.h"
53 #include "BKE_main.h"
54 #include "BKE_packedFile.h"
55 #include "BKE_scene.h"
56
57 #include "BIF_gl.h"
58 #include "BIF_graphics.h"
59 #include "BIF_mainqueue.h"
60 #include "BIF_graphics.h"
61 #include "BIF_editsound.h"
62 #include "BIF_usiblender.h"
63 #include "BIF_drawscene.h"      /* set_scene() */
64 #include "BIF_screen.h"         /* waitcursor and more */
65 #include "BIF_usiblender.h"
66 #include "BIF_toolbox.h"
67
68 #include "BLO_writefile.h"
69 #include "BLO_readfile.h"
70
71 #include "BDR_drawmesh.h"
72
73 #include "IMB_imbuf.h"  // for quicktime_init
74
75 #include "RE_renderconverter.h"
76
77 #include "BPY_extern.h"
78
79 #include "playanim_ext.h"
80 #include "mydevice.h"
81 #include "render.h"
82 #include "nla.h"
83 #include "datatoc.h"
84
85 /* for passing information between creator and gameengine */
86 #include "SYS_System.h"
87
88 #include <signal.h>
89
90 #ifdef __FreeBSD__
91 # include <sys/types.h>
92 # include <floatingpoint.h>
93 # include <sys/rtprio.h>
94 #endif
95
96 // from buildinfo.c
97 extern char * build_date;
98 extern char * build_time;
99 extern char * build_platform;
100 extern char * build_type;
101
102 /*      Local Function prototypes */
103 static void print_help();
104 static void print_version();
105
106
107 /* defined in ghostwinlay and winlay, we can't include carbon here, conflict with DNA */
108 #ifdef __APPLE__
109 extern int checkAppleVideoCard(void);
110 extern void getMacAvailableBounds(short *top, short *left, short *bottom, short *right);
111 extern void     winlay_get_screensize(int *width_r, int *height_r);
112 extern void     winlay_process_events(int wait_for_event);
113 #endif
114
115
116 /* for the callbacks: */
117
118 extern int pluginapi_force_ref(void);  /* from blenpluginapi:pluginapi.c */
119
120 char bprogname[FILE_MAXDIR+FILE_MAXFILE]; /* from blenpluginapi:pluginapi.c */
121
122
123 /* Initialise callbacks for the modules that need them */
124 void setCallbacks(void); 
125
126 #if defined(__sgi) || defined(__alpha__)
127 static void fpe_handler(int sig)
128 {
129         // printf("SIGFPE trapped\n");
130 }
131 #endif
132
133 /* handling ctrl-c event in console */
134 static void blender_esc(int sig)
135 {
136         static int count = 0;
137         
138         G.afbreek = 1;  /* forces render loop to read queue, not sure if its needed */
139         
140         if (sig == 2) {
141                 if (count) {
142                         printf("\nBlender killed\n");
143                         exit(2);
144                 }
145                 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
146                 count++;
147         }
148 }
149
150 static void print_version(void)
151 {
152         printf ("Blender %d.%02d\n", G.version/100, G.version%100);
153 }
154
155 static void print_help(void)
156 {
157         printf ("Blender V %d.%02d\n", G.version/100, G.version%100);
158         printf ("Usage: blender [options ...] [file]\n");
159                                 
160         printf ("\nRender options:\n");
161         printf ("  -b <file>\tRender <file> in background\n");
162         printf ("    -S <name>\tSet scene <name>\n");
163         printf ("    -f <frame>\tRender frame <frame> and save it\n");                          
164         printf ("    -s <frame>\tSet start to frame <frame> (use with -a)\n");
165         printf ("    -e <frame>\tSet end to frame (use with -a)<frame>\n");
166         printf ("    -a\t\tRender animation\n");
167                                 
168         printf ("\nAnimation options:\n");
169         printf ("  -a <file(s)>\tPlayback <file(s)>\n");
170         printf ("    -p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>\n");
171         printf ("    -m\t\tRead from disk (Don't buffer)\n");
172                                 
173         printf ("\nWindow options:\n");
174         printf ("  -w\t\tForce opening with borders\n");
175 #ifdef WIN32
176         printf ("  -W\t\tForce opening without borders\n");
177 #endif
178         printf ("  -p <sx> <sy> <w> <h>\tOpen with lower left corner at <sx>, <sy>\n");
179         printf ("                      \tand width and height <w>, <h>\n");
180         printf ("\nGame Engine specific options:\n");
181         printf ("  -g fixedtime\t\tRun on 50 hertz without dropping frames\n");
182         printf ("  -g vertexarrays\tUse Vertex Arrays for rendering (usually faster)\n");
183         printf ("  -g noaudio\t\tNo audio in Game Engine\n");
184         printf ("  -g nomipmap\t\tNo Texture Mipmapping\n");
185         printf ("  -g linearmipmap\tLinear Texture Mipmapping instead of Nearest (default)\n");
186
187         printf ("\nMisc options:\n");
188         printf ("  -d\t\tTurn debugging on\n");
189         printf ("  -noaudio\tDisable audio on systems that support audio\n");
190         printf ("  -h\t\tPrint this help text\n");
191         printf ("  -y\t\tDisable OnLoad scene scripts, use -Y to find out why its -y\n");
192         printf ("  -P <filename>\tRun the given Python script (filename or Blender Text)\n");
193 #ifdef WIN32
194         printf ("  -R\t\tRegister .blend extension\n");
195 #endif
196         printf ("  -v\t\tPrint Blender version and exit\n");
197 }
198
199
200 double PIL_check_seconds_timer(void);
201 extern void winlay_get_screensize(int *width_r, int *height_r);
202
203 int main(int argc, char **argv)
204 {
205         int a, i, stax, stay, sizx, sizy;
206         SYS_SystemHandle syshandle;
207         Scene *sce;
208
209 #if defined(WIN32) || defined (__linux__)
210         int audio = 1;
211 #else
212         int audio = 0;
213 #endif
214         setCallbacks();
215
216 #ifdef __APPLE__
217                 /* patch to ignore argument finder gives us (pid?) */
218         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
219                 extern int GHOST_HACK_getFirstFile(char buf[]);
220                 static char firstfilebuf[512];
221                 int scr_x,scr_y;
222
223                 argc= 1;
224
225         /* first let us check if we are hardware accelerated and with VRAM > 16 Mo */
226         
227         if (checkAppleVideoCard()) {
228                         short top, left, bottom, right;
229                         
230                         winlay_get_screensize(&scr_x, &scr_y); 
231                         getMacAvailableBounds(&top, &left, &bottom, &right);
232                         setprefsize(left +10,scr_y - bottom +10,right-left -20,bottom - 64);
233
234         } else {
235                                 winlay_get_screensize(&scr_x, &scr_y);
236
237                 /* 40 + 684 + (headers) 22 + 22 = 768, the powerbook screen height */
238                 setprefsize(120, 40, 850, 684);
239         }
240     
241                 winlay_process_events(0);
242                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
243                         argc= 2;
244                         argv[1]= firstfilebuf;
245                 }
246         }
247
248 #endif
249
250 #ifdef __FreeBSD__
251         fpsetmask(0);
252 #endif
253 #ifdef __linux__
254     #ifdef __alpha__
255         signal (SIGFPE, fpe_handler);
256     #endif
257 #endif
258 #if defined(__sgi)
259         signal (SIGFPE, fpe_handler);
260 #endif
261
262         // copy path to executable in bprogname. playanim and creting runtimes
263         // need this.
264
265         BLI_where_am_i(bprogname, argv[0]);
266
267                 /* Hack - force inclusion of the plugin api functions,
268                  * see blenpluginapi:pluginapi.c
269                  */
270         pluginapi_force_ref();
271         
272         initglobals();  /* blender.c */
273
274         syshandle = SYS_GetSystem();
275         GEN_init_messaging_system();
276
277         /* first test for background */
278
279         G.f |= G_SCENESCRIPT; /* scenescript always set! */
280
281         for(a=1; a<argc; a++) {
282
283                 /* Handle unix and windows style help requests */
284                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
285                         print_help();
286                         exit(0);
287                 }
288
289                 /* Handle long version request */
290                 if (!strcmp(argv[a], "--version")){
291                         print_version();
292                         exit(0);
293                 }
294
295                 /* Handle -* switches */
296                 else if(argv[a][0] == '-') {
297                         switch(argv[a][1]) {
298                         case 'a':
299                                 playanim(argc-1, argv+1);
300                                 exit(0);
301                                 break;
302                         case 'b':
303                         case 'B':
304                                 G.background = 1;
305                                 a= argc;
306                                 break;
307
308          case 'm':
309              /* unified render pipeline */
310 /*               G.magic = 1; has become obsolete */
311                          printf("-m: enable unified renderer has become obsolete. Set \n");
312                          printf("\tthis option per individual file now.\n");
313              break;
314
315                         case 'y':
316                                 G.f &= ~G_SCENESCRIPT;
317                                 break;
318
319                         case 'Y':
320                                 printf ("-y was used to disable scene scripts because,\n");
321                                 printf ("\t-p being taken, Ton was of the opinion that Y\n");
322                                 printf ("\tlooked like a split (disabled) snake, and also\n");
323                                 printf ("\twas similar to a python's tongue (unproven).\n\n");
324
325                                 printf ("\tZr agreed because it gave him a reason to add a\n");
326                                 printf ("\tcompletely useless text into Blender.\n\n");
327                                 
328                                 printf ("\tADDENDUM! Ton, in defense, found this picture of\n");
329                                 printf ("\tan Australian python, exhibiting her (his/its) forked\n");
330                                 printf ("\tY tongue. It could be part of an H Zr retorted!\n\n");
331                                 printf ("\thttp://www.users.bigpond.com/snake.man/\n");
332                                 
333                                 exit(252);
334                                 
335                         case 'h':                       
336                                 print_help();
337                                 exit(0);
338                         
339                         case 'v':
340                                 print_version();
341                                 exit(0);
342
343                         default:
344                                 break;
345                         }
346                 }
347         }
348
349 #ifdef __sgi
350         setuid(getuid()); /* end superuser */
351 #endif
352
353         /* for all platforms, even windos has it! */
354         if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
355
356                 /* background render uses this font too */
357         BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
358                 /* init struct R, Osa, defmaterial, filters */
359         RE_init_render_data();
360
361         if(G.background==0) {
362                 for(a=1; a<argc; a++) {
363                         if(argv[a][0] == '-') {
364                                 switch(argv[a][1]) {
365                                 case 'p':       /* prefsize */
366                                         if (argc-a < 5) {
367                                                 printf ("-p requires four arguments\n");
368                                                 exit(1);
369                                         }
370                                         a++;
371                                         stax= atoi(argv[a]);
372                                         a++;
373                                         stay= atoi(argv[a]);
374                                         a++;
375                                         sizx= atoi(argv[a]);
376                                         a++;
377                                         sizy= atoi(argv[a]);
378
379                                         setprefsize(stax, stay, sizx, sizy);
380                                         break;
381                                 case 'd':
382                                         G.f |= G_DEBUG;         /* std output printf's */ 
383                                         printf ("Blender V %d.%02d\n", G.version/100, G.version%100);
384 #ifdef NAN_BUILDINFO
385                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
386
387 #endif // NAN_BUILDINFO
388                                         for (i = 0; i < argc; i++) {
389                                                 printf("argv[%d] = %s\n", i, argv[i]);
390                                         }
391                                         break;
392             
393                                 case 'w':
394                                         /* XXX, fixme zr, with borders */
395                                         /* there probably is a better way to do
396                                          * this, right now do as if blender was
397                                          * called with "-p 0 0 xres yres" -- sgefant
398                                          */ 
399                                         winlay_get_screensize(&sizx, &sizy);
400                                         setprefsize(0, 0, sizx, sizy);
401 #if 0
402 //#ifdef _WIN32 // FULLSCREEN
403                                         G.windowstate = G_WINDOWSTATE_BORDER;
404 #endif
405                                         break;
406                                 case 'W':
407                                                 /* XXX, fixme zr, borderless on win32 */
408 #if 0
409 //#ifdef _WIN32 // FULLSCREEN
410                                         G.windowstate = G_WINDOWSTATE_FULLSCREEN;
411 #endif
412                                         break;
413                                 case 'R':
414                                 /* Registering filetypes only makes sense on windows...      */
415 #ifdef WIN32
416                                         RegisterBlendExtension(argv[0]);
417 #endif
418                                         break;
419                                 case 'n':
420                                 case 'N':
421                                         if (BLI_strcasecmp(argv[a], "-noaudio") == 0|| BLI_strcasecmp(argv[a], "-nosound") == 0) {
422                                                 /**
423                                                         notify the gameengine that no audio is wanted, even if the user didn't give
424                                                         the flag -g noaudio.
425                                         */
426
427                                                 SYS_WriteCommandLineInt(syshandle,"noaudio",1);
428                                                 audio = 0;
429                                                 if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
430                                         }
431                                         else if (BLI_strcasecmp(argv[a], "-nofrozen") == 0) {
432                                                 /* disable initialization of frozen python modules */
433                                                 if (G.f & G_DEBUG) printf("disable frozen modules\n");
434                                                 G.f |= G_NOFROZEN;
435                                         }
436                                         break;
437                                 }
438                         }
439                 }
440
441                 BPY_start_python(argc, argv);
442                 
443                 /**
444                  * NOTE: sound_init_audio() *must be* after start_python,
445                  * at least on FreeBSD.
446                  * added note (ton): i removed it altogether
447                  */
448
449                 BIF_init();
450
451         }
452         else {
453                 BPY_start_python(argc, argv);
454                 
455                 // (ton) Commented out. I have no idea whats thisfor... will mail around!
456                 // SYS_WriteCommandLineInt(syshandle,"noaudio",1);
457         // audio = 0;
458         // sound_init_audio();
459         // if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
460         }
461
462         /**
463          * NOTE: the U.pythondir string is NULL until BIF_init() is executed,
464          * so we provide the BPY_ function below to append the user defined
465          * pythondir to Python's sys.path at this point.  Simply putting
466          * BIF_init() before BPY_start_python() crashes Blender at startup.
467          * Update: now this function also inits the bpymenus, which also depend
468          * on U.pythondir.
469          */
470         BPY_post_start_python();
471
472 #ifdef WITH_QUICKTIME
473
474         quicktime_init();
475
476 #endif /* WITH_QUICKTIME */
477
478         /* OK we are ready for it */
479
480         for(a=1; a<argc; a++) {
481                 if (G.afbreek==1) break;
482
483                 if(argv[a][0] == '-') {
484                         switch(argv[a][1]) {
485                         case 'p':       /* prefsize */
486                                 a+= 4;
487                                 break;
488
489                         case 'g':
490                                 {
491                                 /**
492                                 gameengine parameters are automaticly put into system
493                                 -g [paramname = value]
494                                 -g [boolparamname]
495                                 example:
496                                 -g novertexarrays
497                                 -g maxvertexarraysize = 512
498                                 */
499
500                                         if(++a < argc)
501                                         {
502                                                 char* paramname = argv[a];
503                                                 /* check for single value versus assignment */
504                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
505                                                 {
506                                                         a++;
507                                                         if (a+1 < argc)
508                                                         {
509                                                                 a++;
510                                                                 /* assignment */
511                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
512                                                         }  else
513                                                         {
514                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
515                                                         }
516                                                         /* name arg eaten */
517
518                                                 } else
519                                                 {
520
521                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
522
523                                                         /* doMipMap */
524                                                         if (!strcmp(argv[a],"nomipmap"))
525                                                         {
526                                                                 set_mipmap(0); //doMipMap = 0;
527                                                         }
528                                                         /* linearMipMap */
529                                                         if (!strcmp(argv[a],"linearmipmap"))
530                                                         {
531                                                                 set_linear_mipmap(1); //linearMipMap = 1;
532                                                         }
533
534
535                                                 } /* if (*(argv[a+1]) == '=') */
536                                         } /*    if(++a < argc)  */
537                                         break;
538                                 }
539                         case 'f':
540                                 a++;
541                                 if (G.scene && a < argc) {
542                                         G.real_sfra = (G.scene->r.sfra);
543                                         G.real_efra = (G.scene->r.efra);
544                                         (G.scene->r.sfra) = atoi(argv[a]);
545                                         (G.scene->r.efra) = (G.scene->r.sfra);
546                                         RE_animrender(NULL);
547                                 }
548                                 break;
549                         case 'a':
550                                 if (G.scene) {
551                                         G.real_sfra = (G.scene->r.sfra);
552                                         G.real_efra = (G.scene->r.efra);
553                                         RE_animrender(NULL);
554                                 }
555                                 break;
556                         case 'S':
557                                 if(++a < argc) {
558                                         set_scene_name(argv[a]);
559                                 }
560                                 break;
561                         case 's':
562                                 a++;
563                                 if(G.scene) {
564                                         if (a < argc) (G.scene->r.sfra) = atoi(argv[a]);
565                                 }
566                                 break;
567                         case 'e':
568                                 a++;
569                                 if(G.scene) {
570                                         if (a < argc) (G.scene->r.efra) = atoi(argv[a]);
571                                 }
572                                 break;
573                         case 'P':
574                                 a++;
575                                 if (a < argc) BPY_run_python_script (argv[a]);
576                                 else printf("\nError: you must specify a Python script after '-P '.\n");
577                                 break;
578                         }
579                 }
580                 else {
581                         BKE_read_file(argv[a], NULL);
582                         sound_initialize_sounds();
583                 }
584         }
585
586         if(G.background)
587         {
588                 exit_usiblender();
589         }
590
591         setscreen(G.curscreen);
592
593         if(G.main->scene.first==0) {
594                 sce= add_scene("1");
595                 set_scene(sce);
596         }
597
598         screenmain();
599
600         return 0;
601 } /* end of int main(argc,argv) */
602
603 static void error_cb(char *err)
604 {
605         error("%s", err);
606 }
607
608 void setCallbacks(void)
609 {
610         /* Error output from the alloc routines: */
611         MEM_set_error_stream(stderr);
612
613
614         /* BLI_blenlib: */
615
616         BLI_setErrorCallBack(error_cb); /* */
617         BLI_setInterruptCallBack(blender_test_break);
618
619         /* render module: execution flow, timers, cursors and display. */
620         RE_set_getrenderdata_callback(RE_rotateBlenderScene);
621         RE_set_freerenderdata_callback(RE_freeRotateBlenderScene);
622 }