Skip to content

Commit

Permalink
port to mac part 1
Browse files Browse the repository at this point in the history
	- change to .net cross platform
  • Loading branch information
Phoenixfirewingz committed Jun 27, 2023
1 parent dfa8a23 commit 8a1e07e
Show file tree
Hide file tree
Showing 16 changed files with 830 additions and 761 deletions.
11 changes: 5 additions & 6 deletions DiscOut/Avolonia/DiscWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
xmlns:controls="clr-namespace:DiscOut.Avalonia"
Width="640" Height="720"
x:Class="DiscOut.Avalonia.DiscWindow"
Title="DISCout" Background="Black">

<Border Background="Gray" BorderThickness="20">
<Grid>
<Panel Background="Black">
Title="DISCout" Margin="0" Padding="0" Background="Gray" Icon="/build_assets/icon.ico">
<Panel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10">
<Grid Margin="0">
<Panel Background="Black" Margin="0">
<controls:GameOpenGLControl Name="GLView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Panel>
<TextBlock Foreground ="White" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" Name="ScoreText"/>
<TextBlock Foreground ="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" Name="DisplayText"/>
</Grid>
</Border>
</Panel>
</Window>
184 changes: 93 additions & 91 deletions DiscOut/Avolonia/DiscWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,115 +10,117 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace DiscOut.Avalonia;
public partial class DiscWindow : Window
namespace DiscOut.Avalonia
{
private readonly HashSet<Key> keys = new();
private static readonly WaveOutEvent MusicPlayer = new();
private readonly ScoreBoard ScoreBoard;
private int CurrentLevel = 1;
private bool GameRestart = false;
private readonly DispatcherTimer ticker = new() { Interval = new TimeSpan(0, 0, 0, 0, 1000 / 60) };
public partial class DiscWindow : Window
{
private readonly HashSet<Key> keys = new HashSet<Key>();
private static readonly WaveOutEvent MusicPlayer = new WaveOutEvent();
private readonly ScoreBoard ScoreBoard;
private int CurrentLevel = 1;
private bool GameRestart = false;
private readonly DispatcherTimer ticker = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 1000 / 60) };
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public static DiscWindow Instance { get; set; }
public static DiscWindow Instance { get; set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
internal Paddle Paddle { get; } = new();
internal Level Level { get; private set; }
public DiscWindow()
{
InitializeComponent();
Instance = this;
Level = new("assets.levels.level_1.json");
ScoreBoard = ScoreBoard.Load();
MusicPlayer.Init(new Mp3FileReader(AssetUtil.OpenEmbeddedFile("assets.sounds.music.mp3")));
MusicPlayer.Volume = 0.25f;
MusicPlayer.Play();
ticker.Tick += delegate
internal Paddle Paddle { get; } = new Paddle();
internal Level Level { get; private set; }
public DiscWindow()
{
if (GameRestart)
InitializeComponent();
Instance = this;
Level = new Level("assets.levels.level_1.json");
ScoreBoard = ScoreBoard.Load();
MusicPlayer.Init(new Mp3FileReader(AssetUtil.OpenEmbeddedFile("assets.sounds.music.mp3")));
MusicPlayer.Volume = 0.25f;
MusicPlayer.Play();
ticker.Tick += delegate
{
if (keys.Contains(Key.Enter))
if (GameRestart)
{
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND);
GameRestart = false;
DisplayText.Text = "";
Level = new("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json");
keys.Clear();
if (keys.Contains(Key.Enter))
{
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND);
GameRestart = false;
DisplayText.Text = "";
Level = new Level("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json");
keys.Clear();
}
DisplayText.Text = "Press Enter To Restart";
}
DisplayText.Text = "Press Enter To Restart";
}
else
{
Paddle.OnKeyDown(keys);
Level.GetBall().OnKeyDown(keys);
if (!Level.GetBall().IsAlive())
else
{
DisplayText.Text = "Press Space To Start!\n " + ScoreBoard.ToString();
Paddle.OnKeyDown(keys);
Level.GetBall().OnKeyDown(keys);
if (!Level.GetBall().IsAlive())
{
DisplayText.Text = "Press Space To Start!\n " + ScoreBoard.ToString();
}
else
DisplayText.Text = "";
}
else
DisplayText.Text = "";
}
if (Paddle.IsDead())
{
Paddle.ResetPaddle();
int Score = Level.GetBall().GetScore();
if (Score > ScoreBoard.entry[0].Score)
if (Paddle.IsDead())
{
Action<int, string> callback = FinishNewUser;
var popupWindow = new PopUpWindow(Score, callback);
var task = popupWindow.ShowDialog(this);
Paddle.ResetPaddle();
int Score = Level.GetBall().GetScore();
if (Score > ScoreBoard.entry[0].Score)
{
Action<int, string> callback = FinishNewUser;
var popupWindow = new PopUpWindow(Score, callback);
var task = popupWindow.ShowDialog(this);
}
GameRestart = true;
}
GameRestart = true;
}
Level.OnUpdate();
Paddle.OnUpdate();
};
ticker.IsEnabled = true;
Level.OnUpdate();
Paddle.OnUpdate();
};
ticker.IsEnabled = true;
Level.OnUpdate();
Paddle.OnUpdate();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true);
GC.WaitForPendingFinalizers();
}
GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized, true, true);
GC.WaitForPendingFinalizers();
}

protected override void OnKeyDown(KeyEventArgs e)
{
keys.Add(e.Key);
base.OnKeyDown(e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
keys.Add(e.Key);
base.OnKeyDown(e);
}

protected override void OnKeyUp(KeyEventArgs e)
{
keys.Remove(e.Key);
base.OnKeyUp(e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
keys.Remove(e.Key);
base.OnKeyUp(e);
}

public override void Render(DrawingContext context)
{
GLView.Render(context);
base.Render(context);
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.MaxValue);
}
public override void Render(DrawingContext context)
{
GLView.Render(context);
base.Render(context);
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.MaxValue);
}

protected void FinishNewUser(int LastScoreData, string UserNameData)
{
ScoreBoard.AddScore(new(UserNameData, LastScoreData));
ScoreBoard.Save(ScoreBoard);
}
protected void FinishNewUser(int LastScoreData, string UserNameData)
{
ScoreBoard.AddScore(new ScoreEntry(UserNameData, LastScoreData));
ScoreBoard.Save(ScoreBoard);
}

protected override void OnClosing(CancelEventArgs e)
{
ticker.IsEnabled = false;
SoundUtils.CleanUp();
MusicPlayer.Stop();
MusicPlayer.Dispose();
Renderer.Dispose();
ScoreBoard.Save(ScoreBoard);
base.OnClosing(e);
}
protected override void OnClosing(CancelEventArgs e)
{
ticker.IsEnabled = false;
SoundUtils.CleanUp();
MusicPlayer.Stop();
MusicPlayer.Dispose();
Renderer.Dispose();
ScoreBoard.Save(ScoreBoard);
base.OnClosing(e);
}

public void LevelWon()
{
CurrentLevel++;
Level = new Level("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json");
public void LevelWon()
{
CurrentLevel++;
Level = new Level("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json");
}
}
}
38 changes: 20 additions & 18 deletions DiscOut/Avolonia/GameOpenGLControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
using Avalonia.OpenGL.Controls;
using DiscOut.Renderer;
using static Avalonia.OpenGL.GlConsts;
namespace DiscOut.Avalonia;
public partial class GameOpenGLControl : OpenGlControlBase
namespace DiscOut.Avalonia
{
internal QuadBatchRenderer? Renderer { get; private set; }
public GameOpenGLControl()
=> InitializeComponent();

protected override unsafe void OnOpenGlRender(GlInterface gl, int fb)
public partial class GameOpenGLControl : OpenGlControlBase
{
gl.ClearColor(0, 0, 0, 0);
gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
gl.Viewport(0, 0, (int)DiscWindow.Instance.Width - ((int)DiscWindow.Instance.Width / 16), (int)DiscWindow.Instance.Height - ((int)DiscWindow.Instance.Height / 10));
DiscWindow.Instance.Paddle.OnRendering(Renderer!);
DiscWindow.Instance.Level.OnRendering(Renderer!);
}
internal QuadBatchRenderer Renderer { get; private set; }
public GameOpenGLControl()
=> InitializeComponent();

protected override void OnOpenGlInit(GlInterface gl, int fb)
=> Renderer = new(gl);
protected override unsafe void OnOpenGlRender(GlInterface gl, int fb)
{
gl.ClearColor(0, 0, 0, 0);
gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
gl.Viewport(0, 0, (int)DiscWindow.Instance.Width - 25, (int)DiscWindow.Instance.Height);
DiscWindow.Instance.Paddle.OnRendering(Renderer);
DiscWindow.Instance.Level.OnRendering(Renderer);
}

protected override void OnOpenGlDeinit(GlInterface gl, int fb)
=> Renderer!.Dispose();
}
protected override void OnOpenGlInit(GlInterface gl, int fb)
=> Renderer = new QuadBatchRenderer(gl);

protected override void OnOpenGlDeinit(GlInterface gl, int fb)
=> Renderer.Dispose();
}
}
57 changes: 30 additions & 27 deletions DiscOut/Avolonia/PopUpWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@
using Avalonia.Interactivity;
using DiscOut.Util;
using System;
namespace DiscOut.Avalonia;
public partial class PopUpWindow : Window
namespace DiscOut.Avalonia
{
private readonly Action<int, string> Callback;
private readonly int Score;

public PopUpWindow()
public partial class PopUpWindow : Window
{
Score = 0;
Callback = (int n, string s) => { };
InitializeComponent();
TextInput.Text = "";
}
private readonly Action<int, string> Callback;
private readonly int Score;

public PopUpWindow(int score, Action<int, string> callback)
{
Callback = callback;
Score = score;
InitializeComponent();
TextInput.Text = "";
}
public PopUpWindow()
{
Score = 0;
Callback = (int n, string s) => { };
InitializeComponent();
TextInput.Text = "";
}

private void OnButtonClick(object sender, RoutedEventArgs e)
{
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND);
if (TextInput.Text.Length >= 1 && TextInput.Text.Length <= 10)
public PopUpWindow(int score, Action<int, string> callback)
{
Callback = callback;
Score = score;
InitializeComponent();
TextInput.Text = "";
}

private void OnButtonClick(object sender, RoutedEventArgs e)
{
Callback.Invoke(Score, TextInput.Text);
Close();
return;
TextInput.Focus();
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND);
if (TextInput.Text.Length >= 1 && TextInput.Text.Length <= 10)
{
Callback.Invoke(Score, TextInput.Text);
Close();
return;
}
TextInput.Text = "must be between 1 & 10 chars";
}
TextInput.Text = "must be between 1 & 10 chars";
}
}
}
Loading

0 comments on commit 8a1e07e

Please sign in to comment.