1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-07-04 23:36:00 +02:00

Initial checkin

This commit is contained in:
Brian Fiete 2019-08-23 11:56:54 -07:00
parent c74712dad9
commit 078564ac9e
3242 changed files with 1616395 additions and 0 deletions

5247
BeefLibs/SDL2/src/SDL2.bf Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,126 @@
using System;
/* Derived from SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013-2016 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>
*
*/
namespace SDL2
{
public static class SDLImage
{
/* Similar to the headers, this is the version we're expecting to be
* running with. You will likely want to check this somewhere in your
* program!
*/
public const int SDL_IMAGE_MAJOR_VERSION = 2;
public const int SDL_IMAGE_MINOR_VERSION = 0;
public const int SDL_IMAGE_PATCHLEVEL = 2;
public enum InitFlags : int32
{
JPG = 0x00000001,
PNG = 0x00000002,
TIF = 0x00000004,
WEBP = 0x00000008
}
public static void SDL_IMAGE_VERSION(out SDL.Version X)
{
X.major = SDL_IMAGE_MAJOR_VERSION;
X.minor = SDL_IMAGE_MINOR_VERSION;
X.patch = SDL_IMAGE_PATCHLEVEL;
}
[LinkName("IMG_Linked_Version")]
public static extern SDL.Version Linked_Version();
[LinkName("IMG_Init")]
public static extern int Init(InitFlags flags);
[LinkName("IMG_Quit")]
public static extern void Quit();
[LinkName("IMG_Load")]
public static extern SDL.Surface* Load(char8* file);
[LinkName("IMG_Load_RW")]
public static extern SDL.Surface* Load_RW(
SDL.RWOps* src,
int32 freesrc
);
[LinkName("IMG_LoadTyped_RW")]
public static extern SDL.Surface* LoadTyped_RW(
SDL.RWOps* src,
int32 freesrc,
char8* type
);
[LinkName("IMG_LoadTexture")]
private static extern SDL.Texture* LoadTexture(
SDL.Renderer* renderer,
char8* file
);
[LinkName("IMG_LoadTexture_RW")]
public static extern SDL.Texture* LoadTexture_RW(
SDL.Renderer* renderer,
SDL.RWOps* src,
int32 freesrc
);
[LinkName("IMG_LoadTextureTyped_RW")]
public static extern SDL.Texture* LoadTextureTyped_RW(
SDL.Renderer* renderer,
SDL.RWOps* src,
int32 freesrc,
char8* type
);
[LinkName("IMG_ReadXPMFromArray")]
public static extern SDL.Surface* ReadXPMFromArray(char8** xpm);
[LinkName("IMG_SavePNG")]
public static extern int32 SavePNG(SDL.Surface* surface, char8* file);
[LinkName("IMG_SavePNG_RW")]
public static extern int SavePNG_RW(
SDL.Surface* surface,
SDL.RWOps* dst,
int32 freedst
);
[LinkName("IMG_SaveJPG")]
public static extern int32 SaveJPG(SDL.Surface* surface, char8* file, int32 quality);
[LinkName("IMG_SaveJPG_RW")]
public static extern int SaveJPG_RW(
SDL.Surface* surface,
SDL.RWOps* dst,
int32 freedst,
int32 quality
);
}
}

View file

