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.



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

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)
  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.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def char_input_handler
  @char_input_handler
end

#cursor_pos_handlerObject

Returns the value of attribute cursor_pos_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def cursor_pos_handler
  @cursor_pos_handler
end

#framebuffer_size_handlerObject

Returns the value of attribute framebuffer_size_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def framebuffer_size_handler
  @framebuffer_size_handler
end

#key_press_handlerObject

Returns the value of attribute key_press_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def key_press_handler
  @key_press_handler
end

#key_release_handlerObject

Returns the value of attribute key_release_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def key_release_handler
  @key_release_handler
end

#key_repeat_handlerObject

Returns the value of attribute key_repeat_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def key_repeat_handler
  @key_repeat_handler
end

#mouse_button_press_handlerObject

Returns the value of attribute mouse_button_press_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def mouse_button_press_handler
  @mouse_button_press_handler
end

#mouse_button_release_handlerObject

Returns the value of attribute mouse_button_release_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def mouse_button_release_handler
  @mouse_button_release_handler
end

#scroll_handlerObject

Returns the value of attribute scroll_handler.



12
13
14
# File 'lib/mittsu/renderers/glfw_window.rb', line 12

def scroll_handler
  @scroll_handler
end

Instance Method Details

#framebuffer_sizeObject



87
88
89
90
91
# File 'lib/mittsu/renderers/glfw_window.rb', line 87

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



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

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)


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

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

#joystick_buttons(joystick = GLFW_JOYSTICK_1) ⇒ Object



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

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

#joystick_name(joystick = GLFW_JOYSTICK_1) ⇒ Object



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

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

#joystick_present?(joystick = GLFW_JOYSTICK_1) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#key_down?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#mouse_button_down?(button) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#mouse_positionObject



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

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



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

def on_character_input &block
  @char_input_handler = block
end

#on_joystick_button_pressed(&block) ⇒ Object



156
157
158
# File 'lib/mittsu/renderers/glfw_window.rb', line 156

def on_joystick_button_pressed &block
  @joystick_button_press_handler = block
end

#on_joystick_button_released(&block) ⇒ Object



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

def on_joystick_button_released &block
  @joystick_button_release_handler = block
end

#on_key_pressed(&block) ⇒ Object



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

def on_key_pressed &block
  @key_press_handler = block
end

#on_key_released(&block) ⇒ Object



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

def on_key_released &block
  @key_release_handler = block
end

#on_key_typed(&block) ⇒ Object



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

def on_key_typed &block
  @key_repeat_handler = block
end

#on_mouse_button_pressed(&block) ⇒ Object



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

def on_mouse_button_pressed &block
  @mouse_button_press_handler = block
end

#on_mouse_button_released(&block) ⇒ Object



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

def on_mouse_button_released &block
  @mouse_button_release_handler = block
end

#on_mouse_move(&block) ⇒ Object



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

def on_mouse_move &block
  @cursor_pos_handler = block
end

#on_resize(&block) ⇒ Object



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

def on_resize &block
  @framebuffer_size_handler = block
end

#on_scroll(&block) ⇒ Object



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

def on_scroll &block
  @scroll_handler = block
end

#runObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mittsu/renderers/glfw_window.rb', line 75

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

    glfwSwapBuffers @handle
    glfwPollEvents
    poll_joystick_events
  end
  glfwDestroyWindow @handle
  glfwTerminate
end