Skip to content

Instantly share code, notes, and snippets.

View greggman's full-sized avatar
😁

Greggman greggman

😁
View GitHub Profile
@greggman
greggman / macro_list_example.cc
Created January 10, 2013 19:06
D.R.Y. people should love macro lists
// Seems like anyone who follows the "Don't Repeat Yourself" principle
// should be into macro lists
#define COMMANDS(OP) \
OP(Jump) \
OP(Call) \
OP(Return) \
OP(Noop) \
OP(Add) \
@greggman
greggman / gist:8413752
Created January 14, 2014 05:58
Compute the z buffer representations for a particular bit depth. Meant to be pasted into JavaScript console.
res = 24; near = 0.1; far = 3500000; end = 1 << res; rangeInv = 1.0 / (near - far); j = []; last = 0; for(i = end - 16; i < end; ++i) {cz = i; cz = cz * 2.0 / (end - 1) - 1; z = near*far*rangeInv*2/(cz+(near+far)*rangeInv); j.push(" " + i + " = " + z.toFixed(3) + (last == 0 ? "" : (" range: " + (z - last).toFixed(3)))); last = z; }; console.log(j.join("\n") + "\n");
@greggman
greggman / gist:9047289
Created February 17, 2014 09:11
Show GL matrix storage

this code

void printMat(const char* l) {
    float mat[16] = { 0, };
    glGetFloatv(GL_MODELVIEW_MATRIX, mat);
    printf("%s\n", l);
    printf("%f %f %f %f\n", mat[0], mat[1], mat[2], mat[3]);
    printf("%f %f %f %f\n", mat[4], mat[5], mat[6], mat[7]);
    printf("%f %f %f %f\n", mat[8], mat[9], mat[10], mat[11]);

printf("%f %f %f %f\n", mat[12], mat[13], mat[14], mat[15]);

@greggman
greggman / gist:9553597
Created March 14, 2014 18:18
dns server that directs all traffic to the machine its on
# requires https://github.com/tjfontaine/node-dns
var os = require('os')
var dns = require('native-dns');
var server = dns.createServer();
var port = 53;
var interfaces = os.networkInterfaces();
var addresses = [];
for (k in interfaces) {
@greggman
greggman / SetFrustumToSubRectOfLargerView.c
Created April 10, 2014 17:42
Set a frustum to a sub rect of a larger view
void SetFrustumToSubRectOfLargerView(
float fullWidth, // width of large 'virtual' monitor that other views will be sub rect of
float fullHeight, // height of large 'virtual' monitor that other views will be sub rect of
float subRectX, // left edge of sub rect for this view
float subRectY, // top edge of sub rect for this view
float subRectWidth, // width of sub rect for this view
float subRectHeight, // height of sub rect fo this view
float fovInRadians, // field of view in radians for fullwidth x fullheight view.
float zNear, // z near clipping plane
float zFar) // z far clipping plane
@greggman
greggman / foo.js
Created May 3, 2014 09:39
Closure no like?
/**
* Some class
* @constructor
* @param {string} name The name.
*/
var Employee = (function(){
// private static field
// @type {string}
var staticVar;
@greggman
greggman / testme.js
Created May 27, 2014 12:27
works on JSDoc 3.3.0-alpha5 with Java but not with node.js
/*
* Some license text, bla bla bal
*/
"use strict";
define(['./somelibrary'], function(SomeLibrary) {
/**
* @typedef {Object} FooThing~Options
@greggman
greggman / promisesequence.js
Last active November 2, 2022 21:38
Run promises in sequence until one succeeds
var Promise = require('promise');
// A function returns a function that will generates a promise when called.
// It will wait 1 second before fulfilling or rejecting the promise. Second argument f = true mean "fail!"
var fn = function(v, f) {
return function() {
return new Promise(function(fulfill, reject) {
setTimeout(function() {
console.log(new Date());
function resize() {
var width = canvas.clientWidth;
var height = canvas.clientHeight;
if (canvas.width != width ||
canvas.height != height) {
canvas.width = width;
canvas.height = height;
}
}
@greggman
greggman / foo.md
Created November 21, 2014 08:33
Answer on SO is wrong

Make any empty repo

$ mkdir test
$ cd test
$ git init
Initialized empty Git repository in /Users/gregg/temp/delme-git/.git/