Class: GlTail::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/gl_tail/engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Engine

Returns a new instance of Engine.



200
201
202
203
204
205
206
# File 'lib/gl_tail/engine.rb', line 200

def initialize(config)
  @config = config

  @frames = 0
  @t0 = 0
  @left_left = @left_right = @right_left = @right_right = 0.0 # TODO: Why is draw called before these are set by reshape?
end

Instance Method Details

#char_sizeObject



16
17
18
# File 'lib/gl_tail/engine.rb', line 16

def char_size
  @char_size ||= (8.0 / (@config.screen.window_width / 2.0))
end

#do_processObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/gl_tail/engine.rb', line 256

def do_process
  active = @config.do_process

  if active >= 0

    if glutGet(GLUT_ELAPSED_TIME) - @since >= 1000
      @since = glutGet(GLUT_ELAPSED_TIME)
      @config.update
      BlobStore.prune
    end
  end

  self
end

#drawObject



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/gl_tail/engine.rb', line 36

def draw
  @render_time ||= 0
  @t = Time.new

  glClear(GL_COLOR_BUFFER_BIT);
  #    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glPushMatrix()

  positions = Hash.new

  reset_stats

  glPushMatrix()

  glColor([0.15, 0.15, 0.15, 1.0])
  glBegin(GL_QUADS)
    glNormal3f(1.0, 1.0, 0.0)

    glVertex3f(@left_left, @config.screen.aspect, 0.0)
    glVertex3f(@left_right, @config.screen.aspect, 0.0)
    glVertex3f(@left_right, -@config.screen.aspect, 0.0)
    glVertex3f(@left_left, -@config.screen.aspect, 0.0)

    glVertex3f(@right_left, @config.screen.aspect, 0.0)
    glVertex3f(@right_right, @config.screen.aspect, 0.0)
    glVertex3f(@right_right, -@config.screen.aspect, 0.0)
    glVertex3f(@right_left, -@config.screen.aspect, 0.0)

  glEnd()
  glPopMatrix()

  # TODO: do we really need to sort every block on every draw?!
  # Nope. But it was a hash, so keeping order was a bit hard.
  @config.blocks.sort { |k,v| k.order <=> v.order}.each do |block|
    # glPushMatrix + glTranslate3f to render each element relativ to its containing block instead of the screen?
    positions[block.is_right] = block.render(self, positions[block.is_right] || 0 )
  end

  glPopMatrix()

  @frames += 1
  t = glutGet(GLUT_ELAPSED_TIME)
  if t - @t0 >= 10000
    seconds = (t - @t0) / 1000.0
    $FPS = @frames / seconds
    printf("%d frames in %6.3f seconds = %6.3f FPS\n",
    @frames, seconds, $FPS) if $VRB > 0
    @t0, @frames = t, 0
    puts "Elements[#{stats[0]}], Activities[#{stats[1]}], Blobs[#{BlobStore.used}/#{BlobStore.size}]" if $VRB > 0
  end
  @render_time = (Time.new - @t)
end

#highlight_colorObject



24
25
26
# File 'lib/gl_tail/engine.rb', line 24

def highlight_color
  @config.screen.highlight_color
end

#idleObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gl_tail/engine.rb', line 90

def idle
  @last_run ||= Time.new
  @last_run_time ||= 0
  delta = (Time.new - @last_run) - @last_run_time
  if @config.screen.wanted_fps > 0 && delta < (1000.0/(@config.screen.wanted_fps*1000.0))
    sleep((1000.0/(@config.screen.wanted_fps*1000.0) - delta))
  end
  @last_run = Time.new
  glutPostRedisplay()
  glutSwapBuffers()
  do_process
  @last_run_time = (@last_run_time.to_f * (@config.screen.wanted_fps-1.0) + (Time.new - @last_run).to_f) / @config.screen.wanted_fps.to_f if @config.screen.wanted_fps > 0
end

#key(k, x, y) ⇒ Object

Change view angle, exit upon ESC



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gl_tail/engine.rb', line 115

def key(k, x, y)
  case k
  when 27 # Escape
    exit
  when 32 # Space
    @config.screen.bounce ||= false
    @config.screen.bounce = !@config.screen.bounce
  when 102 #f
    @config.screen.wanted_fps = case @config.screen.wanted_fps
                                when 0
                                  60
                                when 60
                                  50
                                when 50
                                  45
                                when 45
                                  30
                                when 30
                                  25
                                when 25
                                  20
                                when 20
                                  0
                                end
    puts "WANTED_FPS[#{@config.screen.wanted_fps}]"
  when 98
    @config.screen.mode = 1 - @config.screen.mode.to_i
    BlobStore.empty
  end
  puts "Keypress: #{k}"
  glutPostRedisplay()
end

#line_sizeObject



20
21
22
# File 'lib/gl_tail/engine.rb', line 20

def line_size
  @config.screen.line_size
end

#motion(x, y) ⇒ Object



