1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00
planetblupi/src/movie.cpp

281 lines
6.0 KiB
C++
Raw Normal View History

2017-01-21 17:27:46 +01:00
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
2017-01-21 17:27:46 +01:00
#include "def.h"
#include "blupi.h"
2017-01-21 17:27:46 +01:00
#include "movie.h"
#include "misc.h"
#include "event.h"
#include "kitchensink/kitchensink.h"
2017-01-21 17:27:46 +01:00
// Initialize avi libraries.
bool CMovie::initAVI()
2017-01-21 17:27:46 +01:00
{
// Initialize Kitchensink with network support and all formats.
Sint32 err = Kit_Init (KIT_INIT_FORMATS);
if (err != 0)
{
fprintf (stderr, "Unable to initialize Kitchensink: %s", Kit_GetError());
return false;
}
return true;
2017-01-21 17:27:46 +01:00
}
// Closes the opened AVI file and the opened device type. |
void CMovie::termAVI()
{
Kit_Quit();
2017-01-21 17:27:46 +01:00
}
// Close the movie and anything associated with it. |
// This function clears the <m_fPlaying> and <m_fMovieOpen> flags |
2017-01-21 17:27:46 +01:00
2017-02-05 09:15:08 +01:00
void CMovie::fileCloseMovie()
2017-01-21 17:27:46 +01:00
{
m_fPlaying = false; // can't be playing any longer
m_fMovieOpen = false; // no more movies open
if (m_videoTex)
{
SDL_DestroyTexture (m_videoTex);
m_videoTex = nullptr;
}
if (m_player)
{
SDL_CloseAudioDevice (m_audioDev);
Kit_ClosePlayer (m_player);
m_player = nullptr;
}
if (m_movie)
{
Kit_CloseSource (m_movie);
m_movie = nullptr;
}
2017-01-21 17:27:46 +01:00
}
// Open an AVI movie. Use CommDlg open box to
// open and then handle the initialization to
// show the movie and position it properly. Keep
// the movie paused when opened.
// Sets <m_fMovieOpen> on success.
bool CMovie::fileOpenMovie (RECT rect, const char *pFilename)
2017-01-21 17:27:46 +01:00
{
const auto path = GetBaseDir() + pFilename;
// we got a filename, now close any old movie and open the new one. */
if (m_fMovieOpen)
fileCloseMovie();
// Open up the sourcefile.
// This can be a local file, network url, ...
m_movie = Kit_CreateSourceFromUrl (path.c_str());
if (m_movie)
{
// Create the player
m_player = Kit_CreatePlayer (m_movie);
if (m_player == nullptr)
return false;
pinfo = new Kit_PlayerInfo;
Kit_GetPlayerInfo (m_player, pinfo);
SDL_AudioSpec wanted_spec, audio_spec;
SDL_memset (&wanted_spec, 0, sizeof (wanted_spec));
wanted_spec.freq = pinfo->audio.samplerate;
wanted_spec.format = pinfo->audio.format;
wanted_spec.channels = pinfo->audio.channels;
m_audioDev = SDL_OpenAudioDevice (nullptr, 0, &wanted_spec, &audio_spec, 0);
SDL_PauseAudioDevice (m_audioDev, 0);
m_videoTex = SDL_CreateTexture (
g_renderer,
pinfo->video.format,
SDL_TEXTUREACCESS_STATIC,
pinfo->video.width,
pinfo->video.height
);
if (m_videoTex == nullptr)
return false;
return true;
}
else
{
// generic error for open
m_fMovieOpen = false;
return false;
}
2017-01-21 17:27:46 +01:00
}
// Play/pause the movie depending on the state
2017-02-05 18:25:39 +01:00
void CMovie::playMovie()
2017-01-21 17:27:46 +01:00
{
m_fPlaying = !m_fPlaying; // swap the play flag
// play/pause the AVI movie
if (m_fPlaying)
{
SDL_RenderSetLogicalSize (g_renderer, pinfo->video.width, pinfo->video.height);
Kit_PlayerPlay (m_player);
}
else
Kit_PlayerPause (m_player);
2017-01-21 17:27:46 +01:00
}
CMovie::CMovie()
{
m_bEnable = false;
m_fPlaying = false;
m_fMovieOpen = false;
m_movie = nullptr;
m_player = nullptr;
m_videoTex = nullptr;
pinfo = nullptr;
2017-02-12 18:06:36 +01:00
m_ret = 0;
2017-01-21 17:27:46 +01:00
}
CMovie::~CMovie()
{
termAVI();
2017-01-21 17:27:46 +01:00
}
// Ouvre la librairie avi.
bool CMovie::Create()
2017-01-21 17:27:46 +01:00
{
if (initAVI())
{
m_bEnable = true;
return true;
}
else
{
m_bEnable = false;
return false;
}
2017-01-21 17:27:46 +01:00
}
2017-02-12 00:44:46 +01:00
// Retourne l'état de DirectMovie.
2017-01-21 17:27:46 +01:00
bool CMovie::GetEnable()
2017-01-21 17:27:46 +01:00
{
return m_bEnable;
2017-01-21 17:27:46 +01:00
}
// Indique si un film existe.
bool CMovie::IsExist (const char *pFilename)
2017-01-21 17:27:46 +01:00
{
const auto path = GetBaseDir() + pFilename;
FILE *file;
2017-01-21 17:27:46 +01:00
file = fopen (path.c_str(), "rb");
if (file == nullptr)
return false;
2017-01-21 17:27:46 +01:00
fclose (file);
return true;
2017-01-21 17:27:46 +01:00
}
// Montre un film avi.
bool CMovie::Play (RECT rect, const char *pFilename)
2017-01-21 17:27:46 +01:00
{
if (!m_bEnable)
return false;
if (!fileOpenMovie (rect, pFilename))
return false;
2017-02-12 19:05:05 +01:00
playMovie();
CEvent::PushUserEvent (WM_MOVIE_PLAY);
return true;
2017-01-21 17:27:46 +01:00
}
// Stoppe le film avi.
2017-02-05 09:15:08 +01:00
void CMovie::Stop()
2017-01-21 17:27:46 +01:00
{
if (!m_bEnable)
return;
fileCloseMovie();
SDL_RenderSetLogicalSize (g_renderer, LXIMAGE, LYIMAGE);
}
void CMovie::Pause()
{
if (!m_bEnable || !m_fPlaying)
return;
if (Kit_GetPlayerState (m_player) != KIT_PLAYING)
return;
Kit_PlayerPause (m_player);
}
void CMovie::Resume()
{
if (!m_bEnable || !m_fPlaying)
return;
if (Kit_GetPlayerState (m_player) != KIT_PAUSED)
return;
Kit_PlayerPlay (m_player);
}
bool CMovie::Render()
{
if (!m_bEnable || !m_fPlaying)
return false;
if (Kit_GetPlayerState (m_player) == KIT_STOPPED)
return false;
// Refresh audio
if (SDL_GetQueuedAudioSize (m_audioDev) < AUDIOBUFFER_SIZE)
{
Sint32 need = AUDIOBUFFER_SIZE - m_ret;
SDL_LockAudio();
while (need > 0)
{
m_ret = Kit_GetAudioData (m_player, (unsigned char *) m_audiobuf,
AUDIOBUFFER_SIZE, (size_t) SDL_GetQueuedAudioSize (m_audioDev));
need -= m_ret;
if (m_ret > 0)
SDL_QueueAudio (m_audioDev, m_audiobuf, m_ret);
else
break;
}
SDL_UnlockAudio();
SDL_PauseAudioDevice (m_audioDev, 0);
}
// Clear screen with black
SDL_SetRenderDrawColor (g_renderer, 0, 0, 0, 255);
SDL_RenderClear (g_renderer);
// Refresh videotexture and render it
Kit_GetVideoData (m_player, m_videoTex);
SDL_RenderCopy (g_renderer, m_videoTex, nullptr, nullptr);
SDL_RenderPresent (g_renderer);
CEvent::PushUserEvent (WM_MOVIE_PLAY);
return true;
2017-01-21 17:27:46 +01:00
}