Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Commit

Permalink
More browser compatibility fixes.
Browse files Browse the repository at this point in the history
Now also works in Opera. At least runs in Safari, but broken.
  • Loading branch information
Stéphan Kochen committed Dec 19, 2011
1 parent 6a3d00a commit 4f04b54
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions game.coffee
Expand Up @@ -6,16 +6,13 @@ Made for Ludum Dare 22. Licensed GPLv3, see the LICENSE file.

{ PI } = Math
TwoPI = PI * 2
window.requestAnimationFrame ?=
mozRequestAnimationFrame ?
webkitRequestAnimationFrame ?
msRequestAnimationFrame
### FIXME: Not in FF
window.cancelRequestAnimationFrame ?=
mozCancelRequestAnimationFrame ?
webkitCancelRequestAnimationFrame ?
msCancelRequestAnimationFrame
###

unless window.requestAnimationFrame
for prefix in ['moz', 'webkit', 'ms', 'o']
if window["#{prefix}RequestAnimationFrame"]
window.requestAnimationFrame = window["#{prefix}RequestAnimationFrame"]
window.cancelRequestAnimationFrame = window["#{prefix}CancelRequestAnimationFrame"]
break


buildCanvas = (options={}) ->
Expand Down Expand Up @@ -641,8 +638,8 @@ class LevelStructure
controller =
initialize: ->
@up = @down = @left = @right = @action = 0
document.onkeydown = @keyDown.bind(this)
document.onkeyup = @keyUp.bind(this)
document.onkeydown = (e) => @keyDown e
document.onkeyup = (e) => @keyUp e

keyDown: (e) ->
switch e.keyCode
Expand Down Expand Up @@ -687,22 +684,17 @@ gameLoop = window.gameLoop =
state.fsm = 'title'
state.counter = 0

@tick = @tick.bind this
@frame = @frame.bind this

@initialize = ->

start: ->
return if @interval
@initialize()
@interval = setInterval @tick, 25
@interval = setInterval (=> @tick()), 25

### FIXME: Not in FF
stop: ->
clearInterval @interval if @interval
cancelRequestAnimationFrame @frameRequest if @frameRequest
cancelRequestAnimationFrame? @frameRequest if @frameRequest
@interval = @frameRequest = null
###

# Start up a new level.
loadLevel: (idx) ->
Expand Down Expand Up @@ -765,8 +757,10 @@ gameLoop = window.gameLoop =
state.counter++
@nextLevel() if state.counter is 100

unless @frameRequest
@frameRequest = requestAnimationFrame @frame
if window.requestAnimationFrame
@frameRequest ||= requestAnimationFrame => @frame()
else
@frame()

# Render a frame.
frame: ->
Expand Down

0 comments on commit 4f04b54

Please sign in to comment.