44using System . Diagnostics ;
55using System . Diagnostics . CodeAnalysis ;
66using System . Runtime . CompilerServices ;
7+ using System . Runtime . InteropServices ;
78using Silk . NET . Input . SDL3 . DataStructures ;
89using Silk . NET . Input . SDL3 . Devices . Joysticks ;
910using Silk . NET . Input . SDL3 . Devices . Pointers ;
@@ -152,7 +153,6 @@ public unsafe SdlInputBackend(SdlPlatformInfo info)
152153 public void Update ( IInputHandler ? handler = null )
153154 {
154155 ref var eventArgs = ref _eventProcessingArgs ;
155- var events = eventArgs . Events ;
156156 Sdl . PumpEvents ( ) ;
157157
158158 // QUESTION - do we want this before or after the event processing? or should
@@ -162,26 +162,28 @@ public void Update(IInputHandler? handler = null)
162162 UpdatePointerTargets ( eventArgs . SdlWindowTargets , eventArgs . SdlDisplayTargets ) ;
163163
164164 // actually process the events in-order
165- if ( events . HasEvents )
165+
166+ var rawEvents = _rawEvents . ConsumeWithoutClearing ( ) ;
167+
168+ #if DEBUG
169+ var previousTimestamp = 0ul ;
170+ var previousStopwatchTimestamp = 0L ;
171+ #endif
172+
173+ for ( var i = 0 ; i < rawEvents . Length ; ++ i )
166174 {
167- #if DEBUG
168- var previousTimestamp = 0ul ;
169- var previousStopwatchTimestamp = 0L ;
170- #endif
171- while ( events . TryDequeue ( out var evt ) )
175+ ref readonly var evt = ref rawEvents [ i ] ;
176+ #if DEBUG
177+ if ( evt . Event . Common . Timestamp < previousTimestamp || evt . StopwatchTimestamp < previousStopwatchTimestamp )
172178 {
173- #if DEBUG
174- if ( evt . Event . Common . Timestamp < previousTimestamp || evt . StopwatchTimestamp < previousStopwatchTimestamp )
175- {
176- InputLog . Error ( "Needs pre-sort by timestamp - please alert maintainer" ) ;
177- }
179+ InputLog . Error ( "Needs pre-sort by timestamp - please alert maintainer" ) ;
180+ }
178181
179- previousTimestamp = evt . Event . Common . Timestamp ;
180- previousStopwatchTimestamp = evt . StopwatchTimestamp ;
181- #endif
182+ previousTimestamp = evt . Event . Common . Timestamp ;
183+ previousStopwatchTimestamp = evt . StopwatchTimestamp ;
184+ #endif
182185
183- ProcessEvent ( evt . Event , evt . StopwatchTimestamp , ref eventArgs ) ;
184- }
186+ ProcessEvent ( evt . Event , evt . StopwatchTimestamp , ref eventArgs ) ;
185187 }
186188
187189 var devices = eventArgs . Devices ;
@@ -211,7 +213,7 @@ public void Update(IInputHandler? handler = null)
211213 private unsafe byte OnEvent ( void * arg0 , Event * arg1 )
212214 {
213215 var timestamp = Stopwatch . GetTimestamp ( ) ;
214- _eventProcessingArgs . Events . Add ( ref * arg1 , timestamp ) ;
216+ _rawEvents . Add ( new TimedRawSdlEvent ( * arg1 , timestamp ) ) ;
215217 return 1 ;
216218 }
217219
@@ -562,6 +564,7 @@ private void ReleaseUnmanagedResources()
562564 _inputSubscriptionEventPtr . Dispose ( ) ;
563565 Sdl . QuitSubSystem ( SdlInitFlags ) ;
564566 _sdlInputEvents . Dispose ( ) ;
567+ _rawEvents . Dispose ( ) ;
565568 }
566569
567570 public void Dispose ( )
@@ -622,49 +625,11 @@ public TimedRawSdlEvent(Event @event, long timestamp)
622625 }
623626 }
624627
625- private class EventQueue
626- {
627- private TimedRawSdlEvent [ ] _events = new TimedRawSdlEvent [ 256 ] ;
628- private int _addIndex ;
629- private int _dequeueIndex ;
630-
631-
632- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
633- public void Add ( ref Event p0 , long timestamp )
634- {
635- if ( _addIndex == _events . Length )
636- {
637- Array . Resize ( ref _events , _events . Length * 2 ) ;
638- }
639-
640- _events [ _addIndex ++ ] = new TimedRawSdlEvent ( p0 , timestamp ) ;
641- }
642-
643-
644- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
645- public bool TryDequeue ( out TimedRawSdlEvent p0 )
646- {
647- if ( _dequeueIndex >= _addIndex )
648- {
649- // clear
650- _addIndex = 0 ;
651- _dequeueIndex = 0 ;
652-
653- // return that we're empty
654- p0 = default ;
655- return false ;
656- }
657-
658-
659- p0 = _events [ _dequeueIndex ++ ] ;
660- return true ;
661- }
662-
663- public bool HasEvents => _addIndex > 0 ;
664- }
665628
666629 /// <summary>
667630 /// A struct containing all the data required for processing SDL events.
631+ /// This is a struct to prevent additional pointer indirections, but the field is applied to should be considered
632+ /// an
668633 /// </summary>
669634 private struct ProcessEventArgs
670635 {
@@ -676,7 +641,6 @@ private struct ProcessEventArgs
676641 private readonly List < SdlDevice > _devices ;
677642 private readonly ISdlInputEventQueue < ConnectionEvent > _connectionEventQueue ;
678643 private readonly HashSet < nint > _deviceRegistry = [ ] ;
679- public readonly EventQueue Events = new ( ) ;
680644
681645 /// <param name="backend">The SDL input backend that these args are for</param>
682646 /// <param name="connectionEventQueue">The event queue for connection events</param>
@@ -763,6 +727,7 @@ private enum QueuedEventType : byte
763727
764728 // NOTE: Be careful where these are used!
765729 private ProcessEventArgs _eventProcessingArgs ;
730+ private NativeMemory < TimedRawSdlEvent > _rawEvents ;
766731
767732 private readonly EventFilter _inputSubscriptionEventPtr ;
768733 private readonly SdlInputEventContext _sdlInputEvents ;
0 commit comments