Class: Mittsu::GLFW::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/renderers/glfw_window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, title) ⇒ Window

Returns a new instance of Window.



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
# File 'lib/mittsu/renderers/glfw_window.rb', line 26

def initialize(width, height, title)
  glfwInit

  glfwWindowHint GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE
  glfwWindowHint GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE
  glfwWindowHint GLFW_CONTEXT_VERSION_MAJOR, 3
  glfwWindowHint GLFW_CONTEXT_VERSION_MINOR, 3
  glfwWindowHint GLFW_CONTEXT_REVISION, 0

  @width, @height, @title = width, height, title
  @handle = glfwCreateWindow(@width, @height, @title, nil, nil)
  if @handle.null?
    raise "Unable to create window."
  end
  glfwMakeContextCurrent @handle
  glfwSwapInterval 1

  this = self
  @key_callback = ::GLFW::create_callback(:GLFWkeyfun) do |window_handle, key, scancode, action, mods|
    if action == GLFW_PRESS
      this.key_press_handler.call(key) unless this.key_press_handler.nil?
      this.key_repeat_handler.call(key) unless this.key_repeat_handler.nil?
    elsif action == GLFW_RELEASE
      this.key_release_handler.call(key) unless this.key_release_handler.nil?
    elsif action == GLFW_REPEAT
      this.key_repeat_handler.call(key) unless this.key_repeat_handler.nil?
    end
  end
  glfwSetKeyCallback(@handle, @key_callback)

  @char_callback = ::GLFW::create_callback(:GLFWcharfun) do |window_handle, codepoint|
    char = [codepoint].pack('U')
    this.char_input_handler.call(char) unless this.char_input_handler.nil?
  end
  glfwSetCharCallback(@handle, @char_callback)

  @cursor_pos_callback = ::GLFW::create_callback(:GLFWcursorposfun) do |window_handle, xpos, ypos|
    this.cursor_pos_handler.call(Vector2.new(xpos, ypos)) unless this.cursor_pos_handler.nil?
  end
  glfwSetCursorPosCallback(@handle, @cursor_pos_callback)

  @mouse_button_callback = ::GLFW::create_callback(:GLFWmousebuttonfun) do |window_handle, button, action, mods|
    mpos = this.mouse_position
    if action == GLFW_PRESS
      this.mouse_button_press_handler.call(button, mpos) unless this.mouse_button_press_handler.nil?
    elsif action == GLFW_RELEASE
      this.mouse_button_release_handler.call(button, mpos) unless this.mouse_button_release_handler.nil?
    end
  end
  glfwSetMouseButtonCallback(@handle, @mouse_button_callback)

  @scroll_callback = ::GLFW::create_callback(:GLFWscrollfun) do |window_handle, xoffset, yoffset|
    this.scroll_handler.call(Vector2.new(xoffset, yoffset)) unless this.scroll_handler.nil?
  end
  glfwSetScrollCallback(@handle, @scroll_callback)

  @frabuffer_size_callback = ::GLFW::create_callback(:GLFWframebuffersizefun) do |window_handle, new_width, new_height|
    this.framebuffer_size_handler.call(new_width, new_height) unless this.framebuffer_size_handler.nil?
  end
  glfwSetFramebufferSizeCallback(@handle, @frabuffer_size_callback)

  @joystick_buttons = poll_all_joysticks_buttons
end

Instance Attribute Details

#char_input_handlerObject

Returns the value of attribute char_input_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def char_input_handler
  @char_input_handler
end

#cursor_pos_handlerObject

Returns the value of attribute cursor_pos_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def cursor_pos_handler
  @cursor_pos_handler
end

#framebuffer_size_handlerObject

Returns the value of attribute framebuffer_size_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def framebuffer_size_handler
  @framebuffer_size_handler
end

#key_press_handlerObject

Returns the value of attribute key_press_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def key_press_handler
  @key_press_handler
end

#key_release_handlerObject

Returns the value of attribute key_release_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def key_release_handler
  @key_release_handler
end

#key_repeat_handlerObject

Returns the value of attribute key_repeat_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def key_repeat_handler
  @key_repeat_handler
end

#mouse_button_press_handlerObject

Returns the value of attribute mouse_button_press_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def mouse_button_press_handler
  @mouse_button_press_handler
end

#mouse_button_release_handlerObject

Returns the value of attribute mouse_button_release_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def mouse_button_release_handler
  @mouse_button_release_handler
end

#scroll_handlerObject

Returns the value of attribute scroll_handler.



24
25
26
# File 'lib/mittsu/renderers/glfw_window.rb', line 24

def scroll_handler
  @scroll_handler
end

Instance Method Details

#framebuffer_sizeObject



102
103
104
105
106
# File 'lib/mittsu/renderers/glfw_window.rb', line 102

def framebuffer_size
  width, height = ' '*8, ' '*8
  glfwGetFramebufferSize(@handle, width, height)
  [width.unpack('L')[0], height.unpack('L')[0]]
