Skip to content

Commit

Permalink
Merge branch 'master' of gitlab.com:arctic-fox/espresso
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Apr 10, 2022
2 parents 9e9fdc3 + 5182dff commit e5fa8f1
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 63 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: espresso
version: 0.2.0
version: 0.2.1

authors:
- Michael Miller <[email protected]>
Expand Down
6 changes: 3 additions & 3 deletions src/espresso/error_handling.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ module Espresso
# and raises an exception if one did.
private def check_error
code = LibGLFW.get_error(out description)
if code != LibGLFW::ErrorCode::NoError
raise translate_error(code, description)
end
return if code.no_error?

raise translate_error(code, description)
end

# Checks for errors from GLFW after a method has been called.
Expand Down
2 changes: 1 addition & 1 deletion src/espresso/events/mouse_enter_event.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Espresso
# Creates the mouse event.
protected def initialize(pointer, entered)
super(pointer)
@entered = int_to_bool(entered)
@entered = entered.to_bool
end

# Indicates whether the mouse left the window's content area.
Expand Down
9 changes: 4 additions & 5 deletions src/espresso/events/window_drop_event.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ module Espresso
# Event triggered when one or more files are dropped onto the window.
struct WindowDropEvent < WindowEvent
# List of file paths.
getter paths : Array(String)
getter paths : Slice(String)

# Creates the event.
protected def initialize(pointer, count, paths)
protected def initialize(pointer, count, paths_pointer)
super(pointer)
# Use Slice for safer and easier pointer access.
slice = Slice.new(paths, count, read_only: true)
@paths = Array.new(count) { |i| String.new(slice[i]) }
slice = Slice.new(paths_pointer, count, read_only: true)
@paths = slice.map(read_only: true) { |str_ptr| String.new(str_ptr) }
end
end
end
12 changes: 6 additions & 6 deletions src/espresso/input/joystick.cr
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module Espresso
def axes? : Indexable(Float)?
count = uninitialized Int32
pointer = expect_truthy { LibGLFW.get_joystick_axes(@id, pointerof(count)) }
pointer ? Slice.new(pointer, count) : nil
Slice.new(pointer, count) if pointer
end

# Retrieves the values of all axes of this joystick.
Expand Down Expand Up @@ -124,7 +124,7 @@ module Espresso
def buttons? : Indexable(ButtonState)?
count = uninitialized Int32
pointer = expect_truthy { LibGLFW.get_joystick_buttons(@id, pointerof(count)) }
pointer ? Slice.new(pointer, count).unsafe_as(Slice(ButtonState)) : nil
Slice.new(pointer, count).unsafe_as(Slice(ButtonState)) if pointer
end

# Retrieves the state of all buttons of this joystick.
Expand Down Expand Up @@ -162,7 +162,7 @@ module Espresso
def hats? : Indexable(JoystickHatState)?
count = uninitialized Int32
pointer = expect_truthy { LibGLFW.get_joystick_hats(@id, pointerof(count)) }
pointer ? Slice.new(pointer, count).unsafe_as(Slice(JoystickHatState)) : nil
Slice.new(pointer, count).unsafe_as(Slice(JoystickHatState)) if pointer
end

# Retrieves the state of all hats of this joystick.
Expand All @@ -187,7 +187,7 @@ module Espresso
# This can be used instead of first calling `#connected?`.
def name? : String?
chars = expect_truthy { LibGLFW.get_joystick_name(@id) }
chars ? String.new(chars) : nil
String.new(chars) if chars
end

# Retrieves the name, encoded as UTF-8, of this joystick.
Expand Down Expand Up @@ -215,7 +215,7 @@ module Espresso
# depending on what hardware information the platform specific APIs provide.
def guid? : String?
chars = expect_truthy { LibGLFW.get_joystick_guid(@id) }
chars ? String.new(chars) : nil
String.new(chars) if chars
end

# Retrieves the SDL compatible GUID, as a UTF-8 encoded hexadecimal string, of this joystick.
Expand Down Expand Up @@ -274,7 +274,7 @@ module Espresso
def state? : GamepadState?
state = uninitialized LibGLFW::GamepadState
result = expect_truthy { LibGLFW.get_gamepad_state(@id, pointerof(state)) }
result.to_bool ? GamepadState.new(state) : nil
GamepadState.new(state) if result.to_bool
end

