Skip to content

Instantly share code, notes, and snippets.

@mrange
mrange / 0_README.md
Last active June 29, 2022 14:29
Simple physics game in F# and WPF

Simple physics game in F# and WPF

This is an example on how to use WPF and F# to do a simple physics game using Verlet Integration.

The program creates a system of particles and constraints. Particles have inertia and is affected by gravity but their motion is also contrained by the constraints.

You control the ship by firing rockets attached to the ship. Use the arrow keys to fire the rockets.

I tried to annotate the source code to help guide a developer familiar with languages like C#. If you have suggestions for how to improve it please leave a comment below.

@mrange
mrange / 0_ReadMe.md
Last active April 21, 2022 14:56
VB + ImageSharp => :(

VB.NET + ImageSharp => :(

When trying out VB + ImageSharp I run into strange compiler errors.

1_ImageSharp.cs works fine in C# but the same program in VB 2_ImageSharp.vb produces the errors:

2>C:\code\2_ImageSharp.vb(7,34): error BC32044: Type argument 'Rgba32' does not inherit from or implement the constraint type '?'.
2>C:\code\2_ImageSharp.vb(7,34): error BC30649: '' is an unsupported type.
@mrange
mrange / Program.cs
Created April 17, 2022 17:25
Play mp3 using windows MCI
using System.Runtime.InteropServices;
using System.Text;
static class Program
{
// Using MCI Win32 API
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder? buffer, int bufferSize, IntPtr hwndCallback);
// This is needed for MCI from my testing, it does makes sense
@mrange
mrange / 0_README.md
Last active June 3, 2024 08:06
Simple physics in F# and WPF

Simple physics in F# and WPF

This is an example on how to use WPF and F# to do some simple physics using Verlet Integration.

The program creates a system of particles and constraints. Particles have inertia and is affected by gravity but their motion is also contrained by the constraints.

I tried to annotate the source code to help guide a developer familiar with languages like C#. If you have suggestions for how to improve it please leave a comment below.

How to run

@mrange
mrange / 0_README.md
Last active April 17, 2022 16:11
Simple 2D graphics in F# and WPF

Simple 2D graphics in F# and WPF

This is an example on how to use WPF and F# to draw simple graphics intended to help devs interested in F# to experiment with a simple yet not trivial app.

I tried to annotate the source code to help guide a developer familiar with languages like C#. If you have suggestions for how to improve it please leave a comment below.

How to run

  1. WPF requires a Windows box, maybe I get around to do a Avalonia demo later.
  2. Install dotnet: https://dotnet.microsoft.com/en-us/download
@mrange
mrange / 0_README.md
Last active April 22, 2022 13:36
Inferring C# from JSON with T4

C# from JSON in T4

A microblog with minimal prose and maximum amount of code

Inspired by the release notes in VS about "copy JSON to C#" I tinkered with making a T4 template to do so.

The T4 template uses some new C# language feautures + System.Text.Json so it won't work with T4 bundled with Visual Studio (still on .NET framework).

However it works fine from command line but you need to use a build of dotnet that support raw string literals. The version on my machine is 6.0.300-preview.22204.3

@mrange
mrange / infer.fs
Last active July 4, 2024 00:16
Inferring C# from JSON
module InferSchema =
open System
open System.Text.Json
// The Model for an inferred schema
type InferredSchema =
| Undefined
| Boolean
| Integer
| Float
@mrange
mrange / func.glsl
Last active March 21, 2022 07:29
GLSL Useful functions
void test(float a) {
// sin produces a value between -1.0 and 1.0 that is periodic depending on the input
float u = sin(a);
// Often it is useful to make the value from sin to 0.0 and 1.0
// When u is -1.0 v will be 0.0 and when us is 1.0 v will be 1.0
float v = 0.5+0.5*u;
// mix is a useful function that allows you interpolate from the first input to the second depending on the third
// The third should be in the range 0.0 and 1.0
@mrange
mrange / Program.cs
Created March 15, 2022 19:48
SQL Info Message Capture
using Microsoft.Data.SqlClient;
using System.Collections.Concurrent;
using var conn = new SqlConnection("");
using var capturer = conn.CaptureSqlInfoMessages();
Console.WriteLine("Hello, World!");
static class Extensions
{
@mrange
mrange / Program.cs
Last active February 23, 2022 09:09
C# Nullable
class NullableWoes
{
public void F(int x) => Console.WriteLine(x);
// Works, overloads F
public void F(int? x) => Console.WriteLine(x);
// Works, overloads F
public void F(string x) => Console.WriteLine(x);
// Does not work
public void F(string? x) => Console.WriteLine(x);