Class: RBGLox::Window

Inherits:
Object
  • Object
show all
Includes:
Gl, Glfw, Glu, Glut
Defined in:
lib/rbglox/window.rb

Overview

Semi-abstract Window class

Direct Known Subclasses

App

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWindow

Constructor



11
12
13
14
15
16
17
18
# File 'lib/rbglox/window.rb', line 11

def initialize
  @title = 'rbGLox'
  @width = 640
  @height= 480
  @multisample = 0
  @vsync = 0
  @watch = nil
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



8
9
10
# File 'lib/rbglox/window.rb', line 8

def height
  @height
end

#multisampleObject

Returns the value of attribute multisample.



8
9
10
# File 'lib/rbglox/window.rb', line 8

def multisample
  @multisample
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/rbglox/window.rb', line 8

def title
  @title
end

#vsyncObject

Returns the value of attribute vsync.



8
9
10
# File 'lib/rbglox/window.rb', line 8

def vsync
  @vsync
end

#watchObject

Returns the value of attribute watch.



8
9
10
# File 'lib/rbglox/window.rb', line 8

def watch
  @watch
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/rbglox/window.rb', line 8

def width
  @width
end

Instance Method Details

#begin_frameObject

User defined method called when entering a frame



148
149
150
151
# File 'lib/rbglox/window.rb', line 148

def begin_frame
  glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
  glLoadIdentity
end

#closeObject

Closes the currently active open window



21
22
23
# File 'lib/rbglox/window.rb', line 21

def close
  glfwCloseWindow
end

#drawObject

User defined draw method

Raises:

  • (NotImplementedError)


159
160
161
# File 'lib/rbglox/window.rb', line 159

def draw
   raise NotImplementedError
end

#end_frameObject

User defined method called when leaving the frame



154
155
156
# File 'lib/rbglox/window.rb', line 154

def end_frame
  glfwSwapBuffers
end

#execObject

Opens the window and fires up the event loop



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rbglox/window.rb', line 61

def exec
  glutInit

  glfwOpenWindowHint GLFW_WINDOW_NO_RESIZE, true
  #glfwOpenWindowHint GLFW_FSAA_SAMPLE, multisample
 
  glfwOpenWindow width, height, 0, 0, 0, 0, 32, 0, GLFW_WINDOW

  glfwSetWindowPos 0, 0
  glfwSetWindowTitle title
  glfwEnable GLFW_KEY_REPEAT
  glfwSwapInterval vsync

  glfwSetWindowSizeCallback lambda { |w, h| resize(w, h) }

  trap('INT') do
    glfwCloseWindow
  end

  unless watch.nil?
    puts "Watching #{watch} for changes ..."
    dw = DirectoryWatcher.new watch, :glob => "**/*", :pre_load => true, :interval => 1
    dw.add_observer do |*args|
      args.each do |event|
        reload event
      end
    end
    dw.start
  end

  glClearColor 0.2, 0.2, 0.2, 1
glClearDepth 1.0

  glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL 
	
  glShadeModel GL_SMOOTH
  glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST

  glEnable GL_TEXTURE_2D
glEnable GL_COLOR_MATERIAL
glEnable GL_NORMALIZE

  puts 'init'
  init

  while true
    break unless glfwGetWindowParam GLFW_OPENED
    break if key? GLFW_KEY_ESC
    
    update

    begin_frame
      draw
    end_frame
  end

  unless watch.nil?
    dw.stop
  end

  shutdown
  puts 'shutdown'
end

#initObject

User defined initializations



127
128
# File 'lib/rbglox/window.rb', line 127

def init
end

#key?(key) ⇒ Boolean

Returns TRUE if the given key is PRESSED, otherwise FALSE

Returns:

  • (Boolean)


26
27
28
# File 'lib/rbglox/window.rb', line 26

def key?(key)
  glfwGetKey(key) == GLFW_PRESS
end

#mouse?(button = GLFW_MOUSE_BUTTON_LEFT) ⇒ Boolean

Returns TRUE if the given mouse button is PRESSED, otherwise FALSE

Returns:

  • (Boolean)


31
32
33
# File 'lib/rbglox/window.rb', line 31

def mouse?(button = GLFW_MOUSE_BUTTON_LEFT)
  glfwGetMouseButton(button) == GLFW_PRESS
end

#mouse_left?Boolean

Return TRUE if the left mouse button is PRESSED, otherwise FALSE

Returns:

  • (Boolean)


36
37
38
# File 'lib/rbglox/window.rb', line 36

def mouse_left?
  mouse?
end

#mouse_pos?Boolean

Returns the current mouse position (tuple)

Returns:

  • (Boolean)


46
47
48
# File 'lib/rbglox/window.rb', line 46

def mouse_pos?
  glfwGetMousePos
end

#mouse_right?Boolean

Return TRUE if the right mouse button is PRESSED, otherwise FALSE

Returns:

  • (Boolean)


41
42
43
# File 'lib/rbglox/window.rb', line 41

def mouse_right?
  mouse? GLFW_MOUSE_BUTTON_RIGHT
end

#mouse_wheel?Boolean

Returns the current mouse wheel delta position

Returns:

  • (Boolean)


51
52
53
# File 'lib/rbglox/window.rb', line 51

def mouse_wheel?
  glfwGetMouseWheel
end

#reload(event) ⇒ Object

User defined reload event method



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

def reload(event)
  # empty
end

#resize(w, h) ⇒ Object

User defined window resize, sets up viewport and perspective projection



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rbglox/window.rb', line 135

def resize(w, h)
  glViewport 0, 0, w, h
	
glMatrixMode GL_PROJECTION
glLoadIdentity
	
gluPerspective 45.0, w.to_f/h.to_f, 0.1, 1000.0
	
glMatrixMode GL_MODELVIEW
glLoadIdentity
end

#shutdownObject

User defined shutdown



131
132
# File 'lib/rbglox/window.rb', line 131

def shutdown
end

#timeObject

Returns the current tick count



56
57
58
# File 'lib/rbglox/window.rb', line 56

def time
  glfwGetTime
end

#updateObject

User defined update method



164
165
166
# File 'lib/rbglox/window.rb', line 164

def update
  # empty
end