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