Page 1 of 1

Module for Rendering CODE DOODLES From LOVE2D

Posted: Sun Apr 16, 2017 2:44 pm
by PolyDude64
Hello Everyone,

I've been switching from Gamemaker Studio To LOVE2D and I enjoyed using this lua framework so much, I made a tool for programmers who are bored, ready for a challenge in rendering custom animations in LOVE2D (sort of like, creating animations by code in a framework/game engine). It will export image sequences in your %appdata%\LOVE\"folder_name" directory so that you can convert them into a video.

for example, I have this drawing code that I want to render as a video:

Code: Select all

function love.draw()
	love.graphics.setColor(0, 0, 0)
  	local room_width, room_height = love.graphics.getWidth(), love.graphics.getHeight()
  	local medx, medy = room_width/2, room_height/2
  	local time = love.timer.getTime()*2
  	--start scene
  	love.graphics.push()
  	love.graphics.translate(0, room_height/5)
  	--triangle base
  	love.graphics.polygon("fill",medx-50,medy+50,medx+50,medy+50,medx,medy-50)
  	--seesaw
  	love.graphics.setLineWidth(25)
  	local length = 400
  	local saw = math.sin(time)*(math.pi/10)
  	love.graphics.push()
  	love.graphics.translate(medx,medy-50)
  	love.graphics.rotate(saw)
    		love.graphics.line(-length,-10,length,-10)
    		local rad = 50
    		love.graphics.circle("fill", -math.sin(time)*length,-20-(rad),rad)
  	love.graphics.pop()
 	 --end scene
 	 love.graphics.pop()
end
which is gonna look like this:
Image

when you use the module, there will be additions and changes
  1. place the module at the same directory as main.lua
  2. at the beginning of the whole code, type : local render = require "render"
  3. at the beginning of the love.load() code, type: render.init()
  4. at the end of the love.update() code, type : render.update()
  5. at the end of the love.draw() code, type : render.draw()
  6. instead of love.timer.getTime(), use : render.time()
  7. instead of dt or love.timer.getDelta(), use : render.spf()

Code: Select all

local render = require "render"
function love.load()
	render.init(1280,720,3,false,{255,255,255},60,0,100,"render","png")
	--Add Your Variables here
end

function love.update()
	--Update Variables Here
	render.update()
end

function love.draw()
	--Draw Here
	render.draw()
end
When you run the main.lua file, it would automatically render image sequences:
Image

Then if you're done, it will launch a message box:
Image

Paste the directory at the address bar and you will get a collection of images:
Image

Then combine the image sequences depending on the given fps on any video software (might work/not, depends):
Image

Congratulations! You just made an animation(ish) video with just a bunch of code!
Here is the template, module, & instructions in a txt file. Sorry, my internet is bad
TEMPLATE

Code: Select all

local render = require "render"
function love.load()
	render.init(1280,720,3,false,{255,255,255},60,0,100,"render","png")
	--Add Your Variables here
end

function love.update()
	--Update Variables Here
	render.update()
end

function love.draw()
	--Draw Here
	render.draw()
end
INSTRUCTIONS

Code: Select all

HOW TO USE THE RENDERING MODULE

step 1-layout your code, example:
---------------------------------------------------------------------------------------------------
local render = require "render"
function love.load()
  render.init(1280,720,3,false,{255,255,255},60,0,100,"render","png")
end

function love.update(dt)
  --UPDATE STUFF HERE
  render.update()
end

function love.draw()
  --DRAW STUFF HERE
  render.end_draw()
end
---------------------------------------------------------------------------------------------------
API:
  render.init(width,height,mssaa,draw_alpha,color,fps,start,endin,name,format)
      initializes the render module within the arguments
  --width - screen width
  --height - screen height
  --msaa - msaa (default:1)
  --draw_alpha - checks if it will draw the alpha channel or not
  --color - color table
  --fps - frames per second
  --start - starting frame to render
  --endin - ending frame to render
  --name - export directory folder name
  --format - ex:"png", "jpg"

  render.update()
      updates the rendering system, written at the end of the love.update(dt) function

  render.end_draw()
      draws within the rendering system, written at the end of the love.draw(dt) function

  render.frame()
      returns the frame number currently rendering

  render.time()
      returns the seconds in each frame, same thing as love.timer.getTime()

  render.fts(n)
      converts the given frames to seconds
  --n - given number of frames

  render.stf(n)
      converts the given number of seconds to the number of frames
  --n - given number of seconds

  render.spf()
      returns the number of seconds per frame, same thing as love.timer.getDelta()
MODULE

Code: Select all

local module = {}

function module.init(width,height,mssaa,draw_alpha,color,fps,start,endin,name,format)
  --width - screen width
  --height - screen height
  --msaa - msaa (default:1)
  --draw_alpha - checks if it will draw the alpha channel or not
  --color - color table
  --fps - frames per second
  --start - starting frame to render
  --endin - ending frame to render
  --name - export directory folder name
  --format - ex:"png", "jpg"
  love.window.setMode( width, height,{msaa=mssaa})
  function check(n) if n == true then return 0 else return 255 end
  love.graphics.setBackgroundColor({color[1],color[2],color[3],check(draw_alpha)})
  render = {}
  render.alpha = draw_alpha
  render.fps = fps
  render.stt = start-1
  render.nd = endin
  render.frmt = format
  render.frame = render.stt
  render.time = (1/render.fps)*render.frame
  render.draw_check = false
  love.filesystem.setIdentity(name)
end

function module.update()
  --place this code at the end of the love.update function
  if render.draw_check == true then
    if render.frame ~= render.stt then
      local scr_sht = love.graphics.newScreenshot(render.alpha)
      scr_sht:encode(render.frmt,(render.frame)..'.'..render.frmt)
    end
    render.frame = render.frame + 1
    render.time = (1/render.fps)*render.frame
    local title = "frame"..render.frame.." | "..render.time.."s"
    if render.frame > render.nd then
      title = "DONE RENDERING"
      love.system.setClipboardText(love.filesystem.getAppdataDirectory().."/LOVE/"..love.filesystem.getIdentity())
      love.window.requestAttention(false)
      love.window.showMessageBox("notice", "Image sequence saved in directory: ("..love.filesystem.getAppdataDirectory().."/LOVE/"..love.filesystem.getIdentity().."), directory copied to clipboard")
      love.event.quit()
    end
    love.window.setTitle(render.fps.."fps".."/".."frames: "..(render.stt+1).."-"..render.nd.." | "..title)
    render.draw_check = false
  end
end

function module.frame()
  return render.frame
end

function module.time()
  return render.time
end

function module.end_draw()
  render.draw_check = true
end

function module.fts(n)
  return (1/render.fps)*n
end

function module.stf(n)
  return n/render.fps
end

function module.spf()
  return 1/fps
end
return module
Thank you for viewing my first post! Feedback and Criticism would be appreciated!
(sry for my bad english thou)