194
195
196
197
198
# File 'lib/gl_tail/engine.rb', line 194

def motion(x, y)
  if @mouse == GLUT_DOWN then
  end
  @x0, @y0 = x, y
end

#mouse(button, state, x, y) ⇒ Object



189
190
191
192
# File 'lib/gl_tail/engine.rb', line 189

def mouse(button, state, x, y)
  @mouse = state
  @x0, @y0 = x, y
end

#render_string(left, right = nil) ⇒ Object



8
9
10
# File 'lib/gl_tail/engine.rb', line 8

def render_string(left,right = nil)
  FontStore.render_string(self, left,right)
end

#reset_statsObject



28
29
30
# File 'lib/gl_tail/engine.rb', line 28

def reset_stats
  @stats = [0, 0]
end

#reshape(width, height) ⇒ Object

New window size or exposure



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/gl_tail/engine.rb', line 154

def reshape(width, height)
  @config.reshape(width, height)

  # reset char size
  @char_size = nil

  @left_left = @config.screen.left.alignment + char_size * (@config.screen.left.size + 1)
  @left_right = @config.screen.left.alignment + char_size * (@config.screen.left.size + 8)

  @right_left = @config.screen.right.alignment - char_size * (@config.screen.right.size + 1)
  @right_right = @config.screen.right.alignment - char_size * (@config.screen.right.size + 8)

  glViewport(0, 0, width, height)
  glMatrixMode(GL_PROJECTION)
  glLoadIdentity()

  #    glFrustum(-1.0, 1.0, [email protected], @config.screen.aspect, 5.0, 60.0)
  glOrtho(-1.0, 1.0, -@config.screen.aspect, @config.screen.aspect, -1.0, 1.0)

  @config.screen.line_size = @config.screen.aspect * 2 / (@config.screen.window_height/13.0)
  @config.screen.top = @config.screen.aspect - @config.screen.line_size

  puts "Reshape: #{width}x#{height} = #{@config.screen.aspect}/#{@config.screen.line_size}" if $VRB > 0

  glMatrixMode(GL_MODELVIEW)
  glLoadIdentity()
  glTranslate(0.0, 0.0, 0.0)

  BlobStore.empty # Flush cached objects to recreate with correct size
end

#screenObject



12
13
14
# File 'lib/gl_tail/engine.rb', line 12

def screen
  @config.screen
end

#special(k, x, y) ⇒ Object

Change view angle



149
150
151
# File 'lib/gl_tail/engine.rb', line 149

def special(k, x, y)
  glutPostRedisplay()
end

#startObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/gl_tail/engine.rb', line 208

def start
  glutInit()
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)

  glutInitWindowPosition(0, 0)
  glutInitWindowSize(@config.screen.window_width, @config.screen.window_height)
  glutCreateWindow('glTail')

  glutDisplayFunc(method(:draw).to_proc)
  glutReshapeFunc(method(:reshape).to_proc)
  glutKeyboardFunc(method(:key).to_proc)
  glutSpecialFunc(method(:special).to_proc)
  glutVisibilityFunc(method(:visible).to_proc)
  glutMouseFunc(method(:mouse).to_proc)
  glutMotionFunc(method(:motion).to_proc)

  glutIdleFunc(method(:idle).to_proc)
  #    glutTimerFunc(33, method(:timer).to_proc, 0)

  glLightfv(GL_LIGHT0, GL_POSITION, [5.0, 5.0, 0.0, 0.0])
  glLightfv(GL_LIGHT0, GL_AMBIENT, [0,0,0,1])

  glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.1,0.1,0.1,1]);
#      glLightModel(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
#      glLightModel(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);

  glDisable(GL_CULL_FACE)
  glEnable(GL_LIGHTING)
  glEnable(GL_LIGHT0)
  glEnable(GL_TEXTURE_2D)
#      glShadeModel(GL_FLAT)
  glDisable(GL_DEPTH_TEST)
#      glDisable(GL_NORMALIZE)
#      glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)
  glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST )

  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
  glEnable(GL_COLOR_MATERIAL)
  glEnable(GL_NORMALIZE)
  FontStore.generate_font

  @since = glutGet(GLUT_ELAPSED_TIME)

  @config.init

  glutMainLoop()
end

#statsObject



32
33
34
# File 'lib/gl_tail/engine.rb', line 32

def stats
  @stats
end

#timer(value) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/gl_tail/engine.rb', line 104

def timer(value)
  glutTimerFunc(15, method(:timer).to_proc, 0)
  #    t = glutGet(GLUT_ELAPSED_TIME)
  glutPostRedisplay()
  glutSwapBuffers()
  do_process
  #    t = glutGet(GLUT_ELAPSED_TIME) - t
  #    t = 14 if t > 14
end

#visible(vis) ⇒ Object



185
186
187
# File 'lib/gl_tail/engine.rb', line 185

def visible(vis)
  #    glutIdleFunc((vis == GLUT_VISIBLE ? method(:idle).to_proc : nil))
end