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