Browse Source

Better API

Josh Yelon 18 years ago
parent
commit
00893f9f14
3 changed files with 64 additions and 66 deletions
  1. 23 21
      panda/src/movies/movieVideo.I
  2. 36 38
      panda/src/movies/movieVideo.cxx
  3. 5 7
      panda/src/movies/movieVideo.h

+ 23 - 21
panda/src/movies/movieVideo.I

@@ -37,41 +37,43 @@ size_y() const {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: MovieVideo::frame_start
+//     Function: MovieVideo::at_end
 //       Access: Published
 //       Access: Published
-//  Description: MovieVideo streams have variable frame rates.  Each
-//               frame will specify how long it is to be displayed.
-//               These lengths may not be equal from frame to frame.
-//
-//               The frame_start function returns the time at which
-//               the current frame should start being displayed.  This
-//               time is relative to the start of the stream.
+//  Description: Returns true if there is no video left to fetch.
+//               
+//               It is not an error to call 'fetch' when at_end, 
+//               doing so will yield a black frame of one second
+//               duration.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-INLINE double MovieVideo::
-frame_start() const {
-  return _frame_start;
+INLINE bool MovieVideo::
+at_end() const {
+  return _at_end;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: MovieVideo::frame_end
+//     Function: MovieVideo::next_start
 //       Access: Published
 //       Access: Published
-//  Description: Return the time at which this frame should stop
-//               being displayed.  This time is relative to the start
-//               of the stream.
+//  Description: Indicates the time at which the current frame should
+//               stop being displayed and when the next frame should
+//               start being displayed.  This time is relative to the
+//               start of the stream.
 //
 //
-//               The frame_end function returns the time at which
-//               the current frame should stop being displayed.  This
-//               time is relative to the start of the stream.
+//               MovieVideo streams have variable frame rates.  Each
+//               frame will specify how long it is to be displayed.
+//               These lengths may not be equal from frame to frame.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE double MovieVideo::
 INLINE double MovieVideo::
-frame_end() const {
-  return _frame_end;
+next_start() const {
+  return _next_start;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: MovieVideo::approx_len
 //     Function: MovieVideo::approx_len
 //       Access: Published
 //       Access: Published
-//  Description: Get the vertical size of the movie.
+//  Description: Get the approximate length of the movie.
+//               This is not guaranteed to be accurate. The only
+//               accurate way to determine when the movie is over
+//               is to check the at_end flag.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE double MovieVideo::
 INLINE double MovieVideo::
 approx_len() const {
 approx_len() const {

+ 36 - 38
panda/src/movies/movieVideo.cxx

@@ -25,24 +25,24 @@ TypeHandle MovieVideo::_type_handle;
 //     Function: MovieVideo::Constructor
 //     Function: MovieVideo::Constructor
 //       Access: Published
 //       Access: Published
 //  Description: This constructor returns a null video stream --- a
 //  Description: This constructor returns a null video stream --- a
-//               stream of plain blue frames that last 1 second each.
-//               To get more interesting video, you need to construct
-//               a subclass of this class.
+//               stream of plain blue and white frames that last one
+//               second each. To get more interesting video, you need
+//               to construct a subclass of this class.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 MovieVideo::
 MovieVideo::
 MovieVideo(const string &name, double len) :
 MovieVideo(const string &name, double len) :
   Namable(name),
   Namable(name),
   _size_x(1),
   _size_x(1),
   _size_y(1),
   _size_y(1),
+  _last_frame(false),
+  _next_start(0.0),
   _approx_len(len),
   _approx_len(len),
-  _frame_start(0.0),
-  _frame_end(1.0)
 {
 {
-  if (len <= 0.0) {
-    _approx_len = 1.0;
+  if (len < 0.0) {
+    _approx_len = 0.0;
   }
   }
-  if (_frame_end > _approx_len) {
-    _frame_end = _approx_len;
+  if (_approx_len == 0.0) {
+    _at_end = true;
   }
   }
 }
 }
 
 
@@ -56,44 +56,42 @@ MovieVideo::
 }
 }
  
  
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: MovieVideo::load_image
+//     Function: MovieVideo::fetch_into
 //       Access: Published, Virtual
 //       Access: Published, Virtual
-//  Description: Copy the current frame into a texture's ram image.
+//  Description: Load the next frame into a texture's ram image.
+//               Advances the frame pointer.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void MovieVideo::
 void MovieVideo::
-load_image(Texture *t) {
+fetch_into(Texture *t) {
 
 
   // The following is the implementation of the null video
   // The following is the implementation of the null video
   // stream --- a stream of solid blue frames.  Normally,
   // stream --- a stream of solid blue frames.  Normally,
   // this method will be overridden by the subclass.
   // this method will be overridden by the subclass.
   
   
-  if (_ram_image==0) {
-    _ram_image = PTA_uchar::empty_array(4);
-    _ram_image.set_element(0,128);
-    _ram_image.set_element(1,128);
-    _ram_image.set_element(2,255);
-    _ram_image.set_element(3,255);
-  }
   t->setup_texture(Texture::TT_2d_texture, 1, 1, 1,
   t->setup_texture(Texture::TT_2d_texture, 1, 1, 1,
                    Texture::T_unsigned_byte, Texture::F_rgba);
                    Texture::T_unsigned_byte, Texture::F_rgba);
-  t->set_ram_image(_ram_image);
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: MovieVideo::next_frame
-//       Access: Published, Virtual
-//  Description: Advances to the next frame.
-////////////////////////////////////////////////////////////////////
-void MovieVideo::
-next_frame() {
-
-  // The following is the implementation of the null video
-  // stream --- a stream of solid blue frames.  Normally,
-  // this method will be overridden by the subclass.
-
-  _frame_start = _frame_end;
-  _frame_end = _frame_end + 1.0;
-  if (_frame_end > _approx_len) {
-    _frame_end = _approx_len;
+  PTA_uchar *img = t->modify_ram_image();
+  
+  int frame_index = (int)next_start;
+  if (at_end) {
+    img.set_element(0,0);
+    img.set_element(1,0);
+    img.set_element(2,0);
+    img.set_element(3,255);
+  } else if (frame_index & 1) {
+    img.set_element(0,128);
+    img.set_element(1,128);
+    img.set_element(2,255);
+    img.set_element(3,255);
+  } else {
+    img.set_element(0,255);
+    img.set_element(1,255);
+    img.set_element(2,255);
+    img.set_element(3,255);
+  }
+  
+  _next_start = _next_start + 1.0;
+  if (_next_start > _approx_len) {
+    _at_end = true;
   }
   }
 }
 }

+ 5 - 7
panda/src/movies/movieVideo.h

@@ -33,11 +33,10 @@ class EXPCL_PANDA_MOVIES MovieVideo : public TypedWritableReferenceCount, public
   MovieVideo(const string &name, double len);
   MovieVideo(const string &name, double len);
   INLINE int size_x() const;
   INLINE int size_x() const;
   INLINE int size_y() const;
   INLINE int size_y() const;
-  INLINE double frame_start() const;
-  INLINE double frame_end() const;
+  INLINE bool at_end() const;
   INLINE double approx_len() const;
   INLINE double approx_len() const;
-  virtual void load_image(Texture *t);
-  virtual void next_frame();
+  INLINE double next_start() const;
+  virtual void fetch_into(Texture *t);
   
   
  public:
  public:
   virtual ~MovieVideo();
   virtual ~MovieVideo();
@@ -45,10 +44,9 @@ class EXPCL_PANDA_MOVIES MovieVideo : public TypedWritableReferenceCount, public
  private:
  private:
   int _size_x;
   int _size_x;
   int _size_y;
   int _size_y;
-  double _frame_start;
-  double _frame_end;
+  bool _at_end;
+  double _next_start;
   double _approx_len;
   double _approx_len;
-  PTA_uchar _ram_image;
   
   
 public:
 public:
   static TypeHandle get_class_type() {
   static TypeHandle get_class_type() {