home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / AVIWrite.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  4.9 KB  |  188 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20. #include "AVIWrite.h"
  21.  
  22. AVIWrite::AVIWrite()
  23. {
  24.   m_failed = false;
  25.   m_file = NULL;
  26.   m_stream = NULL;
  27.   m_streamCompressed = NULL;
  28.   m_streamSound = NULL;
  29.   m_samplesSound = 0;
  30.   
  31.   AVIFileInit();
  32. }
  33.  
  34. AVIWrite::~AVIWrite()
  35. {
  36.   if(m_streamSound)
  37.     AVIStreamClose(m_streamSound);
  38.  
  39.   if(m_streamCompressed)
  40.     AVIStreamClose(m_streamCompressed);
  41.   
  42.   if(m_stream)
  43.     AVIStreamClose(m_stream);
  44.  
  45.   if(m_file)
  46.     AVIFileClose(m_file);
  47.  
  48.   AVIFileExit();
  49. }
  50.  
  51. void AVIWrite::SetVideoFormat(BITMAPINFOHEADER *bh)
  52. {
  53.   // force size to 0x28 to avoid extra fields
  54.   memcpy(&m_bitmap, bh, 0x28);
  55. }
  56.  
  57. void AVIWrite::SetSoundFormat(WAVEFORMATEX *format)
  58. {
  59.   memcpy(&m_soundFormat, format, sizeof(WAVEFORMATEX));
  60.   ZeroMemory(&m_soundHeader, sizeof(AVISTREAMINFO));
  61.   // setup the sound stream header
  62.   m_soundHeader.fccType = streamtypeAUDIO;
  63.   m_soundHeader.dwQuality = (DWORD)-1;
  64.   m_soundHeader.dwScale = format->nBlockAlign;
  65.   m_soundHeader.dwInitialFrames = 1;
  66.   m_soundHeader.dwRate = format->nAvgBytesPerSec;
  67.   m_soundHeader.dwSampleSize = format->nBlockAlign;
  68.   
  69.   // create the sound stream
  70.   if(FAILED(AVIFileCreateStream(m_file, &m_streamSound, &m_soundHeader))) {
  71.     m_failed = true;
  72.     return;
  73.   }
  74.   
  75.   // setup the sound stream format
  76.   if(FAILED(AVIStreamSetFormat(m_streamSound, 0 , (void *)&m_soundFormat,
  77.                                sizeof(WAVEFORMATEX)))) {
  78.     m_failed = true;
  79.     return;
  80.   }
  81. }
  82.  
  83. bool AVIWrite::Open(const char *filename)
  84. {
  85.   // create the AVI file
  86.   if(FAILED(AVIFileOpen(&m_file,
  87.                         filename,
  88.                         OF_WRITE | OF_CREATE,
  89.                         NULL))) {
  90.     m_failed = true;
  91.     return false;
  92.   }
  93.   // setup the video stream information
  94.   ZeroMemory(&m_header, sizeof(AVISTREAMINFO));
  95.   m_header.fccType = streamtypeVIDEO;
  96.   m_header.dwScale = 1;
  97.   m_header.dwRate = m_fps;
  98.   m_header.dwSuggestedBufferSize  = m_bitmap.biSizeImage;
  99.  
  100.   // create the video stream
  101.   if(FAILED(AVIFileCreateStream(m_file,
  102.                                 &m_stream,
  103.                                 &m_header))) {
  104.     m_failed = true;
  105.     return false;
  106.   }
  107.       
  108.   ZeroMemory(&m_options, sizeof(AVICOMPRESSOPTIONS));
  109.   m_arrayOptions[0] = &m_options;
  110.  
  111.   // call the dialog to setup the compress options to be used
  112.   if(!AVISaveOptions(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), 0, 1, &m_stream, m_arrayOptions)) {
  113.     m_failed = true;
  114.     return false;
  115.   }
  116.   
  117.   // create the compressed stream
  118.   if(FAILED(AVIMakeCompressedStream(&m_streamCompressed, m_stream, &m_options, NULL))) {
  119.     m_failed = true;
  120.     return false;
  121.   }
  122.   
  123.   // setup the video stream format
  124.   if(FAILED( AVIStreamSetFormat(m_streamCompressed, 0,
  125.                                 &m_bitmap,
  126.                                 m_bitmap.biSize +
  127.                                 m_bitmap.biClrUsed * sizeof(RGBQUAD)))) {
  128.     m_failed = true;
  129.     return false;
  130.   }
  131.  
  132.   return true;
  133. }
  134.  
  135. bool AVIWrite::AddSound(const char *sound, int len)
  136. {
  137.   // return if we failed somewhere already
  138.   if(m_failed)
  139.     return false;
  140.  
  141.   int samples = len / m_soundFormat.nBlockAlign;
  142.  
  143.   if(FAILED(AVIStreamWrite(m_streamSound,
  144.                            m_samplesSound,
  145.                            samples,
  146.                            (LPVOID)sound,
  147.                            len,
  148.                            0,
  149.                            NULL,
  150.                            NULL))) {
  151.     m_failed = true;
  152.     return false;
  153.   }
  154.   m_samplesSound += samples;
  155.  
  156.   return true;
  157. }
  158.  
  159. bool AVIWrite::AddFrame(const int frame, const char *bmp)
  160. {
  161.   if (m_failed)
  162.     return false;
  163.   
  164.   // write the frame to the video stream
  165.   if(FAILED(AVIStreamWrite(m_streamCompressed,
  166.                            frame,
  167.                            1,
  168.                            (LPVOID)bmp,
  169.                            m_bitmap.biSizeImage,
  170.                            AVIIF_KEYFRAME,
  171.                            NULL,
  172.                            NULL))) {
  173.     m_failed = true;
  174.     return false;
  175.   }
  176.   return true;
  177. }
  178.  
  179. bool AVIWrite::IsSoundAdded()
  180. {
  181.   return m_streamSound != NULL;
  182. }
  183.  
  184. void AVIWrite::SetFPS(int f)
  185. {
  186.   m_fps = f;
  187. }
  188.