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