SDL2# - C# Wrapper for SDL2
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Minor style fixes, UTF8 update for mixer/ttf

+135 -93
+25 -24
src/SDL2.cs
··· 85 85 { 86 86 Debug.Assert(str != null); 87 87 int bufferSize = Utf8Size(str); 88 - byte* buffer = (byte*)Marshal.AllocHGlobal(bufferSize); 88 + byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize); 89 89 fixed (char* strPtr = str) 90 90 { 91 91 Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize); ··· 95 95 internal static unsafe byte* Utf8EncodeNullable(string str) 96 96 { 97 97 int bufferSize = Utf8SizeNullable(str); 98 - byte* buffer = (byte*)Marshal.AllocHGlobal(bufferSize); 98 + byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize); 99 99 fixed (char* strPtr = str) 100 100 { 101 - Encoding.UTF8.GetBytes(strPtr, str != null ? str.Length + 1 : 0, buffer, bufferSize); 101 + Encoding.UTF8.GetBytes( 102 + strPtr, 103 + (str != null) ? (str.Length + 1) : 0, 104 + buffer, 105 + bufferSize 106 + ); 102 107 } 103 108 return buffer; 104 109 } 105 110 106 111 /* This is public because SDL_DropEvent needs it! */ 107 - internal static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr = false) 112 + public static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr = false) 108 113 { 109 114 if (s == IntPtr.Zero) 110 115 { ··· 268 273 utf8File, 269 274 utf8Mode 270 275 ); 271 - Marshal.FreeHGlobal((IntPtr)utf8Mode); 272 - Marshal.FreeHGlobal((IntPtr)utf8File); 276 + Marshal.FreeHGlobal((IntPtr) utf8Mode); 277 + Marshal.FreeHGlobal((IntPtr) utf8File); 273 278 return rwOps; 274 279 } 275 280 ··· 1746 1751 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] 1747 1752 public static extern void SDL_GL_DeleteContext(IntPtr context); 1748 1753 1749 - /* IntPtr refers to a function pointer */ 1750 - [DllImport(nativeLibName, EntryPoint = "SDL_GL_GetProcAddress", CallingConvention = CallingConvention.Cdecl)] 1751 - private static extern unsafe IntPtr INTERNAL_SDL_GL_GetProcAddress( 1752 - byte* proc 1753 - ); 1754 - public static unsafe IntPtr SDL_GL_GetProcAddress(string proc) 1755 - { 1756 - int utf8ProcBufSize = Utf8Size(proc); 1757 - byte* utf8Proc = stackalloc byte[utf8ProcBufSize]; 1758 - return INTERNAL_SDL_GL_GetProcAddress( 1759 - Utf8Encode(proc, utf8Proc, utf8ProcBufSize) 1760 - ); 1761 - } 1762 - 1763 1754 [DllImport(nativeLibName, EntryPoint = "SDL_GL_LoadLibrary", CallingConvention = CallingConvention.Cdecl)] 1764 1755 private static extern unsafe int INTERNAL_SDL_GL_LoadLibrary(byte* path); 1765 1756 public static unsafe int SDL_GL_LoadLibrary(string path) ··· 1768 1759 int result = INTERNAL_SDL_GL_LoadLibrary( 1769 1760 utf8Path 1770 1761 ); 1771 - Marshal.FreeHGlobal((IntPtr)utf8Path); 1762 + Marshal.FreeHGlobal((IntPtr) utf8Path); 1772 1763 return result; 1773 1764 } 1774 1765 ··· 1776 1767 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] 1777 1768 public static extern IntPtr SDL_GL_GetProcAddress(IntPtr proc); 1778 1769 1770 + /* IntPtr refers to a function pointer */ 1771 + public static unsafe IntPtr SDL_GL_GetProcAddress(string proc) 1772 + { 1773 + int utf8ProcBufSize = Utf8Size(proc); 1774 + byte* utf8Proc = stackalloc byte[utf8ProcBufSize]; 1775 + return SDL_GL_GetProcAddress( 1776 + (IntPtr) Utf8Encode(proc, utf8Proc, utf8ProcBufSize) 1777 + ); 1778 + } 1779 + 1779 1780 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] 1780 1781 public static extern void SDL_GL_UnloadLibrary(); 1781 1782 ··· 2133 2134 int result = INTERNAL_SDL_Vulkan_LoadLibrary( 2134 2135 utf8Path 2135 2136 ); 2136 - Marshal.FreeHGlobal((IntPtr)utf8Path); 2137 + Marshal.FreeHGlobal((IntPtr) utf8Path); 2137 2138 return result; 2138 2139 } 2139 2140 ··· 4322 4323 int result = INTERNAL_SDL_SetClipboardText( 4323 4324 utf8Text 4324 4325 ); 4325 - Marshal.FreeHGlobal((IntPtr)utf8Text); 4326 + Marshal.FreeHGlobal((IntPtr) utf8Text); 4326 4327 return result; 4327 4328 } 4328 4329 ··· 6306 6307 int result = INTERNAL_SDL_GameControllerAddMapping( 6307 6308 utf8MappingString 6308 6309 ); 6309 - Marshal.FreeHGlobal((IntPtr)utf8MappingString); 6310 + Marshal.FreeHGlobal((IntPtr) utf8MappingString); 6310 6311 return result; 6311 6312 } 6312 6313
+5 -5
src/SDL2_image.cs
··· 98 98 IntPtr handle = INTERNAL_IMG_Load( 99 99 utf8File 100 100 ); 101 - Marshal.FreeHGlobal((IntPtr)utf8File); 101 + Marshal.FreeHGlobal((IntPtr) utf8File); 102 102 return handle; 103 103 } 104 104 ··· 147 147 renderer, 148 148 utf8File 149 149 ); 150 - Marshal.FreeHGlobal((IntPtr)utf8File); 150 + Marshal.FreeHGlobal((IntPtr) utf8File); 151 151 return handle; 152 152 } 153 153 ··· 188 188 freesrc, 189 189 utf8Type 190 190 ); 191 - Marshal.FreeHGlobal((IntPtr)utf8Type); 191 + Marshal.FreeHGlobal((IntPtr) utf8Type); 192 192 return handle; 193 193 } 194 194 ··· 212 212 surface, 213 213 utf8File 214 214 ); 215 - Marshal.FreeHGlobal((IntPtr)utf8File); 215 + Marshal.FreeHGlobal((IntPtr) utf8File); 216 216 return result; 217 217 } 218 218 ··· 240 240 utf8File, 241 241 quality 242 242 ); 243 - Marshal.FreeHGlobal((IntPtr)utf8File); 243 + Marshal.FreeHGlobal((IntPtr) utf8File); 244 244 return result; 245 245 } 246 246
+25 -14
src/SDL2_mixer.cs
··· 200 200 201 201 /* IntPtr refers to a Mix_Music* */ 202 202 [DllImport(nativeLibName, EntryPoint = "Mix_LoadMUS", CallingConvention = CallingConvention.Cdecl)] 203 - private static extern IntPtr INTERNAL_Mix_LoadMUS( 204 - byte[] file 203 + private static extern unsafe IntPtr INTERNAL_Mix_LoadMUS( 204 + byte* file 205 205 ); 206 - public static IntPtr Mix_LoadMUS(string file) 206 + public static unsafe IntPtr Mix_LoadMUS(string file) 207 207 { 208 - return INTERNAL_Mix_LoadMUS(SDL.UTF8_ToNative(file)); 208 + byte* utf8File = SDL.Utf8Encode(file); 209 + IntPtr handle = INTERNAL_Mix_LoadMUS( 210 + utf8File 211 + ); 212 + Marshal.FreeHGlobal((IntPtr) utf8File); 213 + return handle; 209 214 } 210 215 211 216 /* IntPtr refers to a Mix_Chunk* */ ··· 569 574 public static extern int Mix_PlayingMusic(); 570 575 571 576 [DllImport(nativeLibName, EntryPoint = "Mix_SetMusicCMD", CallingConvention = CallingConvention.Cdecl)] 572 - private static extern int INTERNAL_Mix_SetMusicCMD( 573 - byte[] command 577 + private static extern unsafe int INTERNAL_Mix_SetMusicCMD( 578 + byte* command 574 579 ); 575 - public static int Mix_SetMusicCMD(string command) 580 + public static unsafe int Mix_SetMusicCMD(string command) 576 581 { 577 - return INTERNAL_Mix_SetMusicCMD( 578 - SDL.UTF8_ToNative(command) 582 + byte* utf8Cmd = SDL.Utf8Encode(command); 583 + int result = INTERNAL_Mix_SetMusicCMD( 584 + utf8Cmd 579 585 ); 586 + Marshal.FreeHGlobal((IntPtr) utf8Cmd); 587 + return result; 580 588 } 581 589 582 590 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] ··· 586 594 public static extern int Mix_GetSynchroValue(); 587 595 588 596 [DllImport(nativeLibName, EntryPoint = "Mix_SetSoundFonts", CallingConvention = CallingConvention.Cdecl)] 589 - private static extern int INTERNAL_Mix_SetSoundFonts( 590 - byte[] paths 597 + private static extern unsafe int INTERNAL_Mix_SetSoundFonts( 598 + byte* paths 591 599 ); 592 - public static int Mix_SetSoundFonts(string paths) 600 + public static unsafe int Mix_SetSoundFonts(string paths) 593 601 { 594 - return INTERNAL_Mix_SetSoundFonts( 595 - SDL.UTF8_ToNative(paths) 602 + byte* utf8Paths = SDL.Utf8Encode(paths); 603 + int result = INTERNAL_Mix_SetSoundFonts( 604 + utf8Paths 596 605 ); 606 + Marshal.FreeHGlobal((IntPtr) utf8Paths); 607 + return result; 597 608 } 598 609 599 610 [DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)]
+80 -50
src/SDL2_ttf.cs
··· 95 95 96 96 /* IntPtr refers to a TTF_Font* */ 97 97 [DllImport(nativeLibName, EntryPoint = "TTF_OpenFont", CallingConvention = CallingConvention.Cdecl)] 98 - private static extern IntPtr INTERNAL_TTF_OpenFont( 99 - byte[] file, 98 + private static extern unsafe IntPtr INTERNAL_TTF_OpenFont( 99 + byte* file, 100 100 int ptsize 101 101 ); 102 - public static IntPtr TTF_OpenFont(string file, int ptsize) 102 + public static unsafe IntPtr TTF_OpenFont(string file, int ptsize) 103 103 { 104 - return INTERNAL_TTF_OpenFont( 105 - SDL.UTF8_ToNative(file), 104 + byte* utf8File = SDL.Utf8Encode(file); 105 + IntPtr handle = INTERNAL_TTF_OpenFont( 106 + utf8File, 106 107 ptsize 107 108 ); 109 + Marshal.FreeHGlobal((IntPtr) utf8File); 110 + return handle; 108 111 } 109 112 110 113 /* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */ ··· 118 121 119 122 /* IntPtr refers to a TTF_Font* */ 120 123 [DllImport(nativeLibName, EntryPoint = "TTF_OpenFontIndex", CallingConvention = CallingConvention.Cdecl)] 121 - private static extern IntPtr INTERNAL_TTF_OpenFontIndex( 122 - byte[] file, 124 + private static extern unsafe IntPtr INTERNAL_TTF_OpenFontIndex( 125 + byte* file, 123 126 int ptsize, 124 127 long index 125 128 ); 126 - public static IntPtr TTF_OpenFontIndex( 129 + public static unsafe IntPtr TTF_OpenFontIndex( 127 130 string file, 128 131 int ptsize, 129 132 long index 130 133 ) { 131 - return INTERNAL_TTF_OpenFontIndex( 132 - SDL.UTF8_ToNative(file), 134 + byte* utf8File = SDL.Utf8Encode(file); 135 + IntPtr handle = INTERNAL_TTF_OpenFontIndex( 136 + utf8File, 133 137 ptsize, 134 138 index 135 139 ); 140 + Marshal.FreeHGlobal((IntPtr) utf8File); 141 + return handle; 136 142 } 137 143 138 144 /* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */ ··· 282 288 283 289 /* font refers to a TTF_Font* */ 284 290 [DllImport(nativeLibName, EntryPoint = "TTF_SizeUTF8", CallingConvention = CallingConvention.Cdecl)] 285 - public static extern int INTERNAL_TTF_SizeUTF8( 291 + public static extern unsafe int INTERNAL_TTF_SizeUTF8( 286 292 IntPtr font, 287 - byte[] text, 293 + byte* text, 288 294 out int w, 289 295 out int h 290 296 ); 291 - public static int TTF_SizeUTF8( 297 + public static unsafe int TTF_SizeUTF8( 292 298 IntPtr font, 293 299 string text, 294 300 out int w, 295 301 out int h 296 302 ) { 297 - return INTERNAL_TTF_SizeUTF8( 303 + byte* utf8Text = SDL.Utf8Encode(text); 304 + int result = INTERNAL_TTF_SizeUTF8( 298 305 font, 299 - SDL.UTF8_ToNative(text), 306 + utf8Text, 300 307 out w, 301 308 out h 302 309 ); 310 + Marshal.FreeHGlobal((IntPtr) utf8Text); 311 + return result; 303 312 } 304 313 305 314 /* font refers to a TTF_Font* */ ··· 329 338 * Only available in 2.0.16 or higher. 330 339 */ 331 340 [DllImport(nativeLibName, EntryPoint = "TTF_MeasureUTF8", CallingConvention = CallingConvention.Cdecl)] 332 - public static extern int INTERNAL_TTF_MeasureUTF8( 341 + public static extern unsafe int INTERNAL_TTF_MeasureUTF8( 333 342 IntPtr font, 334 - byte[] text, 343 + byte* text, 335 344 int measure_width, 336 345 out int extent, 337 346 out int count 338 347 ); 339 - public static int TTF_MeasureUTF8( 348 + public static unsafe int TTF_MeasureUTF8( 340 349 IntPtr font, 341 350 string text, 342 351 int measure_width, 343 352 out int extent, 344 353 out int count 345 354 ) { 346 - return INTERNAL_TTF_MeasureUTF8( 355 + byte* utf8Text = SDL.Utf8Encode(text); 356 + int result = INTERNAL_TTF_MeasureUTF8( 347 357 font, 348 - SDL.UTF8_ToNative(text), 358 + utf8Text, 349 359 measure_width, 350 360 out extent, 351 361 out count 352 362 ); 363 + Marshal.FreeHGlobal((IntPtr) utf8Text); 364 + return result; 353 365 } 354 366 355 367 /* font refers to a TTF_Font* ··· 376 388 377 389 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ 378 390 [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Solid", CallingConvention = CallingConvention.Cdecl)] 379 - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Solid( 391 + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Solid( 380 392 IntPtr font, 381 - byte[] text, 393 + byte* text, 382 394 SDL.SDL_Color fg 383 395 ); 384 - public static IntPtr TTF_RenderUTF8_Solid( 396 + public static unsafe IntPtr TTF_RenderUTF8_Solid( 385 397 IntPtr font, 386 398 string text, 387 399 SDL.SDL_Color fg 388 400 ) { 389 - return INTERNAL_TTF_RenderUTF8_Solid( 401 + byte* utf8Text = SDL.Utf8Encode(text); 402 + IntPtr result = INTERNAL_TTF_RenderUTF8_Solid( 390 403 font, 391 - SDL.UTF8_ToNative(text), 404 + utf8Text, 392 405 fg 393 406 ); 407 + Marshal.FreeHGlobal((IntPtr) utf8Text); 408 + return result; 394 409 } 395 410 396 411 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ ··· 418 433 * Only available in 2.0.16 or higher. 419 434 */ 420 435 [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Solid_Wrapped", CallingConvention = CallingConvention.Cdecl)] 421 - public static extern IntPtr INTERNAL_TTF_RenderUTF8_Solid_Wrapped( 436 + public static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Solid_Wrapped( 422 437 IntPtr font, 423 - byte[] text, 438 + byte* text, 424 439 SDL.SDL_Color fg, 425 440 uint wrapLength 426 441 ); 427 - public static IntPtr TTF_RenderUTF8_Solid_Wrapped( 442 + public static unsafe IntPtr TTF_RenderUTF8_Solid_Wrapped( 428 443 IntPtr font, 429 444 string text, 430 445 SDL.SDL_Color fg, 431 446 uint wrapLength 432 447 ) { 433 - return INTERNAL_TTF_RenderUTF8_Solid_Wrapped( 448 + byte* utf8Text = SDL.Utf8Encode(text); 449 + IntPtr result = INTERNAL_TTF_RenderUTF8_Solid_Wrapped( 434 450 font, 435 - SDL.UTF8_ToNative(text), 451 + utf8Text, 436 452 fg, 437 453 wrapLength 438 454 ); 455 + Marshal.FreeHGlobal((IntPtr) utf8Text); 456 + return result; 439 457 } 440 458 441 459 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* ··· 480 498 481 499 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ 482 500 [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Shaded", CallingConvention = CallingConvention.Cdecl)] 483 - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Shaded( 501 + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Shaded( 484 502 IntPtr font, 485 - byte[] text, 503 + byte* text, 486 504 SDL.SDL_Color fg, 487 505 SDL.SDL_Color bg 488 506 ); 489 - public static IntPtr TTF_RenderUTF8_Shaded( 507 + public static unsafe IntPtr TTF_RenderUTF8_Shaded( 490 508 IntPtr font, 491 509 string text, 492 510 SDL.SDL_Color fg, 493 511 SDL.SDL_Color bg 494 512 ) { 495 - return INTERNAL_TTF_RenderUTF8_Shaded( 513 + byte* utf8Text = SDL.Utf8Encode(text); 514 + IntPtr result = INTERNAL_TTF_RenderUTF8_Shaded( 496 515 font, 497 - SDL.UTF8_ToNative(text), 516 + utf8Text, 498 517 fg, 499 518 bg 500 519 ); 520 + Marshal.FreeHGlobal((IntPtr) utf8Text); 521 + return result; 501 522 } 502 523 503 524 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ ··· 525 546 * Only available in 2.0.16 or higher. 526 547 */ 527 548 [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Shaded_Wrapped", CallingConvention = CallingConvention.Cdecl)] 528 - public static extern IntPtr INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( 549 + public static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( 529 550 IntPtr font, 530 - byte[] text, 551 + byte* text, 531 552 SDL.SDL_Color fg, 532 553 SDL.SDL_Color bg, 533 554 uint wrapLength 534 555 ); 535 - public static IntPtr TTF_RenderUTF8_Shaded_Wrapped( 556 + public static unsafe IntPtr TTF_RenderUTF8_Shaded_Wrapped( 536 557 IntPtr font, 537 558 string text, 538 559 SDL.SDL_Color fg, 539 560 SDL.SDL_Color bg, 540 561 uint wrapLength 541 562 ) { 542 - return INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( 563 + byte* utf8Text = SDL.Utf8Encode(text); 564 + IntPtr result = INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( 543 565 font, 544 - SDL.UTF8_ToNative(text), 566 + utf8Text, 545 567 fg, 546 568 bg, 547 569 wrapLength 548 570 ); 571 + Marshal.FreeHGlobal((IntPtr) utf8Text); 572 + return result; 549 573 } 550 574 551 575 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ ··· 590 614 591 615 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ 592 616 [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended", CallingConvention = CallingConvention.Cdecl)] 593 - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Blended( 617 + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Blended( 594 618 IntPtr font, 595 - byte[] text, 619 + byte* text, 596 620 SDL.SDL_Color fg 597 621 ); 598 - public static IntPtr TTF_RenderUTF8_Blended( 622 + public static unsafe IntPtr TTF_RenderUTF8_Blended( 599 623 IntPtr font, 600 624 string text, 601 625 SDL.SDL_Color fg 602 626 ) { 603 - return INTERNAL_TTF_RenderUTF8_Blended( 627 + byte* utf8Text = SDL.Utf8Encode(text); 628 + IntPtr result = INTERNAL_TTF_RenderUTF8_Blended( 604 629 font, 605 - SDL.UTF8_ToNative(text), 630 + utf8Text, 606 631 fg 607 632 ); 633 + Marshal.FreeHGlobal((IntPtr) utf8Text); 634 + return result; 608 635 } 609 636 610 637 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ ··· 628 655 629 656 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ 630 657 [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended_Wrapped", CallingConvention = CallingConvention.Cdecl)] 631 - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Blended_Wrapped( 658 + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Blended_Wrapped( 632 659 IntPtr font, 633 - byte[] text, 660 + byte* text, 634 661 SDL.SDL_Color fg, 635 662 uint wrapped 636 663 ); 637 - public static IntPtr TTF_RenderUTF8_Blended_Wrapped( 664 + public static unsafe IntPtr TTF_RenderUTF8_Blended_Wrapped( 638 665 IntPtr font, 639 666 string text, 640 667 SDL.SDL_Color fg, 641 668 uint wrapped 642 669 ) { 643 - return INTERNAL_TTF_RenderUTF8_Blended_Wrapped( 670 + byte* utf8Text = SDL.Utf8Encode(text); 671 + IntPtr result = INTERNAL_TTF_RenderUTF8_Blended_Wrapped( 644 672 font, 645 - SDL.UTF8_ToNative(text), 673 + utf8Text, 646 674 fg, 647 675 wrapped 648 676 ); 677 + Marshal.FreeHGlobal((IntPtr) utf8Text); 678 + return result; 649 679 } 650 680 651 681 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */