WiiFlow_Lite/source/gui/WiiMovie.cpp

175 lines
3.9 KiB
C++
Raw Normal View History

2012-01-21 21:57:41 +01:00
/***************************************************************************
* Copyright (C) 2010 by Dimok
* (C) 2014 by FIX94
2012-01-21 21:57:41 +01:00
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
* WiiMovie.cpp
*
* for WiiXplorer 2010
***************************************************************************/
#include <unistd.h>
#include "memory/mem2.hpp"
2012-01-21 21:57:41 +01:00
#include "WiiMovie.hpp"
#include "gecko/gecko.hpp"
2012-01-21 21:57:41 +01:00
WiiMovie movie;
void WiiMovie::Init(const char *filepath)
2012-01-21 21:57:41 +01:00
{
Frame = NULL;
BufferPos = 0;
Buffer[0].thread = false;
Buffer[1].thread = false;
VideoFrameCount = 0;
2012-01-21 21:57:41 +01:00
fps = 0.0f;
ExitRequested = false;
Playing = false;
2012-01-21 21:57:41 +01:00
ThreadStack = NULL;
//gprintf("Opening video '%s'\n", filepath);
ReadThread = LWP_THREAD_NULL;
vFile = fopen(filepath, "rb");
if(!vFile)
{
//gprintf("Open video failed\n");
ExitRequested = true;
2012-01-21 21:57:41 +01:00
return;
}
if(Video.Init(vFile) == false)
2012-01-21 21:57:41 +01:00
{
//gprintf("Memory Allocation failed\n");
ExitRequested = true;
2012-01-21 21:57:41 +01:00
return;
}
fps = Video.getFps();
inited = true;
2012-01-21 21:57:41 +01:00
}
void WiiMovie::DeInit()
2012-01-21 21:57:41 +01:00
{
if(inited == false)
return;
inited = false;
//gprintf("Destructing WiiMovie object\n");
Playing = false;
ExitRequested = true;
2012-01-21 21:57:41 +01:00
Stop();
if(ReadThread != LWP_THREAD_NULL)
2012-01-21 21:57:41 +01:00
{
LWP_ResumeThread(ReadThread);
LWP_JoinThread(ReadThread, NULL);
}
if(ThreadStack != NULL)
2012-01-21 21:57:41 +01:00
{
MEM2_lo_free(ThreadStack);
2012-01-21 21:57:41 +01:00
ThreadStack = NULL;
}
Video.DeInit();
if(vFile != NULL)
fclose(vFile);
VideoF.dealloc();
vFile = NULL;
Frame = NULL;
TexHandle.Cleanup(Buffer[0]);
TexHandle.Cleanup(Buffer[1]);
Buffer[0].thread = false;
Buffer[1].thread = false;
BufferPos = 0;
2012-01-21 21:57:41 +01:00
}
bool WiiMovie::Play(bool loop)
2012-01-21 21:57:41 +01:00
{
if(!vFile)
return false;
ThreadStack = (u8*)MEM2_lo_alloc(32768);
if(!ThreadStack)
2012-05-13 19:25:26 +02:00
return false;
2012-01-21 21:57:41 +01:00
//gprintf("Start playing video\n");
Video.loop = loop;
PlayTime.reset();
Playing = true;
Buffer[0].thread = false;
Buffer[1].thread = false;
LWP_CreateThread(&ReadThread, UpdateThread, this, ThreadStack, 32768, 63);
//gprintf("Reading frames thread started\n");
2012-01-21 21:57:41 +01:00
return true;
}
void WiiMovie::Stop()
{
//gprintf("Stopping WiiMovie video\n");
ExitRequested = true;
2012-01-21 21:57:41 +01:00
}
void * WiiMovie::UpdateThread(void *arg)
{
WiiMovie *movie = static_cast<WiiMovie *>(arg);
while(!movie->ExitRequested)
2012-01-21 21:57:41 +01:00
{
movie->ReadNextFrame();
if(movie->Buffer[movie->BufferPos].thread == false)
movie->LoadNextFrame();
else if(movie->Frame->thread == false)
{
movie->Frame = &movie->Buffer[movie->BufferPos];
movie->BufferPos ^= 1;
}
2012-01-21 21:57:41 +01:00
}
return NULL;
}
void WiiMovie::ReadNextFrame()
{
for(u32 FramesNeeded = (u32)(PlayTime.elapsed()*fps); VideoFrameCount <= FramesNeeded; ++VideoFrameCount)
{
if(VideoFrameCount == FramesNeeded)
Playing = Video.loadNextFrame();
else
Video.loadNextFrame(true);
}
2012-01-21 21:57:41 +01:00
}
void WiiMovie::LoadNextFrame()
{
if(!vFile || !Playing)
return;
Video.getCurrentFrame(VideoF);
if(!VideoF.data)
return;
TexData *CurFrame = &Buffer[BufferPos];
if(TexHandle.fromTHP(CurFrame, VideoF.data, VideoF.getWidth(), VideoF.getHeight()) == TE_OK)
{
CurFrame->thread = true;
Frame = CurFrame;
BufferPos ^= 1;
}
2012-01-21 21:57:41 +01:00
}
bool WiiMovie::Continue()
2012-01-21 21:57:41 +01:00
{
if(!vFile || !Playing)
return false;
2012-01-21 21:57:41 +01:00
return true;
}