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