Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm implementation #21

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
bin/
obj/
.vs/

*~
*swp
/MiniRuntime.a
*.bc
/MiniRuntime.o
/MiniRuntimea
/zerosnake.html
/zerosnake.ilexe
/zerosnake.js
/SeeSharpSnake.sln
/zerosnake.pdb
/zerosnake.txt
/zerosnake.wasm
49 changes: 0 additions & 49 deletions Game/FrameBuffer.cs

This file was deleted.

169 changes: 91 additions & 78 deletions Game/Game.cs
Original file line number Diff line number Diff line change
@@ -1,116 +1,129 @@
using System;
using Thread = System.Threading.Thread;
using SeeSharpSnake;
using SeeSharpSnake.Game;

struct Game
unsafe struct Game
{
enum Result
{
Win, Loss
}
static Music music;

private Random _random;

private Game(uint randomSeed)
enum Result
{
_random = new Random(randomSeed);
Win, Loss, None
}

private Result Run(ref FrameBuffer fb)
{
Snake s = new Snake(
(byte)(_random.Next() % FrameBuffer.Width),
(byte)(_random.Next() % FrameBuffer.Height),
(Snake.Direction)(_random.Next() % 4));

MakeFood(s, out byte foodX, out byte foodY);
static Game game;

long gameTime = Environment.TickCount64;
// sprites are 4x4
internal const byte boardWidth = 40;
internal const byte boardHeight = 40;

while (true)
{
fb.Clear();
static Random _random;

if (!s.Update())
{
s.Draw(ref fb);
return Result.Loss;
}
Snake s;
byte foodX, foodY;

s.Draw(ref fb);
static readonly byte[] foodSprite = new byte[]
{
0b10100101,
0b10100101,
};
static readonly byte[] foodSprite2 = new byte[]
{
0b11111111,
0b11111111,
};

/// <summary>
/// runs once at startup
/// </summary>
[System.Runtime.InteropServices.UnmanagedCallersOnly(EntryPoint = "start")]
public static void start()
{
game = new Game(42);
}

if (Console.KeyAvailable)
{
ConsoleKeyInfo ki = Console.ReadKey(intercept: true);
switch (ki.Key)
{
case ConsoleKey.UpArrow:
s.Course = Snake.Direction.Up; break;
case ConsoleKey.DownArrow:
s.Course = Snake.Direction.Down; break;
case ConsoleKey.LeftArrow:
s.Course = Snake.Direction.Left; break;
case ConsoleKey.RightArrow:
s.Course = Snake.Direction.Right; break;
}
}
[System.Runtime.InteropServices.UnmanagedCallersOnly(EntryPoint = "update")]
public static void update()
{
game.Main();
}

if (s.HitTest(foodX, foodY))
{
if (s.Extend())
MakeFood(s, out foodX, out foodY);
else
return Result.Win;
}

fb.SetPixel(foodX, foodY, '*');
private Game(uint randomSeed)
{
_random = new Random(randomSeed);
music.SetUp();
s = new Snake(
(byte)(_random.Next() % boardWidth),
(byte)(_random.Next() % boardHeight),
(Snake.Direction)(_random.Next() % 4));

fb.Render();
MakeFood(s, out foodX, out foodY);
}

gameTime += 100;
Result Update()
{
music.PlayTune();

long delay = gameTime - Environment.TickCount64;
if (delay >= 0)
Thread.Sleep((int)delay);
if (!s.Update())
{
s.Draw();
return Result.Loss;
}

s.Draw();

switch ((*W4.GAMEPAD1)) // just handle single key
{
case W4.BUTTON_UP:
s.Course = Snake.Direction.Up; break;
case W4.BUTTON_DOWN:
s.Course = Snake.Direction.Down; break;
case W4.BUTTON_LEFT:
s.Course = Snake.Direction.Left; break;
case W4.BUTTON_RIGHT:
s.Course = Snake.Direction.Right; break;
}

if (s.HitTest(foodX, foodY))
{
if (s.Extend())
MakeFood(s, out foodX, out foodY);
else
gameTime = Environment.TickCount64;
return Result.Win;
}

fixed (byte* spAddr = &(foodSprite[0]))
{
W4.blit(spAddr, foodX << 2, foodY << 2, 4, 4, W4.BLIT_1BPP);
}

return Result.None;
}

void MakeFood(in Snake snake, out byte foodX, out byte foodY)
static void MakeFood(in Snake snake, out byte foodX, out byte foodY)
{
do
{
foodX = (byte)(_random.Next() % FrameBuffer.Width);
foodY = (byte)(_random.Next() % FrameBuffer.Height);
foodX = (byte)(_random.Next() % boardWidth);
foodY = (byte)(_random.Next() % boardHeight);
}
while (snake.HitTest(foodX, foodY));
}

public static void Main()
void Main()
{
Console.SetWindowSize(FrameBuffer.Width, FrameBuffer.Height);
Console.SetBufferSize(FrameBuffer.Width, FrameBuffer.Height);
Console.Title = "See Sharp Snake";
Console.CursorVisible = false;
Result result = Update();

FrameBuffer fb = new FrameBuffer();

while (true)
if (result != Result.None)
{
Game g = new Game((uint)Environment.TickCount64);
Result result = g.Run(ref fb);

string message = result == Result.Win ? "You win" : "You lose";

int position = (FrameBuffer.Width - message.Length) / 2;
for (int i = 0; i < message.Length; i++)
fixed (char* fixedMsg = message)
{
fb.SetPixel(position + i, FrameBuffer.Height / 2, message[i]);
W4.textUtf16((byte*) fixedMsg, (uint) message.Length * 2, 70, 40);
}

fb.Render();

Console.ReadKey(intercept: true);
}
}
}

92 changes: 92 additions & 0 deletions Game/Music.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace SeeSharpSnake.Game
{
unsafe struct Music
{
internal const int notesLength = 26;
const int quarterNote = 16;

internal fixed uint NoteFrequencies[notesLength];
internal fixed uint NoteDurations[notesLength];

static int tuneLength = 60;
static int noteFrame = 0;
static int tuneFrame = 0;
static int tunePos = 0;

void AddNote(uint f, uint d, int ix)
{
NoteFrequencies[ix] = f;
NoteDurations[ix] = d;
}

// Thankyou Matthew Smith!
public void SetUp()
{
int i = 0;
// bar 1, 7 notes
AddNote(123, quarterNote, i++); // b3
AddNote(138, quarterNote, i++); // c#4
AddNote(146, quarterNote, i++); // d4
AddNote(164, quarterNote, i++); // e4
AddNote(184, quarterNote, i++); // F#4
AddNote(146, quarterNote, i++); // d4
AddNote(184, quarterNote * 2, i++); // F#4

// bar 2, 6 notes, 13 total
AddNote(174, quarterNote, i++); // F4
AddNote(138, quarterNote, i++); // c#4
AddNote(174, quarterNote * 2, i++); // F4
AddNote(164, quarterNote, i++); // e4
AddNote(130, quarterNote, i++); // c4
AddNote(164, quarterNote * 2, i++); // e4

// bar 3, 8 notes, 21 total
AddNote(123, quarterNote, i++); // b3
AddNote(138, quarterNote, i++); // c#4
AddNote(146, quarterNote, i++); // d4
AddNote(164, quarterNote, i++); // e4
AddNote(184, quarterNote, i++); // F#4
AddNote(146, quarterNote, i++); // d4
AddNote(184, quarterNote, i++); // F#4
AddNote(246, quarterNote, i++); // b4

// bar 4, 5 notes, 26 total, ok so this is the original not the MS version, shoot me.
AddNote(219, quarterNote, i++); // a4
AddNote(184, quarterNote, i++); // F#4
AddNote(219, quarterNote, i++); // a4
AddNote(184, quarterNote, i++); // F#4
AddNote(219, quarterNote * 4, i++); // a4
}

internal void PlayTune()
{
// next note?
if (noteFrame == NoteDurations[tunePos])
{
tunePos++;
noteFrame = 0;

// loop?
if (tunePos == notesLength)
{
tunePos = 0;
}
}

if (noteFrame == 0)
{
W4.tone(NoteFrequencies[tunePos], NoteDurations[tunePos], 70, 0);
}

noteFrame++;
tuneFrame++;
// loop
if (tuneFrame == tuneLength)
{
tuneFrame = 0;
}
}

}

}
Loading