home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / SprocketExamples / SprocketInvaders / Source / MoviePlayback.c < prev    next >
Encoding:
Text File  |  1998-07-14  |  3.4 KB  |  148 lines  |  [TEXT/CWIE]

  1. //•    ----------------------------------------    Includes
  2.  
  3. #include <Movies.h>
  4.  
  5. #include "Graphics.h"
  6. #include "MoviePlayback.h"
  7.  
  8. //•    ----------------------------------------    Definitions
  9. //•    ----------------------------------------    Types
  10. //•    ----------------------------------------    Private Variables
  11.  
  12. static Movie    gTheMovie;
  13. static CGrafPtr    gMovieWindow;
  14. static short    gMovieRefNum;
  15. static Rect        gMovieBox;
  16.  
  17. //•    ----------------------------------------    Private Functions
  18.  
  19. static void CenterRectOnWindow(RectPtr theRect, CGrafPtr theWindow);
  20.  
  21. //•    ----------------------------------------    Public Variables
  22.  
  23. FSSpec    gMovieSpec;
  24.  
  25. //•    --------------------    PlaybackMovie
  26.  
  27. void
  28. PlaybackMovie(void)
  29. {
  30. GDHandle    movieDevice;
  31. OSErr    theErr;
  32. short    movieResID = 0;
  33. Boolean    wasChanged;
  34.  
  35.     GraphicsGetUnderlayGrafPort(&gMovieWindow, &movieDevice);
  36.  
  37.     theErr = OpenMovieFile(&gMovieSpec, &gMovieRefNum, fsRdPerm);
  38.     if (theErr)
  39.         return;
  40.         
  41.     theErr = NewMovieFromFile(&gTheMovie, gMovieRefNum, &movieResID, "\p", newMovieActive, &wasChanged);
  42.     if (theErr)
  43.         return;
  44.  
  45.     //•    Find the dimensions of the movie and translate them to a (0, 0) base
  46.     GetMovieBox(gTheMovie, &gMovieBox);
  47.     OffsetRect(&gMovieBox, -gMovieBox.left, -gMovieBox.top);
  48.     CenterRectOnWindow(&gMovieBox, gMovieWindow);
  49.  
  50.     SetMovieGWorld(gTheMovie, gMovieWindow, gGameGDH);
  51.     SetMovieBox(gTheMovie, &gMovieBox);
  52.     
  53.     //•    Rewind the movie
  54.     SetMovieTimeValue(gTheMovie, 0L);
  55.     
  56.     //•    Preroll the movie and start playback
  57.     MoviesTask(gTheMovie, 0);
  58.     MoviesTask(gTheMovie, 0);
  59.     StartMovie(gTheMovie);
  60.     
  61.     FlushEvents(everyEvent, 0);
  62.  
  63.     //•    Service the movie until it is finished.  Minimally look for key strokes and mouse clicks.
  64.     MoviesTask(gTheMovie, 0);
  65.     GraphicsSetUnderlayRectDirty(&gMovieBox);
  66. }
  67.  
  68. //•    --------------------    ServiceMoviePlayback
  69.  
  70. void
  71. ServiceMoviePlayback(void)
  72. {
  73.     if (gTheMovie)
  74.     {
  75.         //•    If the movie is finished then rewind it and start the playback over again
  76.         if (IsMovieDone(gTheMovie))
  77.         {
  78.             SetMovieTimeValue(gTheMovie, 0L);
  79.             StartMovie(gTheMovie);
  80.         }
  81.  
  82.         //•    Otherwise, service the movie and update the underlay buffer
  83.         MoviesTask(gTheMovie, 0);
  84.         GraphicsSetUnderlayRectDirty(&gMovieBox);
  85.     }
  86. }
  87.  
  88. //•    --------------------    ShutdownMoviePlayback
  89.  
  90. void
  91. ShutdownMoviePlayback(void)
  92. {
  93.     if (gTheMovie)
  94.     {
  95.         StopMovie(gTheMovie);
  96.         DisposeMovie(gTheMovie);
  97.         CloseMovieFile(gMovieRefNum);
  98.         
  99.         gTheMovie = 0;
  100.     }
  101. }
  102.  
  103. //•    --------------------    CenterRectOnWindow
  104.  
  105. static void
  106. CenterRectOnWindow(RectPtr theRect, CGrafPtr theWindow)
  107. {
  108. short    newH, newV;
  109. Rect        wRect;
  110.  
  111.     wRect = theWindow->portRect;
  112.  
  113.     //•    Zero the origin of the rects
  114.     OffsetRect(&wRect, -wRect.left, -wRect.top);
  115.     OffsetRect(theRect, -theRect->left, -theRect->top);
  116.  
  117.     //•    Find the difference between the two rects' sizes
  118.     newH = (wRect.right - wRect.left) - (theRect->right - theRect->left);
  119.     newV = (wRect.bottom - wRect.top) - (theRect->bottom - theRect->top);
  120.  
  121.     //•    Half the difference so that it's centered top-to-bottom and left-to-right
  122.     newH >>= 1;
  123.     newV >>= 1;
  124.  
  125.     //•    Add that offset to the upper-left of the monitor
  126.     newH += wRect.left;
  127.     newV += wRect.top;
  128.  
  129.     //•    Center the rect
  130.     OffsetRect(theRect, newH, newV);
  131. }
  132.  
  133. //•    --------------------    SelectBackgroundMovie
  134.  
  135. void
  136. SelectBackgroundMovie(void)
  137. {
  138. SFTypeList            typeList = { MovieFileType };
  139. StandardFileReply    theReply;
  140.  
  141.     StandardGetFile(nil, 1, typeList, &theReply);
  142.  
  143.     if (! theReply.sfGood)
  144.         return;
  145.         
  146.     BlockMoveData(&theReply.sfFile, &gMovieSpec, sizeof (FSSpec));
  147. }
  148.