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
52
53
54
# 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.DepthFunc(GL::LEQUAL) # R_Clear
  # Quake's axis flip reverses winding, so front faces are culled (R_SetupGL)
  GL.CullFace(GL::FRONT)
  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



67
68
69
# File 'lib/quake/window.rb', line 67

def aspect_ratio
  @width.to_f / @height
end

#closeObject



71
72
73
74
75
# File 'lib/quake/window.rb', line 71

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

#poll_eventsObject



60
61
62
63
64
65
# File 'lib/quake/window.rb', line 60

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

#swapObject



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

def swap
  SDL.GL_SwapWindow(@window)
end