Skip to content

Instantly share code, notes, and snippets.

@jsimmons
jsimmons / template.lua
Created December 4, 2010 23:25
Simple templating system in Lua using Lua as the templating language.
local concat, coroutine, insert, loadstring, open, pcall, setfenv, setmetatable =
table.concat, coroutine, table.insert, loadstring, io.open, pcall, setfenv, setmetatable
module 'spin.template'
function parse_file(path, env)
local file, err = open(path, 'r')
if not file then return nil, err end
local source = file:read('*a')
@jsimmons
jsimmons / printtable.lua
Created January 3, 2011 04:46
Prints tables - yay
local function print_table(tab, level, done)
level = level or 1
done = done or {}
assert(type(tab) == 'table')
for k, v in pairs(tab) do
if done[v] then
print(('\t'):rep(level) .. ('\t'):rep(level) .. '[' .. tostring(k) .. '] already printed!')
else
local t = type(v)
if t == 'table' then
@jsimmons
jsimmons / ring-buffer.c
Created May 31, 2011 12:08
Didn't want to loose this to the depths of my projects folder. Note to self, comment your damn implementation too.
#include "ring-buffer.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
RingBuffer *rb_new(size_t size)
{
RingBuffer *buf = malloc(sizeof(*buf));
assert(buf != NULL);
@jsimmons
jsimmons / generator.lua
Created May 31, 2011 12:18
Hacky GL header generator in Lua. Goal: Create a LuaJIT FFI interface for OpenGL
local parser = require 'parser'
--
-- Generation options.
--
local SPEC_PATH = './specs/'
local TM_PATH = SPEC_PATH .. 'gl.tm'
local ENUM_PATH = 'enum.spec'
local ENUMEXT_PATH = SPEC_PATH .. 'enumext.spec'
@jsimmons
jsimmons / sdl.lua
Created May 31, 2011 12:21
In order to test my gl bindings I needed a gl context, so I quickly bound SDL up.
local ffi = require 'ffi'
ffi.cdef [[
enum SDL_INIT_FLAGS {
SDL_INIT_TIMER = 0x00000001,
SDL_INIT_AUDIO = 0x00000010,
SDL_INIT_VIDEO = 0x00000020,
SDL_INIT_JOYSTICK = 0x00000200,
SDL_INIT_HAPTIC = 0x00001000,
SDL_INIT_NOPARACHUTE = 0x00100000,
@jsimmons
jsimmons / event.h
Created May 15, 2012 02:01
SGL Core headers
#ifndef SGL_EVENT_H
#define SGL_EVENT_H
#include <stdint.h>
#include "button.h"
struct _SWindow;
typedef enum
@jsimmons
jsimmons / main.lua
Created May 15, 2012 02:05
Playing with SGL and the LuaJIT FFI
local ffi = require 'ffi'
local sgl = require 'sgl'
local gl = require 'opengl'
local shader = require 'shader'
local C = ffi.C;
local window = sgl.window {
screen_width = 1440;
@jsimmons
jsimmons / sg_tpool.c
Created July 8, 2012 12:11
Task scheduler. Don't use this it's broken.
#include "sg_tpool.h"
#include <pthread.h>
#include <semaphore.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct
{
STaskID id;
@jsimmons
jsimmons / spsc.c
Created July 9, 2012 01:08
Single producer single consumer lock-free on FIFO
#include <pthread.h>
#include <stdio.h>
#define PRODUCER_COUNT 1
#define CONSUMER_COUNT 1
#define THREAD_COUNT (PRODUCER_COUNT + CONSUMER_COUNT)
#define QUEUE_SIZE 64
// Let's go the easy way and keep a gap between head and tail when full.
@jsimmons
jsimmons / mpmc.c
Created July 9, 2012 02:58
Multiple Producer Multiple Consumer with two mutexes on FIFO
#include <pthread.h>
#include <stdio.h>
#define PRODUCER_COUNT 4
#define CONSUMER_COUNT 4
#define THREAD_COUNT (PRODUCER_COUNT + CONSUMER_COUNT)
#define WORK_ITERS 10000
#define WORK_COUNT 10000
#define QUEUE_SIZE 64