Class: MiniGL::GameWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/minigl/global.rb

Overview

The main class for a MiniGL game, holds references to globally accessible objects and constants.

Instance Method Summary collapse

Constructor Details

#initialize(scr_w, scr_h = nil, fullscreen = true, gravity = Vector.new(0, 1), min_speed = Vector.new(0.01, 0.01), ramp_contact_threshold = 4, ramp_slip_threshold = 1, ramp_slip_force = 1, kb_held_delay = 40, kb_held_interval = 5, double_click_delay = 8) ⇒ GameWindow

Creates a game window (initializing a game with all MiniGL features enabled).

Parameters:

scr_w

Width of the window, in pixels.

scr_h

Height of the window, in pixels.

fullscreen

Whether the window must be initialized in full screen mode.

gravity

A Vector object representing the horizontal and vertical components of the force of gravity. Essentially, this force will be applied to every object which calls move, from the Movement module.

min_speed

A Vector with the minimum speed for moving objects, i.e., the value below which the speed will be rounded to zero.

ramp_contact_threshold

The maximum horizontal movement an object can perform in a single frame and keep contact with a ramp when it’s above one.

ramp_slip_threshold

The maximum ratio between height and width of a ramp above which the objects will always slip down when trying to ‘climb’ that ramp.

ramp_slip_force

The force that will be applied in the horizontal direction when the object is slipping from a steep ramp.

kb_held_delay

The number of frames a key must be held by the user before the “held” event (that can be checked with KB.key_held?) starts to trigger.

kb_held_interval

The interval, in frames, between each triggering of the “held” event, after the key has been held for more than kb_held_delay frames.

double_click_delay

The maximum interval, in frames, between two clicks, to trigger the “double click” event (checked with Mouse.double_click?).

Obs.: This method accepts named parameters, but scr_w and scr_h are mandatory.



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
# File 'lib/minigl/global.rb', line 199

def initialize(scr_w, scr_h = nil, fullscreen = true,
               gravity = Vector.new(0, 1), min_speed = Vector.new(0.01, 0.01),
               ramp_contact_threshold = 4, ramp_slip_threshold = 1, ramp_slip_force = 1,
               kb_held_delay = 40, kb_held_interval = 5, double_click_delay = 8)
  if scr_w.is_a? Hash
    scr_h = scr_w[:scr_h]
    fullscreen = scr_w.fetch(:fullscreen, true)
    gravity = scr_w.fetch(:gravity, Vector.new(0, 1))
    min_speed = scr_w.fetch(:min_speed, Vector.new(0.01, 0.01))
    ramp_contact_threshold = scr_w.fetch(:ramp_contact_threshold, 4)
    ramp_slip_threshold = scr_w.fetch(:ramp_slip_threshold, 1.1)
    ramp_slip_force = scr_w.fetch(:ramp_slip_force, 0.1)
    kb_held_delay = scr_w.fetch(:kb_held_delay, 40)
    kb_held_interval = scr_w.fetch(:kb_held_interval, 5)
    double_click_delay = scr_w.fetch(:double_click_delay, 8)
    scr_w = scr_w[:scr_w]
  end

  super scr_w, scr_h, fullscreen
  G.window = self
  G.gravity = gravity
  G.min_speed = min_speed
  G.ramp_contact_threshold = ramp_contact_threshold
  G.ramp_slip_threshold = ramp_slip_threshold
  G.ramp_slip_force = ramp_slip_force
  G.kb_held_delay = kb_held_delay
  G.kb_held_interval = kb_held_interval
  G.double_click_delay = double_click_delay
  KB.initialize
  Mouse.initialize
  Res.initialize
end

Instance Method Details

#button_down(id) ⇒ Object



249
250
251
252
# File 'lib/minigl/global.rb', line 249

def button_down(id)
  super
  Mouse.register_button_down(id)
end

#button_up(id) ⇒ Object



254
255
256
257
# File 'lib/minigl/global.rb', line 254

def button_up(id)
  super
  Mouse.register_button_up(id)
end

#clear(color) ⇒ Object

Draws a rectangle with the size of the entire screen, in the given color.

Parameters:

color

Color of the rectangle to be drawn, in hexadecimal RRGGBB format.



236
237
238
239
240
241
242
# File 'lib/minigl/global.rb', line 236

def clear(color)
  color |= 0xff000000
  draw_quad 0, 0, color,
            width, 0, color,
            width, height, color,
            0, height, color, 0
end

#toggle_fullscreenObject

Toggles the window between windowed and full screen mode.



245
246
247
# File 'lib/minigl/global.rb', line 245

def toggle_fullscreen
  self.fullscreen = !fullscreen?
end