From 160d10233e7e3c8f8ac1453a55dbcd88047f5fd9 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 22 Nov 2021 21:14:16 -0800 Subject: [PATCH] tighten up scripting --- .../objects/Renderer/Create_0.gml | 1 + .../TheorafileGMS_Scripts.gml | 76 ++++++++++++++----- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/gamemaker/TheorafileGMS/objects/Renderer/Create_0.gml b/gamemaker/TheorafileGMS/objects/Renderer/Create_0.gml index 3fb44aa..654dac7 100644 --- a/gamemaker/TheorafileGMS/objects/Renderer/Create_0.gml +++ b/gamemaker/TheorafileGMS/objects/Renderer/Create_0.gml @@ -1,3 +1,4 @@ video = Theorafile_Open("celery_man.ogg"); +Theorafile_SetLoop(video, true); Theorafile_Play(video); diff --git a/gamemaker/TheorafileGMS/scripts/TheorafileGMS_Scripts/TheorafileGMS_Scripts.gml b/gamemaker/TheorafileGMS/scripts/TheorafileGMS_Scripts/TheorafileGMS_Scripts.gml index df9aeaa..939d1e6 100644 --- a/gamemaker/TheorafileGMS/scripts/TheorafileGMS_Scripts/TheorafileGMS_Scripts.gml +++ b/gamemaker/TheorafileGMS/scripts/TheorafileGMS_Scripts/TheorafileGMS_Scripts.gml @@ -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)) @@ -108,14 +141,14 @@ function Theorafile_UpdateTextures(video) function Theorafile_UpdateSurface(video) { if (video.state == Theorafile_VideoState.Playing) - { +{ video.elapsed_time += 1 / room_speed; - - var _current_frame = video.elapsed_time * video.framesPerSecond; - - if (floor(_current_frame) > video.currentFrame) + + var _current_frame = floor(video.elapsed_time * video.framesPerSecond); + + 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 {