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