@ -0,0 +1,411 @@
#region License
/* SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013-2016 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>
*
*/
#endregion
#region Using Statements
using System;
using System.Runtime.InteropServices;
#endregion
namespace SDL2
{
public static class SDLMixer
{
/* Similar to the headers, this is the version we're expecting to be
* running with. You will likely want to check this somewhere in your
* program!
*/
public const int32 MIXER_MAJOR_VERSION = 2;
public const int32 MIXER_MINOR_VERSION = 0;
public const int32 MIXER_PATCHLEVEL = 2;
/* In C, you can redefine this value before including SDL_mixer.h.
* We're not going to allow this in SDL2#, since the value of this
* variable is persistent and not dependent on preprocessor ordering.
*/
public const int32 MIX_CHANNELS = 8;
public static readonly int32 MIX_DEFAULT_FREQUENCY = 22050;
public static readonly uint16 MIX_DEFAULT_FORMAT =
BitConverter.IsLittleEndian ? SDL.AUDIO_S16LSB : SDL.AUDIO_S16MSB;
public static readonly int32 MIX_DEFAULT_CHANNELS = 2;
public static readonly uint8 MIX_MAX_VOLUME = 128;
public enum MIX_InitFlags
{
Flac = 0x00000001,
Mod = 0x00000002,
Mp3 = 0x00000008,
Ogg = 0x00000010,
Mid = 0x00000020,
}
public enum Fading
{
NoFading,
FadingOut,
FadingIn
}
public enum MusicType
{
None,
Cmd,
Wav,
Mod,
Mid,
Ogg,
Mp3,
Mp3Mad,
Flac,
Modplug
}
public function void MixFuncDelegate(void* udata, uint8* stream, int32 len);
public function void Mix_EffectFunc_t(int32 chan, void* stream, int32 len, void* udata);
public function void Mix_EffectDone_t(int32 chan, void* udata);
public function void MusicFinishedDelegate();
public function void ChannelFinishedDelegate(int32 channel);
public function int32 SoundFontDelegate(char8* a, void* b);
public static void SDL_MIXER_VERSION(out SDL.Version X)
{
X.major = MIXER_MAJOR_VERSION;
X.minor = MIXER_MINOR_VERSION;
X.patch = MIXER_PATCHLEVEL;
}
[LinkName("Version ")]
public static extern SDL.Version MIX_Linked_Version();
[LinkName("Mix_Init")]
public static extern int32 Init(MIX_InitFlags flags);
[LinkName("Mix_Quit")]
public static extern void Quit();
[LinkName("Mix_OpenAudio")]
public static extern int32 OpenAudio(
int frequency,
uint16 format,
int channels,
int chunksize
);
[LinkName("Mix_AllocateChannels")]
public static extern int32 AllocateChannels(int32 numchans);
[LinkName("Mix_QuerySpec")]
public static extern int32 QuerySpec(
out int32 frequency,
out uint16 format,
out int32 channels
);
/* src refers to an SDL_RWops*, IntPtr to a Mix_Chunk* */
/* THIS IS A PUBLIC RWops FUNCTION! */
[LinkName("Mix_LoadWAV_RW")]
public static extern Chunk* LoadWAV_RW(
SDL.RWOps* src,
int freesrc
);
public static Chunk* LoadWAV(StringView file)
{
SDL.RWOps* rwops = SDL.RWFromFile(file.ToScopeCStr!(), "rb");
return LoadWAV_RW(rwops, 1);
}
[LinkName("Mix_LoadMUS")]
public static extern Music* LoadMUS(char8* file);
[LinkName("Mix_QuickLoad_WAV")]
public static extern Chunk* QuickLoad_WAV(uint8* mem);
[LinkName("Mix_QuickLoad_RAW")]
public static extern Chunk* QuickLoad_RAW(uint8* mem, uint len);
[LinkName("Mix_FreeChunk")]
public static extern void FreeChunk(Chunk* chunk);
[LinkName("Mix_FreeMusic")]
public static extern void FreeMusic(Music* music);
[LinkName("Mix_GetNumChunkDecoders")]
public static extern int32 GetNumChunkDecoders();
[LinkName("Mix_GetChunkDecoder")]
public static extern char8* Mix_GetChunkDecoder(int32 index);
[LinkName("Mix_GetNumMusicDecoders")]
public static extern int32 GetNumMusicDecoders();
[LinkName("Mix_GetMusicDecoder")]
public static extern char8* GetMusicDecoder(int32 index);
[LinkName("Mix_GetMusicType")]
public static extern MusicType GetMusicType(Music* music);
[LinkName("Mix_SetPostMix")]
public static extern void SetPostMix(
MixFuncDelegate mix_func,
void* arg
);
[LinkName("Mix_HookMusic")]
public static extern void HookMusic(
MixFuncDelegate mix_func,
void* arg
);
[LinkName("Mix_HookMusicFinished")]
public static extern void HookMusicFinished(
MusicFinishedDelegate music_finished
);
/* IntPtr refers to a void* */
[LinkName("Mix_GetMusicHookData")]
public static extern void* GetMusicHookData();
[LinkName("Mix_ChannelFinished")]
public static extern void ChannelFinished(
ChannelFinishedDelegate channel_finished
);
[LinkName("Mix_RegisterEffect")]
public static extern int32 RegisterEffect(
int chan,
Mix_EffectFunc_t f,
Mix_EffectDone_t d,
void* arg // void*
);
[LinkName("Mix_UnregisterEffect")]
public static extern int32 UnregisterEffect(
int channel,
Mix_EffectFunc_t f
);
[LinkName("Mix_UnregisterAllEffects")]
public static extern int32 UnregisterAllEffects(int32 channel);
[LinkName("Mix_SetPanning")]
public static extern int32 SetPanning(
int channel,
uint8 left,
uint8 right
);
[LinkName("Mix_SetPosition")]
public static extern int32 SetPosition(
int channel,
int16 angle,
uint8 distance
);
[LinkName("Mix_SetDistance")]
public static extern int32 SetDistance(int32 channel, uint8 distance);
[LinkName("Mix_SetReverseStereo")]
public static extern int32 SetReverseStereo(int32 channel, int32 flip);
[LinkName("Mix_ReserveChannels")]
public static extern int32 ReserveChannels(int32 num);
[LinkName("Mix_GroupChannel")]
public static extern int32 GroupChannel(int32 which, int32 tag);
[LinkName("Mix_GroupChannels")]
public static extern int32 GroupChannels(int32 from, int32 to, int32 tag);
[LinkName("Mix_GroupAvailable")]
public static extern int32 GroupAvailable(int32 tag);
[LinkName("Mix_GroupCount")]
public static extern int32 GroupCount(int32 tag);
[LinkName("Mix_GroupOldest")]
public static extern int32 GroupOldest(int32 tag);
[LinkName("Mix_GroupNewer")]
public static extern int32 GroupNewer(int32 tag);
public struct Music;
public struct Chunk;
public static int32 PlayChannel(int channel, Chunk* chunk, int loops)
{
return PlayChannelTimed(channel, chunk, loops, -1);
}
/* chunk refers to a Mix_Chunk* */
[LinkName("Mix_PlayChannelTimed")]
public static extern int32 PlayChannelTimed(
int channel,
Chunk* chunk,
int loops,
int ticks
);
[LinkName("Mix_PlayMusic")]
public static extern int32 PlayMusic(Music* music, int32 loops);
[LinkName("Mix_FadeInMusic")]
public static extern int32 FadeInMusic(
Music* music,
int loops,
int ms
);
[LinkName("Mix_FadeInMusicPos")]
public static extern int32 FadeInMusicPos(
Music* music,
int loops,
int ms,
double position
);
public static int32 Mix_FadeInChannel(
int channel,
Chunk* chunk,
int loops,
int ms
)
{
return FadeInChannelTimed(channel, chunk, loops, ms, -1);
}
[LinkName("Mix_FadeInChannelTimed")]
public static extern int32 FadeInChannelTimed(
int channel,
Chunk* chunk,
int loops,
int ms,
int ticks
);
[LinkName("Mix_Volume")]
public static extern int32 Volume(int32 channel, int32 volume);
/* chunk refers to a Mix_Chunk* */
[LinkName("Mix_VolumeChunk")]
public static extern int32 VolumeChunk(
Chunk* chunk,
int volume
);
[LinkName("Mix_VolumeMusic")]
public static extern int32 VolumeMusic(int32 volume);
[LinkName("Mix_HaltChannel")]
public static extern int32 HaltChannel(int32 channel);
[LinkName("Mix_HaltGroup")]
public static extern int32 HaltGroup(int32 tag);
[LinkName("Mix_HaltMusic")]
public static extern int32 HaltMusic();
[LinkName("Mix_ExpireChannel")]
public static extern int32 ExpireChannel(int32 channel, int32 ticks);
[LinkName("Mix_FadeOutChannel")]
public static extern int32 FadeOutChannel(int32 which, int32 ms);
[LinkName("Mix_FadeOutGroup")]
public static extern int32 FadeOutGroup(int32 tag, int32 ms);
[LinkName("Mix_FadeOutMusic")]
public static extern int32 FadeOutMusic(int32 ms);
[LinkName("Mix_FadingMusic")]
public static extern Fading FadingMusic();
[LinkName("Mix_FadingChannel")]
public static extern Fading FadingChannel(int32 which);
[LinkName("Mix_Pause")]
public static extern void Pause(int32 channel);
[LinkName("Mix_Resume")]
public static extern void Resume(int32 channel);
[LinkName("Mix_Paused")]
public static extern int32 Paused(int32 channel);
[LinkName("Mix_PauseMusic")]
public static extern void PauseMusic();
[LinkName("Mix_ResumeMusic")]
public static extern void ResumeMusic();
[LinkName("Mix_RewindMusic")]
public static extern void RewindMusic();
[LinkName("Mix_PausedMusic")]
public static extern int32 PausedMusic();
[LinkName("Mix_SetMusicPosition")]
public static extern int32 SetMusicPosition(double position);
[LinkName("Mix_Playing")]
public static extern int32 Playing(int32 channel);
[LinkName("Mix_PlayingMusic")]
public static extern int32 PlayingMusic();
[LinkName("Mix_SetMusicCMD")]
public static extern int32 Mix_SetMusicCMD(char8* command);
[LinkName("Mix_SetSynchroValue")]
public static extern int32 SetSynchroValue(int32 value);
[LinkName("Mix_GetSynchroValue")]
public static extern int32 GetSynchroValue();
[LinkName("Mix_SetSoundFonts")]
public static extern int32 SetSoundFonts(char8* paths);
[LinkName("Mix_GetSoundFonts")]
public static extern char8* GetSoundFonts();
[LinkName("Mix_EachSoundFont")]
public static extern int32 EachSoundFont(SoundFontDelegate func, void* data);
[LinkName("Mix_GetChunk")]
public static extern void* GetChunk(int32 channel);
[LinkName("Mix_CloseAudio")]
public static extern void CloseAudio();
}
}

