Skip to content

Instantly share code, notes, and snippets.

@jmcd
jmcd / RGBACGContext.swift
Last active April 15, 2024 02:58
Create CGContext (then images) from raw pixel data
import QuartzCore
extension CGContext {
private static let colorSpace = CGColorSpaceCreateDeviceRGB()
private static let bitmapInfo =
CGBitmapInfo.byteOrder32Big.rawValue |
CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
static func from(pixels: [UInt32], width: Int) -> CGContext? {
@jmcd
jmcd / split.swift
Created June 18, 2015 07:05
Master Detail with a UISplitViewController without StoryBoards
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = SplitVc()
@jmcd
jmcd / RedmondButtonStyle.swift
Last active April 22, 2023 13:55
SwiftUI ButtonStyle that mimics classic beveled buttons from NeXTSTEP and Windows 95
import SwiftUI
extension Color {
static var redmondBackground = Color(white: 0.78)
static var redmondShadow = Color(white: 0.55)
}
extension Font {
static var redmondLabel = Font(UIFont(name: "MicrosoftSansSerif", size: UIFont.labelFontSize)!)
}
@jmcd
jmcd / _recording.gif
Last active December 20, 2022 08:45
A demo of an animation system for SDL. Sequencing function-pointers that change the values of structs representing the model that gets rendered.
_recording.gif
@jmcd
jmcd / TestOutputHelperTextWriterAdapter.cs
Last active December 8, 2022 09:04
Capture `Console.Out` in an XUnit `ITestOutputHelper`
public class TestOutputHelperTextWriterAdapter : TextWriter
{
private readonly ITestOutputHelper output;
private string currentLine = "";
public TestOutputHelperTextWriterAdapter(ITestOutputHelper output)
{
this.output = output;
}
namespace RomTile;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
internal static class Program
{
private const int TileWidth = 8;
private const int TileHeight = 8;
private const int NumberOfPixelsInATile = TileWidth * TileHeight;
@jmcd
jmcd / KeyExchange.swift
Last active April 8, 2022 17:37
Diffie–Hellman key exchange simple implementation
import Foundation
protocol Cipher {
func encrypt(message: String, secret: Int) -> String
func decrypt(message: String, secret: Int) -> String
}
struct XOR: Cipher {
private func impl(message: String, secret: Int) -> String? {
@jmcd
jmcd / Ball.cs
Created April 3, 2022 06:41
Bouncing balls in MonoGame
namespace MonoGameCross_PlatformDesktopApplication1
{
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public static class Program
{
[STAThread]
private static void Main()
@jmcd
jmcd / AutoGarbage.cs
Created March 28, 2022 07:45
Hack to magic up objects with random property values
using System.Text;
public static class AutoGarbage
{
private const string Chars = "qwertyuiopasdfghjklzxcvbnm1234567890";
public static T Next<T>(this Random random) => (T)Next(random, typeof(T));
private static object Next(this Random random, Type type)
{
@jmcd
jmcd / ASCIIFoldingAnalyzer.cs
Created November 19, 2012 10:48
Searching for text stored with accents in Lucene.NET
using System;
using System.IO;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using NUnit.Framework;