Welcome to mirror list, hosted at ThFree Co, Russian Federation.

playanim.c « src « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ef07d910cc1fd5f1126cc66f330bdcdcae208fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/**
 * $Id$
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/times.h>
#include <sys/wait.h>
#else
#include <io.h>
#include "BLI_winstuff.h"
#endif   
#include "MEM_guardedalloc.h"

#include "PIL_time.h"

#include <math.h>

#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "BLI_editVert.h"

#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"

#include "BDR_editcurve.h"

#include "BKE_global.h"
#include "BKE_utildefines.h"

#include "BIF_gl.h"
#include "BIF_screen.h"
#include "BIF_mywindow.h"

#include "playanim_ext.h"
#include "mydevice.h"
#include "blendef.h"
#include "winlay.h"

/* ***************** gl_util.c ****************** */

static Window *g_window = NULL;
static int qualN = 0;

#define LSHIFT	(1<<0)
#define RSHIFT	(1<<1)
#define SHIFT	(LSHIFT | RSHIFT)
#define LALT	(1<<2)
#define RALT	(1<<3)
#define ALT	(LALT | RALT)
#define LCTRL	(1<<4)
#define RCTRL	(1<<5)
#define LMOUSE	(1<<16)
#define MMOUSE	(1<<17)
#define RMOUSE	(1<<18)
#define MOUSE	(LMOUSE | MMOUSE | RMOUSE)

unsigned short screen_qread(short *val, char *ascii);

/* implementation */
static int qreadN(short *val)
{
	char ascii;
	int event = screen_qread(val, &ascii);

	switch(event){
	case LEFTMOUSE:
		if (*val) qualN |= LMOUSE;
		else qualN &= ~LMOUSE;
		break;
	case MIDDLEMOUSE:
		if (*val) qualN |= MMOUSE;
		else qualN &= ~MMOUSE;
		break;
	case RIGHTMOUSE:
		if (*val) qualN |= RMOUSE;
		else qualN &= ~RMOUSE;
		break;
	case LEFTSHIFTKEY:
		if (*val) qualN |= LSHIFT;
		else qualN &= ~LSHIFT;
		break;
	case RIGHTSHIFTKEY:
		if (*val) qualN |= RSHIFT;
		else qualN &= ~RSHIFT;
		break;
	case LEFTCTRLKEY:
		if (*val) qualN |= LCTRL;
		else qualN &= ~LCTRL;
		break;
	case RIGHTCTRLKEY:
		if (*val) qualN |= RCTRL;
		else qualN &= ~RCTRL;
		break;
	case LEFTALTKEY:
		if (*val) qualN |= LALT;
		else qualN &= ~LALT;
		break;
	case RIGHTALTKEY:
		if (*val) qualN |= RALT;
		else qualN &= ~RALT;
		break;
	}

	return(event);
}

/* ***************** gl_util.c ****************** */




typedef struct pict{
	struct pict *next, *prev;
	char *mem;
	int size;
	char *name;
	struct ImBuf *ibuf;
	struct anim *anim;
	int frame;
	int IB_flags;
}Pict;

static struct ListBase _picsbase = {0,0};
static struct ListBase *picsbase = &_picsbase;
static int fromdisk = FALSE;
static float zoomx = 1.0 , zoomy = 1.0;
static double ptottime = 0.0, swaptime = 0.04;

static int pupdate_time(void)
{
	static double ltime;
	double time;

	time = PIL_check_seconds_timer();

	ptottime += (time - ltime);
	ltime = time;
	return (ptottime < 0);
}