View file

@ -0,0 +1,319 @@
/* SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013-2016 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.Runtime.InteropServices;
namespace SDL2
{
public static class SDLTTF
{
/* Similar to the headers, this is the version we're expecting to be
* running with. You will likely want to check this somewhere in your
* program!
*/
public const int32 SDL_TTF_MAJOR_VERSION = 2;
public const int32 SDL_TTF_MINOR_VERSION = 0;
public const int32 SDL_TTF_PATCHLEVEL = 12;
public const int32 UNICODE_BOM_NATIVE = 0xFEFF;
public const int32 UNICODE_BOM_SWAPPED = 0xFFFE;
public const int32 TTF_STYLE_NORMAL = 0x00;
public const int32 TTF_STYLE_BOLD = 0x01;
public const int32 TTF_STYLE_ITALIC = 0x02;
public const int32 TTF_STYLE_UNDERLINE = 0x04;
public const int32 TTF_STYLE_STRIKETHROUGH = 0x08;
public const int32 TTF_HINTING_NORMAL = 0;
public const int32 TTF_HINTING_LIGHT = 1;
public const int32 TTF_HINTING_MONO = 2;
public const int32 TTF_HINTING_NONE = 3;
public static void SDL_TTF_VERSION(out SDL.Version X)
{
X.major = SDL_TTF_MAJOR_VERSION;
X.minor = SDL_TTF_MINOR_VERSION;
X.patch = SDL_TTF_PATCHLEVEL;
}
[LinkName("TTF_LinkedVersion")]
public static extern SDL.Version LinkedVersion();
[LinkName("TTF_ByteSwappedUNICODE")]
public static extern void ByteSwappedUNICODE(int32 swapped);
[LinkName("TTF_Init")]
public static extern int32 Init();
/* IntPtr refers to a TTF_Font* */
[LinkName("TTF_OpenFont")]
public static extern Font* OpenFont(char8* file, int32 ptsize);
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
/* THIS IS A PUBLIC RWops FUNCTION! */
[LinkName("TTF_OpenFontRW")]
public static extern Font* OpenFontRW(
SDL.RWOps* src,
int freesrc,
int ptsize
);
/* IntPtr refers to a TTF_Font* */
[LinkName("TTF_OpenFontIndex")]
public static extern Font* OpenFontIndex(
char8* file,
int32 ptsize,
int64 index
);
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
/* THIS IS A PUBLIC RWops FUNCTION! */
[LinkName("TTF_OpenFontIndexRW")]
public static extern Font* OpenFontIndexRW(
SDL.RWOps* src,
int32 freesrc,
int32 ptsize,
int64 index
);
public struct Font;
[LinkName("TTF_GetFontStyle")]
public static extern int GetFontStyle(Font* font);
[LinkName("TTF_SetFontStyle")]
public static extern void SetFontStyle(Font* font, int32 style);
[LinkName("TTF_GetFontOutline")]
public static extern int GetFontOutline(Font* font);
[LinkName("TTF_SetFontOutline")]
public static extern void SetFontOutline(Font* font, int32 outline);
[LinkName("TTF_GetFontHinting")]
public static extern int GetFontHinting(Font* font);
[LinkName("TTF_SetFontHinting")]
public static extern void SetFontHinting(Font* font, int32 hinting);
[LinkName("TTF_FontHeight")]
public static extern int32 FontHeight(Font* font);
[LinkName("TTF_FontAscent")]
public static extern int32 FontAscent(Font* font);
[LinkName("TTF_FontDescent")]
public static extern int32 FontDescent(Font* font);
[LinkName("TTF_FontLineSkip")]
public static extern int32 FontLineSkip(Font* font);
[LinkName("TTF_GetFontKerning")]
public static extern int32 GetFontKerning(Font* font);
[LinkName("TTF_SetFontKerning")]
public static extern void SetFontKerning(Font* font, int allowed);
[LinkName("TTF_FontFaces")]
public static extern int64 FontFaces(Font* font);
[LinkName("TTF_FontFaceIsFixedWidth")]
public static extern int FontFaceIsFixedWidth(Font* font);
[LinkName("TTF_FontFaceFamilyName")]
public static extern char8* FontFaceFamilyName(Font* font);
[LinkName("TTF_FontFaceStyleName")]
public static extern char8* FontFaceStyleName(Font* font);
[LinkName("TTF_GlyphIsProvided")]
public static extern int32 GlyphIsProvided(Font* font, uint16 ch);
[LinkName("TTF_GlyphMetrics")]
public static extern int32 GlyphMetrics(
Font* font,
uint16 ch,
out int32 minx,
out int32 maxx,
out int32 miny,
out int32 maxy,
out int32 advance
);
[LinkName("TTF_SizeText")]
public static extern int32 SizeText(
Font* font,
char8* text,
out int w,
out int h
);
[LinkName("TTF_SizeUTF8")]
public static extern int32 SizeUTF8(
Font* font,
char8* text,
out int w,
out int h
);
[LinkName("TTF_SizeUNICODE")]
public static extern int32 SizeUNICODE(
Font* font,
char8* text,
out int32 w,
out int32 h
);
[LinkName("TTF_RenderText_Solid")]
public static extern SDL.Surface* RenderText_Solid(
Font* font,
char8 text,
SDL.Color fg
);
[LinkName("TTF_RenderUTF8_Solid")]
public static extern SDL.Surface* RenderUTF8_Solid(
Font* font,
char8* text,
SDL.Color fg
);
[LinkName("TTF_RenderUNICODE_Solid")]
public static extern SDL.Surface* RenderUNICODE_Solid(
Font* font,
char8* text,
SDL.Color fg
);
[LinkName("TTF_RenderGlyph_Solid")]
public static extern SDL.Surface* RenderGlyph_Solid(
Font* font,
uint16 ch,
SDL.Color fg
);
[LinkName("TTF_RenderText_Shaded")]
public static extern SDL.Surface* RenderText_Shaded(
Font* font,
char8* text,
SDL.Color fg,
SDL.Color bg
);
[LinkName("TTF_RenderUTF8_Shaded")]
public static extern SDL.Surface* RenderUTF8_Shaded(
Font* font,
char8* text,
SDL.Color fg,
SDL.Color bg
);
[LinkName("TTF_RenderUNICODE_Shaded")]
public static extern SDL.Surface* RenderUNICODE_Shaded(
Font* font,
char8* text,
SDL.Color fg,
SDL.Color bg
);
[LinkName("TTF_RenderGlyph_Shaded")]
public static extern SDL.Surface* RenderGlyph_Shaded(
Font* font,
uint16 ch,
SDL.Color fg,
SDL.Color bg
);
[LinkName("TTF_RenderText_Blended")]
public static extern SDL.Surface* RenderText_Blended(
Font* font,
char8* text,
SDL.Color fg
);
[LinkName("TTF_RenderUTF8_Blended")]
public static extern SDL.Surface* RenderUTF8_Blended(
Font* font,
char8* text,
SDL.Color fg
);
[LinkName("TTF_RenderUNICODE_Blended")]
public static extern SDL.Surface* RenderUNICODE_Blended(
Font* font,
char8* text,
SDL.Color fg
);
[LinkName("TTF_RenderText_Blended_Wrapped")]
public static extern SDL.Surface* RenderText_Blended_Wrapped(
Font* font,
char8* text,
SDL.Color fg,
uint wrapped
);
[LinkName("TTF_RenderUTF8_Blended_Wrapped")]
public static extern SDL.Surface* RenderUTF8_Blended_Wrapped(
Font* font,
char8* text,
SDL.Color fg,
uint wrapped
);
[LinkName("TTF_RenderUNICODE_Blended_Wrapped")]
public static extern SDL.Surface* RenderUNICODE_Blended_Wrapped(
Font* font,
char8* text,
SDL.Color fg,
uint wrapped
);
[LinkName("TTF_RenderGlyph_Blended")]
public static extern SDL.Surface* RenderGlyph_Blended(
Font* font,
uint16 ch,
SDL.Color fg
);
[LinkName("TTF_CloseFont")]
public static extern void CloseFont(Font* font);
[LinkName("TTF_Quit")]
public static extern void Quit();
[LinkName("TTF_WasInit")]
public static extern int WasInit();
[LinkName("SDL_GetFontKerningSize")]
public static extern int GetFontKerningSize(
Font* font,
int prev_index,
int index
);
}
}

