Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CScreen is very simple to use, and can be implemented in nearly any type of game
local CScreen = require "cscreen"

function love.load()
CScreen.init(800, 600, true)
CScreen.init(800, 600, true, "resources/lettertile.png")
end

function love.draw(dt)
Expand All @@ -31,11 +31,12 @@ end
<th>Description</th>
</tr>
<tr>
<td>init(tw, th, center)</td>
<td>init(tw, th, center, image)</td>
<td>
<b>tw</b> (800) the target screen width<br>
<b>th</b> (600) the target screen height<br>
<b>center</b> (true) whether or not to letterbox
<b>image</b> (nil) whether or not to use a repeated tile as letterbox
</td>
<td>
Use <em>tw</em> and <em>th</em> to set the target width and height of the game screen. This defaults to 800 and 600, or a 4:3 screen ratio. Set <em>center</em> to true to center, or letterbox, the game screen (generally this should be true). Usually this is called in <em>love.load()</em>.
Expand Down Expand Up @@ -66,7 +67,7 @@ end
none
</td>
<td>
Actually draws the letterbox borders using <em>love.graphics.rectangle(..)</em> using the set color (see <em>setColor()</em>), then restores the previously set color. <b>**This is called at the end of <em>love.draw(dt)</em>, as drawing after this line will result in an incorrect ratio!</b>
Actually draws the letterbox borders using <em>love.graphics.rectangle(..)</em> using the set color (see <em>setColor()</em>), if image is nil, then restores the previously set color. Otherwise, draws the letterbox borders as a repeated tile of the image. <b>**This is called at the end of <em>love.draw(dt)</em>, as drawing after this line will result in an incorrect ratio!</b>
</td>
</tr>
<tr>
Expand All @@ -81,4 +82,13 @@ end
Sets the color to use for the screen letterbox borders (default is black).
</td>
</tr>
</table>
<tr>
<td>setImage(image)</td>
<td>
<b>image</b> new image<br>
</td>
<td>
Replaces old image by a new one, if in letterbox mode.
</td>
</tr>
</table>
54 changes: 45 additions & 9 deletions cscreen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,41 @@ local rx, ry, ctr = 800, 600, true
local rxv, ryv, fsv, fsvr = 800, 600, 1.0, 1.0
local tx, ty, rwf, rhf = 0, 0, 800, 600
local cr, cg, cb, ca = 0, 0, 0, 255
local ltile = nil

-- Initializes CScreen with the initial size values
function CScreen.init(tw, th, cntr)
function CScreen.init(tw, th, cntr, image)
rx = tw or 800
ry = th or 600
ctr = cntr or false
CScreen.update(love.graphics.getWidth(), love.graphics.getHeight())
if image and ctr then
ltile = love.graphics.newImage(image)
ltw, lth = ltile:getDimensions()
ltile:setWrap("repeat", "repeat")
end
CScreen.update(love.graphics.getWidth(), love.graphics.getHeight())
end

-- Draws letterbox borders
function CScreen.cease()
if ctr then
local pr, pg, pb, pa = love.graphics.getColor()
love.graphics.setColor(cr, cg, cb, ca)
love.graphics.scale(fsvr, fsvr)

if tx ~= 0 then
love.graphics.rectangle("fill", -tx, 0, tx, rhf)
love.graphics.rectangle("fill", rxv, 0, tx, rhf)
elseif ty ~= 0 then
love.graphics.rectangle("fill", 0, -ty, rwf, ty)
love.graphics.rectangle("fill", 0, ryv, rwf, ty)
if not ltile then
love.graphics.setColor(cr, cg, cb, ca)
if tx ~= 0 then
love.graphics.rectangle("fill", -tx, 0, tx, rhf)
love.graphics.rectangle("fill", rxv, 0, tx, rhf)
elseif ty ~= 0 then
love.graphics.rectangle("fill", 0, -ty, rwf, ty)
love.graphics.rectangle("fill", 0, ryv, rwf, ty)
end
else
if tx ~= 0 or ty ~= 0 then
love.graphics.draw(ltile, rightbox, rbx, rby)
love.graphics.draw(ltile, leftbox, lbx, lby)
end
end

love.graphics.setColor(pr, pg, pb, pa)
Expand Down Expand Up @@ -87,6 +100,20 @@ function CScreen.update(w, h)
rhf = h
rxv = rx * fsv
ryv = ry * fsv
-- Updating quads
if ltile then
if tx ~= 0 then
rightbox = love.graphics.newQuad(0, 0, tx, rhf, ltw, lth)
rbx, rby = -tx, 0
leftbox = love.graphics.newQuad(0, 0, tx, rhf, ltw, lth)
lbx, lby = rxv, 0
elseif ty ~= 0 then
rightbox = love.graphics.newQuad(0, 0, rwf, ty, ltw, lth)
rbx, rby = 0, -ty
leftbox = love.graphics.newQuad(0, 0, rwf, ty, ltw, lth)
lbx, lby = 0, ryv
end
end
end

-- Convert from window coordinates to target coordinates
Expand All @@ -102,5 +129,14 @@ function CScreen.setColor(r, g, b, a)
ca = a
end

function CScreen.setImage(image)
if image and ctr then
ltile = love.graphics.newImage(image)
ltw, lth = ltile:getDimensions()
ltile:setWrap("repeat", "repeat")
end
CScreen.update(love.graphics.getWidth(), love.graphics.getHeight())
end

-- Return the table for use
return CScreen