2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Campbell Barton
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/windowmanager/intern/wm_playanim.c
31 * \note This file uses ghost directly and none of the WM definitions.
32 * this could be made into its own module, alongside creator/
35 #include <sys/types.h>
42 # include <sys/times.h>
43 # include <sys/wait.h>
47 #include "MEM_guardedalloc.h"
53 #include "BLI_blenlib.h"
56 #include "IMB_imbuf_types.h"
57 #include "IMB_imbuf.h"
59 #include "BKE_blender.h"
60 #include "BKE_global.h"
61 #include "BKE_utildefines.h"
64 #include "BIF_glutil.h"
70 # elif defined(__APPLE__)
71 # include <QuickTime/Movies.h>
72 # endif /* __APPLE__ */
73 #endif /* WITH_QUICKTIME */
75 #include "DNA_scene_types.h"
76 #include "BLI_utildefines.h"
77 #include "ED_datafiles.h" /* for fonts */
78 #include "wm_event_types.h"
79 #include "GHOST_C-api.h"
88 typedef struct PlayState {
101 struct PlayAnimPict *picture;
103 /* set once at the start */
107 /* saves passing args */
108 struct ImBuf *curframe_ibuf;
111 /* ***************** gl_util.c ****************** */
113 static GHOST_SystemHandle g_system = NULL;
114 static void *g_window = NULL;
116 static int qualN = 0;
118 #define LSHIFT (1 << 0)
119 #define RSHIFT (1 << 1)
120 #define SHIFT (LSHIFT | RSHIFT)
121 #define LALT (1 << 2)
122 #define RALT (1 << 3)
123 #define ALT (LALT | RALT)
124 #define LCTRL (1 << 4)
125 #define RCTRL (1 << 5)
126 #define LMOUSE (1 << 16)
127 #define MMOUSE (1 << 17)
128 #define RMOUSE (1 << 18)
129 #define MOUSE (LMOUSE | MMOUSE | RMOUSE)
131 unsigned short screen_qread(short *val, char *ascii);
133 void playanim_window_get_size(int *width_r, int *height_r)
135 GHOST_RectangleHandle bounds = GHOST_GetClientBounds(g_window);
136 *width_r = GHOST_GetWidthRectangle(bounds);
137 *height_r = GHOST_GetHeightRectangle(bounds);
138 GHOST_DisposeRectangle(bounds);
143 static int qreadN(short *val)
147 int event = screen_qread(val, &ascii);
149 int event = 123456789;
154 if (*val) qualN |= LMOUSE;
155 else qualN &= ~LMOUSE;
158 if (*val) qualN |= MMOUSE;
159 else qualN &= ~MMOUSE;
162 if (*val) qualN |= RMOUSE;
163 else qualN &= ~RMOUSE;
166 if (*val) qualN |= LSHIFT;
167 else qualN &= ~LSHIFT;
170 if (*val) qualN |= RSHIFT;
171 else qualN &= ~RSHIFT;
174 if (*val) qualN |= LCTRL;
175 else qualN &= ~LCTRL;
178 if (*val) qualN |= RCTRL;
179 else qualN &= ~RCTRL;
182 if (*val) qualN |= LALT;
186 if (*val) qualN |= RALT;
195 typedef struct PlayAnimPict {
196 struct PlayAnimPict *next, *prev;
206 static struct ListBase _picsbase = {NULL, NULL};
207 static struct ListBase *picsbase = &_picsbase;
208 static int fromdisk = FALSE;
209 static int fstep = 1;
210 static float zoomx = 1.0, zoomy = 1.0;
211 static double ptottime = 0.0, swaptime = 0.04;
213 static int pupdate_time(void)
218 time = PIL_check_seconds_timer();
220 ptottime += (time - ltime);
222 return (ptottime < 0);
225 static void toscreen(PlayAnimPict *picture, struct ImBuf *ibuf, int fontid)
229 printf("no ibuf !\n");
232 if (ibuf->rect == NULL && ibuf->rect_float) {
233 IMB_rect_from_float(ibuf);
234 imb_freerectfloatImBuf(ibuf);
236 if (ibuf->rect == NULL)
239 GHOST_ActivateWindowDrawingContext(g_window);
241 glRasterPos2f(0.0f, 0.0f);
243 glDrawPixels(ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
247 //if (picture && (qualN & (SHIFT | LMOUSE))) {
248 if (picture && fontid != -1) {
249 char str[32 + FILE_MAX];
251 // glRasterPos2f(0.02f, 0.03f);
252 BLI_snprintf(str, sizeof(str), "%s | %.2f frames/s", picture->name, fstep / swaptime);
253 // BMF_DrawString(font, str);
255 BLF_enable(fontid, BLF_ASPECT);
256 BLF_aspect(fontid, 1.0f / ibuf->x, 1.0f / ibuf->y, 1.0f);
257 BLF_position(fontid, 0.02f, 0.03f, 0.0f);
258 BLF_draw(fontid, str, 256); // XXX
259 printf("Drawing text '%s'\n", str);
262 GHOST_SwapWindowBuffers(g_window);
265 static void build_pict_list(char *first, int totframes, int fstep, int fontid)
267 char *mem, filepath[FILE_MAX];
269 PlayAnimPict *picture = NULL;
270 struct ImBuf *ibuf = NULL;
271 char str[32 + FILE_MAX];
274 if (IMB_isanim(first)) {
275 anim = IMB_open_anim(first, IB_rect, 0);
278 ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
280 toscreen(NULL, ibuf, fontid);
284 for (pic = 0; pic < IMB_anim_get_duration(anim, IMB_TC_NONE); pic++) {
285 picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "Pict");
286 picture->anim = anim;
287 picture->frame = pic;
288 picture->IB_flags = IB_rect;
289 BLI_snprintf(str, sizeof(str), "%s : %d", first, pic + 1);
290 picture->name = strdup(str);
291 BLI_addtail(picsbase, picture);
295 printf("couldn't open anim %s\n", first);
301 BLI_strncpy(filepath, first, sizeof(filepath));
308 * If set, all reads and writes on the resulting file descriptor will
309 * be performed directly to or from the user program buffer, provided
310 * appropriate size and alignment restrictions are met. Refer to the
311 * F_SETFL and F_DIOINFO commands in the fcntl(2) manual entry for
312 * information about how to determine the alignment constraints.
313 * O_DIRECT is a Silicon Graphics extension and is only supported on
314 * local EFS and XFS file systems.
317 while (IMB_ispic(filepath) && totframes) {
321 file = open(filepath, O_BINARY | O_RDONLY, 0);
322 if (file < 0) return;
323 picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "picture");
324 if (picture == NULL) {
325 printf("Not enough memory for pict struct \n");
329 size = BLI_file_descriptor_size(file);
337 picture->size = size;
338 picture->IB_flags = IB_rect;
340 if (fromdisk == FALSE) {
341 mem = (char *)MEM_mallocN(size, "build pic list");
343 printf("Couldn't get memory\n");
349 if (read(file, mem, size) != size) {
350 printf("Error while reading %s\n", filepath);
362 picture->name = strdup(filepath);
364 BLI_addtail(picsbase, picture);
369 if (ptottime > 1.0) {
371 ibuf = IMB_ibImageFromMemory((unsigned char *)picture->mem, picture->size,
372 picture->IB_flags, picture->name);
375 ibuf = IMB_loadiffname(picture->name, picture->IB_flags);
378 toscreen(picture, ibuf, fontid);
385 BLI_newname(filepath, +fstep);
389 switch (qreadN(&val)) {
402 static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
404 PlayState *ps = (PlayState *)ps_void;
406 GHOST_TEventType type = GHOST_GetEventType(evt);
410 /* convert ghost event into value keyboard or mouse */
411 val = ELEM(type, GHOST_kEventKeyDown, GHOST_kEventButtonDown);
413 if (ps->wait2 && ps->stopped) {
418 case GHOST_kEventWindowSize:
419 case GHOST_kEventWindowMove:
423 playanim_window_get_size(&sizex, &sizey);
424 GHOST_ActivateWindowDrawingContext(g_window);
426 glViewport(0, 0, sizex, sizey);
427 glScissor(0, 0, sizex, sizey);
429 zoomx = (float) sizex / ps->ibufx;
430 zoomy = (float) sizey / ps->ibufy;
431 zoomx = floor(zoomx + 0.5);
432 zoomy = floor(zoomy + 0.5);
433 if (zoomx < 1.0) zoomx = 1.0;
434 if (zoomy < 1.0) zoomy = 1.0;
436 sizex = zoomx * ps->ibufx;
437 sizey = zoomy * ps->ibufy;
439 glPixelZoom(zoomx, zoomy);
442 toscreen(ps->picture, ps->curframe_ibuf, ps->fontid);
443 //XXX25 while (qtest()) qreadN(&val);
447 case GHOST_kEventQuit:
448 case GHOST_kEventWindowClose:
463 //XXX25 int event = qreadN(&val);
464 /* printf("%d %d\n", event, val); */
473 ps->noskip = !ps->noskip;
477 ps->pingpong = !ps->pingpong;
482 if (ps->curframe_ibuf)
483 printf(" Name: %s | Speed: %.2f frames/s\n", ps->curframe_ibuf->name, fstep / swaptime);
486 swaptime = fstep / 5.0;
495 ps->picture = picsbase->first;
507 ps->next = ps->direction = -1;
520 ps->picture = picsbase->last;
532 ps->next = ps->direction = 1;
542 if (qualN & LMOUSE) {
545 playanim_window_get_size(&sizex, &sizey);
546 ps->picture = picsbase->first;
548 while (ps->picture) {
550 ps->picture = ps->picture->next;
552 i = (i * val) / sizex;
553 ps->picture = picsbase->first;
555 if (ps->picture->next == NULL) break;
556 ps->picture = ps->picture->next;
567 printf("pause:%d\n", ps->pause);
578 printf("pause:%d\n", ps->pause);
588 ps->once = ps->wait2 = FALSE;
600 ps->wait2 = ps->sstep = FALSE;
605 if (ps->sstep) ps->wait2 = FALSE;
608 ps->wait2 = !ps->wait2;
613 swaptime = fstep / 60.0;
616 swaptime = fstep / 50.0;
619 swaptime = fstep / 30.0;
623 swaptime = fstep / 24.0;
625 swaptime = fstep / 25.0;
628 swaptime = fstep / 20.0;
631 swaptime = fstep / 15.0;
634 swaptime = fstep / 12.0;
637 swaptime = fstep / 10.0;
640 swaptime = fstep / 6.0;
649 /* int ofsx, ofsy; */ /* UNUSED */
652 if (zoomx > 1.0) zoomx -= 1.0;
653 if (zoomy > 1.0) zoomy -= 1.0;
654 // playanim_window_get_position(&ofsx, &ofsy);
655 playanim_window_get_size(&sizex, &sizey);
656 /* ofsx += sizex / 2; */ /* UNUSED */
657 /* ofsy += sizey / 2; */ /* UNUSED */
658 sizex = zoomx * ps->ibufx;
659 sizey = zoomy * ps->ibufy;
660 /* ofsx -= sizex / 2; */ /* UNUSED */
661 /* ofsy -= sizey / 2; */ /* UNUSED */
662 // window_set_position(g_window,sizex,sizey);
663 GHOST_SetClientSize(g_window, sizex, sizey);
677 //XXX25 if (go == FALSE) break;
683 void playanim_window_open(const char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
685 GHOST_TWindowState inital_state;
686 GHOST_TUns32 scr_w, scr_h;
688 GHOST_GetMainDisplayDimensions(g_system, &scr_w, &scr_h);
690 posy = (scr_h - posy - sizey);
692 if (start_maximized == G_WINDOWSTATE_FULLSCREEN)
693 inital_state = start_maximized ? GHOST_kWindowStateFullScreen : GHOST_kWindowStateNormal;
695 inital_state = start_maximized ? GHOST_kWindowStateMaximized : GHOST_kWindowStateNormal;
696 #if defined(__APPLE__) && !defined(GHOST_COCOA)
698 extern int macPrefState; /* creator.c */
699 initial_state += macPrefState;
703 g_window = GHOST_CreateWindow(g_system,
705 posx, posy, sizex, sizey,
707 GHOST_kDrawingContextTypeOpenGL,
708 FALSE /* no stereo */, FALSE);
712 // GHOST_SetWindowUserData(ghostwin, win);
714 // GHOST_DisposeWindow(g_system, ghostwin);
720 void playanim(int argc, const char **argv)
722 struct ImBuf *ibuf = NULL;
723 char filepath[FILE_MAX];
724 GHOST_TUns32 maxwinx, maxwiny;
725 /* short c233 = FALSE, yuvx = FALSE; */ /* UNUSED */
727 /* This was done to disambiguate the name for use under c++. */
728 struct anim *anim = NULL;
729 int start_x = 0, start_y = 0;
736 /* ps.doubleb = TRUE;*/ /* UNUSED */
749 /* resetmap = FALSE */
754 if (argv[1][0] == '-') {
755 switch (argv[1][1]) {
761 start_x = atoi(argv[2]);
762 start_y = atoi(argv[3]);
767 printf("too few arguments for -p (need 2): skipping\n");
772 double fps = atof(argv[2]);
773 double fps_base = atof(argv[3]);
776 printf("invalid fps,"
779 swaptime = fps_base / fps;
784 printf("too few arguments for -f (need 2): skipping\n");
788 sfra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
793 efra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
798 fstep = MIN2(MAXFRAME, MAX2(1, atoi(argv[2])));
804 printf("unknown option '%c': skipping\n", argv[1][1]);
815 #ifdef WITH_QUICKTIME
816 #if defined(_WIN32) || defined(__APPLE__) && !defined(GHOST_COCOA)
817 /* Initialize QuickTime */
823 if (InitializeQTML(0) != noErr)
824 G.have_quicktime = FALSE;
826 G.have_quicktime = TRUE;
828 if (EnterMovies() != noErr)
829 G.have_quicktime = FALSE;
831 #endif /* _WIN32 || __APPLE__ && !defined(GHOST_COCOA)*/
832 G.have_quicktime = TRUE;
833 #endif /* WITH_QUICKTIME */
836 BLI_strncpy(filepath, argv[1], sizeof(filepath));
839 BLI_current_working_dir(filepath, sizeof(filepath));
840 BLI_add_slash(filepath);
843 if (IMB_isanim(filepath)) {
844 anim = IMB_open_anim(filepath, IB_rect, 0);
846 ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
847 IMB_close_anim(anim);
851 else if (!IMB_ispic(filepath)) {
856 ibuf = IMB_loadiffname(filepath, IB_rect);
860 printf("couldn't open %s\n", filepath);
865 #if !defined(WIN32) && !defined(__APPLE__)
872 // extern void add_to_mainqueue(wmWindow *win, void *user_data, short evt, short val, char ascii);
874 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(ghost_event_proc, &ps);
876 g_system = GHOST_CreateSystem();
877 GHOST_AddEventConsumer(g_system, consumer);
881 playanim_window_open("Blender:Anim", start_x, start_y, ibuf->x, ibuf->y, 0);
882 //XXX25 window_set_handler(g_window, add_to_mainqueue, NULL);
884 glMatrixMode(GL_PROJECTION);
886 glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
887 glMatrixMode(GL_MODELVIEW);
890 GHOST_GetMainDisplayDimensions(g_system, &maxwinx, &maxwiny);
892 //GHOST_ActivateWindowDrawingContext(g_window);
894 /* initialize the font */
896 ps.fontid = BLF_load_mem("monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
897 BLF_size(ps.fontid, 11, 72);
902 if (maxwinx % ibuf->x) maxwinx = ibuf->x * (1 + (maxwinx / ibuf->x));
903 if (maxwiny % ibuf->y) maxwiny = ibuf->y * (1 + (maxwiny / ibuf->y));
905 glClearColor(0.0, 0.0, 0.0, 0.0);
906 glClear(GL_COLOR_BUFFER_BIT);
908 GHOST_SwapWindowBuffers(g_window);
910 if (sfra == -1 || efra == -1) {
911 /* one of the frames was invalid, just use all images */
916 build_pict_list(filepath, (efra - sfra) + 1, fstep, ps.fontid);
918 for (i = 2; i < argc; i++) {
919 BLI_strncpy(filepath, argv[i], sizeof(filepath));
920 build_pict_list(filepath, (efra - sfra) + 1, fstep, ps.fontid);
931 ps.direction = -ps.direction;
933 if (ps.direction == 1) {
934 ps.picture = picsbase->first;
937 ps.picture = picsbase->last;
940 if (ps.picture == NULL) {
941 printf("couldn't find pictures\n");
945 if (ps.direction == 1) {
946 ps.picture = ps.picture->next;
949 ps.picture = ps.picture->prev;
952 if (ptottime > 0.0) ptottime = 0.0;
955 if (ibuf != NULL && ibuf->ftype == 0) IMB_freeImBuf(ibuf);
957 if (ps.picture->ibuf) {
958 ibuf = ps.picture->ibuf;
960 else if (ps.picture->anim) {
961 ibuf = IMB_anim_absolute(ps.picture->anim, ps.picture->frame, IMB_TC_NONE, IMB_PROXY_NONE);
963 else if (ps.picture->mem) {
964 ibuf = IMB_ibImageFromMemory((unsigned char *) ps.picture->mem, ps.picture->size,
965 ps.picture->IB_flags, ps.picture->name);
968 ibuf = IMB_loadiffname(ps.picture->name, ps.picture->IB_flags);
972 BLI_strncpy(ibuf->name, ps.picture->name, sizeof(ibuf->name));
975 GHOST_SetTitle(g_window, picture->name);
978 while (pupdate_time()) PIL_sleep_ms(1);
979 ptottime -= swaptime;
980 toscreen(ps.picture, ibuf, ps.fontid);
983 printf("error: can't play this image type\n");
988 if (ps.picture->next == NULL) {
991 else if (ps.picture->prev == NULL) {
996 ps.next = ps.direction;
1000 int hasevent = GHOST_ProcessEvents(g_system, 0);
1002 GHOST_DispatchEvents(g_system);
1006 ps.wait2 = ps.sstep;
1008 if (ps.wait2 == 0 && ps.stopped == 0) {
1014 if (ps.picture && ps.next) {
1015 /* always at least set one step */
1016 while (ps.picture) {
1018 ps.picture = ps.picture->prev;
1021 ps.picture = ps.picture->next;
1024 if (ps.once && ps.picture != NULL) {
1025 if (ps.picture->next == NULL) {
1028 else if (ps.picture->prev == NULL) {
1033 if (ps.wait2 || ptottime < swaptime || ps.turbo || ps.noskip) break;
1034 ptottime -= swaptime;
1036 if (ps.picture == NULL && ps.sstep) {
1038 ps.picture = picsbase->last;
1040 else if (ps.next > 0) {
1041 ps.picture = picsbase->first;
1045 if (ps.go == FALSE) {
1050 ps.picture = picsbase->first;
1052 while (ps.picture) {
1053 if (ps.picture && ps.picture->anim && (anim != ps.picture->anim)) {
1054 // to prevent divx crashes
1055 anim = ps.picture->anim;
1056 IMB_close_anim(anim);
1059 if (ps.picture->ibuf) {
1060 IMB_freeImBuf(ps.picture->ibuf);
1062 if (ps.picture->mem) {
1063 MEM_freeN(ps.picture->mem);
1066 ps.picture = ps.picture->next;
1068 #ifdef WITH_QUICKTIME
1069 #if defined(_WIN32) || defined(__APPLE__) && !defined(GHOST_COCOA)
1070 if (G.have_quicktime) {
1076 #endif /* _WIN32 || __APPLE__ && !defined(GHOST_COCOA) */
1077 #endif /* WITH_QUICKTIME */
1080 if (ibuf) IMB_freeImBuf(ibuf);
1081 BLI_freelistN(picsbase);
1083 GHOST_DisposeWindow(g_system, g_window);
1085 totblock = MEM_get_memory_blocks_in_use();
1086 if (totblock != 0) {
1087 printf("Error Totblock: %d\n", totblock);