tighten up scripting

main
cosmonaut 2021-11-22 21:14:16 -08:00
parent c5c5ed6c18
commit 160d10233e
2 changed files with 57 additions and 20 deletions

View File

@ -1,3 +1,4 @@
video = Theorafile_Open("celery_man.ogg");
Theorafile_SetLoop(video, true);
Theorafile_Play(video);

View File

@ -8,7 +8,7 @@ enum Theorafile_VideoState
function Theorafile_Open(filename)
{
var _handle = ptr(TheorafileGMS_Open(filename));
show_debug_message("handle: " + string(_handle));
show_debug_message("Successfully loaded Theorafile video with handle: " + string(_handle));
var _yWidth = TheorafileGMS_Width(_handle);
var _yHeight = TheorafileGMS_Height(_handle);
@ -33,7 +33,7 @@ function Theorafile_Open(filename)
vertex_texcoord(_vbuff, 0, 2);
vertex_end(_vbuff);
return {
var _video = {
handle: _handle,
bufferSize: _bufferSize,
yuvBuffer: buffer_create(_bufferSize, buffer_fixed, 4),
@ -49,10 +49,17 @@ function Theorafile_Open(filename)
elapsed_time: 0,
currentFrame: 0,
state: Theorafile_VideoState.Stopped,
looped: false,
looping: false,
loopCounter: 0,
timesToLoop: 0,
triangle_buffer: _vbuff,
triangle_buffer_format: _vformat
};
// grab the first frame ASAP
while (TheorafileGMS_ReadVideo(_video.handle, buffer_get_address(_video.yuvBuffer), 1) == 0) { }
return _video;
}
function Theorafile_Close(video)
@ -61,7 +68,7 @@ function Theorafile_Close(video)
surface_free(video.uSurface);
surface_free(video.vSurface);
surface_free(video.videoSurface);
//buffer_delete(video.yuvBuffer);
buffer_delete(video.yuvBuffer);
vertex_format_delete(video.triangle_buffer_format);
vertex_delete_buffer(video.triangle_buffer);
TheorafileGMS_Close(video.handle);
@ -69,17 +76,43 @@ function Theorafile_Close(video)
function Theorafile_Play(video)
{
if (video.state == Theorafile_VideoState.Stopped)
if (video.state != Theorafile_VideoState.Playing)
{
if (video.handle != 0 && TheorafileGMS_HasVideo(video.handle) == 1)
{
video.elapsed_time = 0;
video.state = Theorafile_VideoState.Playing;
TheorafileGMS_ReadVideo(video.handle, buffer_get_address(video.yuvBuffer), 1);
}
}
}
function Theorafile_SetLoop(video, loop = true, times = 10000000000000)
{
video.looping = loop;
video.loopCounter = 0;
video.timesToLoop = times;
}
function Theorafile_Reset(video)
{
TheorafileGMS_Reset(video.handle);
// grab the first frame ASAP
while (TheorafileGMS_ReadVideo(video.handle, buffer_get_address(video.yuvBuffer), 1) == 0) { }
video.elapsed_time = 0;
video.currentFrame = 0;
}
function Theorafile_Pause(video)
{
video.state = Theorafile_VideoState.Paused;
}
function Theorafile_Stop(video)
{
Theorafile_Reset(video);
video.state = Theorafile_VideoState.Stopped;
}
function Theorafile_UpdateTextures(video)
{
if (!surface_exists(video.ySurface))
@ -111,11 +144,11 @@ function Theorafile_UpdateSurface(video)
{
video.elapsed_time += 1 / room_speed;
var _current_frame = video.elapsed_time * video.framesPerSecond;
var _current_frame = floor(video.elapsed_time * video.framesPerSecond);
if (floor(_current_frame) > video.currentFrame)
if (_current_frame > video.currentFrame)
{
if TheorafileGMS_ReadVideo(video.handle, buffer_get_address(video.yuvBuffer), 1) == 1
if TheorafileGMS_ReadVideo(video.handle, buffer_get_address(video.yuvBuffer), _current_frame - video.currentFrame) >= 1
{
Theorafile_UpdateTextures(video);
@ -140,21 +173,24 @@ function Theorafile_UpdateSurface(video)
surface_reset_target();
}
video.currentFrame = floor(_current_frame);
video.currentFrame = _current_frame;
}
var _ended = TheorafileGMS_EndOfStream(video.handle);
if (_ended)
{
video.elapsed_time = 0;
video.most_recent_time = 0;
show_debug_message("ended!");
Theorafile_Reset(video);
TheorafileGMS_Reset(video.handle);
if (video.looped)
if (video.looping)
{
video.most_recent_time = get_timer();
video.loopCounter += 1;
if video.loopCounter > video.timesToLoop
{
video.looping = false;
video.state = Theorafile_VideoState.Stopped;
}
}
else
{