static void toscreen(struct ImBuf *ibuf)
{
	if (ibuf == 0){
		printf("no ibuf !\n");
		return;
	}

	glRasterPos2f(-1, -1);

	glDrawPixels(ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
	
	pupdate_time();
	
	window_swap_buffers(g_window);
}

static void build_pict_list(char * first)
{
	int size,pic,file;
	char *mem, name[256];
	short val;
	struct pict * picture = 0;
	struct ImBuf *ibuf = 0;
	int count = 0;
	char str[100];
	struct anim * anim;
	
	if (IMB_isanim(first)) {
		anim = IMB_open_anim(first, IB_rect);
		if (anim) {
			ibuf = IMB_anim_absolute(anim, 0);
			if (ibuf) {
				toscreen(ibuf);
				IMB_freeImBuf(ibuf);
			}
			
			for (pic = 0; pic < IMB_anim_get_duration(anim); pic ++) {
				picture = (Pict*)MEM_callocN(sizeof(Pict),"Pict");
				picture->anim = anim;
				picture->frame = pic;
				picture->IB_flags = IB_rect;
				sprintf(str, "%s : %d", first, pic + 1);
				picture->name = strdup(str);
				BLI_addtail(picsbase, picture);
			}
		} else printf("couldn't open anim %s\n", first);
	} else {
	
		strcpy(name,first);
	
		pupdate_time();
		ptottime = 1.0;
		
/*
     O_DIRECT
            If set, all reads and writes on the resulting file descriptor will
            be performed directly to or from the user program buffer, provided
            appropriate size and alignment restrictions are met.  Refer to the
            F_SETFL and F_DIOINFO commands in the fcntl(2) manual entry for
            information about how to determine the alignment constraints.
            O_DIRECT is a Silicon Graphics extension and is only supported on
            local EFS and XFS file systems.
*/
		while(IMB_ispic(name)){
			file = open(name, O_BINARY|O_RDONLY, 0);
			if (file < 0) return;
			picture = (struct pict*)calloc(1, sizeof(struct pict));
			if (picture == 0){
				printf("Not enough memory for pict struct \n");
				close(file);
				return;
			}
			size = BLI_filesize(file);
			picture->size = size;
			picture->IB_flags = IB_rect;
						
			if (fromdisk == FALSE) {
				mem=(char *)malloc(size);
				if (mem==0){
					printf("Couldn't get memory\n");
					close(file);
					free(picture);
					return;
				}
		
				if (read(file,mem,size) != size){
					printf("Error while reading %s\n",name);
					close(file);
					free(picture);
					free(mem);
					return;
				}
			} else mem = 0;
			
			picture->mem = mem;
			picture->name = strdup(name);
			close(file);
			BLI_addtail(picsbase,picture);
			count++;
			
			pupdate_time();
			
			if (ptottime > 1.0) {				
				if (picture->mem) ibuf = IMB_ibImageFromMemory((int *) picture->mem, picture->size, picture->IB_flags);
				else ibuf = IMB_loadiffname(picture->name, picture->IB_flags);
				if (ibuf) {
					toscreen(ibuf);
					IMB_freeImBuf(ibuf);
					glDrawBuffer(GL_FRONT);

					cpack(-1);
					glRasterPos2i(10,  10);
					sprintf(str, "%4d: %s", count, name);
					glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
					glDrawBuffer(GL_BACK);
				}
				pupdate_time();
				ptottime = 0.0;
			}
			
			BLI_newname(name, +1);
			
			while(qtest()){
				switch(qreadN(&val)){
				case ESCKEY:
					if (val) return;
					break;
				}
			}
		}
	}
	return;
}

void playanim(int argc, char **argv)
{
	struct ImBuf *ibuf = 0;
	struct pict *picture = 0;
	char name[256];
	short val = 0, go = TRUE, ibufx = 0, ibufy = 0;
	int event, stopped = FALSE, maxwinx, maxwiny;
	short /*  c233 = FALSE, */ /*  yuvx = FALSE, */ once = FALSE, sstep = FALSE, wait2 = FALSE, /*  resetmap = FALSE, */ pause = 0;
	short pingpong = FALSE, direction = 1, next = 1, turbo = FALSE, /*  doubleb = TRUE, */ noskip = FALSE;
	int sizex, sizey, ofsx, ofsy, i;
	/* This was done to disambiguate the name for use under c++. */
	struct anim * anim = 0;
	int start_x= 0, start_y= 0;

	while (argc > 1) {
		if (argv[1][0] == '-'){
			switch(argv[1][1]) {
				case 'm':
					fromdisk = TRUE;
					break;
				case 'p':
					if (argc>3) {
						start_x= atoi(argv[2]);
						start_y= atoi(argv[3]);
						argc-= 2; 
						argv+= 2;
					} else {
						printf("too few arguments for -p (need 2): skipping\n");
					}
					break;
				default:
					printf("unknown option '%c': skipping\n", argv[1][1]);
					break;
			}
			argc--;
			argv++;
		} else break;
	}
	
	if (argc > 1) strcpy(name,argv[1]);
	else {
		BLI_getwdN(name);
		if (name[strlen(name)-1] != '/') strcat(name,"/");
	}
		
	if (IMB_isanim(name)) {
		anim = IMB_open_anim(name, IB_rect);
		if (anim) {
			ibuf = IMB_anim_absolute(anim, 0);
			IMB_close_anim(anim);
			anim = NULL;
		}
	} else if (!IMB_ispic(name)) {
		exit(1);
	}

	if (ibuf == 0) ibuf = IMB_loadiffname(name, IB_rect);
	if (ibuf == 0){
		printf("couldn't open %s\n",name);
		exit(1);
	}
	
	#if !defined(WIN32) && !defined(__APPLE__)
	if (fork()) exit(0);
	#endif
	
	winlay_get_screensize(&maxwinx, &maxwiny);

		/* XXX, fixme zr */
	{
		extern void add_to_mainqueue(Window *win, void *user_data, short evt, short val, char ascii);

		g_window = window_open("Blender:Anim", start_x, start_y, ibuf->x, ibuf->y, 0);
		window_set_handler(g_window, add_to_mainqueue, NULL);
	}

	ibufx = ibuf->x;
	ibufy = ibuf->y;
	
	if (maxwinx % ibuf->x) maxwinx = ibuf->x * (1 + (maxwinx / ibuf->x));
	if (maxwiny % ibuf->y) maxwiny = ibuf->y * (1 + (maxwiny / ibuf->y));
	
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	window_swap_buffers(g_window);
	
	build_pict_list(name);
	
	for (i = 2; i < argc; i++){
		strcpy(name, argv[i]);
		build_pict_list(name);
	}

	IMB_freeImBuf(ibuf); 
	ibuf = 0;

	pupdate_time();
	ptottime = 0;

	while (go){
		if (pingpong) direction = -direction;

		if (direction == 1) picture = picsbase->first;
		else picture = picsbase->last;

		if (picture == 0){
			printf("couldn't find pictures\n");
			go = FALSE;
		}
		if (pingpong){
			if (direction == 1) picture = picture->next;
			else picture = picture->prev;
		}
		if (ptottime > 0.0) ptottime = 0.0;

		while (picture){
			if (ibuf != 0 && ibuf->type == 0) IMB_freeImBuf(ibuf);

			if (picture->ibuf) ibuf = picture->ibuf;
			else if (picture->anim) ibuf = IMB_anim_absolute(picture->anim, picture->frame);
			else if (picture->mem) ibuf = IMB_ibImageFromMemory((int *) picture->mem, picture->size, picture->IB_flags);
			else ibuf = IMB_loadiffname(picture->name, picture->IB_flags);
			
			if (ibuf){
				strcpy(ibuf->name, picture->name);
				
				while (pupdate_time()) PIL_sleep_ms(1);
				ptottime -= swaptime;
				toscreen(ibuf);
			} /* else deleten */
			else {
				printf("error: can't play this image type\n");
				exit(0);
			}
			
			if (once){
				if (picture->next == 0) wait2 = TRUE;
				else if (picture->prev == 0) wait2 = TRUE;
			}
			
			next = direction;
			
			while ((qtest() != 0 ) || ( wait2 != 0)){
				if (wait2 && stopped) {
					stopped = FALSE;
				}
			
				event = qreadN(&val);
				/* printf("%d %d\n", event, val); */
				
				if (wait2){
					pupdate_time();
					ptottime = 0;
				}
				switch (event){
				case AKEY:
					if (val)
						noskip = !noskip;
					break;
				case PKEY:
					if (val)
						pingpong = !pingpong;
					break;
				case SLASHKEY:
					if (val) {
						if (qualN & SHIFT) {
							if (ibuf)
								printf(" Name: %s | Speed: %.2f frames/s\n", ibuf->name, 1.0 / swaptime);
						} else {
							swaptime = 1.0 / 5.0;
						}
					}
					break;
				case LEFTARROWKEY:
					if (val){
						sstep = TRUE;
						wait2 = FALSE;
						if (qualN & SHIFT) {
							picture = picsbase->first;
							next = 0;
						} else {
							next = -1;
						}
					}
					break;
				case DOWNARROWKEY:
					if (val){
						wait2 = FALSE;
						if (qualN & SHIFT) {
							next = direction = -1;
						} else {
							next = -10;
							sstep = TRUE;
						}
					}
					break;
				case RIGHTARROWKEY:
					if (val){
						sstep = TRUE;
						wait2 = FALSE;
						if (qualN & SHIFT) {
							picture = picsbase->last;
							next = 0;
						} else {
							next = 1;
						}
					}
					break;
				case UPARROWKEY:
					if (val){
						wait2 = FALSE;
						if (qualN & SHIFT) {
							next = direction = 1;
						} else {
							next = 10;
							sstep = TRUE;
						}
					}
					break;
				case LEFTMOUSE:
				case MOUSEX:
					if (qualN & LMOUSE) {
						window_get_size(g_window,&sizex,&sizey);
						picture = picsbase->first;
						i = 0;
						while (picture){
							i ++;
							picture = picture->next;
						}
						i = (i * val) / sizex;
						picture = picsbase->first;
						for (; i > 0; i--){
							if (picture->next == 0) break;
							picture = picture->next;
						}
						sstep = TRUE;
						wait2 = FALSE;
						next = 0;
					}
					break;
					go= FALSE;
					break;
				case EQUALKEY:
					if (val) {
						if (qualN & SHIFT) {
							pause ++;
							printf("pause:%d\n", pause);
						} else swaptime /= 1.1;
					}
					break;
				case MINUSKEY:
					if (val) {
						if (qualN & SHIFT) {
							pause --;
							printf("pause:%d\n", pause);
						} else swaptime *= 1.1;
					}
					break;
				case PAD0:
					if (val){
						if (once) once = wait2 = FALSE;
						else {
							picture = 0;
							once = TRUE;
							wait2 = FALSE;
						}
					}
					break;
				case RETKEY:
				case PADENTER:
					if (val){
						wait2 = sstep = FALSE;
					}
					break;
				case PADPERIOD:
					if (val){
						if (sstep) wait2 = FALSE;
						else {
							sstep = TRUE;
							wait2 = !wait2;
						}
					}
					break;
				case PAD1:
					swaptime = 1.0 / 60.0;
					break;
				case PAD2:
					swaptime = 1.0 / 50.0;
					break;
				case PAD3:
					swaptime = 1.0 / 30.0;
					break;
				case PAD4:
					swaptime = 1.0 / 25.0;
					break;
				case PAD5:
					swaptime = 1.0 / 20.0;
					break;
				case PAD6:
					swaptime = 1.0 / 15.0;
					break;
				case PAD7:
					swaptime = 1.0 / 12.0;
					break;
				case PAD8:
					swaptime = 1.0 / 10.0;
					break;
				case PAD9:
					swaptime = 1.0 / 6.0;
					break;
				case PADPLUSKEY:
					if (val == 0) break;
					zoomx += 2.0;
					zoomy += 2.0;
				case PADMINUS:
					if (val == 0) break;
					if (zoomx > 1.0) zoomx -= 1.0;
					if (zoomy > 1.0) zoomy -= 1.0;
					window_get_position(g_window,&ofsx,&ofsy);
					window_get_size(g_window,&sizex,&sizey);
					ofsx += sizex/2;
					ofsy += sizey/2;
					sizex = zoomx * ibufx;
					sizey = zoomy * ibufy;
					ofsx -= sizex/2;
					ofsy -= sizey/2;
/* 					window_set_position(g_window,sizex,sizey); */
					window_set_size(g_window,sizex,sizey);
					break;
				case RESHAPE:
				case REDRAW:
					window_get_size(g_window,&sizex,&sizey);
					window_make_active(g_window);
					
					glViewport(0,  0, sizex, sizey);
					glScissor(0,  0, sizex, sizey);

					zoomx = (float) sizex / ibufx;
					zoomy = (float) sizey / ibufy;
					zoomx = floor(zoomx + 0.5);
					zoomy = floor(zoomy + 0.5);
					if (zoomx < 1.0) zoomx = 1.0;
					if (zoomy < 1.0) zoomy = 1.0;

					sizex = zoomx * ibufx;
					sizey = zoomy * ibufy;

					glPixelZoom(zoomx, zoomy);
					glEnable(GL_DITHER);
					ptottime = 0.0;
					toscreen(ibuf);
					while (qtest()) qreadN(&val);
					
					break;
				case ESCKEY:
				case WINCLOSE:
				case WINQUIT:
					go = FALSE;
					break;
				}
				if (go == FALSE) break;
			}
			
			wait2 = sstep;
			
			if (wait2 == 0 && stopped == 0) {
				stopped = TRUE;
			}

			pupdate_time();
					
			if (picture && next) {
				/* altijd minstens 1 stap zetten */
				while (picture){
					if (next < 0) picture = picture->prev;
					else picture = picture->next;
	
					if (once && picture != 0){
						if (picture->next == 0) wait2 = TRUE;
						else if (picture->prev == 0) wait2 = TRUE;
					}

					if (wait2 || ptottime < swaptime || turbo || noskip) break;
					ptottime -= swaptime;
				}
				if (picture == 0 && sstep) {
					if (next < 0) picture = picsbase->last;
					else if (next > 0) picture = picsbase->first;									
				}
			}
			if (go == FALSE) break;
		}
	}
	picture = picsbase->first;
	anim = NULL;
	while (picture) {
		if (picture && picture->anim && (anim != picture->anim)) {
			// to prevent divx crashes
			anim = picture->anim;
			IMB_close_anim(anim);
		}
		picture = picture->next;
	}
}