-
Notifications
You must be signed in to change notification settings - Fork 0
/
hlpr.lua
346 lines (300 loc) · 7.94 KB
/
hlpr.lua
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
-- hlpr libary: it's not about nice coding, ist about fast and easy coding
-- by Rukano and Headchant, 2011
-- global math
pi = math.pi
sin = math.sin
deg = math.deg
rad = math.rad
State = State or {}
function declare(t)
for i,v in pairs(t) do
if not State[i] then
State[i] = v
_G[i] = State[i]
end
end
end
require "color"
module(...,package.seeall)
-- syntax shortcuts
checkMode = love.graphics.checkMode
circle = love.graphics.circle
clear = love.graphics.clear
draw = love.graphics.draw
drawq = love.graphics.drawq
getBackgroundColor = love.graphics.getBackgroundColor
getBlendMode = love.graphics.getBlendMode
getCaption =love.graphics.getCaption
getColor = love.graphics.getColor
getColorMode = love.graphics.getColorMode
getFont = love.graphics.getFont
getHeight = love.graphics.getHeight
getLineStipple = love.graphics.getLineStipple
getLineStyle = love.graphics.getLineStyle
getLineWidth = love.graphics.getLineWidth
getMaxPointSize = love.graphics.getMaxPointSize
getModes = love.graphics.getModes
getPointSize = love.graphics.getPointSize
getPointStyle = love.graphics.getPointStyle
getScissor = love.graphics.getScissor
getWidth = love.graphics.getWidth
isCreated = love.graphics.isCreated
line = love.graphics.line
newFont = love.graphics.newFont
newFrameBuffer = love.graphics.newFramebuffer
newImage = love.graphics.newImage
newImageFont = love.graphics.newImageFont
newParticleSystem = love.graphics.newParticleSystem
newQuad = love.graphics.newQuad
newScreenshot = love.graphics.newScreenshot
newSpriteBatch = love.graphics.newSpriteBatch
point = love.graphics.point
polygon = love.graphics.polygon
pop = love.graphics.pop
present = love.graphics.present
print = love.graphics.print
printf = love.graphics.printf
push = love.graphics.push
quad = love.graphics.quad
rectagle = love.graphics.rectangle
reset = love.graphics.reset
rotate = love.graphics.rotate
scale = love.graphics.scale
setBackgroundColor = love.graphics.setBackgroundColor
setBlendMode = love.graphics.setBlendMode
setCaption = love.graphics.setCaption
setColor = love.graphics.setColor
setColorMode = love.graphics.setColorMode
setFont = love.graphics.setFont
setIcon = love.graphics.setIcon
setLine = love.graphics.setLine
setLineStipple = love.graphics.setLineStipple
setLineStyle = love.graphics.setLineStyle
setLineWidth = love.graphics.setLineWidth
setMode = love.graphics.setMode
setPoint = love.graphics.setPoint
setPointSize = love.graphics.setPointSize
setPointStyle = love.graphics.setPointStyle
setRenderTarget = love.graphics.setRenderTarget
setScissor = love.graphics.setScissor
toggleFullscreen = love.graphics.toggleFullscreen
translate = love.graphics.translate
triangle = love.graphics.triangle
function color(r, g,b,a)
local color={}
local alpha=a or 255
local name=r or "azure"
if type(r) == "string" then
alpha = g or alpha
color = x11_color_table[name]
else
color[1]=r
color[2]=g
color[3]=b
end
color[4]=alpha
return color
end
-- clip withing range
function clip(n,min,max)
return math.min(math.max(n, min or -math.huge), max or math.huge)
end
-- wrap within range, updated version
function wrap(n, min, max)
local min = min or 0
return ((n - min) % ((max or 0) - min)) + min
end
-- setColor white
function white()
love.graphics.setColor(255,255,255,255)
end
-- setColor black
function black()
love.graphics.setColor(0,0,0,255)
end
-- shorter setColor white
function w()
white()
end
-- shorter setColor black
function b()
black()
end
-- fill the screen with translucent black
function clear_black(alpha)
love.graphics.setColor(0,0,0,alpha)
love.graphics.rectangle("fill", 0,0,800,600)
end
-- shorter clear
function cls(alpha)
clear_black(alpha)
end
-- one time clear
function cls_once()
love.graphics.setColor(0,0,0,255)
love.graphics.rectangle("fill", 0,0,800,600)
end
-- returns random values from -1 to 1, g sets the equidistance
function norm_random()
return 2 * math.random() - 1
end
-- shorte norm_random
function n_rnd()
return norm_random()
end
-- drunk, brownnoise/drunk walk: x = x +/- random(width)
function drunk(x, width, g)
x = x or 0
width = width or 1
g = g or 100
return (x + width*norm_random())
end
-- drnk, shorter version of drunk, start is only used the first time
-- this makes some sense whatsoever...
function drnk(width)
local last = 0
return function()
last = last + width * norm_random()
return last
end
end
-- scaling functions:
function linlin(n,inMin,inMax,outMin,outMax,clip)
-- ported and adapted from:
-- SuperCollider SimpleNumber:linlin
local n=n or 0 -- to avoid giving back nil
local clip=clip or "minmax" -- default:clip minmax
if (inMin == nil) or (inMax == nil) or (outMin == nil) or (outMax == nil) then
-- just in case you forgot the parameters...
return n
end
if clip == "minmax" then
if n <= inMin then
return minoutMin
elseif n >= inMax then
return outMax
end
elseif clip == "min" then
if n <= inMin then
return outMin
end
elseif clip == "max" then
if n >= inMax then
return outMax
end
end
-- here is the magic!
n = (((n-inMin)/(inMax-inMin)) * (outMax-outMin)) + outMin
return n
end
function linexp(n,inMin,inMax,outMin,outMax,clip)
-- ported and adapted from:
-- SuperCollider SimpleNumber:linexp
local n=n or 0.00001 -- to avoid giving back nil
local clip=clip or "minmax" -- default:clip minmax
if (inMin == nil) or (inMax == nil) or (outMin == nil) or (outMax == nil) then
-- just in case...
return n
end
if clip == "minmax" then
if n <= inMin then
return outMin
elseif n >= inMax then
return outMax
end
elseif clip == "min" then
if n <= inMin then
return outMin
end
elseif clip == "max" then
if n >= inMax then
return outMax
end
end
-- here is the magic!
n = math.pow(outMax/outMin, (n-inMin)/(inMax-inMin)) * outMin
return n
end
function explin(n,inMin,inMax,outMin,outMax,clip)
-- ported and adapted from:
-- SuperCollider SimpleNumber:explin
local n=n or 0.00001 -- to avoid giving back nil
local clip=clip or "minmax" -- default:clip minmax
if (inMin == nil) or (inMax == nil) or (outMin == nil) or (outMax == nil) then
-- just in case...
return n
end
if clip == "minmax" then
if n <= inMin then
return outMin
elseif n >= inMax then
return outMax
end
elseif clip == "min" then
if n <= inMin then
return outMin
end
elseif clip == "max" then
if n >= inMax then
return outMax
end
end
-- here is the magic!
n = (((math.log(n/inMin)) / (math.log(inMax/inMin))) * (outMax-outMin)) + outMin
return n
end
function expexp(n,inMin,inMax,outMin,outMax,clip)
-- ported and adapted from:
-- SuperCollider SimpleNumber:expexp
local n=n or 0.00001 -- to avoid giving back nil
local clip=clip or "minmax" -- default:clip minmax
if (inMin == nil) or (inMax == nil) or (outMin == nil) or (outMax == nil) then
-- just in case...
return n
end
if clip == "minmax" then
if n <= inMin then
return outMin
elseif n >= inMax then
return outMax
end
elseif clip == "min" then
if n <= inMin then
return outMin
end
elseif clip == "max" then
if n >= inMax then
return outMax
end
end
-- here is the magic!
n = math.pow(outMax/outMin, math.log(n/inMin) / math.log(inMax/inMin)) * outMin
return n
end
-- returns easy sine oscillator
function sin()
local x = 0
return function(dt)
x = x + (dt or 0)
if x > 2 * pi then x = x - 2*pi end
return math.sin(x)
end
end
-- updates all objects in the _object table
function update_objects(dt)
for i,v in ipairs(_internal_object_table) do
v:update(dt)
end
end
-- rotate around center
function rotateCenter(angle)
local angle=angle or 0
local w, h = getWidth(), getHeight()
translate(w/2, h/2)
rotate(angle)
translate(-w/2, -h/2)
end
-- return a random table entry
function choose(table)
return table[math.random(#table)]
end