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