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