aboutsummaryrefslogtreecommitdiff
path: root/libs/o-ten-one/init.lua
blob: 7909357662db1d1efd3ce604e061262390e64bed (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
local splashlib = {
  _VERSION     = "v1.2.0",
  _DESCRIPTION = "a 0.10.1 splash",
  _URL         = "https://github.com/love2d-community/splashes",
  _LICENSE     = [[Copyright (c) 2016 love-community members (as per git commits in repository above)

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 acknowledgement 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.

The font used in this splash is "Handy Andy" by www.andrzejgdula.com]]
}

local current_module = (...):gsub("%.init$", "")
local current_folder = current_module:gsub("%.", "/")

local timer = require(current_module .. ".timer")

local colors = {
  bg =     {.42, .75, .89},
  white =  {  1,   1,   1},
  blue =   {.15, .67, .88},
  pink =   {.91, .29,  .6},
  shadow = {0, 0, 0, .33}
}

-- patch shader:send if 'lighten' gets optimized away
local function safesend(shader, name, ...)
  if shader:hasUniform(name) then
    shader:send(name, ...)
  end
end

function splashlib.new(init)
  init = init or {}
  local self = {}
  local width, height = love.graphics.getDimensions()

  self.background = init.background == nil and colors.bg or init.background
  self.delay_before = init.delay_before or 0.3
  self.delay_after = init.delay_after or 0.7

  if init.fill == "rain" then
    local rain = {}
    rain.spacing_x = 110
    rain.spacing_y = 80
    rain.image = love.graphics.newImage(current_folder .. "/baby.png")
    rain.img_w = rain.image:getWidth()
    rain.img_h = rain.image:getHeight()
    rain.ox = -rain.img_w / 2
    rain.oy = -rain.img_h / 2
    rain.batch = love.graphics.newSpriteBatch(rain.image, 512)
    rain.t = 0

    local gradient = love.graphics.newMesh({
      {    0, height/4, 0, 0,  0, 0, 0,   0},
      {width, height/4, 0, 0,  0, 0, 0,   0},
      {width, height,   0, 0,  0, 0, 0, 200},
      {    0, height,   0, 0,  0, 0, 0, 200},
    }, "fan", "static")
    do
      local batch = rain.batch

      local sx = rain.spacing_x
      local sy = rain.spacing_y
      local ox = rain.ox
      local oy = rain.oy

      local batch_w = 2 * math.ceil(love.graphics.getWidth() / sx) + 2
      local batch_h = 2 * math.ceil(love.graphics.getHeight() / sy) + 2

      batch:clear()

      if batch:getBufferSize() < batch_w * batch_h then
        batch:setBufferSize(batch_w * batch_h)
      end

      for i = 0, batch_h - 1 do
        for j = 0, batch_w - 1 do
          local is_even = (j % 2) == 0
          local offset_y = is_even and 0 or sy / 2
          local x = ox + j * sx
          local y = oy + i * sy + offset_y
          batch:add(x, y)
        end
      end

      batch:flush()
    end

    function self.fill()
      local y = rain.spacing_y * select(2, math.modf(self.elapsed))

      local small_y = -rain.spacing_y + y / 2
      local big_y = -rain.spacing_y + y

      love.graphics.setBlendMode("subtract")
      love.graphics.setColor(1, 1, 1, .5)
      love.graphics.draw(rain.batch, -rain.spacing_x, small_y, 0, 0.5, 0.5)

      love.graphics.setBlendMode("alpha")
      love.graphics.setColor(.81, .81, .81, 1)
      love.graphics.draw(rain.batch, -rain.spacing_x, big_y)

      love.graphics.draw(gradient)
    end
  end

  -- radial mask shader
  self.maskshader = love.graphics.newShader((init.fill == "lighten" and "#define LIGHTEN" or "") .. [[

  extern number radius;
  extern number blur;
  extern number shadow;
  extern number lighten;

  vec4 desat(vec4 color) {
    number g = dot(vec3(.299, .587, .114), color.rgb);
    return vec4(g, g, g, 1.0) * lighten;
  }

  vec4 effect(vec4 global_color, Image canvas, vec2 tc, vec2 _)
  {
    // radial mask
    vec4 color = Texel(canvas, tc);
    number r = length((tc - vec2(.5)) * love_ScreenSize.xy);
    number s = smoothstep(radius+blur, radius-blur, r);
    #ifdef LIGHTEN
    color = color + desat(color) * (1.0-s);
    #else
    color.a *= s;
    #endif
    color.a *= global_color.a;

    // add shadow on lower diagonal along the circle
    number sr = 7. * (1. - smoothstep(-.1,.04,(1.-tc.x)-tc.y));
    s = (1. - pow(exp(-pow(radius-r, 2.) / sr),3.) * shadow);

    return color - vec4(1, 1, 1, 0) * (1.0-s);
  }
  ]])

  -- this shader makes the text appear from left to right
  self.textshader = love.graphics.newShader[[
  extern number alpha;

  vec4 effect(vec4 color, Image logo, vec2 tc, vec2 sc)
  {
    //Probably would be better to just use the texture's dimensions instead; faster reaction.
    vec2 sd = sc / love_ScreenSize.xy;

    if (sd.x <= alpha) {
      return color * Texel(logo, tc);
    }
    return vec4(0);
  }
  ]]

  -- this shader applies a stroke effect on the logo using a gradient mask
  self.logoshader = love.graphics.newShader[[
  //Using the pen extern, only draw out pixels that have their color below a certain treshold.
  //Since pen will eventually equal 1.0, the full logo will be drawn out.

  extern number pen;
  extern Image mask;

  vec4 effect(vec4 color, Image logo, vec2 tc, vec2 sc)
  {
    number value = max(Texel(mask, tc).r, max(Texel(mask, tc).g, Texel(mask, tc).b));
    number alpha = Texel(mask, tc).a;

    //probably could be optimzied...
    if (alpha > 0.0) {
      if (pen >= value) {
        return color * Texel(logo, tc);
      }
    }
    return vec4(0);
  }
  ]]

  self.canvas = love.graphics.newCanvas()

  self.elapsed = 0
  self.alpha = 1
  self.heart = {
    sprite = love.graphics.newImage(current_folder .. "/heart.png"),
    scale = 0,
    rot   = 0
  }

  self.stripes = {
    rot     = 0,
    height  = 100,
    offset  = -2 * width,
    radius  = math.max(width, height),
    lighten = 0,
    shadow  = 0,
  }

  self.text = {
    obj   = love.graphics.newText(love.graphics.newFont(current_folder .. "/handy-andy.otf", 22), "made with"),
    alpha = 0
  }
  self.text.width, self.text.height = self.text.obj:getDimensions()

  self.logo = {
    sprite = love.graphics.newImage(current_folder .. "/logo.png"),
    mask   = love.graphics.newImage(current_folder .. "/logo-mask.png"),
    pen    = 0
  }
  self.logo.width, self.logo.height = self.logo.sprite:getDimensions()

  safesend(self.maskshader, "radius",  width*height)
  safesend(self.maskshader, "lighten", 0)
  safesend(self.maskshader, "shadow",  0)
  safesend(self.maskshader, "blur",    1)

  safesend(self.textshader, "alpha", 0)

  safesend(self.logoshader, "pen", 0)
  safesend(self.logoshader, "mask", self.logo.mask)

  timer.clear()
  timer.script(function(wait)

    wait(self.delay_before)

    -- roll in stripes
    timer.tween(0.5, self.stripes, {offset = 0})
    wait(0.3)

    timer.tween(0.3, self.stripes, {rot = -5 * math.pi / 18, height=height})
    wait(0.2)

    -- hackety hack: execute timer to update shader every frame
    timer.every(0, function()
      safesend(self.maskshader, "radius",  self.stripes.radius)
      safesend(self.maskshader, "lighten", self.stripes.lighten)
      safesend(self.maskshader, "shadow",  self.stripes.shadow)
      safesend(self.textshader, "alpha",   self.text.alpha)
      safesend(self.logoshader, "pen",     self.logo.pen)
    end)

    -- focus the heart, desaturate the rest
    timer.tween(0.2, self.stripes, {radius  = 170*love.graphics.getDPIScale()})
    timer.tween(0.4, self.stripes, {lighten = .06}, "quad")
    wait(0.2)

    timer.tween(0.2, self.stripes,  {radius = 70*love.graphics.getDPIScale()}, "out-back")
    timer.tween(0.7, self.stripes,  {shadow = .3}, "back")
    timer.tween(0.8, self.heart,    {scale  =  1}, "out-elastic", nil, 1, 0.3)

    -- write out the text
    timer.tween(.75, self.text, {alpha = 1}, "linear")

    -- draw out the logo, in parts
    local mult = 0.65
    local function tween_and_wait(dur, pen, easing)
      timer.tween(mult * dur, self.logo, {pen = pen/255}, easing)
      wait(mult * dur)
    end
    tween_and_wait(0.175,  50, "in-quad")     -- L
    tween_and_wait(0.300, 100, "in-out-quad") -- O
    tween_and_wait(0.075, 115, "out-sine")    -- first dot on O
    tween_and_wait(0.075, 129, "out-sine")    -- second dot on O
    tween_and_wait(0.125, 153, "in-out-quad") -- \
    tween_and_wait(0.075, 179, "in-quad")     -- /
    tween_and_wait(0.250, 205, "in-quart")    -- e->break
    tween_and_wait(0.150, 230, "out-cubic")   -- e finish
    tween_and_wait(0.150, 244, "linear")      -- ()
    tween_and_wait(0.100, 255, "linear")      -- R
    wait(0.4)

    -- no more skipping
    wait(self.delay_after)
    self.done = true

    timer.tween(0.3, self, {alpha = 0})
    wait(0.3)

    timer.clear()

    if self.onDone then self.onDone() end
  end)

  self.draw = splashlib.draw
  self.update = splashlib.update
  self.skip = splashlib.skip

  return self
end

function splashlib:draw()
  local width, height = love.graphics.getDimensions()

  if self.background then
    love.graphics.clear(self.background)
  end

  if self.fill and self.elapsed > self.delay_before + 0.6 then
    self:fill()
  end

  self.canvas:renderTo(function()
    love.graphics.push()
    love.graphics.translate(width / 2, height / 2)

    love.graphics.push()
    love.graphics.rotate(self.stripes.rot)
    love.graphics.setColor(colors.pink)
    love.graphics.rectangle(
      "fill",
      self.stripes.offset - width, -self.stripes.height,
      width * 2, self.stripes.height
    )

    love.graphics.setColor(colors.blue)
    love.graphics.rectangle(
      "line", -- draw line for anti aliasing
      -width - self.stripes.offset, 0,
      width * 2, self.stripes.height
    )
    love.graphics.rectangle(
      "fill",
      -width - self.stripes.offset, 0,
      width * 2, self.stripes.height
    )
    love.graphics.pop()

    love.graphics.setColor(1, 1, 1, self.heart.scale)
    love.graphics.draw(self.heart.sprite, 0, 5, self.heart.rot, self.heart.scale, self.heart.scale, 43, 39)
    love.graphics.pop()
  end)

  love.graphics.setColor(1, 1, 1, self.alpha)
  love.graphics.setShader(self.maskshader)
  love.graphics.draw(self.canvas, 0,0)
  love.graphics.setShader()

  love.graphics.push()
  love.graphics.setShader(self.textshader)
  love.graphics.draw(
    self.text.obj,
    (width  / 2) - (self.text.width   / 2),
    (height / 2) - (self.text.height  / 2) + (height / 10) + 62
  )
  love.graphics.pop()

  love.graphics.push()
  love.graphics.setShader(self.logoshader)
  love.graphics.draw(
    self.logo.sprite,
    (width  / 2) - (self.logo.width   / 4),
    (height / 2) + (self.logo.height  / 4) + (height / 10),
    0, 0.5, 0.5
  )
  love.graphics.setShader()
  love.graphics.pop()
end

function splashlib:update(dt)
  timer.update(dt)
  self.elapsed = self.elapsed + dt
end

function splashlib:skip()
  if not self.done then
    self.done = true

    timer.tween(0.3, self, {alpha = 0})
    timer.after(0.3, function ()
      timer.clear() -- to be safe
      if self.onDone then self.onDone() end
    end)
  end
end

setmetatable(splashlib, { __call = function(self, ...) return self.new(...) end })

return splashlib