ASGL2 3.1.0
dotnet add package ASGL2 --version 3.1.0
NuGet\Install-Package ASGL2 -Version 3.1.0
<PackageReference Include="ASGL2" Version="3.1.0" />
<PackageVersion Include="ASGL2" Version="3.1.0" />
<PackageReference Include="ASGL2" />
paket add ASGL2 --version 3.1.0
#r "nuget: ASGL2, 3.1.0"
#:package ASGL2@3.1.0
#addin nuget:?package=ASGL2&version=3.1.0
#tool nuget:?package=ASGL2&version=3.1.0
ASGL2
A small 2D game library for .NET 10, built on SDL2. Runs on Windows and Linux from the same code and the same assets, and is documented so that both people and AI coding assistants can pick it up quickly (see "Documentation in this package" below).
- Game loop and states:
GameBase(fixed 60 Hz logic loop, separate render loop),BaseStatescreens. - Graphics:
Renderer(any window size, fullscreen), sprites and sprite sheets, scrolling and parallax layers, star fields,Camera(dead zone, look-ahead, shake), TrueType text (TtfFont: UTF-8, rendered sharp at any window scale). - Tile maps: layers, collision (
MoveAndCollide), animated tiles, and a JSON map format (TileMapData,*.tilemap.json) that the ASGL map editor reads and writes. - Gameplay helpers: waypoints, oscillators, phase machines, timers, hitbox and convex polygon collision, particle and floating-text effects.
- Input and audio: keyboard, mouse and gamepads with per-tick pressed/released edges, text input; sound effects with overlapping sound pools and per-play volume, streamed background music (
MusicPlayer, with fades) and pitch-bendable loops, through SDL2_mixer (WAV, OGG, MP3, FLAC). - Embedded assets: sprites, sheets, tile maps, sounds, music and fonts can be loaded from resources compiled into the game's executable as well as from files.
- UI toolkit (
ASGL2.UI): panels, buttons, text boxes, list boxes, dialogs (including modal ones that wait for an answer), toolbar and status bar, drawn with SDL. - 3D (
ASGL2.Graphics3D): a software renderer on top of the 2D renderer, with textures and an OBJ loader.
The SDL2 native libraries are required and are not in this package
ASGL2 calls SDL2 through the managed ppy.SDL2-CS binding (which this package depends on and installs for you), but the
native libraries themselves are not included. ASGL2 needs SDL2 2.0.18 or later, SDL2_ttf (text) and
SDL2_mixer (sound). Without them your game fails at start-up with a DllNotFoundException (a game that only lacks
SDL2_mixer runs, silently, if it uses SoundPool).
Windows
Put these 64-bit files next to your game's .exe (a .NET 10 program runs as 64-bit on 64-bit Windows):
| File | What it is | Where to get it |
|---|---|---|
SDL2.dll |
SDL2 | SDL2-2.x.y-win32-x64.zip from https://github.com/libsdl-org/SDL/releases |
SDL2_ttf.dll |
SDL2_ttf, used for text | SDL2_ttf-2.x.y-win32-x64.zip from https://github.com/libsdl-org/SDL_ttf/releases |
libfreetype-6.dll, zlib1.dll |
needed by older SDL2_ttf builds | the same SDL2_ttf zip (newer builds have FreeType built in; then you do not need them) |
SDL2_mixer.dll |
SDL2_mixer, used for sound (WAV, OGG, MP3 and FLAC are built in) | SDL2_mixer-2.x.y-win32-x64.zip from https://github.com/libsdl-org/SDL_mixer/releases |
The simplest way to copy them on every build is to put them in your project and mark them "Copy to output directory"
(Copy if newer), or add this to your .csproj (the files are ignored on Linux, so it is safe for a cross-platform project):
<ItemGroup>
<Content Include="native\SDL2.dll" CopyToOutputDirectory="PreserveNewest" />
<Content Include="native\SDL2_ttf.dll" CopyToOutputDirectory="PreserveNewest" />
<Content Include="native\libfreetype-6.dll" CopyToOutputDirectory="PreserveNewest" />
<Content Include="native\zlib1.dll" CopyToOutputDirectory="PreserveNewest" />
<Content Include="native\SDL2_mixer.dll" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Linux
Install the libraries from your distribution; ASGL2 finds them under their usual names (libSDL2-2.0.so.0,
libSDL2_ttf-2.0.so.0, libSDL2_mixer-2.0.so.0). Ubuntu 22.04 or newer and Debian 12 or newer are recent enough.
| Distribution | Command |
|---|---|
| Ubuntu, Debian, Mint, Zorin, Pop!_OS | sudo apt install libsdl2-2.0-0 libsdl2-ttf-2.0-0 libsdl2-mixer-2.0-0 |
| Fedora | sudo dnf install SDL2 SDL2_ttf SDL2_mixer |
| Arch, Manjaro | sudo pacman -S sdl2 sdl2_ttf sdl2_mixer |
Text needs a TrueType font: FontFinder looks in the usual font folders and falls back to DejaVu Sans, which most
distributions install by default (fonts-dejavu-core on Debian/Ubuntu).
Running and shipping
dotnet runbuilds and runs your game on either system.- To give a game to players who do not have .NET, publish it self-contained:
dotnet publish -c Release -r linux-x64 --self-contained(or-r win-x64) and ship the output folder. Linux players still install the SDL2 packages above; on Windows, ship the DLLs in the folder. - Sound stutters (most often in virtual machines)? Give the mixer a bigger buffer: set
SoundManager.AudioBufferSize = 4096;before the first sound, or run the game with the environment variableASGL_AUDIO_BUFFER=4096.
SDL2, SDL2_ttf and SDL2_mixer use the zlib license; FreeType and zlib have their own permissive licenses. Check their terms if you redistribute them with your game.
A first game
using ASGL2;
using SDL2;
public enum Screen { Play }
public sealed class MyGame : GameBase
{
public override bool Initialize()
{
Renderer.Instance.Initialize("My game", new RendererSettings
{
LogicalWidth = 800, // the drawing area; the window is scaled to fit it
LogicalHeight = 480,
WindowWidth = 1200,
WindowHeight = 720,
});
ClearColor = SDLColors.FromRgb(20, 24, 40);
SetLogicFPS(60);
SetRenderFPS(60);
RegisterState(Screen.Play, typeof(PlayState));
ChangeGameState(Screen.Play, null);
return true;
}
}
public sealed class PlayState : BaseState
{
public PlayState(object sender, object[] args) : base(sender, args) { }
public override void Initialize() { }
public override void Terminate() { }
public override void GameLogic(object sender, SDL.SDL_Event e)
{
// once per logic tick (60 a second): input, movement, collisions
}
public override void Render(object sender)
{
// once per frame: draw with Renderer.Instance, sprites, tile maps, the UI...
}
}
internal static class Program
{
private static void Main()
{
new MyGame().StartGame();
}
}
Fullscreen and sharp text
The window can be any size: the game draws at its logical size and SDL scales it to fit. Toggle fullscreen with
Renderer.Instance.ToggleFullscreen(), and keep text crisp by rendering it at the window's scale:
font.RenderScale = TtfFont.OutputScale(Renderer.Instance.SDLRenderer); // before drawing; only does work when it changes
Loading a tile map made with the map editor
TileMapData data = TileMapData.Load("Assets/Levels/level1.tilemap.json");
GenericTileMap map = data.CreateMap(Renderer.Instance.SDLRenderer, null, 800, 480, "Assets/Levels/level1.tilemap.json");
Point? start = data.FindSpawn("player");
// every logic tick:
map.UpdateTileAnimations(); // animated tiles (coins, water...) defined in the map file
// every frame:
map.Render(Renderer.Instance.SDLRenderer, 0, 0);
Upgrading from 2.x
ASGL2 3.0 is the cross-platform release. Most games only need to be retargeted to .NET 10 and given SDL2_mixer.dll;
these are the changes that can affect existing code:
- Targets .NET 10 (2.x targeted .NET Framework 4.8). Change your project's
TargetFrameworktonet10.0. - Sound uses SDL2_mixer instead of CSCore (Windows only). Ship
SDL2_mixer.dll(Windows) or installlibsdl2-mixer-2.0-0(Linux). WAV, OGG, MP3 and FLAC all play.SoundManager.SetMasterVolulme/GetMasterVolulmeare nowSetMasterVolume/GetMasterVolume(the old names still work but are obsolete). Master volume and mute now apply to the game's own sound, not to the Windows volume.- Sound plays on one output device, chosen with
SoundManager.DefaultDeviceIndexbefore the first sound (default: the system's default device). LoopingVariableRateSoundno longer takes anMMDevice;VariableRateLoopSampleSourceandSoundManager.GetTempFolder()were removed.
- Images are decoded by StbImageSharp instead of System.Drawing; the
Surface(IntPtr, Bitmap)constructor was removed. SoundPool.Playskips copies of an effect that are still playing (it used to try the next copy in turn whether or not it had finished), so rapid repeats no longer drop sounds while a free copy exists.
New in 3.1
- Zoom:
Camera.Zoom(1 toCamera.MaxZoom) shows less of the world, enlarged;VisibleRect,VisibleWidth/VisibleHeightandSourceRectdescribe what shows, and the zoomed view still reaches the edges of the world. At zoom 1 (the default) the camera behaves exactly as in 3.0. - Render targets:
RenderTargetdraws into an off-screen image (Begin/End) that is then drawn to the window, stretched (Draw, orDrawZoomed(camera)for a zoomed camera). Draw the world into one and the HUD after it, and only the world zooms. - Spawn marker settings:
SpawnData.Propertiesholds a marker's ownkey = valuesettings (set in the map editor by double-clicking a marker);TileMapData.GetSpawnData(type)returns the markers with them, andSpawnData.GetProperty,GetInt,GetFloat,GetBoolread them with defaults. Markers without settings are saved exactly as before, so 3.0 maps and games are unaffected.
New in 3.0
- Music:
MusicPlayerstreams one background track (OGG recommended) with looping, fade in/out, pause and its own volume. - Sound:
SoundPool.Play(name, volume)plays an effect quieter for one play (distance-based volume);SoundPool.Add(name, byte[] data, fileType, ...)loads an effect from memory;SoundManager.IsPlaying;SoundManager.AudioBufferSize(or theASGL_AUDIO_BUFFERenvironment variable). - Text:
TtfFontpasses text as UTF-8;TtfFont(renderer, byte[] fontData, size)opens a font from memory;TtfFont.RenderScaleandTtfFont.OutputScalerender text at the window's real resolution, so it stays sharp when the game is scaled up or fullscreen. - Embedded assets:
SpriteSheet(renderer, ResourceInfo),GenericTileMap/IsometricTileMapconstructors that take the sheet as aResourceInfo,TileMapData.Parse(json, name)andTileMapData.CreateMap(..., ResourceInfo sheet). - UI and loop:
Dialog.ShowModal/Dialog.AskModal(dialogs that wait for the answer) andGameBase.IsExiting/PumpInput/RenderFrame.
Documentation in this package
The package carries its documentation as plain Markdown in the docs/ folder (one page per area, generated from the
same XML doc comments that IntelliSense shows), with docs/llms.txt as an index. That makes it easy to hand to an AI
coding assistant: point it at llms.txt, or at the page for the area you are working on. The full guides, the demo
games and their source are at https://asgl.ServeGame.com.
Requirements
- .NET 10, on 64-bit Windows or Linux (x64; Linux on ARM64 should work with the distribution's SDL2 packages but is untested).
- The SDL2 native libraries, which are not in the package (SDL 2.0.18 or later):
- Windows: put
SDL2.dll,SDL2_ttf.dll(withlibfreetype-6.dll,zlib1.dll) andSDL2_mixer.dllnext to your program. - Linux (Debian/Ubuntu):
sudo apt install libsdl2-2.0-0 libsdl2-ttf-2.0-0 libsdl2-mixer-2.0-0(see above for other distributions).
- Windows: put
- Dependencies installed automatically by NuGet:
ppy.SDL2-CS,StbImageSharp.
License
MIT. See the license in the package metadata. The dependencies (ppy.SDL2-CS, StbImageSharp, SDL2, SDL2_ttf, SDL2_mixer) have
their own licenses.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- ppy.SDL2-CS (>= 1.0.82)
- StbImageSharp (>= 2.30.16)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.