forked from CodeNMore/CScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcscreen.lua
More file actions
142 lines (124 loc) · 3.76 KB
/
cscreen.lua
File metadata and controls
142 lines (124 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
--[[
CScreen v1.3 by CodeNMore
A simple way to make resolution-independent Love2D games
Tested for LOVE 0.10.1
See: https://github.com/CodeNMore/CScreen
Zlib License:
Copyright (c) 2016 CodeNMore
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be appreciated
but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
--]]
local CScreen = {}
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, image)
rx = tw or 800
ry = th or 600
ctr = cntr or false
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.scale(fsvr, fsvr)
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)
end
end
-- Scales and centers all graphics properly
function CScreen.apply()
if ctr then
love.graphics.translate(tx, ty)
end
love.graphics.scale(fsv, fsv)
end
-- Updates CScreen when the window size changes
function CScreen.update(w, h)
local sx = w / rx
local sy = h / ry
fsv = math.min(sx, sy)
fsvr = 1 / fsv
-- Centering
if ctr and fsv == sx then -- Vertically
tx = 0
ty = (h / 2) - (ry * fsv / 2)
elseif ctr and fsv == sy then -- Horizontally
ty = 0
tx = (w / 2) - (rx * fsv / 2)
end
-- Variable sets
rwf = w
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
function CScreen.project(x, y)
return math.floor((x - tx) / fsv), math.floor((y - ty) / fsv)
end
-- Change letterbox color
function CScreen.setColor(r, g, b, a)
cr = r
cg = g
cb = b
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