Module: Engine

Defined in:
lib/engine/gl.rb,
lib/engine/font.rb,
lib/engine/mesh.rb,
lib/engine/path.rb,
lib/engine/debug.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/ui/rect.rb,
lib/engine/material.rb,
lib/engine/component.rb,
lib/engine/debugging.rb,
lib/engine/autoloader.rb,
lib/engine/quaternion.rb,
lib/engine/video_mode.rb,
lib/engine/game_object.rb,
lib/engine/metal/device.rb,
lib/engine/polygon_mesh.rb,
lib/engine/screenshoter.rb,
lib/engine/compute_shader.rb,
lib/engine/matrix_helpers.rb,
lib/engine/compute_texture.rb,
lib/engine/importers/obj_file.rb,
lib/engine/tangent_calculator.rb,
lib/engine/metal/compute_shader.rb,
lib/engine/metal/metal_bindings.rb,
lib/engine/metal/compute_texture.rb,
lib/engine/opengl/compute_shader.rb,
lib/engine/standard_objects/cube.rb,
lib/engine/importers/obj_importer.rb,
lib/engine/opengl/compute_texture.rb,
lib/engine/standard_objects/plane.rb,
lib/engine/importers/font_importer.rb,
lib/engine/standard_objects/sphere.rb,
lib/engine/standard_meshes/quad_mesh.rb,
lib/engine/serialization/serializable.rb,
lib/engine/serialization/graph_serializer.rb,
lib/engine/serialization/yaml_persistence.rb,
lib/engine/serialization/object_serializer.rb,
lib/engine/standard_objects/default_material.rb

Defined Under Namespace

Modules: AutoLoader, Components, Debug, Debugging, GL, MatrixHelpers, Metal, OpenGL, Physics, Serializable, Serialization, StandardObjects, UI Classes: Camera, Component, ComputeShader, ComputeTexture, Cursor, Font, FontImporter, GameObject, Input, Material, Mesh, NoEarsException, ObjFile, ObjImporter, Path, PolygonMesh, QuadMesh, Quaternion, Screenshoter, Shader, TangentCalculator, Texture, VideoMode, Window

Class Method Summary collapse

Class Method Details

.closeObject



99
100
101
102
103
104
# File 'lib/engine/engine.rb', line 99

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

.engine_started?Boolean

Returns:

  • (Boolean)


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

def self.engine_started?
  @engine_started
end

.fpsObject



95
96
97
# File 'lib/engine/engine.rb', line 95

def self.fps
  @fps
end

.main_game_loop(&first_frame_block) ⇒ Object



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
89
90
91
92
93
# File 'lib/engine/engine.rb', line 46

def self.main_game_loop(&first_frame_block)
  @game_stopped = false
  @old_time = Time.now
  @time = Time.now
  @fps = 0
  Window.get_framebuffer_size
  Engine::GL.Clear(Engine::GL::COLOR_BUFFER_BIT | Engine::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

    Rendering::RenderPipeline.draw unless @game_stopped

    if Screenshoter.scheduled_screenshot
      Screenshoter.take_screenshot
    end

    Window.get_framebuffer_size

    if OS.mac?
      # Async swap keeps the main thread free while waiting on the display link
      @swap_buffers_promise = Concurrent::Promise.new do
        GLFW.SwapBuffers(Window.window)
      end
      @swap_buffers_promise.execute
    else
      # GLX requires SwapBuffers on the thread owning the context, so
      # Windows and Linux swap synchronously
      GLFW.SwapBuffers(Window.window)
    end

    Engine::Input.update_key_states
    GLFW.PollEvents
  end
end

.open_windowObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/engine/engine.rb', line 25

def self.open_window
  @old_time = Time.now
  @time = Time.now

  Window.create_window
  GLFW.MakeContextCurrent(Window.window)
  Engine::GL.InitGLEW

  Input.init
  Rendering::GpuTimer.enable if ENV['GPU_PROFILE']

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

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

  GLFW.SwapInterval(OS.mac? ? 0 : 1)
end


121
122
123
124
125
126
127
128
129
130
# File 'lib/engine/engine.rb', line 121

def self.print_fps(delta_time)
  @time_since_last_fps_print = (@time_since_last_fps_print || 0) + delta_time
  @frame = (@frame || 0) + 1
  if @time_since_last_fps_print > 1
    @fps = @frame / @time_since_last_fps_print
    puts "FPS: #{@fps}"
    @time_since_last_fps_print = 0
    @frame = 0
  end
end

.set_opengl_blend_modeObject



132
133
134
135
# File 'lib/engine/engine.rb', line 132

def self.set_opengl_blend_mode
  Engine::GL.Enable(Engine::GL::BLEND)
  Engine::GL.BlendFunc(Engine::GL::SRC_ALPHA, Engine::GL::ONE_MINUS_SRC_ALPHA)
end

.start(close_key: Input::KEY_ESCAPE, debug_key: nil, fullscreen_key: nil, opengl_version: nil, &first_frame_block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/engine/engine.rb', line 7

def self.start(close_key: Input::KEY_ESCAPE, debug_key: nil, fullscreen_key: nil, opengl_version: nil, &first_frame_block)
  Engine::AutoLoader.load
  return if ENV["BUILDING"] == "true"

  Window.opengl_version = opengl_version if opengl_version
  Input.close_key = close_key
  Input.debug_key = debug_key
  Input.fullscreen_key = fullscreen_key

  open_window
  main_game_loop(&first_frame_block)
  terminate
end

.stop_gameObject



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

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

.terminateObject



116
117
118
119
# File 'lib/engine/engine.rb', line 116

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