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