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