Skip to content

Instantly share code, notes, and snippets.

Avatar
😁

Greggman greggman

😁
View GitHub Profile
@greggman
greggman / macro_list_example.cc
Created Jan 10, 2013
D.R.Y. people should love macro lists
View macro_list_example.cc
// 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 Jan 14, 2014
Compute the z buffer representations for a particular bit depth. Meant to be pasted into JavaScript console.
View gist:8413752
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 Feb 17, 2014
Show GL matrix storage
View gist:9047289

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 Mar 14, 2014
dns server that directs all traffic to the machine its on
View gist:9553597
# 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 Apr 10, 2014
Set a frustum to a sub rect of a larger view
View SetFrustumToSubRectOfLargerView.c
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
Closure no like?
View foo.js
/**
* 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
works on JSDoc 3.3.0-alpha5 with Java but not with node.js
View testme.js
/*
* Some license text, bla bla bal
*/
"use strict";
define(['./somelibrary'], function(SomeLibrary) {
/**
* @typedef {Object} FooThing~Options
@greggman
greggman / promisesequence.js
Last active Aug 9, 2016
Run promises in sequence until one succeeds
View promisesequence.js
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());
View resize-canvas-to-css.js
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 Nov 21, 2014
Answer on SO is wrong
View foo.md

Make any empty repo

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