File tree Expand file tree Collapse file tree 5 files changed +34
-40
lines changed Expand file tree Collapse file tree 5 files changed +34
-40
lines changed Original file line number Diff line number Diff line change @@ -74,15 +74,14 @@ public Music(Stream stream) :
7474 public Music ( byte [ ] bytes ) :
7575 base ( IntPtr . Zero )
7676 {
77- var pin = GCHandle . Alloc ( bytes , GCHandleType . Pinned ) ;
78- try
77+ unsafe
7978 {
80- CPointer = sfMusic_createFromMemory ( pin . AddrOfPinnedObject ( ) , ( UIntPtr ) bytes . Length ) ;
81- }
82- finally
83- {
84- pin . Free ( ) ;
79+ fixed ( void * ptr = bytes )
80+ {
81+ CPointer = sfMusic_createFromMemory ( ( IntPtr ) ptr , ( UIntPtr ) bytes . Length ) ;
82+ }
8583 }
84+
8685 if ( IsInvalid )
8786 {
8887 throw new LoadingFailedException ( "music" ) ;
Original file line number Diff line number Diff line change @@ -72,15 +72,14 @@ public SoundBuffer(Stream stream) :
7272 public SoundBuffer ( byte [ ] bytes ) :
7373 base ( IntPtr . Zero )
7474 {
75- var pin = GCHandle . Alloc ( bytes , GCHandleType . Pinned ) ;
76- try
77- {
78- CPointer = sfSoundBuffer_createFromMemory ( pin . AddrOfPinnedObject ( ) , ( UIntPtr ) bytes . Length ) ;
79- }
80- finally
75+ unsafe
8176 {
82- pin . Free ( ) ;
77+ fixed ( void * ptr = bytes )
78+ {
79+ CPointer = sfSoundBuffer_createFromMemory ( ( IntPtr ) ptr , ( UIntPtr ) bytes . Length ) ;
80+ }
8381 }
82+
8483 if ( IsInvalid )
8584 {
8685 throw new LoadingFailedException ( "sound buffer" ) ;
Original file line number Diff line number Diff line change @@ -62,15 +62,14 @@ public Font(Stream stream) : base(IntPtr.Zero)
6262 public Font ( byte [ ] bytes ) :
6363 base ( IntPtr . Zero )
6464 {
65- var pin = GCHandle . Alloc ( bytes , GCHandleType . Pinned ) ;
66- try
65+ unsafe
6766 {
68- CPointer = sfFont_createFromMemory ( pin . AddrOfPinnedObject ( ) , ( UIntPtr ) bytes . Length ) ;
69- }
70- finally
71- {
72- pin . Free ( ) ;
67+ fixed ( void * ptr = bytes )
68+ {
69+ CPointer = sfFont_createFromMemory ( ( IntPtr ) ptr , ( UIntPtr ) bytes . Length ) ;
70+ }
7371 }
72+
7473 if ( IsInvalid )
7574 {
7675 throw new LoadingFailedException ( "font" ) ;
Original file line number Diff line number Diff line change @@ -87,15 +87,14 @@ public Image(Stream stream) :
8787 public Image ( byte [ ] bytes ) :
8888 base ( IntPtr . Zero )
8989 {
90- var pin = GCHandle . Alloc ( bytes , GCHandleType . Pinned ) ;
91- try
92- {
93- CPointer = sfImage_createFromMemory ( pin . AddrOfPinnedObject ( ) , ( UIntPtr ) bytes . Length ) ;
94- }
95- finally
90+ unsafe
9691 {
97- pin . Free ( ) ;
92+ fixed ( void * ptr = bytes )
93+ {
94+ CPointer = sfImage_createFromMemory ( ( IntPtr ) ptr , ( UIntPtr ) bytes . Length ) ;
95+ }
9896 }
97+
9998 if ( IsInvalid )
10099 {
101100 throw new LoadingFailedException ( "image" ) ;
Original file line number Diff line number Diff line change @@ -205,21 +205,19 @@ public Texture(byte[] bytes, bool srgb = false) :
205205 public Texture ( byte [ ] bytes , IntRect area , bool srgb = false ) :
206206 base ( IntPtr . Zero )
207207 {
208- var pin = GCHandle . Alloc ( bytes , GCHandleType . Pinned ) ;
209- try
208+ unsafe
210209 {
211- if ( srgb )
210+ fixed ( void * ptr = bytes )
212211 {
213- CPointer = sfTexture_createSrgbFromMemory ( pin . AddrOfPinnedObject ( ) , ( UIntPtr ) bytes . Length , ref area ) ;
212+ if ( srgb )
213+ {
214+ CPointer = sfTexture_createSrgbFromMemory ( ( IntPtr ) ptr , ( UIntPtr ) bytes . Length , ref area ) ;
215+ }
216+ else
217+ {
218+ CPointer = sfTexture_createFromMemory ( ( IntPtr ) ptr , ( UIntPtr ) bytes . Length , ref area ) ;
219+ }
214220 }
215- else
216- {
217- CPointer = sfTexture_createFromMemory ( pin . AddrOfPinnedObject ( ) , ( UIntPtr ) bytes . Length , ref area ) ;
218- }
219- }
220- finally
221- {
222- pin . Free ( ) ;
223221 }
224222
225223 if ( IsInvalid )
You can’t perform that action at this time.
0 commit comments