Skip to content

Commit

Permalink
Some naming convention changes. Yeet SimpleQuad
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Dec 2, 2024
1 parent e31f302 commit 0bc14b9
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 241 deletions.
2 changes: 1 addition & 1 deletion Axolotl2D.Cef/CefBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ internal override void UpdateTexture()

openGL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)renderedFrameSize.X, (uint)renderedFrameSize.Y, 0, PixelFormat.Bgra, PixelType.UnsignedByte, ref MemoryMarshal.GetReference(browserFrameBuffer.AsSpan()));

int location = openGL.GetUniformLocation(game._shaderProgram, "uTexture");
int location = openGL.GetUniformLocation(game.shaderProgramPointer, "uTexture");
openGL.Uniform1(location, 0);

openGL.BindTexture(TextureTarget.Texture2D, 0);
Expand Down
16 changes: 8 additions & 8 deletions Axolotl2D.Cef/CefBrowserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace Axolotl2D.Cef
/// </summary>
public class CefBrowserManager
{
private readonly Dictionary<string, CefBrowser> _browsers = new();
private readonly ILazyDependencyLoader<Game> _lazyGame;
private readonly Dictionary<string, CefBrowser> registeredBrowsers = new();
private readonly ILazyDependencyLoader<Game> lazyLoadedGame;

/// <summary>
/// Creates a new instance of <see cref="CefBrowserManager"/>.
/// </summary>
/// <param name="game">Game to use for initialization</param>
public CefBrowserManager(ILazyDependencyLoader<Game> game)
{
_lazyGame = game;
lazyLoadedGame = game;

var cefSett = new CefSettings
{
Expand All @@ -38,17 +38,17 @@ public CefBrowserManager(ILazyDependencyLoader<Game> game)
/// <param name="baseUrl">URL to register browser with</param>
public void RegisterBrowser(string key, string baseUrl)
{
if (!_lazyGame.IsLoaded)
if (!lazyLoadedGame.IsLoaded)
{
throw new Exception("Attempted to load assets before game initialization!");
}
if (_browsers.ContainsKey(key))
if (registeredBrowsers.ContainsKey(key))
{
throw new Exception($"Browser with key {key} already exists!");
}
var _game = _lazyGame.Value;
var _game = lazyLoadedGame.Value;
var browser = new CefBrowser(_game, Vector2.Zero, Vector2.One, baseUrl);
_browsers.Add(key, browser);
registeredBrowsers.Add(key, browser);
}

/// <summary>
Expand All @@ -59,7 +59,7 @@ public void RegisterBrowser(string key, string baseUrl)
/// <returns>Whether retrieving was succesful.</returns>
public bool TryGetBrowser(string key, out CefBrowser? browser)
{
var success = _browsers.TryGetValue(key, out CefBrowser? output);
var success = registeredBrowsers.TryGetValue(key, out CefBrowser? output);
browser = output;
return success;
}
Expand Down
31 changes: 12 additions & 19 deletions Axolotl2D.Example/ExampleGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Axolotl2D.Example
{
public class ExampleGame : Game
{
private readonly ILogger<ExampleGame> _logger;
private readonly SpriteManager _assetManager;
private readonly CefBrowserManager _cefBrowserManager;
private readonly ILogger<ExampleGame> logger;
private readonly SpriteManager assetManager;
private readonly CefBrowserManager browserManager;

private readonly Song _song;

Expand All @@ -23,30 +23,23 @@ public ExampleGame(IServiceProvider services, ILogger<ExampleGame> logger, Sprit
// It is recommended to hook OnLoad to load assets
OnLoad += Load;

OnDraw += Draw;

this._logger = logger;
this._assetManager = assetManager;
this._cefBrowserManager = cefBrowserManager;
this.logger = logger;
this.assetManager = assetManager;
this.browserManager = cefBrowserManager;

_song = audioPlayer.LoadSong(Assembly.GetEntryAssembly()!.GetManifestResourceStream("Axolotl2D.Example.Resources.Music.SpaceJazz.wav")!);
}

private void Draw(double frameDelta, double frameRate)
{

}

public void Load()
{
// preload assets
_assetManager.LoadSprite("logo", Assembly.GetEntryAssembly()!.GetManifestResourceStream("Axolotl2D.Example.Resources.Sprites.logo.png")!);
assetManager.LoadSprite("logo", Assembly.GetEntryAssembly()!.GetManifestResourceStream("Axolotl2D.Example.Resources.Sprites.logo.png")!);

_cefBrowserManager.RegisterBrowser("github", "https://naamloos.github.io/Axolotl2D.Webtest/");
_cefBrowserManager.RegisterBrowser("google", "https://google.com");
_cefBrowserManager.RegisterBrowser("discord", "https://discord.com/app");
browserManager.RegisterBrowser("github", "https://naamloos.github.io/Axolotl2D.Webtest/");
browserManager.RegisterBrowser("google", "https://google.com");
browserManager.RegisterBrowser("discord", "https://discord.com/app");

_logger.LogInformation("Loaded Game");
logger.LogInformation("Loaded Game");

_song.Play();
}
Expand All @@ -56,7 +49,7 @@ protected override void Cleanup()
// unhook events
OnLoad -= Load;

_logger.LogInformation("Cleaned up events and unloading game...");
logger.LogInformation("Cleaned up events and unloading game...");
}
}
}
6 changes: 0 additions & 6 deletions Axolotl2D.Example/Scenes/ExampleScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ public override void Draw(double frameDelta, double frameRate)

public override void Load()
{
// get streams for resources
using var mochiCat = GetType().Assembly.GetManifestResourceStream("Axolotl2D.Example.Resources.Sprites.mochicat.png")!;
using var rei = GetType().Assembly.GetManifestResourceStream("Axolotl2D.Example.Resources.Sprites.rei.png")!;

// It is not recommended to load Sprites any time a scene is initialized, as it can cause memory leaks.
// At this moment it is not possible to do this any other way. This will be fixed in the future.
this.assetManager.TryGetSprite("logo", out axolotlLogo);

keyboard = game.GetKeyboard();
Expand Down
64 changes: 32 additions & 32 deletions Axolotl2D.Example/Scenes/ExampleScene2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,74 @@ namespace Axolotl2D.Example.Scenes
{
public class ExampleScene2 : BaseScene
{
private IKeyboard? _keyboard;
private readonly ILogger<ExampleScene2> _logger;
private IKeyboard? keyboard;
private readonly ILogger<ExampleScene2> logger;

private CefBrowserManager _cefBrowserManager;
private CefBrowser? _cef1;
private CefBrowser? _cef2;
private CefBrowser? _cef3;
private CefBrowserManager browserManager;
private CefBrowser? browser1;
private CefBrowser? browser2;
private CefBrowser? browser3;

public ExampleScene2(ILogger<ExampleScene2> logger, CefBrowserManager cefBrowserManager)
{
_logger = logger;
_cefBrowserManager = cefBrowserManager;
this.logger = logger;
browserManager = cefBrowserManager;
}

public override void Load()
{
Game.Title = "Scene 2";
Game.ClearColor = Color.RamptoerismeBlue;
_keyboard = Game.GetKeyboard()!;
_logger.LogInformation("Loaded Example Scene 2");
keyboard = Game.GetKeyboard()!;
logger.LogInformation("Loaded Example Scene 2");

_cefBrowserManager.TryGetBrowser("github", out _cef1);
_cefBrowserManager.TryGetBrowser("google", out _cef2);
_cefBrowserManager.TryGetBrowser("discord", out _cef3);
browserManager.TryGetBrowser("github", out browser1);
browserManager.TryGetBrowser("google", out browser2);
browserManager.TryGetBrowser("discord", out browser3);

if (_cef1 == null || _cef2 == null || _cef3 == null)
if (browser1 == null || browser2 == null || browser3 == null)
{
_logger.LogError("Failed to load CEF browsers!");
logger.LogError("Failed to load CEF browsers!");
return;
}

Resize(Game.Viewport);

_cef1.Enable();
_cef2.Enable();
_cef3.Enable();
browser1.Enable();
browser2.Enable();
browser3.Enable();
}

public override void Unload()
{
_logger.LogInformation("Unloaded Example Scene 2");
_cef1?.Disable();
_cef2?.Disable();
_cef3?.Disable();
logger.LogInformation("Unloaded Example Scene 2");
browser1?.Disable();
browser2?.Disable();
browser3?.Disable();
}

public override void Draw(double frameDelta, double frameRate)
{
_cef1?.Draw();
_cef2?.Draw();
_cef3?.Draw();
browser1?.Draw();
browser2?.Draw();
browser3?.Draw();
}

public override void Resize(Vector2 size)
{
// set both browsers to half of the screen
if (_cef1 != null)
_cef1.Size = new Vector2(size.X / 2, size.Y / 2);
if (_cef2 != null)
_cef2.Bounds = (new Vector2(size.X / 2, 0), new Vector2(size.X / 2, size.Y / 2));
if (_cef3 != null)
_cef3.Bounds = (new Vector2(0, size.Y / 2), new Vector2(size.X, size.Y / 2));
if (browser1 != null)
browser1.Size = new Vector2(size.X / 2, size.Y / 2);
if (browser2 != null)
browser2.Bounds = (new Vector2(size.X / 2, 0), new Vector2(size.X / 2, size.Y / 2));
if (browser3 != null)
browser3.Bounds = (new Vector2(0, size.Y / 2), new Vector2(size.X, size.Y / 2));
}

private bool? wasKeyPressed = null;
public override void Update(double frameDelta)
{
if (_keyboard!.IsKeyPressed(Key.Escape))
if (keyboard!.IsKeyPressed(Key.Escape))
{
if (wasKeyPressed == false)
{
Expand Down
28 changes: 14 additions & 14 deletions Axolotl2D/Audio/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ namespace Axolotl2D.Audio
/// </summary>
public unsafe class AudioPlayer : IDisposable
{
private readonly ALContext _alContext;
private readonly AL _al;
private readonly Device* _device;
private readonly Context* _context;
private readonly ALContext alContext;
private readonly AL openAL;
private readonly Device* devicePointer;
private readonly Context* contextPointer;

/// <summary>
/// Creates a new instance of the audio player.
/// </summary>
/// <exception cref="Exception">Something went wrong initializing the audio player.</exception>
public AudioPlayer()
{
_alContext = ALContext.GetApi(true);
_al = AL.GetApi();
_device = _alContext.OpenDevice("");
if (_device == null)
alContext = ALContext.GetApi(true);
openAL = AL.GetApi();
devicePointer = alContext.OpenDevice("");
if (devicePointer == null)
{
throw new Exception("Could not create device");
}

_context = _alContext.CreateContext(_device, null);
_alContext.MakeContextCurrent(_context);
contextPointer = alContext.CreateContext(devicePointer, null);
alContext.MakeContextCurrent(contextPointer);

_al.GetError();
openAL.GetError();
}

/// <summary>
/// Disposes of the audio player.
/// </summary>
public void Dispose()
{
_alContext.DestroyContext(_context);
_alContext.CloseDevice(_device);
alContext.DestroyContext(contextPointer);
alContext.CloseDevice(devicePointer);
}

/// <summary>
Expand All @@ -48,7 +48,7 @@ public void Dispose()
/// <returns>The loaded song</returns>
public Song LoadSong(Stream songStream)
{
return new Song(songStream, _al, _alContext);
return new Song(songStream, openAL, alContext);
}
}
}
38 changes: 18 additions & 20 deletions Axolotl2D/Audio/Song.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ namespace Axolotl2D.Audio
/// </summary>
public class Song
{
private readonly uint _buffer;
private readonly uint _source;
private readonly uint songBufferPointer;
private readonly uint sourcePointer;

private readonly ALContext _alContext;
private readonly AL _al;
private readonly ALContext alContext;
private readonly AL openAL;

internal unsafe Song(Stream songStream, AL _al, ALContext _alContext)
{
this._al = _al;
this._alContext = _alContext;
this.openAL = _al;
this.alContext = _alContext;

ReadOnlySpan<byte> file = ReadStream(songStream);
int index = 0;
Expand All @@ -42,9 +42,9 @@ internal unsafe Song(Stream songStream, AL _al, ALContext _alContext)
short bitsPerSample = -1;
BufferFormat format = 0;

_source = _al.GenSource();
_buffer = _al.GenBuffer();
_al.SetSourceProperty(_source, SourceBoolean.Looping, true);
sourcePointer = _al.GenSource();
songBufferPointer = _al.GenBuffer();
_al.SetSourceProperty(sourcePointer, SourceBoolean.Looping, true);

while (index + 4 < file.Length)
{
Expand Down Expand Up @@ -113,7 +113,7 @@ internal unsafe Song(Stream songStream, AL _al, ALContext _alContext)
index += size;

fixed (byte* pData = data)
_al.BufferData(_buffer, format, pData, size, sampleRate);
_al.BufferData(songBufferPointer, format, pData, size, sampleRate);
Console.WriteLine($"Read {size} bytes Data");
}
else if (identifier == "JUNK")
Expand All @@ -138,31 +138,29 @@ internal unsafe Song(Stream songStream, AL _al, ALContext _alContext)

private byte[] ReadStream(Stream stream)
{
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
return ms.ToArray();
}
using MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
return ms.ToArray();
}

/// <summary>
/// Starts playing the song.
/// </summary>
public void Play()
{
_al.SetSourceProperty(_source, SourceInteger.Buffer, _buffer);
_al.SourcePlay(_source);
openAL.SetSourceProperty(sourcePointer, SourceInteger.Buffer, songBufferPointer);
openAL.SourcePlay(sourcePointer);
}

/// <summary>
/// Stops playing the song.
/// </summary>
public void Stop()
{
_al.SourceStop(_source);
openAL.SourceStop(sourcePointer);

_al.DeleteSource(_source);
_al.DeleteBuffer(_buffer);
openAL.DeleteSource(sourcePointer);
openAL.DeleteBuffer(songBufferPointer);
}
}
}
2 changes: 1 addition & 1 deletion Axolotl2D/Drawable/BaseDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public float Rotation
internal BaseDrawable(Game game)
{
this.game = game;
openGL = game._openGL!;
openGL = game.openGL!;

// Create a VAO.
vaoPointer = openGL.GenVertexArray();
Expand Down
Loading

0 comments on commit 0bc14b9

Please sign in to comment.