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.



16
17
18
19
20
21
22
23
24
25
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
# File 'lib/mittsu/renderers/glfw_window.rb', line 16

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.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def char_input_handler
  @char_input_handler
end

#cursor_pos_handlerObject

Returns the value of attribute cursor_pos_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def cursor_pos_handler
  @cursor_pos_handler
end

#framebuffer_size_handlerObject

Returns the value of attribute framebuffer_size_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def framebuffer_size_handler
  @framebuffer_size_handler
end

#key_press_handlerObject

Returns the value of attribute key_press_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def key_press_handler
  @key_press_handler
end

#key_release_handlerObject

Returns the value of attribute key_release_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def key_release_handler
  @key_release_handler
end

#key_repeat_handlerObject

Returns the value of attribute key_repeat_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def key_repeat_handler
  @key_repeat_handler
end

#mouse_button_press_handlerObject

Returns the value of attribute mouse_button_press_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def mouse_button_press_handler
  @mouse_button_press_handler
end

#mouse_button_release_handlerObject

Returns the value of attribute mouse_button_release_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def mouse_button_release_handler
  @mouse_button_release_handler
end

#scroll_handlerObject

Returns the value of attribute scroll_handler.



14
15
16
# File 'lib/mittsu/renderers/glfw_window.rb', line 14

def scroll_handler
  @scroll_handler
end

Instance Method Details

#framebuffer_sizeObject



92
93
94
95
96
# File 'lib/mittsu/renderers/glfw_window.rb', line 92

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



153
154
155
156
157
158
159
# File 'lib/mittsu/renderers/glfw_window.rb', line 153

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)


173
174
175
# File 'lib/mittsu/renderers/glfw_window.rb', line 173

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

#joystick_buttons(joystick = GLFW_JOYSTICK_1) ⇒ Object



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

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

#joystick_name(joystick = GLFW_JOYSTICK_1) ⇒ Object



177
178
179
# File 'lib/mittsu/renderers/glfw_window.rb', line 177

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

#joystick_present?(joystick = GLFW_JOYSTICK_1) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/mittsu/renderers/glfw_window.rb', line 169

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

#key_down?(key) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/mittsu/renderers/glfw_window.rb', line 110

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

#mouse_button_down?(button) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#mouse_positionObject



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

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



114
115
116
# File 'lib/mittsu/renderers/glfw_window.rb', line 114

def on_character_input &block
  @char_input_handler = block
end

#on_joystick_button_pressed(&block) ⇒ Object



161
162
163
# File 'lib/mittsu/renderers/glfw_window.rb', line 161

def on_joystick_button_pressed &block
  @joystick_button_press_handler = block
end

#on_joystick_button_released(&block) ⇒ Object



165
166
167
# File 'lib/mittsu/renderers/glfw_window.rb', line 165

def on_joystick_button_released &block
  @joystick_button_release_handler = block
end

#on_key_pressed(&block) ⇒ Object



98
99
100
# File 'lib/mittsu/renderers/glfw_window.rb', line 98

def on_key_pressed &block
  @key_press_handler = block
end

#on_key_released(&block) ⇒ Object



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

def on_key_released &block
  @key_release_handler = block
end

#on_key_typed(&block) ⇒ Object



106
107
108
# File 'lib/mittsu/renderers/glfw_window.rb', line 106

def on_key_typed &block
  @key_repeat_handler = block
end

#on_mouse_button_pressed(&block) ⇒ Object



122
123
124
# File 'lib/mittsu/renderers/glfw_window.rb', line 122

def on_mouse_button_pressed &block
  @mouse_button_press_handler = block
end

#on_mouse_button_released(&block) ⇒ Object



126
127
128
# File 'lib/mittsu/renderers/glfw_window.rb', line 126

def on_mouse_button_released &block
  @mouse_button_release_handler = block
end

#on_mouse_move(&block) ⇒ Object



118
119
120
# File 'lib/mittsu/renderers/glfw_window.rb', line 118

def on_mouse_move &block
  @cursor_pos_handler = block
end

#on_resize(&block) ⇒ Object



144
145
146
# File 'lib/mittsu/renderers/glfw_window.rb', line 144

def on_resize &block
  @framebuffer_size_handler = block
end

#on_scroll(&block) ⇒ Object



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

def on_scroll &block
  @scroll_handler = block
end

#runObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mittsu/renderers/glfw_window.rb', line 80

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

    glfwSwapBuffers @handle
    glfwPollEvents
    poll_joystick_events
  end
  glfwDestroyWindow @handle
  glfwTerminate
end

#set_mouselock(value) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/mittsu/renderers/glfw_window.rb', line 181

def set_mouselock(value)
  if value
    glfwSetInputMode(@handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED)
  else
    glfwSetInputMode(@handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL)
  end
end