blob: cb1a9479d45af3f2f548b1be03b808c69debf20d [file] [log] [blame]
Serge Bazanski18c1a262022-07-07 14:24:53 +02001-- push.lua v0.1
2
3-- Copyright (c) 2016 Ulysse Ramage
4-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
8local push = {}
9setmetatable(push, push)
10
11function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, f)
12
13 f = f or {}
14
15 self._WWIDTH, self._WHEIGHT = WWIDTH, WHEIGHT
16 self._RWIDTH, self._RHEIGHT = RWIDTH, RHEIGHT
17 self._fullscreen = f.fullscreen or self._fullscreen or false
18 self._resizable = f.resizable or self._resizable or false
19 if f.canvas == nil then f.canvas = true end
20
21 love.window.setMode( self._RWIDTH, self._RHEIGHT, {fullscreen = self._fullscreen, borderless = false, resizable = self._resizable} )
22
23 self:initValues()
24
25 if f.canvas then self:createCanvas() end
26
27 self._borderColor = {0, 0, 0}
28
29 self._drawFunctions = {["start"]=push.start, ["end"]=push.finish}
30end
31
32function push:createCanvas()
33 self._canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT)
34end
35
36function push:initValues()
37 self._SCALEX, self._SCALEY = self._RWIDTH/self._WWIDTH, self._RHEIGHT/self._WHEIGHT
38 self._SCALE = math.min(self._SCALEX, self._SCALEY)
39 self._OFFSET = {x = (self._SCALEX - self._SCALE) * (self._WWIDTH/2), y = (self._SCALEY - self._SCALE) * (self._WHEIGHT/2)}
40 self._GWIDTH, self._GHEIGHT = self._RWIDTH-self._OFFSET.x*2, self._RHEIGHT-self._OFFSET.y*2
41
42 self._INV_SCALE = 1/self._SCALE
43end
44
45function push:setShader(shader)
46 self._shader = shader
47end
48
49--[[ DEPRECATED ]]--
50function push:apply(operation, shader)
51 if operation == "start" then
52 self:start()
53 elseif operation == "finish" or operation == "end" then
54 self:finish(shader)
55 end
56end
57
58function push:start()
59 if self._canvas then
60 love.graphics.push()
61 love.graphics.setCanvas(self._canvas)
62 else
63 love.graphics.translate(self._OFFSET.x, self._OFFSET.y)
64 love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE, self._WHEIGHT*self._SCALE)
65 love.graphics.push()
66 love.graphics.scale(self._SCALE)
67 end
68end
69
70function push:finish(shader)
71 love.graphics.setBackgroundColor(unpack(self._borderColor))
72 if self._canvas then
73 love.graphics.pop()
74 love.graphics.setCanvas()
75
76 love.graphics.translate(self._OFFSET.x, self._OFFSET.y)
77 love.graphics.setColor(1.0, 1.0, 1.0)
78 love.graphics.setShader(shader or self._shader)
79 love.graphics.draw(self._canvas, 0, 0, 0, self._SCALE, self._SCALE)
80 love.graphics.setCanvas(self._canvas)
81 love.graphics.clear()
82 love.graphics.setCanvas()
83 love.graphics.setShader()
84 else
85 love.graphics.pop()
86 love.graphics.setScissor()
87 end
88end
89
90function push:calculateScale(offset)
91 self._SCALEX, self._SCALEY = self._RWIDTH/self._WWIDTH, self._RHEIGHT/self._WHEIGHT
92 self._SCALE = math.min(self._SCALEX, self._SCALEY)+offset
93 self._OFFSET = {x = (self._SCALEX - self._SCALE) * (self._WWIDTH/2), y = (self._SCALEY - self._SCALE) * (self._WHEIGHT/2)}
94end
95
96function push:setBorderColor(color, g, b)
97 self._borderColor = g and {color, g, b} or color
98end
99
100function push:toGame(x, y)
101 x, y = x-self._OFFSET.x, y-self._OFFSET.y
102 local normalX, normalY = x/self._GWIDTH, y/self._GHEIGHT
103 x, y = (x>=0 and x<=self._WWIDTH*self._SCALE) and normalX*self._WWIDTH or nil, (y>=0 and y<=self._WHEIGHT*self._SCALE) and normalY*self._WHEIGHT or nil
104 return x, y
105end
106
107--doesn't work - TODO
108function push:toReal(x, y)
109 return x+self._OFFSET.x, y+self._OFFSET.y
110end
111
112function push:switchFullscreen(winw, winh)
113 self._fullscreen = not self._fullscreen
114 local windowWidth, windowHeight = love.window.getDesktopDimensions()
115 self._RWIDTH = self._fullscreen and windowWidth or winw or windowWidth*.5
116 self._RHEIGHT = self._fullscreen and windowHeight or winh or windowHeight*.5
117 self:initValues()
118 love.window.setFullscreen(self._fullscreen, "desktop")
119end
120
121function push:resize(w, h)
122 self._RWIDTH = w
123 self._RHEIGHT = h
124 self:initValues()
125end
126
127function push:getWidth() return self._WWIDTH end
128function push:getHeight() return self._WHEIGHT end
129function push:getDimensions() return self._WWIDTH, self._WHEIGHT end
130
131return push