Class: SFML::ContextSettings

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

Overview

Configuration for the underlying OpenGL context that backs a ‘Window` / `RenderWindow`. Passed at window-creation time to request anti-aliasing, depth/stencil buffers, or a specific OpenGL version.

settings = SFML::ContextSettings.new(antialiasing: 4)
window   = SFML::RenderWindow.new(800, 600, "Smooth", context: settings)

Or use the ‘RenderWindow.new` shortcut directly:

window = SFML::RenderWindow.new(800, 600, "Smooth", antialiasing: 4)

Anti-aliasing is the most common knob — typical values are 0 (off), 2, 4, or 8. The driver picks the closest level it actually supports; query ‘window.context_settings` after creation to see what you got.

Constant Summary collapse

DEFAULT_DEPTH_BITS =
0
DEFAULT_STENCIL_BITS =
0
DEFAULT_AA_LEVEL =
0
DEFAULT_MAJOR_VERSION =
1
DEFAULT_MINOR_VERSION =
1
ATTRIBUTE_FLAGS =
{
  default: C::Window::ContextAttribute::DEFAULT,
  core:    C::Window::ContextAttribute::CORE,
  debug:   C::Window::ContextAttribute::DEBUG,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depth_bits: DEFAULT_DEPTH_BITS, stencil_bits: DEFAULT_STENCIL_BITS, antialiasing: DEFAULT_AA_LEVEL, major_version: DEFAULT_MAJOR_VERSION, minor_version: DEFAULT_MINOR_VERSION, attributes: :default, srgb_capable: false) ⇒ ContextSettings

Returns a new instance of ContextSettings.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sfml/window/context_settings.rb', line 35

def initialize(depth_bits: DEFAULT_DEPTH_BITS,
               stencil_bits: DEFAULT_STENCIL_BITS,
               antialiasing: DEFAULT_AA_LEVEL,
               major_version: DEFAULT_MAJOR_VERSION,
               minor_version: DEFAULT_MINOR_VERSION,
               attributes: :default,
               srgb_capable: false)
  @depth_bits      = Integer(depth_bits)
  @stencil_bits    = Integer(stencil_bits)
  @antialiasing    = Integer(antialiasing)
  @major_version   = Integer(major_version)
  @minor_version   = Integer(minor_version)
  @attribute_flags = _attribute_mask(attributes)
  @srgb_capable    = !!srgb_capable
  freeze
end

Instance Attribute Details

#antialiasingObject (readonly)

Returns the value of attribute antialiasing.



31
32
33
# File 'lib/sfml/window/context_settings.rb', line 31

def antialiasing
  @antialiasing
end

#attribute_flagsObject (readonly)

Returns the value of attribute attribute_flags.



31
32
33
# File 'lib/sfml/window/context_settings.rb', line 31

def attribute_flags
  @attribute_flags
end

#depth_bitsObject (readonly)

Returns the value of attribute depth_bits.



31
32
33
# File 'lib/sfml/window/context_settings.rb', line 31

def depth_bits
  @depth_bits
end

#major_versionObject (readonly)

Returns the value of attribute major_version.



31
32
33
# File 'lib/sfml/window/context_settings.rb', line 31

def major_version
  @major_version
end

#minor_versionObject (readonly)

Returns the value of attribute minor_version.



31
32
33
# File 'lib/sfml/window/context_settings.rb', line 31

def minor_version
  @minor_version
end

#srgb_capableObject (readonly)

Returns the value of attribute srgb_capable.



31
32
33
# File 'lib/sfml/window/context_settings.rb', line 31

def srgb_capable
  @srgb_capable
end

#stencil_bitsObject (readonly)

Returns the value of attribute stencil_bits.



31
32
33
# File 'lib/sfml/window/context_settings.rb', line 31

def stencil_bits
  @stencil_bits
end

Class Method Details

.from_native(struct) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sfml/window/context_settings.rb', line 67

def self.from_native(struct)
  attrs = ATTRIBUTE_FLAGS.find { |_, v| v == struct[:attribute_flags] }&.first || :default
  new(
    depth_bits:      struct[:depth_bits],
    stencil_bits:    struct[:stencil_bits],
    antialiasing:    struct[:anti_aliasing_level],
    major_version:   struct[:major_version],
    minor_version:   struct[:minor_version],
    attributes:      attrs,
    srgb_capable:    struct[:s_rgb_capable],
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



80
81
82
83
84
85
86
87
88
89
# File 'lib/sfml/window/context_settings.rb', line 80

def ==(other)
  other.is_a?(ContextSettings) &&
    depth_bits == other.depth_bits &&
    stencil_bits == other.stencil_bits &&
    antialiasing == other.antialiasing &&
    major_version == other.major_version &&
    minor_version == other.minor_version &&
    attribute_flags == other.attribute_flags &&
    srgb_capable == other.srgb_capable
end

#hashObject



92
93
94
# File 'lib/sfml/window/context_settings.rb', line 92

def hash = [@depth_bits, @stencil_bits, @antialiasing,
@major_version, @minor_version, @attribute_flags,
@srgb_capable].hash

#to_nativeObject

Build the matching CSFML struct on the heap. Returned struct is owned by the caller (FFI::AutoPointer is not set — pass by reference into a window-creation call and let it copy).



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sfml/window/context_settings.rb', line 55

def to_native
  s = C::Window::ContextSettings.new
  s[:depth_bits]          = @depth_bits
  s[:stencil_bits]        = @stencil_bits
  s[:anti_aliasing_level] = @antialiasing
  s[:major_version]       = @major_version
  s[:minor_version]       = @minor_version
  s[:attribute_flags]     = @attribute_flags
  s[:s_rgb_capable]       = @srgb_capable
  s
end

#to_sObject Also known as: inspect



96
97
98
99
# File 'lib/sfml/window/context_settings.rb', line 96

def to_s
  "ContextSettings(aa=#{@antialiasing}, depth=#{@depth_bits}, " \
    "stencil=#{@stencil_bits}, gl=#{@major_version}.#{@minor_version})"
end