Class: Hate::Graphics::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/hate/graphics/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w = nil, h = nil, title = 'New Game') ⇒ 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
# File 'lib/hate/graphics/window.rb', line 14

def initialize(w=nil, h=nil, title='New Game')
  @width  = w || Hate::Graphics::DEFAULT_WIDTH
  @height = h || Hate::Graphics::DEFAULT_HEIGHT
  
  @frames = 0
  
  if SDL.Init(SDL::INIT_EVERYTHING) == 0
     SDL.EnableUNICODE(1)
  else
     throw Main::Graphics::SDLException, "SDL Exception: #{SDL.GetError()}"
  end
  SDL.SetVideoMode(@width, @height, 24, SDL::OPENGL)
  SDL.Init(SDL::INIT_VIDEO)

  glEnable(GL_DEPTH_TEST)
  glEnable(GL_CULL_FACE)
  glEnable(GL_COLOR_MATERIAL)
  
  glShadeModel(GL_SMOOTH)
  glClearColor(1.0, 1.0, 1.0, 0.0)
  glClearDepth(1.0)
  glDepthFunc(GL_LEQUAL)
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Small performance hit
  
  # Hardware lights
  Hate::Graphics::Manager.lights.each_with_index do |light, i|
    glLightfv(eval("GL_LIGHT%i" % (i + 1)), GL_AMBIENT, light.ambient)
    glLightfv(eval("GL_LIGHT%i" % (i + 1)), GL_DIFFUSE, light.diffuse)
    glLightfv(eval("GL_LIGHT%i" % (i + 1)), GL_POSITION, light.position)
    glEnable(eval("GL_LIGHT%i" % (i + 1)))
  end
  glEnable(GL_LIGHTING) if Hate::Graphics::Manager.lights.size > 0
  
  reshape(@width, @height)
end

Instance Attribute Details

#framesObject (readonly)

Returns the value of attribute frames.



12
13
14
# File 'lib/hate/graphics/window.rb', line 12

def frames
  @frames
end

#heightObject (readonly)

Returns the value of attribute height.



11
12
13
# File 'lib/hate/graphics/window.rb', line 11

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



11
12
13
# File 'lib/hate/graphics/window.rb', line 11

def width
  @width
end

Instance Method Details

#drawObject



50
51
52
53
54
55
56
# File 'lib/hate/graphics/window.rb', line 50

def draw
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  Hate::Core::Callbacks.draw
  Hate::Graphics::Manager.run
  SDL.GL_SwapBuffers()
  @frames += 1
end

#events(event) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hate/graphics/window.rb', line 69

def events(event)
  case event.type
  when SDL::KEYDOWN
    Hate::Input::Keyboard.pressed(event.keysym.sym)
  when SDL::KEYUP
    Hate::Input::Keyboard.released(event.keysym.sym)
  when SDL::MOUSEBUTTONDOWN
    Hate::Input::Mouse.pressed(event.x, event.y, event.button)
  when SDL::MOUSEBUTTONUP
    Hate::Input::Mouse.released(event.x, event.y, event.button)
  when SDL::MOUSEMOTION
    Hate::Input::Mouse.motion(event.x, event.y, event.xrel, event.yrel, event.state)
  when SDL::VIDEORESIZE
    reshape(event.w, event.h)
  end
end

#quitObject



97
98
99
100
# File 'lib/hate/graphics/window.rb', line 97

def quit
  SDL.QuitSubSystem(SDL::INIT_VIDEO)
  SDL.Quit
end

#reshape(x, y) ⇒ Object



58
59
60
# File 'lib/hate/graphics/window.rb', line 58

def reshape(x, y)
  Hate::Graphics::Manager.default_camera.reshape(x, y)
end

#startObject



90
91
92
93
94
95
# File 'lib/hate/graphics/window.rb', line 90

def start
  loop do
    update
    draw
  end
end

#timeObject



86
87
88
# File 'lib/hate/graphics/window.rb', line 86

def time
  
end

#updateObject



62
63
64
65
66
67
# File 'lib/hate/graphics/window.rb', line 62

def update
  until (event = SDL::PollEvent()).nil?
    events(event)
  end
  Hate::Core::Callbacks.update if @frames % 2 == 0 # Only call update every second cycle
end