Class: Quake::Window

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

Constant Summary collapse

DEFAULT_WIDTH =
1280
DEFAULT_HEIGHT =
720
TITLE =
"RubyQuake"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT, visible: true) ⇒ Window

Returns a new instance of Window.



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
# File 'lib/quake/window.rb', line 15

def initialize(width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT, visible: true)
  @width = width
  @height = height
  @visible = visible

  SDL.load_lib("/opt/homebrew/lib/libSDL2.dylib")
  SDL.Init(SDL::INIT_VIDEO)

  SDL.GL_SetAttribute(SDL::GL_CONTEXT_MAJOR_VERSION, 2)
  SDL.GL_SetAttribute(SDL::GL_CONTEXT_MINOR_VERSION, 1)
  SDL.GL_SetAttribute(SDL::GL_DOUBLEBUFFER, 1)
  SDL.GL_SetAttribute(SDL::GL_DEPTH_SIZE, 24)

  window_flags = SDL::WINDOW_OPENGL
  window_flags |= visible ? SDL::WINDOW_SHOWN : SDL::WINDOW_HIDDEN

  @window = SDL.CreateWindow(
    TITLE,
    SDL::WINDOWPOS_CENTERED_MASK,
    SDL::WINDOWPOS_CENTERED_MASK,
    width, height,
    window_flags
  )

  @context = SDL.GL_CreateContext(@window)
  SDL.GL_MakeCurrent(@window, @context)
  SDL.GL_SetSwapInterval(visible ? 1 : 0) # vsync only for visible

  GL.load_lib
  GLU.load_lib

  GL.Viewport(0, 0, width, height)
  GL.Enable(GL::DEPTH_TEST)
  GL.ClearColor(0.0, 0.0, 0.0, 1.0)

  SDL.SetRelativeMouseMode(SDL::TRUE) if visible
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



13
14
15
# File 'lib/quake/window.rb', line 13

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/quake/window.rb', line 13

def width
  @width
end

Instance Method Details

#aspect_ratioObject



64
65
66
# File 'lib/quake/window.rb', line 64

def aspect_ratio
  @width.to_f / @height
end

#closeObject



68
69
70
71
72
# File 'lib/quake/window.rb', line 68

def close
  SDL.GL_DeleteContext(@context)
  SDL.DestroyWindow(@window)
  SDL.Quit
end

#poll_eventsObject



57
58
59
60
61
62
# File 'lib/quake/window.rb', line 57

def poll_events
  @event ||= SDL::Event.new
  while SDL.PollEvent(@event.to_ptr) != 0
    yield @event
  end
end

#swapObject



53
54
55
# File 'lib/quake/window.rb', line 53

def swap
  SDL.GL_SwapWindow(@window)
end