Skip to content

Instantly share code, notes, and snippets.

@karlgluck
karlgluck / How to Access Data in the Backbuffer in Direct3D 9.cpp
Created January 17, 2014 03:35
This is the code for accessing pixel data from the backbuffer in a D3D9 application. Keywords: LPDIRECT3DSURFACE9 read backbuffer copy back buffer directly access back buffer Direct3D device DirectX 9
void demoExtractBackBufferPixels(LPDIRECT3DDEVICE9 d3d_device) {
// TODO: In your app, add FAILED() macros to check the HRESULTs passed back
// by each of the API calls. I leave these out for clarity.
// Grab the backbuffer from the Direct3D device
LPDIRECT3DSURFACE9 back_buffer = NULL;
d3d_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back_buffer);
// Get the buffer's description and make an offscreen surface in system memory.
@karlgluck
karlgluck / RoombaVictory.ps1
Created September 18, 2019 00:55
Play the Roomba's victory jingle using the Powershell beep. Because why not?
[console]::beep(260,200)
[console]::beep(320,200)
[console]::beep(380,150)
[console]::beep(520,200)
Start-Sleep -Milliseconds 200
[console]::beep(520,150)
[console]::beep(700,800)
@karlgluck
karlgluck / Extensible-Merkle-Trees.md
Last active November 26, 2018 08:05
I present a simple algorithm that allows billions of one-time signatures to be used for the same public key with today's technology

Extensible Merkle Trees

I present a simple algorithm that lets one use an undetermined number of OTS's for the same public key at the expense of a larger signature. My scheme would allow at least 4.2 billion one-time signatures to be used with a single public key using today's technology.

Background

The Merkle signature scheme (MSS) is a well-known way to use a one-time signature (OTS) like the Lamport-Diffie OTS to create a public key cryptosystem. Briefly, one creates a hash tree of height h from 2^h OTS public keys leading to a root public key. To sign a message, one then simply creates a signature from one of the leaf OTS's as usual and provides evidence of its presence in the tree by giving the sequence of hashes that lead from it to the root public key of the MSS.

@karlgluck
karlgluck / PerfectPixelCamera.cs
Created September 13, 2018 22:35
Main file from the Perfect Pixel Camera asset on the Unity asset store, licensed with MIT https://assetstore.unity.com/packages/tools/camera/perfect-pixel-camera-by-gg-ez-100000
// Copyright 2018 Karl Gluck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@karlgluck
karlgluck / ECS.cs
Last active May 17, 2018 00:59
My quick and incomplete version of Overwatch's Entity-Component System from the GDC17 presentation. Mostly written to think about how this would be implemented in a language with reflection.
public class ECSEntity
{
public ArrayList Components;
public static ECSEntity Acquire ()
{
throw new System.NotImplementedException();
ECSEntity retval = null;
return retval;
}
private static Vector3 ViewportToWorldPointUnity (Vector3 point, float fieldOfView, float aspect, Vector3 from, Vector3 to, Vector3 up)
{
// The zNear / zFar magic numbers are what Unity's viewport always uses regardless of camera settings
var matP = Matrix4x4.Perspective (fieldOfView, aspect, 0.6f /* zNear */, 1000f /* zFar */);
// For some reason we have to reverse these coordinates
from = -from;
to = -to;
@karlgluck
karlgluck / GvimOmniSharpSetup.md
Last active September 21, 2017 20:13
Sadly, Gvim + OmniSharp-vim is just not worth the hassle on Windows. Locks up for 1s+ every cursor movement.

How to set up Gvim + OmniSharp-vim on Windows 10

Install Gvim

Right-click the C:\Program Files (x86)\Vim folder and change its Security permissions to allow full control & editing -> You shouldn't have to give admin permissions to do anything else in this guide

Install pathogen.vim

Clone OmniSharp-vim to C:\Program Files (x86)\Vim\vimfiles\bundle\omnisharp-vim

@karlgluck
karlgluck / IsPointInside.cs
Last active July 13, 2017 21:06
Winding method for finding whether a point is contained by a 2D polygon defined by a vertex loop (C#/Unity)
public static bool IsPointInside (Vector2 point, Vector2[] polygonBoundary)
{
int i = 0, j = 1;
int retval = 0;
while (j < polygonBoundary.Length)
{
var pointI = polygonBoundary[i];
var pointJ = polygonBoundary[j];
int side = (pointJ.x - pointI.x) * (point.y - pointI.y) - (point.x - pointI.x) * (pointJ.y - pointI.y);
int upward = ((side > 0) & (pointI.y <= point.y) & (pointJ.y > point.y)) ? +1 : 0;
@karlgluck
karlgluck / Binder.cs
Last active March 7, 2017 22:55
Super simple predeclaration framework for Unity that I'm toying with! Basically you can just declare any delegate field with [Bind] on it and this will look around for a static function or single member on another class that implements it, and hook that up on launch.
using System;
using System.Collections;
using System.Reflection;
using UnityEngine.Scripting;
// [Bind] is used to annotate a static field or property of type Func or Action. It causes
// the value of that member to be mapped to a function somewhere else in the program with the
// same name as that field. The target function can be a static function or an instance
// function whose class has a static property/field of its own type named "Singleton".
//
@karlgluck
karlgluck / google-spreadsheet-as-database.gs
Last active March 7, 2017 22:54
Easy way to make a key-value store in a Google Spreadsheet. Just publish as a webapp and use jsonp.
function doGet(request)
{
var lock = LockService.getPublicLock();
lock.waitLock(15000);
var ss = SpreadsheetApp.openById(request.parameter.id);
var sheet = ss.getSheetByName('Sheet1');
var obj;
var dataRange = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var dataValues = dataRange.getValues();
if (request.parameter.k && request.parameter.v) {