271
BeefLibs/SDL2/src/SDLApp.bf Normal file
View file

@ -0,0 +1,271 @@
using SDL2;
using System;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace SDL2
{
class Image
{
public SDL.Surface* mSurface;
public SDL.Texture* mTexture;
public ~this()
{
if (mTexture != null)
SDL.DestroyTexture(mTexture);
if (mSurface != null)
SDL.FreeSurface(mSurface);
}
public Result<void> Load(StringView fileName)
{
let origSurface = SDLImage.Load(fileName.ToScopeCStr!());
if (origSurface == null)
return .Err;
mSurface = origSurface;
mTexture = SDL.CreateTextureFromSurface(gApp.mRenderer, mSurface);
return .Ok;
}
}
class Sound
{
public SDLMixer.Chunk* mChunk;
public ~this()
{
if (mChunk != null)
SDLMixer.FreeChunk(mChunk);
}
public Result<void> Load(StringView fileName)
{
mChunk = SDLMixer.LoadWAV(fileName);
if (mChunk == null)
return .Err;
return .Ok;
}
}
class Font
{
public SDLTTF.Font* mFont;
public this()
{
}
public ~this()
{
if (mFont != null)
SDLTTF.CloseFont(mFont);
}
public Result<void> Load(StringView fileName, int32 pointSize)
{
mFont = SDLTTF.OpenFont(fileName.ToScopeCStr!(), pointSize);
if (mFont == null)
return .Err;
return .Ok;
}
}
public class SDLApp
{
public SDL.Window* mWindow;
public SDL.Renderer* mRenderer;
public SDL.Surface* mScreen;
public int32 mUpdateCnt;
public String mTitle = new .("Beef Sample") ~ delete _;
public int32 mWidth = 1024;
public int32 mHeight = 768;
public bool* mKeyboardState;
public this()
{
gApp = this;
}
public ~this()
{
if (mRenderer != null)
SDL.DestroyRenderer(mRenderer);
if (mWindow != null)
SDL.DestroyWindow(mWindow);
}
public void Init()
{
String exePath = scope .();
Environment.GetExecutableFilePath(exePath);
String exeDir = scope .();
Path.GetDirectoryPath(exePath, exeDir);
Directory.SetCurrentDirectory(exeDir);
SDL.Init(.Video | .Events | .Audio);
SDL.EventState(.JoyAxisMotion, .Disable);
SDL.EventState(.JoyBallMotion, .Disable);
SDL.EventState(.JoyHatMotion, .Disable);
SDL.EventState(.JoyButtonDown, .Disable);
SDL.EventState(.JoyButtonUp, .Disable);
SDL.EventState(.JoyDeviceAdded, .Disable);
SDL.EventState(.JoyDeviceRemoved, .Disable);
mWindow = SDL.CreateWindow(mTitle, .Undefined, .Undefined, mWidth, mHeight, .Shown);
mRenderer = SDL.CreateRenderer(mWindow, -1, .Accelerated);
mScreen = SDL.GetWindowSurface(mWindow);
SDLImage.Init(.PNG | .JPG);
SDLMixer.OpenAudio(44100, SDLMixer.MIX_DEFAULT_FORMAT, 2, 4096);
SDLTTF.Init();
}
public bool IsKeyDown(SDL.Scancode scancode)
{
if (mKeyboardState == null)
return false;
return mKeyboardState[(int)scancode];
}
public virtual void Update()
{
}
public virtual void Draw()
{
}
public virtual void KeyDown(SDL.KeyboardEvent evt)
{
if (evt.keysym.scancode == .Grave)
{
GC.Report();
}
}
public virtual void KeyUp(SDL.KeyboardEvent evt)
{
}
public virtual void MouseDown(SDL.MouseButtonEvent evt)
{
}
public virtual void MouseUp(SDL.MouseButtonEvent evt)
{
}
public void Draw(Image image, float x, float y)
{
SDL.Rect srcRect = .(0, 0, image.mSurface.w, image.mSurface.h);
SDL.Rect destRect = .((int32)x, (int32)y, image.mSurface.w, image.mSurface.h);
SDL.RenderCopy(mRenderer, image.mTexture, &srcRect, &destRect);
}
public void Draw(Image image, float x, float y, float rot)
{
SDL.Rect srcRect = .(0, 0, image.mSurface.w, image.mSurface.h);
SDL.Rect destRect = .((int32)x, (int32)y, image.mSurface.w, image.mSurface.h);
SDL.RenderCopyEx(mRenderer, image.mTexture, &srcRect, &destRect, 0, null, .None);
}
public void PlaySound(Sound sound, float volume = 1.0f, float pan = 0.5f)
{
int32 channel = SDLMixer.PlayChannel(-1, sound.mChunk, 0);
//SDLMixer.SetPanning()
SDLMixer.Volume(channel, (int32)(volume * 128));
}
public void Render()
{
SDL.SetRenderDrawColor(mRenderer, 0, 0, 0, 255);
SDL.RenderClear(mRenderer);
Draw();
SDL.RenderPresent(mRenderer);
}
public void Run()
{
Stopwatch sw = scope .();
sw.Start();
int curPhysTickCount = 0;
while (true)
{
int32 waitTime = 1;
SDL.Event event;
while (SDL.PollEvent(out event) != 0)
{
switch (event.type)
{
case .Quit:
return;
case .KeyDown:
KeyDown(event.key);
case .KeyUp:
KeyDown(event.key);
case .MouseButtonDown:
MouseDown(event.button);
case .MouseButtonUp:
MouseUp(event.button);
default:
}
if (event.type == .Quit)
return;
waitTime = 0;
}
// Fixed 60 Hz update
double msPerTick = 1000 / 60.0;
int newPhysTickCount = (int)(sw.ElapsedMilliseconds / msPerTick);
int addTicks = newPhysTickCount - curPhysTickCount;
if (curPhysTickCount == 0)
{
// Initial render
Render();
}
else
{
mKeyboardState = SDL.GetKeyboardState(null);
addTicks = Math.Min(addTicks, 20); // Limit catchup
if (addTicks > 0)
{
for (int i < addTicks)
{
mUpdateCnt++;
Update();
}
Render();
}
else
{
Thread.Sleep(1);
}
}
curPhysTickCount = newPhysTickCount;
}
}
}
static
{
public static SDLApp gApp;
}
}