home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / MPlay / mpeg_play2.0 / nextstep.m < prev    next >
Encoding:
Text File  |  1995-12-13  |  6.3 KB  |  292 lines

  1. /*
  2.  *    nextstep.m, copyright Paul A. Griffin 1995
  3.  */
  4.  
  5. #import <appkit/appkit.h>
  6. #import <sys/param.h>
  7. #import <string.h>
  8. #import "video.h"
  9. #import "nextstep.h"
  10.  
  11. static id app, window, cview, controller;
  12.  
  13. static NXRect viewRect, windowRect;
  14. static NXModalSession session;
  15. static int pixelsHigh, pixelsWide, bytesPerRow;
  16. static float videoRatio, scaleRatio;
  17.  
  18. void ExecuteDisplay(VidStream *vid_stream);
  19. void InitDisplay(int w, int h);
  20.  
  21. @interface CWindow:Window 
  22. {
  23. - showTitle:(int)dots :(const char *)filename; 
  24. @end
  25.  
  26. @implementation CWindow
  27. - showTitle:(int)dots :(const char *)filename; 
  28. {     
  29.     unsigned char newname[MAXPATHLEN];
  30.     
  31.     strcpy(newname, filename);
  32.     strcat(newname," "); 
  33.     while(dots-- > 0) strcat(newname , ".");
  34.     
  35.     [self setTitleAsFilename:(const char *)newname];
  36.     showSpeed = 1;
  37. }
  38. @end
  39.  
  40. @interface CView:View 
  41. {
  42. - drawSelf:(const NXRect *)rects :(int)rectCount;
  43. -(BOOL)acceptsFirstMouse; /*YES*/
  44. - mouseDown:(NXEvent *)theEvent; /* left mouse */ 
  45. - rightMouseDown:(NXEvent *)theEvent;
  46. @end
  47.  
  48. @implementation CView
  49. - drawSelf:(const NXRect *)rects :(int)rectCount
  50. {
  51.     int hf = holdFrame;
  52.     static int first_time  = 1;
  53.     
  54.     [super drawSelf:rects :rectCount];
  55.  
  56.     if(curVidStream != NULL && !first_time) {
  57.         drawFrame = 1;
  58.         ExecuteDisplay(curVidStream);
  59.         drawFrame = 0;
  60.     }
  61.     
  62.     first_time = 0;
  63.     
  64.     return self;
  65. }
  66. - (BOOL)acceptsFirstMouse{
  67.     return YES;
  68. }
  69.  
  70. - mouseDown:(NXEvent *)theEvent
  71. {
  72.     unsigned char title[10]; 
  73.  
  74.     if(theEvent->flags & NX_ALPHASHIFTMASK){
  75.         if(holdFrame == 1){
  76.             loopFlag = loopFlag ? 0 : 1;
  77.             /* display the loop status */    
  78.             if(loopFlag) sprintf(title, "loop", frames_per_sec);
  79.             else sprintf(title, "no loop", frames_per_sec);
  80.             [window setTitle:(const char *)title];
  81.         }
  82.         else{
  83.             doRewind = 1;
  84.             holdFrame = 1;
  85.         }
  86.         return nil;
  87.     }
  88.     
  89.     doRewind = holdFrame == 3 ? 1 : doRewind;
  90.     
  91.     holdFrame = holdFrame > 0 ? 0 : 2;    
  92.     
  93.     [window showTitle:(int)holdFrame :(const char *)filename];
  94.  
  95.     return nil;
  96. }
  97.  
  98. - rightMouseDown:(NXEvent *)theEvent
  99. {
  100.     /* speed control */
  101.     int k;
  102.     unsigned char title[20]; 
  103.     
  104.     k = (theEvent->flags & NX_ALPHASHIFTMASK) ? -1 : 1;
  105.  
  106.     if(holdFrame == 0 || !showSpeed) frames_per_sec += k;
  107.     if(frames_per_sec < 0) frames_per_sec = 0;
  108.     showSpeed = 0;
  109.     
  110.     if(holdFrame > 0){    
  111.         /* display the frames/sec in the title bar */    
  112.         sprintf(title, "%d/sec", frames_per_sec); 
  113.         [window setTitle:(const char *)title];
  114.     }
  115.  
  116.     frame_interval = frames_per_sec>0 ? 1.0/(1.0*frames_per_sec) : 0.0001;
  117.  
  118.     return nil;
  119. }
  120. @end
  121.  
  122. @interface Controller:Object
  123. {
  124. }
  125. - windowWillClose:sender;
  126. - windowWillResize:sender toSize:(NXSize *)frameSize;
  127. - windowDidResize:sender;
  128. @end
  129.  
  130. @implementation Controller
  131.  
  132. - windowWillClose:sender
  133. {
  134.     /* window is about to close, so kill the program */
  135.     int_handler();        
  136. }
  137. - windowWillResize:sender toSize:(NXSize *)frameSize;
  138. {
  139.     /* this override makes sure that the window resizing preserves
  140.      * the movie's aspect ratio, and is quantized in scale units of .1 
  141.      */
  142.      
  143.     NXRect newRect;
  144.     NXSize fill, new;
  145.     unsigned char title[7];
  146.     float ratio, scale;    
  147.     
  148.     
  149.     
  150.     fill.width = windowRect.size.width - viewRect.size.width;
  151.     fill.height = windowRect.size.height - viewRect.size.height;
  152.     
  153.     new.width = frameSize->width - fill.width;
  154.     new.height = frameSize->height - fill.height;
  155.      
  156.     ratio = (float)new.width/(float)new.height;
  157.     
  158.     /* adjust aspect */
  159.     if(ratio >= videoRatio){/* crank up the height */
  160.         frameSize->height = fill.height + (float)new.width/videoRatio;
  161.     }
  162.     else if(ratio < videoRatio){/* crank up the width */    
  163.         frameSize->width = fill.width + (float)new.height*videoRatio;
  164.     }
  165.     
  166.     /* quantize size */
  167.     scale = ((float)frameSize->width/(float)pixelsWide);
  168.             
  169.     if(scale-scaleRatio > 0.1) scaleRatio +=0.1;
  170.     if(scale-scaleRatio < -0.1) scaleRatio -=0.1;
  171.     
  172.     ratio = scaleRatio/scale;
  173.     new.width = ratio*(frameSize->width - fill.width);
  174.     new.height = ratio*(frameSize->height - fill.height);
  175.     frameSize->width = new.width+fill.width;     
  176.     frameSize->height = new.height+fill.height;     
  177.             
  178.     /* display the scale in the title bar */    
  179.     sprintf(title, "%3.1f", scaleRatio); 
  180.     [window setTitle:(const char *)title];
  181.     
  182. }
  183. - windowDidResize:sender
  184. {
  185.     [window getFrame:&windowRect];
  186.     [cview getFrame:&viewRect];
  187.     viewRect.origin.x = viewRect.origin.y = 0.0;
  188.  
  189.     [window showTitle:(int)holdFrame :(const char *)filename];
  190.  
  191.     return self;
  192. }
  193.     
  194.  
  195.  
  196. @end
  197.  
  198. void InitDisplay(int w, int h)
  199. {
  200.     holdFrame = 1;
  201.     drawFrame = 0;
  202.     showSpeed = 1;
  203.     doRewind  = 0;
  204.  
  205.     NXApp = [Application new];
  206.  
  207.     viewRect.origin.x = 8.0 * 64.0;
  208.     viewRect.origin.y = 0.0; //512.0;
  209.  
  210.     pixelsHigh = h;
  211.     pixelsWide = w;
  212.     bytesPerRow = 4*16*((w +15)/16); 
  213.     /* 16 comes from macroblock mb_width = (h_size + 15) / 16 */
  214.  
  215.  
  216.     videoRatio = (float)w/(float)h;
  217.  
  218.     scaleRatio = 1.0;
  219.     
  220.     viewRect.size.width = (int)magFlag*w;
  221.     viewRect.size.height = (int)magFlag*h;
  222.  
  223.     window = [[CWindow alloc] initContent:&viewRect style:NX_RESIZEBARSTYLE
  224.         backing:NX_RETAINED buttonMask:NX_CLOSEBUTTONMASK defer:YES];
  225.     [window setDepthLimit:NX_TwentyFourBitRGBDepth];
  226.     [window useOptimizedDrawing:YES];
  227.     
  228.     [window showTitle:1 :(const char *)filename];
  229.  
  230.     [window setBackgroundGray:NX_BLACK];
  231.  
  232.     [NXApp beginModalSession:&session for:window];
  233.  
  234.     /* the movie's view */
  235.     viewRect.origin.x = viewRect.origin.y = 0.0;
  236.     cview = [[CView alloc] initFrame:&viewRect];    
  237.     [cview setClipping:NO];
  238.     [window setContentView:cview];    
  239.     
  240.     /* delegate (for button control) */
  241.     controller = [[Controller alloc] init];    
  242.     [window setDelegate:controller];
  243.  
  244.     [[window display] makeKeyAndOrderFront:nil];
  245.     
  246.     [NXApp runModalSession:&session];
  247.     
  248.     [window getFrame:&windowRect];
  249.     [cview getFrame:&viewRect];
  250.     viewRect.origin.x = viewRect.origin.y = 0.0;
  251.  
  252. }
  253.  
  254. void ExecuteDisplay(VidStream *vid_stream)
  255. {
  256.     /* process window server events */
  257.     [NXApp runModalSession:&session];
  258.  
  259.     if(holdFrame < 4){ /* no end of file error */
  260.         [window disableDisplay];
  261.         [cview lockFocus];                
  262.             NXDrawBitmap(&viewRect, pixelsWide, pixelsHigh,
  263.             8, 3, 32, bytesPerRow, NO, NO, NX_RGBColorSpace,
  264.             (const unsigned char *const*)&vid_stream->current->display);        
  265.             DPSFlush();
  266.         [cview unlockFocus];
  267.         [window reenableDisplay];
  268.     }
  269.     else holdFrame =3;
  270.        
  271.     if(frames_per_sec == 0 && holdFrame == 0) holdFrame = 1;
  272.  
  273.     if(holdFrame>0 || drawFrame == 1) 
  274.         [window showTitle:(int)holdFrame :(const char *)filename];
  275.  
  276.  
  277.     while(holdFrame>0 && !drawFrame && !doRewind) 
  278.         [NXApp runModalSession:&session];
  279.  
  280.  
  281.   totNumFrames++;
  282. }
  283.  
  284. void TerminateDisplay()
  285. {
  286.     [window close];
  287. }
  288.  
  289.  
  290.