| 1 | local gl = require 'gl' |
| 2 | |
| 3 | assert(from, 'missing from argument') |
| 4 | assert(to, 'missing to argument') |
| 5 | assert(duration, 'missing duration argument') |
| 6 | callback = callback or function () end |
| 7 | |
| 8 | local counter = 0; |
| 9 | |
| 10 | function update() |
| 11 | counter = counter + 1 |
| 12 | |
| 13 | if counter > duration then |
| 14 | callback() |
| 15 | self.dead = true |
| 16 | end |
| 17 | end |
| 18 | |
| 19 | function fade_draw() |
| 20 | local t = counter / duration |
| 21 | local opacity = from * (1 - t) + to * t |
| 22 | |
| 23 | gl.glColor4d(0, 0, 0, opacity) |
| 24 | gl.glBegin(gl.GL_QUADS) |
| 25 | gl.glVertex2d(game.constants.screen_left, game.constants.screen_bottom) |
| 26 | gl.glVertex2d(game.constants.screen_right, game.constants.screen_bottom) |
| 27 | gl.glVertex2d(game.constants.screen_right, game.constants.screen_top) |
| 28 | gl.glVertex2d(game.constants.screen_left, game.constants.screen_top) |
| 29 | gl.glEnd() |
| 30 | gl.glColor3d(1, 1, 1) |
| 31 | end |