home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / goodies / qtfullscreen / qtfullscreen.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  6.3 KB  |  224 lines

  1. //////////
  2. //
  3. //    File:        QTFullScreen.c
  4. //
  5. //    Contains:    Functions to display full-screen QuickTime movies.
  6. //
  7. //    Written by:    Tim Monroe
  8. //
  9. //    Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <3>         04/30/99    rtm        added QTFullScreen_PlayMovieOnFullScreen, to play an existing
  14. //                                    movie full screen
  15. //       <2>         03/17/98    rtm        finally got back to this; now it's working on Mac and Windows
  16. //       <1>         12/22/97    rtm        first file
  17. //
  18. //    This file contains functions that illustrate how to play QuickTime movies full screen. The
  19. //    key elements to displaying full screen movies are the calls BeginFullScreen and EndFullScreen,
  20. //    introduced in QuickTime 2.5. In the function QTFullScreen_PlayOnFullScreen, we prompt the user
  21. //  for a movie, open that movie, configure it to play full screen, associate a movie controller,
  22. //    and then let the controller handle events. Your application should call the function
  23. //    QTFullScreen_EventLoopAction in its event loop (on MacOS) or when it gets idle events (on Windows).
  24. //
  25. //    In the function QTFullScreen_PlayMovieOnFullScreen, we take an existing movie and play it full
  26. //    screen; in this function, we use the Movie Toolbox to start the movie and to give it processor
  27. //    time.
  28. // 
  29. //////////
  30.  
  31. #include "QTFullScreen.h"
  32.  
  33. // gloabl variables
  34. WindowPtr                    gFullScreenWindow = NULL;        // the full-screen window
  35. MovieController                gMC = NULL;                        // movie controller for the full-screen window
  36. Ptr                            gRestoreState = NULL;            // restore state; used when closing the full-screen window
  37.  
  38.  
  39. //////////
  40. //
  41. // QTFullScreen_PlayOnFullScreen
  42. // Prompt the user for a movie and play it full screen.
  43. //
  44. //////////
  45.  
  46. OSErr QTFullScreen_PlayOnFullScreen (void)
  47. {
  48.     FSSpec                myFSSpec;
  49.     Movie                myMovie = NULL;
  50.     short                myRefNum = 0;
  51.     SFTypeList            myTypeList = {MovieFileType, 0, 0, 0};
  52.     StandardFileReply    myReply;
  53.     long                myFlags = fullScreenDontChangeMenuBar | fullScreenAllowEvents;
  54.     OSErr                myErr = noErr;
  55.     
  56.     StandardGetFilePreview(NULL, 1, myTypeList, &myReply);
  57.     if (!myReply.sfGood)
  58.         goto bail;
  59.     
  60.     // make an FSSpec record
  61.     FSMakeFSSpec(myReply.sfFile.vRefNum, myReply.sfFile.parID, myReply.sfFile.name, &myFSSpec);
  62.  
  63.     myErr = OpenMovieFile(&myFSSpec, &myRefNum, fsRdPerm);
  64.     if (myErr != noErr)
  65.         goto bail;
  66.  
  67.     // now fetch the first movie from the file
  68.     myErr = NewMovieFromFile(&myMovie, myRefNum, NULL, NULL, newMovieActive, NULL);
  69.     if (myErr != noErr)
  70.         goto bail;
  71.     
  72.     CloseMovieFile(myRefNum);
  73.  
  74.     // set up for full-screen display
  75.     myErr = BeginFullScreen(&gRestoreState, NULL, 0, 0, &gFullScreenWindow, NULL, myFlags); 
  76.  
  77. #if TARGET_OS_WIN32
  78.     // on Windows, set a window procedure for the new window and associate a port with that window
  79.     QTMLSetWindowWndProc(gFullScreenWindow, QTFullScreen_HandleMessages);
  80.     CreatePortAssociation(GetPortNativeWindow(gFullScreenWindow), NULL, 0L);
  81. #endif
  82.     
  83.     SetMovieGWorld(myMovie, (CGrafPtr)gFullScreenWindow, GetGWorldDevice((CGrafPtr)gFullScreenWindow));
  84.     SetMovieBox(myMovie, &gFullScreenWindow->portRect);
  85.  
  86.     // create the movie controller
  87.     gMC = NewMovieController(myMovie, &gFullScreenWindow->portRect, 0);
  88.  
  89. bail:
  90.     return(myErr);
  91. }
  92.  
  93.  
  94. //////////
  95. //
  96. // QTFullScreen_RestoreScreen
  97. //
  98. //////////
  99.  
  100. OSErr QTFullScreen_RestoreScreen (void)
  101. {
  102.     OSErr        myErr = noErr;
  103.     
  104. #if TARGET_OS_WIN32    
  105.     DestroyPortAssociation((CGrafPtr)gFullScreenWindow);
  106. #endif
  107.  
  108.     DisposeMovieController(gMC);
  109.     myErr = EndFullScreen(gRestoreState, 0L); 
  110.     
  111.     return(myErr);
  112. }
  113.  
  114.  
  115. //////////
  116. //
  117. // QTFullScreen_EventLoopAction
  118. // Do any required event loop action processing.
  119. //
  120. //////////
  121.  
  122. OSErr QTFullScreen_EventLoopAction (EventRecord *theEvent)
  123. {
  124.     return(MCIsPlayerEvent(gMC, theEvent));
  125. }
  126.  
  127.  
  128. #if TARGET_OS_WIN32
  129. //////////
  130. //
  131. // QTFullScreen_HandleMessages
  132. // Handle Windows messages for the full-screen window.
  133. // 
  134. //////////
  135.  
  136. LRESULT CALLBACK QTFullScreen_HandleMessages (HWND theWnd, UINT theMessage, UINT wParam, LONG lParam)
  137. {
  138.     MSG                myMsg = {0};
  139.     EventRecord        myMacEvent;
  140.     LONG            myPoints = GetMessagePos();
  141.  
  142.     myMsg.hwnd = theWnd;
  143.     myMsg.message = theMessage;
  144.     myMsg.wParam = wParam;
  145.     myMsg.lParam = lParam;
  146.     myMsg.time = GetMessageTime();
  147.     myMsg.pt.x = LOWORD(myPoints);
  148.     myMsg.pt.y = HIWORD(myPoints);
  149.  
  150.     // translate the Windows message to a Mac event
  151.     WinEventToMacEvent(&myMsg, &myMacEvent);
  152.  
  153.     // pass the Mac event to the movie controller
  154.     QTFullScreen_EventLoopAction(&myMacEvent);
  155.         
  156.     return(DefWindowProc(theWnd, theMessage, wParam, lParam));
  157. }
  158. #endif    // TARGET_OS_WIN32
  159.  
  160.  
  161. //////////
  162. //
  163. // QTFullScreen_MoviePrePrerollCompleteProc
  164. // A completion procedure for pre-prerolling movies.
  165. //
  166. //////////
  167.  
  168. PASCAL_RTN void QTFullScreen_MoviePrePrerollCompleteProc (Movie theMovie, OSErr thePrerollErr, void *theRefcon)
  169. {
  170. #pragma unused(thePrerollErr, theRefcon)
  171.     StartMovie(theMovie);
  172. }
  173.  
  174.  
  175. //////////
  176. //
  177. // QTFullScreen_PlayMovieOnFullScreen
  178. // Play the specified movie full screen (without a movie controller).
  179. //
  180. //////////
  181.  
  182. OSErr QTFullScreen_PlayMovieOnFullScreen (Movie theMovie)
  183. {
  184.     long                myFlags = fullScreenAllowEvents;
  185.     GWorldPtr            myOrigGWorld = NULL;
  186.     Rect                myRect;
  187.     OSErr                myErr = noErr;
  188.     
  189.     StopMovie(theMovie);
  190.     
  191.     // set up for full-screen display
  192.     myErr = BeginFullScreen(&gRestoreState, NULL, 0, 0, &gFullScreenWindow, NULL, myFlags); 
  193.  
  194. #if TARGET_OS_WIN32
  195.     // on Windows, set a window procedure for the new window and associate a port with that window
  196.     QTMLSetWindowWndProc(gFullScreenWindow, QTFullScreen_HandleMessages);
  197.     CreatePortAssociation(GetPortNativeWindow(gFullScreenWindow), NULL, 0L);
  198. #endif
  199.     
  200.     GetMovieBox(theMovie, &myRect);
  201.     GetMovieGWorld(theMovie, &myOrigGWorld, NULL);
  202.     SetMovieGWorld(theMovie, (CGrafPtr)gFullScreenWindow, GetGWorldDevice((CGrafPtr)gFullScreenWindow));
  203.     SetMovieBox(theMovie, &gFullScreenWindow->portRect);
  204.             
  205.     PrePrerollMovie(theMovie, GetMovieTime(theMovie, NULL), GetMoviePreferredRate(theMovie), NewMoviePrePrerollCompleteProc(QTFullScreen_MoviePrePrerollCompleteProc), (void *)0L);    
  206.  
  207.     // play the movie through until the end; of course, a real application would probably want
  208.     // to call MoviesTask in its idle routine instead of just looping mindlessly here; this is
  209.     // left as an exercise for the reader
  210.     while (!IsMovieDone(theMovie))
  211.         MoviesTask(theMovie, 0L);
  212.  
  213.     StopMovie(theMovie);
  214.     SetMovieGWorld(theMovie, myOrigGWorld, NULL);
  215.     
  216.     myErr = EndFullScreen(gRestoreState, 0L); 
  217.     
  218.     SetMovieBox(theMovie, &myRect);
  219.  
  220. bail:
  221.     return(myErr);
  222. }
  223.  
  224.