# Retrieves the state of the specified joystick remapped to an Xbox-like gamepad.
Expand Down
4 changes: 2 additions & 2 deletions src/espresso/input/joystick/joystick_user_data.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module Espresso
else
# Otherwise, create new user data and set it.
JoystickUserData.new.tap do |user_data|
box = Box.box(user_data)
checked { LibGLFW.set_joystick_user_pointer(self, box) }
pointer = Box.box(user_data)
checked { LibGLFW.set_joystick_user_pointer(self, pointer) }
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions src/espresso/input/keyboard.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ module Espresso
# The modifier key bit masks are not key tokens and cannot be used with this method.
#
# **Do not use this method** to implement text input.
def key(key) : KeyState
value = expect_truthy { LibGLFW.get_key(@pointer, key) }
def key(key : Key) : KeyState
value = expect_truthy { LibGLFW.get_key(@pointer, LibGLFW::Key.from_value(key.value)) }
KeyState.new(value.to_i)
end

Expand All @@ -51,7 +51,7 @@ module Espresso
# If the `#sticky?` input mode is enabled,
# this method returns true the first time you call it for a key that was pressed,
# even if that key has already been released.
def key?(key)
def key?(key : Key)
self.key(key).pressed?
end

Expand Down Expand Up @@ -153,7 +153,7 @@ module Espresso
#
# Returns a string if a name is available for the specified *key*, nil otherwise.
def self.key_name?(key : Key)
raise ArgumentError.new("Key must be known") if key == Key::Unknown
raise ArgumentError.new("Key must be known") if key.unknown?

key_name?(key, 0)
end
Expand All @@ -173,7 +173,7 @@ module Espresso
#
# Returns a string if a name is available for the specified *scancode*, nil otherwise.
def self.key_name?(scancode)
key_name?(Key::Unknown, scancode)
key_name?(:unknown, scancode)
end

# Retrieves the name of the specified printable key, encoded as UTF-8.
Expand All @@ -186,7 +186,7 @@ module Espresso
# This behavior allows you to always pass in the arguments from the `#on_key` callback without modification.
protected def self.key_name?(key : Key, scancode)
chars = expect_truthy { LibGLFW.get_key_name(key.native, scancode) }
chars ? String.new(chars) : nil
String.new(chars) if chars
end

# Retrieves the platform-specific scancode of the specified key.
Expand Down
6 changes: 3 additions & 3 deletions src/espresso/input/mouse.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Espresso
# for a mouse button that was pressed,
# even if that mouse button has already been released.
def left : ButtonState
button(MouseButton::Left)
button(:left)
end

# Determines whether the last state reported for the left (primary) mouse button is pressed.
Expand All @@ -64,7 +64,7 @@ module Espresso
# for a mouse button that was pressed,
# even if that mouse button has already been released.
def right : ButtonState
button(MouseButton::Right)
button(:right)
end

# Determines whether the last state reported for the right (secondary) mouse button is pressed.
Expand All @@ -84,7 +84,7 @@ module Espresso
# for a mouse button that was pressed,
# even if that mouse button has already been released.
def middle : ButtonState
button(MouseButton::Middle)
button(:middle)
end

# Determines whether the last state reported for the middle mouse button is pressed.
Expand Down
2 changes: 1 addition & 1 deletion src/espresso/instance_topic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Espresso
# Operates as a proxy between GLFW bindings and Espresso types.
private abstract struct InstanceTopic(EventType) < Topic(EventType)
# Subscribes a listener to the topic.
# *instance* is the object to dettach the listener to.
# *instance* is the object to detach the listener to.
def add_listener(instance, listener : EventType ->) : Nil
@listeners << listener
register_callback(instance) if @listeners.size == 1 # Register on first listener.
Expand Down
2 changes: 1 addition & 1 deletion src/espresso/monitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Espresso
# If there's no monitors, then this returns nil.
def self.primary? : Monitor?
pointer = expect_truthy { LibGLFW.get_primary_monitor }
pointer ? new(pointer) : nil
new(pointer) if pointer
end

# Retrieves the user's primary monitor.
Expand Down
72 changes: 48 additions & 24 deletions src/espresso/monitor/gamma_ramp.cr
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
module Espresso
# Gamma ramp of a monitor.
class GammaRamp
# Array of values describing the response of the red channel.
getter red : Slice(UInt16)

# Array of values describing the response of the green channel.
getter green : Slice(UInt16)
struct GammaRamp
# Number of elements in each channel's array.
getter size : Int32

# Array of values describing the response of the blue channel.
getter blue : Slice(UInt16)
@ramp : Pointer(UInt16)

# Creates a new gamma ramp with all values set to zero.
def initialize(size : Int32 = 256)
@red = Slice.new(size, 0_u16)
@green = Slice.new(size, 0_u16)
@blue = Slice.new(size, 0_u16)
def initialize(@size : Int32 = 256)
@ramp = Pointer(UInt16).malloc(@size * 3)
end

