EAGL.Window (eagl v0.7.0)

View Source

OpenGL window management and application lifecycle.

Correct window creation with cross-platform OpenGL context setup and automated event handling based on wings_gl.erl patterns.

Original Source

Window creation timing, context setup, and event handling patterns are based on Wings3D's wings_gl.erl module: https://github.com/dgud/wings/blob/master/src/wings_gl.erl

Usage

defmodule MyApp do
  use EAGL.Window

  def run_example do
    # 2D rendering (default)
    EAGL.Window.run(__MODULE__, "My 2D App")

    # 3D rendering with depth testing
    EAGL.Window.run(__MODULE__, "My 3D App", depth_testing: true)

    # Custom size and tutorial mode
    EAGL.Window.run(__MODULE__, "Tutorial",
      size: {1280, 720},
      enter_to_exit: true
    )
  end

  @impl true
  def setup do
    # Initialize shaders, load models, set up state
    {:ok, initial_state}
  end

  @impl true
  def render(width, height, state) do
    # Clear screen and render content
    :gl.clearColor(0.2, 0.3, 0.3, 1.0)
    :gl.clear(@gl_color_buffer_bit)
    # ... render content
    :ok
  end

  @impl true
  def cleanup(state) do
    # Clean up OpenGL resources
    :ok
  end

  # Optional: Handle mouse events for camera control
  @impl true
  def handle_event({:mouse_motion, x, y}, state) do
    # Handle mouse movement for camera look around
    {:ok, updated_state}
  end

  @impl true
  def handle_event({:mouse_wheel, x, y, wheel_rotation, wheel_delta}, state) do
    # Handle scroll wheel for camera zoom
    {:ok, updated_state}
  end
end

Summary

Functions

Creates and runs an OpenGL window using the given callback module. The callback module must implement the GLWindowBehaviour.

Functions

run(callback_module, title)

@spec run(module(), String.t()) :: :ok | {:error, term()}

Creates and runs an OpenGL window using the given callback module. The callback module must implement the GLWindowBehaviour.

Options:

  • size: {width, height} tuple, defaults to {1024, 768}. Sets the initial window size.
  • depth_testing: boolean, defaults to false. When true, enables depth testing and requests a depth buffer.
  • enter_to_exit: boolean, defaults to false. When true, pressing ENTER will automatically close the window.
  • timeout: integer, optional. If set, automatically exits after this many milliseconds for automated testing.

run(callback_module, title, opts)

@spec run(module(), String.t(), keyword()) :: :ok | {:error, term()}