--Shader cinema FL 1.1.1
--original 1.0 based on nyan resource by Gamesnert

local shader_cinema
--which texture the effects needs to be applied to
local cinema_tex_name = "greyground256128"
--how many images are in shader_cinema folder (0 to 58)
local image_list = 1
--The song to be played in sync with the movie
--local cinemamusic = "sound/shishi.mp3"
--define the milisecond gab between images (the higher value the slower animation)
local frequencyTic = 40
--((image_list*frequencyTic)/1000) predicted "movie" length in sec,it should equal with sound length
-- don't touch what's below
local numberTex = { }
local numberId = 1
local getLastTick = getTickCount ( )
local getLastTock = getTickCount ( )
local totalTickCount,singleTickCount = 0,0
local frames,lastsec = 0,0,0

-- shader settings
local gUVScale = {-0.2, 0.4}
local gUVPosition = {0.2, 0.1}
local gUVAnim = {0, 0}
local gUVRotAngle = math.rad( 0 )
local gColorMulti = {0.61, 0.61, 0.61, 1}
local gVertexColor = false



local function loadCinemaTextures()
	-- Textures to load
	for i = 0, image_list do
		local tex = dxCreateTexture ( "images/kekw/"..i..".png" )		
		if not tex then
			unloadTextures()
			return false
		end
		table.insert ( numberTex, tex )
	end
	return true
end

local function unloadTextures()
	for index, tex in ipairs ( numberTex ) do
		destroyElement ( tex )
	end
end

local numberOd = image_list
-- renders and counts frames
local function renderScreen()
	if getTickCount ( ) - getLastTick < frequencyTic  then return end
	--Play movie sound on first frame   
	if numberId < numberOd then 
		--outputChatBox('Movie cycle: '..((getTickCount ( ) - getLastTock)/1000)..' s')
		getLastTock = getTickCount ( )
		--change seek position of the sound element
		--play_sound()
	end
	if numberOd < image_list then 
		if ((getTickCount ( ) - getLastTick)) - frequencyTic > 0 then
			singleTickCount = ((getTickCount ( ) - getLastTick) - frequencyTic) / ( frequencyTic)
			totalTickCount = totalTickCount + singleTickCount
			if totalTickCount > 1 then 
				numberId = numberId + math.floor( totalTickCount ) 
				--outputChatBox('First frame ommited: '..numberId..' Frames total: '..math.floor(totalTickCount)) 
				totalTickCount = totalTickCount - math.floor( totalTickCount ) 	
			end 
		end
	end
	numberOd = numberId
	numberId = numberId + 1
	if numberId > #numberTex then
		numberId = numberId - #numberTex
	end
	getLastTick = getTickCount ( )
	if not numberTex [ numberId ] then return end
	dxSetShaderValue ( shader_cinema, "CUSTOMTEX0", numberTex [ numberId ] )	  	
end
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource()),
	function()
		-- Create shader
		shader_cinema, tec = dxCreateShader ( "shaders/UVMod.fx" )
		
		if not shader_cinema then
			outputChatBox( "Could not create shader_cinemaFL. Please use debugscript 3",255,0,0 )
			return
		else
		
        dxSetShaderValue ( shader_cinema, "gUVAnim", gUVAnim)
        dxSetShaderValue ( shader_cinema, "gUVScale", gUVScale)			
        dxSetShaderValue ( shader_cinema, "gUVPosition", gUVPosition)
        dxSetShaderValue ( shader_cinema, "gUVRotAngle", gUVRotAngle)
        dxSetShaderValue ( shader_cinema, "gColorMulti", gColorMulti)
        dxSetShaderValue ( shader_cinema, "gVertexColor", gVertexColor)

		engineApplyShaderToWorldTexture ( shader_cinema, cinema_tex_name )	
		if not loadCinemaTextures ( ) then
			outputChatBox ( "Loading textures failed", 255, 0, 0 )
			destroyElement ( shader_cinema )
			unloadTextures()
			return
		end
		addEventHandler ( "onClientHUDRender", getRootElement (), renderScreen )
	end
end
)
addEventHandler( "onClientResourceStop", getResourceRootElement( getThisResource()),
	function()
		if shader_cinema then
			engineRemoveShaderFromWorldTexture( shader_cinema, cinema_tex_name )
			destroyElement( shader_cinema )
			shader_cinema = nil
			unloadTextures()
		end
	end
)