# Creates a new gamma ramp from a pointer to one from GLFW.
# Contents of the ramp are copied so that they are available
# even after the monitor is disconnected or GLFW is terminated.
protected def initialize(pointer : LibGLFW::GammaRamp*)
ramp = pointer.value
size = ramp.size.to_i
source = pointer.value
@size = source.size.to_i

# Copy contents of arrays since they may be invalidated when the monitor disconnects.
@red = Slice(UInt16).new(size).tap &.copy_from(ramp.red, size)
@green = Slice(UInt16).new(size).tap &.copy_from(ramp.green, size)
@blue = Slice(UInt16).new(size).tap &.copy_from(ramp.blue, size)
@ramp = Pointer(UInt16).malloc(@size * 3)
red_pointer.copy_from(source.red, @size)
green_pointer.copy_from(source.green, @size)
blue_pointer.copy_from(source.blue, @size)
end

# Number of elements in each channel's array.
def size : Int
@red.size
# Array of values describing the response of the red channel.
def red : Slice(UInt16)
red_pointer.to_slice(@size)
end

# Pointer to the start of the red channel data.
@[AlwaysInline]
private def red_pointer : Pointer(UInt16)
@ramp
end

# Array of values describing the response of the green channel.
def green : Slice(UInt16)
green_pointer.to_slice(@size)
end

# Pointer to the start of the green channel data.
@[AlwaysInline]
private def green_pointer : Pointer(UInt16)
@ramp + @size
end

# Array of values describing the response of the blue channel.
def blue : Slice(UInt16)
blue_pointer.to_slice(@size)
end

# Pointer to the start of the blue channel data.
@[AlwaysInline]
private def blue_pointer : Pointer(UInt16)
@ramp + @size + @size
end

# Converts to a GLFW compatible struct.
def to_unsafe
ramp = LibGLFW::GammaRamp.new
ramp.red = @red
ramp.green = @green
ramp.blue = @blue
ramp.size = size
ramp.red = red_pointer
ramp.green = green_pointer
ramp.blue = blue_pointer
ramp.size = @size
ramp
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/espresso/monitor/monitor_user_data.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module Espresso
else
# Otherwise, create new user data and set it.
MonitorUserData.new.tap do |user_data|
box = Box.box(user_data)
checked { LibGLFW.set_monitor_user_pointer(self, box) }
pointer = Box.box(user_data)
checked { LibGLFW.set_monitor_user_pointer(self, pointer) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/espresso/user_data.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Espresso
self.class.instances << self
end

# Custom data the end-user can attach to a window instance.
# Custom data the end-user can attach to an instance.
property pointer : Void* = Pointer(Void).null
end
end
10 changes: 5 additions & 5 deletions src/espresso/window.cr
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ module Espresso
# Possible errors that could be raised are: `NotInitializedError` and `PlatformError`.
def clipboard? : String?
chars = LibGLFW.get_clipboard_string(@pointer)
chars ? String.new(chars) : nil
String.new(chars) if chars
end

# Sets the contents of the system clipboard,
Expand Down Expand Up @@ -792,12 +792,12 @@ module Espresso
# Retrieves the contents of the system clipboard,
# if it contains or is convertible to a UTF-8 encoded string.
# If the clipboard is empty or if its contents cannot be converted,
# nil is returned..
# nil is returned.
#
# Possible errors that could be raised are: `NotInitializedError` and `PlatformError`.
def self.clipboard? : String?
chars = LibGLFW.get_clipboard_string(nil)
chars ? String.new(chars) : nil
String.new(chars) if chars
end

# Attempts to retrieve the monitor the full screen window is using.
Expand All @@ -806,7 +806,7 @@ module Espresso
# Possible errors that could be raised are: `NotInitializedError`.
def monitor? : Monitor?
pointer = expect_truthy { LibGLFW.get_window_monitor(@pointer) }
pointer ? Monitor.new(pointer) : nil
Monitor.new(pointer) if pointer
end

# Retrieves the monitor the full screen window is using.
Expand Down Expand Up @@ -998,7 +998,7 @@ module Espresso
# See also: `#current!`
def self.current? : Window?
pointer = expect_truthy { LibGLFW.get_current_context }
pointer ? Window.new(pointer) : nil
Window.new(pointer) if pointer
end

# Returns the window whose OpenGL or OpenGL ES context is current on the calling thread.
Expand Down
4 changes: 2 additions & 2 deletions src/espresso/window/window_user_data.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module Espresso
else
# Otherwise, create new user data and set it.
WindowUserData.new.tap do |user_data|
box = Box.box(user_data)
checked { LibGLFW.set_window_user_pointer(self, box) }
pointer = Box.box(user_data)
checked { LibGLFW.set_window_user_pointer(self, pointer) }
end
end
end
Expand Down

0 comments on commit e5fa8f1

Please sign in to comment.