Squashed 'Source/OpenTK/Platform/SDL2/sdl2-cs/' content from commit b434bac

git-subtree-dir: Source/OpenTK/Platform/SDL2/sdl2-cs
git-subtree-split: b434bacba859a036b11ec3ac76c3baa058764769
This commit is contained in:
thefiddler 2013-09-27 14:26:50 +02:00
commit ed45dddb98
5 changed files with 5406 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
bin/
obj/
*.userprefs

25
LICENSE Normal file
View file

@ -0,0 +1,25 @@
/* SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013 Ethan Lee.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
*
*/

45
README Normal file
View file

@ -0,0 +1,45 @@
This is SDL2#, a C# wrapper for SDL2.
Project Website: https://github.com/flibitijibibo/SDL2-CS
License
-------
SDL2 and SDL2# are released under the zlib license. See LICENSE for details.
SDL2# currently uses parts of OpenTK, which is released under the MIT license.
See opentk.LICENSE for details.
About SDL2
----------
For more information about SDL2, visit the SDL wiki:
http://wiki.libsdl.org/moin.fcg/FrontPage
About the C# Wrapper
--------------------
The C# wrapper was written to be used for MonoGame's desktop platforms. However,
this is written in a way that can be used for any general C# application.
The wrapper provides bindings for the following libraries:
- SDL2
- SDL2_image
- SDL2_mixer
- SDL2_ttf
We also provide bindings for OpenGL and OpenAL, taken from OpenTK.
Note that SDL2# will not provide every single SDL2 function. This is due to
limitations in the C# language that would cause major conflicts with the native
SDL2 library and its extensions.
SDL2# is a pure port of the C headers. The naming schemes for this library will
be exactly as they are done in the C library, with little-to-no concern for
"appropriate" C# style. The namespace indicates that this is SDL2, the class
names will indicate which library file the function/type/value exists in, and
everything else will be as close to the C version as technically possible.
Roadmap
-------
To see the current roadmap for SDL2#, visit the GitHub issues page:
https://github.com/flibitijibibo/SDL2-CS/issues

106
src/LPUtf8StrMarshaler.cs Normal file
View file

@ -0,0 +1,106 @@
/* SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013 Ethan Lee.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
*
*/
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace SDL2
{
internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler
{
public const string LeaveAllocated = "LeaveAllocated";
private static ICustomMarshaler
_leaveAllocatedInstance = new LPUtf8StrMarshaler(true),
_defaultInstance = new LPUtf8StrMarshaler(false);
public static ICustomMarshaler GetInstance(string cookie)
{
switch (cookie)
{
case "LeaveAllocated":
return _leaveAllocatedInstance;
default:
return _defaultInstance;
}
}
private bool _leaveAllocated;
public LPUtf8StrMarshaler(bool leaveAllocated)
{
_leaveAllocated = leaveAllocated;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
return null;
var ptr = (byte*)pNativeData;
while (*ptr != 0)
{
ptr++;
}
var bytes = new byte[ptr - (byte*)pNativeData];
Marshal.Copy(pNativeData, bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(bytes);
}
public IntPtr MarshalManagedToNative(object ManagedObj)
{
if (ManagedObj == null)
return IntPtr.Zero;
var str = ManagedObj as string;
if (str == null)
{
throw new ArgumentException("ManagedObj must be a string.", "ManagedObj");
}
var bytes = Encoding.UTF8.GetBytes(str);
var mem = Marshal.AllocHGlobal(bytes.Length + 1);
Marshal.Copy(bytes, 0, mem, bytes.Length);
((byte*)mem)[bytes.Length] = 0;
return mem;
}
public void CleanUpManagedData(object ManagedObj)
{
}
public void CleanUpNativeData(IntPtr pNativeData)
{
if (!_leaveAllocated)
{
Marshal.FreeHGlobal(pNativeData);
}
}
public int GetNativeDataSize ()
{
return -1;
}
}
}

5227
src/SDL2.cs Normal file

File diff suppressed because it is too large Load diff