Class: Dare::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/dare/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Window

Creates a new window object to hold all your game goodness

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a window.

Options Hash (opts):

  • :width (Integer) — default: 640

    sets default canvas to a particular width in pixels

  • :height (Integer) — default: 480

    sets default canvas to a particular height in pixels

  • :update_interval (Float) — default: 16.666666

    sets the update interval in milliseconds between updates

  • :border (Boolean) — default: false

    draws a border around the default canvas

  • :canvas (Dare::Canvas) — default: Dare.default_canvas

    a canvas to refer to when drawing.

  • :mouse (Boolean) — default: true

    turn off mouse event listeners by setting to false



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dare/window.rb', line 21

def initialize(opts = {})
  opts[:width] ||= 640
  opts[:height] ||= 480
  opts[:update_interval] ||= 16.666666
  opts[:border] ||= false
  opts[:canvas] ||= Canvas.new(width: opts[:width], height: opts[:height], border: opts[:border])
  opts[:mouse] ||= true
  @width = opts[:width]
  @height = opts[:height]
  @update_interval = opts[:update_interval]
  @clock = opts[:clock]
  @canvas = opts[:canvas]
  Dare.default_canvas ||= @canvas
  @keys = []
  add_mouse_event_listener if opts[:mouse]
  add_keyboard_event_listeners
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



10
11
12
# File 'lib/dare/window.rb', line 10

def canvas
  @canvas
end

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/dare/window.rb', line 10

def height
  @height
end

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/dare/window.rb', line 10

def key
  @key
end

#mouse_xObject (readonly)

Returns the value of attribute mouse_x.



10
11
12
# File 'lib/dare/window.rb', line 10

def mouse_x
  @mouse_x
end

#mouse_yObject (readonly)

Returns the value of attribute mouse_y.



10
11
12
# File 'lib/dare/window.rb', line 10

def mouse_y
  @mouse_y
end

#ticksObject (readonly)

Returns the value of attribute ticks.



10
11
12
# File 'lib/dare/window.rb', line 10

def ticks
  @ticks
end

#update_intervalObject (readonly)

Returns the value of attribute update_interval.



10
11
12
# File 'lib/dare/window.rb', line 10

def update_interval
  @update_interval
end

#widthObject (readonly)

Returns the value of attribute width.



10
11
12
# File 'lib/dare/window.rb', line 10

def width
  @width
end

Instance Method Details

#add_keyboard_event_listenersObject

adds keyboard event listeners to entire page



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dare/window.rb', line 77

def add_keyboard_event_listeners
  Element.find("html").on :keydown do |event|
    @keys[get_key_id(event)] = true
  end
  Element.find("html").on :keyup do |event|
    @keys[get_key_id(event)] = false
  end
  ::Window.on :blur do |event|
    @keys.fill false
  end
end

#add_mouse_event_listenerObject

adds mousemove event listener to main canvas



68
69
70
71
72
73
74
# File 'lib/dare/window.rb', line 68

def add_mouse_event_listener
  Element.find("##{@canvas.id}").on :mousemove do |event|
    coords = get_cursor_position(event)
    @mouse_x = coords.x[:x]
    @mouse_y = coords.x[:y]
  end
end

#button_down?(button) ⇒ Boolean

checks to see if button passed is currently being pressed

Returns:

  • (Boolean)


90
91
92
# File 'lib/dare/window.rb', line 90

def button_down?(button)
  @keys[button]
end

#caption=(title) ⇒ Object Also known as: title=

sets the caption/title of the window to the string passed



162
163
164
# File 'lib/dare/window.rb', line 162

def caption=(title)
  `document.getElementById('pageTitle').innerHTML = #{title}`
end

#drawObject

gets run every frame of animation override this in your subclass of Dare::Window



57
58
59
# File 'lib/dare/window.rb', line 57

def draw

end

#draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z) ⇒ Object

works the same as Gosu::Window.draw_quad



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
# File 'lib/dare/window.rb', line 131

def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z)
  %x{
    var QUALITY = 256;

    var canvas_colors = document.createElement( 'canvas' );
    canvas_colors.width = 2;
    canvas_colors.height = 2;

    var context_colors = canvas_colors.getContext( '2d' );
    context_colors.fillStyle = 'rgba(0,0,0,1)';
    context_colors.fillRect( 0, 0, 2, 2 );

    var image_colors = context_colors.getImageData( 0, 0, 2, 2 );
    var data = image_colors.data;

    var canvas_render = #{@canvas};

    var context_render = #{@canvas.context};
    context_render.translate( - QUALITY / 2, - QUALITY / 2 );
    context_render.scale( QUALITY, QUALITY );

    data[ 0 ] = 255; // Top-left, red component
    data[ 5 ] = 255; // Top-right, green component
    data[ 10 ] = 255; // Bottom-left, blue component

    context_colors.putImageData( image_colors, 0, 0 );
    context_render.drawImage( canvas_colors, 0, 0 );
  }
end

#draw_rect(opts = {}) ⇒ Object

draws a rectangle starting at (top_left, top_left) down to (top_leftwidth, top_leftheight)



119
120
121
122
123
124
125
126
127
128
# File 'lib/dare/window.rb', line 119

def draw_rect(opts = {})
  x = opts[:top_left][0]
  y = opts[:top_left][1]
  width = opts[:width]
  height = opts[:height]
  color = opts[:color]

  `#{@canvas.context}.fillStyle = #{color}`
  `#{@canvas.context}.fillRect(#{x}, #{y}, #{width}, #{height})`
end

#fullscreen?Boolean

checks if game is fullscreen. currently not implemented.

Returns:

  • (Boolean)


169
170
171
# File 'lib/dare/window.rb', line 169

def fullscreen?
  false
end

#get_cursor_position(event) ⇒ Object

sets mouse_x and mouse_y to current mouse positions relative to the main canvas



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dare/window.rb', line 96

def get_cursor_position(event)
  if (event.page_x && event.page_y)
    x = event.page_x
    y = event.page_y
  else
    doc = Opal.Document[0]
    x = event[:clientX] + doc.scrollLeft +
          doc.documentElement.scrollLeft
    y = event[:clientY] + doc.body.scrollTop +
          doc.documentElement.scrollTop
  end
  x -= `#{@canvas.canvas}.offsetLeft`
  y -= `#{@canvas.canvas}.offsetTop`
  Coordinates.new(x: x, y: y)
end

#get_key_id(event) ⇒ Object

retrieves key code of current pressed key for keydown or keyup event



113
114
115
# File 'lib/dare/window.rb', line 113

def get_key_id(event)
  event[:keyCode]
end

#run!Object

starts the game loop for the window.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dare/window.rb', line 40

def run!
  %x{
    function update_loop() {
      #{update};
    }
    function anim_loop() {
      requestAnimationFrame(anim_loop);
      #{@canvas.canvas}.width = #{@canvas.canvas}.width;
      #{draw};
    }
    setInterval(update_loop, #{@update_interval});
    requestAnimationFrame(anim_loop);
  }
end

#text_inputObject

this is here for Gosu API compatability



174
# File 'lib/dare/window.rb', line 174

def text_input; end

#updateObject

gets run every update_interval if it can override this in your subclass of Dare::Window



63
64
65
# File 'lib/dare/window.rb', line 63

def update

end