Module: Engine

Defined in:
lib/engine/font.rb,
lib/engine/mesh.rb,
lib/engine/path.rb,
lib/engine/input.rb,
lib/engine/camera.rb,
lib/engine/cursor.rb,
lib/engine/engine.rb,
lib/engine/shader.rb,
lib/engine/window.rb,
lib/engine/texture.rb,
lib/engine/material.rb,
lib/engine/component.rb,
lib/engine/quaternion.rb,
lib/engine/video_mode.rb,
lib/engine/game_object.rb,
lib/engine/polygon_mesh.rb,
lib/engine/screenshoter.rb,
lib/engine/importers/obj_file.rb,
lib/engine/tangent_calculator.rb,
lib/engine/importers/obj_importer.rb,
lib/engine/importers/font_importer.rb

Defined Under Namespace

Modules: Components, Physics Classes: Camera, Component, Cursor, Font, FontImporter, GameObject, Input, Material, Mesh, NoEarsException, ObjFile, ObjImporter, Path, PolygonMesh, Quaternion, Screenshoter, Shader, TangentCalculator, Texture, VideoMode, Window

Class Method Summary collapse

Class Method Details

.breakpoint(&block) ⇒ Object

Hit a breakpoint within the context of where the breakpoint is defined, assuming a block is passed with a ‘binding.pry` (or an alternative debugger), otherwise hit a breakpoint within this method.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/engine/engine.rb', line 165

def self.breakpoint(&block)
  orig_fullscreen = Window.full_screen?
  if orig_fullscreen
    Window.set_to_windowed
    GLFW.PollEvents # Required to trigger the switch from fullscreen to windowed within this breakpoint
  end

  orig_cursor_mode = Cursor.get_input_mode
  Cursor.enable

  block_given? ? yield : binding.pry
  Cursor.restore_input_mode(orig_cursor_mode)
  Window.set_to_full_screen if orig_fullscreen
  Window.focus_window

  # Reset the time, otherwise delta_time will be off for the next frame, and teleporting occurs
  @time = Time.now
end

.closeObject



152
153
154
155
# File 'lib/engine/engine.rb', line 152

def self.close
  GameObject.destroy_all
  GLFW.SetWindowShouldClose(Window.window, 1)
end

.engine_started?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/engine/engine.rb', line 60

def self.engine_started?
  @engine_started
end

.fpsObject



143
144
145
# File 'lib/engine/engine.rb', line 143

def self.fps
  @fps
end

.load(base_dir) ⇒ Object



64
65
66
67
68
# File 'lib/engine/engine.rb', line 64

def self.load(base_dir)
  base_dir = File.expand_path(base_dir)
  Dir[File.join(base_dir, "components", "**/*.rb")].each { |file| require file }
  Dir[File.join(base_dir, "game_objects", "**/*.rb")].each { |file| require file }
end

.main_game_loop(&first_frame_block) ⇒ Object



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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/engine/engine.rb', line 92

def self.main_game_loop(&first_frame_block)
  @game_stopped = false
  @old_time = Time.now
  @time = Time.now
  @fps = 0
  Window.get_framebuffer_size
  GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT)

  until GLFW.WindowShouldClose(Window.window) == GLFW::TRUE || @game_stopped
    if first_frame_block
      first_frame_block.call
      first_frame_block = nil
    end

    @old_time = @time || Time.now
    @time = Time.now
    delta_time = @time - @old_time

    print_fps(delta_time)
    Physics::PhysicsResolver.resolve
    GameObject.update_all(delta_time)

    @swap_buffers_promise.wait! if @swap_buffers_promise
    GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT)
    GL.Enable(GL::DEPTH_TEST)
    GL.DepthFunc(GL::LESS)

    Rendering::RenderPipeline.draw unless @game_stopped
    GL.Disable(GL::DEPTH_TEST)
    GameObject.render_ui(delta_time)

    if Screenshoter.scheduled_screenshot
      Screenshoter.take_screenshot
    end

    Window.get_framebuffer_size

    if OS.windows?
      GLFW.SwapBuffers(Window.window)
    else
      @swap_buffers_promise = Concurrent::Promise.new do
        GLFW.SwapBuffers(Window.window)
      end
      @swap_buffers_promise.execute
    end

    Engine::Input.update_key_states
    GLFW.PollEvents
  end
end

.open_windowObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/engine/engine.rb', line 70

def self.open_window
  @old_time = Time.now
  @time = Time.now
  @key_callback = create_key_callbacks # This must be an instance variable to prevent garbage collection

  Window.create_window
  GLFW.MakeContextCurrent(Window.window)
  GLFW.SetKeyCallback(Window.window, @key_callback)
  GL.load_lib

  set_opengl_blend_mode
  @engine_started = true
  GL.ClearColor(0.0, 0.0, 0.0, 1.0)

  GL.Enable(GL::CULL_FACE)
  GL.CullFace(GL::BACK)

  GLFW.SwapInterval(0)

  Cursor.hide
end

.start(base_dir:, &first_frame_block) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/engine/engine.rb', line 51

def self.start(base_dir:, &first_frame_block)
  load(base_dir)
  return if ENV["BUILDING"] == "true"

  open_window
  main_game_loop(&first_frame_block)
  terminate
end

.stop_gameObject



157
158
159
160
161
# File 'lib/engine/engine.rb', line 157

def self.stop_game
  @game_stopped = true
  @swap_buffers_promise.wait! if @swap_buffers_promise && !@swap_buffers_promise.complete?
  GameObject.destroy_all
end

.terminateObject



147
148
149
150
# File 'lib/engine/engine.rb', line 147

def self.terminate
  GLFW.DestroyWindow(Window.window)
  GLFW.Terminate
end