--
-- a renderer; contains methods for rendering things, and possibly some modifying state
--

function render_ctor ()
end

render = class (render_ctor)

function render:none ()

   return { "none" }
end

function render:union (RList)

   if (sizeof (RList) == 0) then return { "none" }
   end
   return RList
end

function render:rgba (R, G, B, A)

   return { "rgba", R, G, B, A }
end

function render:translate (X, Y, Sub)

   return { "translate", X, Y, Sub }
end

function render:scale (X, Y, Sub)

   return { "scale", X, Y, Sub }
end

function render:rotate (Theta, Sub)

   return { "rotate", Theta, Sub }
end

function render:camera (X, Y)

   return { "camera", X, Y }
end

function render:rect (X, Y, W, H)

   return { "rect", X, Y, W, H }
end

function render:quad (X1, Y1, X2, Y2, X3, Y3, X4, Y4)

   return { "quad", X1, Y1, X2, Y2, X3, Y3, X4, Y4 }
end

function render:tri (X1, Y1, X2, Y2, X3, Y3)

   return { "tri", X1, Y1, X2, Y2, X3, Y3 }
end

function render:line (X0, Y0, X1, Y1, W)

   return { "line", X0, Y0, X1, Y1, W }
end

function render:dashline (X0, Y0, X1, Y1, W, OnL, OffL)

   return { "dashline", X0, Y0, X1, Y1, W, 50, 50 }
end

function render:circarc (X, Y, R, ThetaStart, ThetaEnd, W)

   return { "circarc", X, Y, R, ThetaStart, ThetaEnd, W }
end

function render:terrain (Y, W, H, SwatchName, Seed)

   return { "terrain", Y, W, H, SwatchName, Seed }
end

function render:zoffs (Z)

   return { "zoffs", Z }
end

function render:picture (Texture)

   return { "picture", Texture }
end

function render:text (X, Y, W, Text)

   return { "text", X, Y, W, Text }
end

function render:centertext (X, Y, W, Text)

   return { "centertext", X, Y, W, Text }
end

function render:righttext (X, Y, W, Text)

   return { "righttext", X, Y, W, Text }
end

--
-- advanced render functions
--

function render:vwedge (X1_1, X1_2, X2_1, X2_2, Y1, Y2)

   -- automatically null, triangle or quad with given coordinates

   if (X1_1 == X1_2 and X2_1 == X2_2) then

      return self:none ()

   elseif (X1_1 == X1_2) then

      return self:tri (X1_1, Y1, X2_1, Y2, X2_2, Y2)

   elseif (X2_1 == X2_2) then

      return self:tri (X1_1, Y1, X2_2, Y2, X1_2, Y1)
   else

      return self:quad (X1_1, Y1, X2_1, Y2, X2_2, Y2, X1_2, Y1)
   end
end

function render:vwedge_border (X1_1, X1_2, X2_1, X2_2, Y1, Y2, BorderW, LineW)

   -- automatically draw a wedge border
   if (X1_1 == X1_2 and X2_1 == X2_2) then

      return self:none ()
   end

   if (X1_1 == X1_2) then Y2 = Y2 + 75
   else

       X1_1 = X1_1 + BorderW
       X1_2 = X1_2 - BorderW
   end

   if (X2_1 == X2_2) then Y1 = Y1 - 75
   else

      X2_1 = X2_1 + BorderW
      X2_2 = X2_2 - BorderW
   end

   return self:union { self:line (X1_1, Y1, X2_1, Y2, LineW), self:line (X1_2, Y1, X2_2, Y2, LineW) }
end