end

#joystick_axes(joystick = GLFW_JOYSTICK_1) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/mittsu/renderers/glfw_window.rb', line 163

def joystick_axes(joystick = GLFW_JOYSTICK_1)
  return [] unless joystick_present?(joystick)
  count = ' ' * 4
  array = glfwGetJoystickAxes(joystick, count)
  count = count.unpack('l')[0]
  array[0, count * 4].unpack('f' * count)
end

#joystick_button_down?(button, joystick = GLFW_JOYSTICK_1) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/mittsu/renderers/glfw_window.rb', line 183

def joystick_button_down?(button, joystick = GLFW_JOYSTICK_1)
  @joystick_buttons[joystick][button]
end

#joystick_buttons(joystick = GLFW_JOYSTICK_1) ⇒ Object



158
159
160
161
# File 'lib/mittsu/renderers/glfw_window.rb', line 158

def joystick_buttons(joystick = GLFW_JOYSTICK_1)
  @joystick_buttons = poll_all_joysticks_buttons
  @joystick_buttons[joystick]
end

#joystick_name(joystick = GLFW_JOYSTICK_1) ⇒ Object



187
188
189
# File 'lib/mittsu/renderers/glfw_window.rb', line 187

def joystick_name(joystick = GLFW_JOYSTICK_1)
  glfwGetJoystickName(joystick)
end

#joystick_present?(joystick = GLFW_JOYSTICK_1) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/mittsu/renderers/glfw_window.rb', line 179

def joystick_present?(joystick = GLFW_JOYSTICK_1)
  glfwJoystickPresent(joystick).nonzero?
end

#key_down?(key) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/mittsu/renderers/glfw_window.rb', line 120

def key_down?(key)
  glfwGetKey(@handle, key) == GLFW_PRESS
end

#mouse_button_down?(button) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/mittsu/renderers/glfw_window.rb', line 146

def mouse_button_down?(button)
  glfwGetMouseButton(@handle, button) == GLFW_PRESS
end

#mouse_positionObject



140
141
142
143
144
# File 'lib/mittsu/renderers/glfw_window.rb', line 140

def mouse_position
  xpos, ypos = ' '*8, ' '*8
  glfwGetCursorPos(@handle, xpos, ypos);
  Vector2.new(xpos.unpack('D')[0], ypos.unpack('D')[0])
end

#on_character_input(&block) ⇒ Object



124
125
126
# File 'lib/mittsu/renderers/glfw_window.rb', line 124

def on_character_input &block
  @char_input_handler = block
end

#on_joystick_button_pressed(&block) ⇒ Object



171
172
173
# File 'lib/mittsu/renderers/glfw_window.rb', line 171

def on_joystick_button_pressed &block
  @joystick_button_press_handler = block
end

#on_joystick_button_released(&block) ⇒ Object



175
176
177
# File 'lib/mittsu/renderers/glfw_window.rb', line 175

def on_joystick_button_released &block
  @joystick_button_release_handler = block
end

#on_key_pressed(&block) ⇒ Object



108
109
110
# File 'lib/mittsu/renderers/glfw_window.rb', line 108

def on_key_pressed &block
  @key_press_handler = block
end

#on_key_released(&block) ⇒ Object



112
113
114
# File 'lib/mittsu/renderers/glfw_window.rb', line 112

def on_key_released &block
  @key_release_handler = block
end

#on_key_typed(&block) ⇒ Object



116
117
118
# File 'lib/mittsu/renderers/glfw_window.rb', line 116

def on_key_typed &block
  @key_repeat_handler = block
end

#on_mouse_button_pressed(&block) ⇒ Object



132
133
134
# File 'lib/mittsu/renderers/glfw_window.rb', line 132

def on_mouse_button_pressed &block
  @mouse_button_press_handler = block
end

#on_mouse_button_released(&block) ⇒ Object



136
137
138
# File 'lib/mittsu/renderers/glfw_window.rb', line 136

def on_mouse_button_released &block
  @mouse_button_release_handler = block
end

#on_mouse_move(&block) ⇒ Object



128
129
130
# File 'lib/mittsu/renderers/glfw_window.rb', line 128

def on_mouse_move &block
  @cursor_pos_handler = block
end

#on_resize(&block) ⇒ Object



154
155
156
# File 'lib/mittsu/renderers/glfw_window.rb', line 154

def on_resize &block
  @framebuffer_size_handler = block
end

#on_scroll(&block) ⇒ Object



150
151
152
# File 'lib/mittsu/renderers/glfw_window.rb', line 150

def on_scroll &block
  @scroll_handler = block
end

#runObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mittsu/renderers/glfw_window.rb', line 90

def run
  while glfwWindowShouldClose(@handle) == 0
    yield

    glfwSwapBuffers @handle
    glfwPollEvents
    poll_joystick_events
  end
  glfwDestroyWindow @handle
  glfwTerminate
end