Skip to content

Instantly share code, notes, and snippets.

Quick Reference for SDL2
========================
Includes both prototypes and short examples of using the call.
int SDL_Init(Uint32 flags)
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
void SDL_Quit(void)
atexit(SDL_Quit);
const char* SDL_GetHint(const char* name)
@OrangeTide
OrangeTide / ipow.c
Created April 14, 2011 19:39
integer pow() using Montgomery's ladder
/* Montgomery's Ladder Technique
* http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.5.4956&rep=rep1&type=pdf
*/
#include <stdio.h>
#include <limits.h>
static unsigned long long ipow(unsigned g, unsigned k)
{
unsigned long long r0, r1;
unsigned j;
@OrangeTide
OrangeTide / copyfd.c
Created June 1, 2011 16:49
fast copy of select's fd_set
#define _XOPEN_SOURCE 700
#include <sys/select.h>
static void copyfdset(fd_set *d, fd_set *s, unsigned fd_max) {
unsigned i;
unsigned n = (fd_max + 1 + __NFDBITS - 1) / __NFDBITS;
for(i = 0; i < n; i++) {
d->fds_bits[i] = s->fds_bits[i];
}
}
@OrangeTide
OrangeTide / debug.h
Created June 4, 2011 06:42
some portable vararg debug macros based on an old example I saw
/* a complicated set of macros to support empty __VA_ARGS__ macros.
* example:
* pr_dbg("Check\n");
* pr_dbg("Check %d\n", 12);
* pr_dbg("Check %d %s %d\n", 42, "Hello", 99);
*
* pr_info() and pr_err() macros don't use __func__ and __LINE__
*/
#ifndef DEBUG_H
#define DEBUG_H
@OrangeTide
OrangeTide / otadd0.c
Created June 27, 2011 21:27
add without using +,++ or -,--
/* otadd0.c : add without using +,++ or -,-- */
/* clean:
int add(int a, int b)
{
int r,n,c;
for(r=c=0,n=1;n;n<<=1) {
c = (a & b) ^ (c & (a ^ b));
c <<= 1;
r |= (a ^ b ^ c) & n;
}
@OrangeTide
OrangeTide / bench.c
Created November 21, 2011 22:30
benchmark of a simple 2D pixel pattern
/* bench.c : XOR pattern without any display/output to get a baseline benchmark */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define BGRX8888(r, g, b) \
((uint32_t)((unsigned char)(b)) << 24 | \
(uint32_t)((unsigned char)(g)) << 16 | \
@OrangeTide
OrangeTide / base.c
Created March 11, 2016 01:49
command-line utility to convert numbers to different formats (hexidecimal, octal, binary, decimal, base-N)
/* base.c : command-line utility to convert numbers to different formats.
* March 10, 2016 - PUBLIC DOMAIN */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
int main(int argc, char **argv)
{
@OrangeTide
OrangeTide / vm.h
Created April 21, 2017 00:41
virtual machine in one header file
/* vm.h - virtual machine */
#ifndef VM_H
#define VM_H
/* Goals:
* - no allocations in library
* - multithread safe
* - easy opcode extensions
* - single file
*/
#include <stddef.h>
@OrangeTide
OrangeTide / bitfield.h
Created April 7, 2011 17:47
bitfield macros
/* bitfield.h - bitfield macros */
/*
* PUBLIC DOMAIN - NO COPYRIGHT CLAIMED - Jonathan Mayo - July 2005
*
* Updated January 2010
*/
#ifndef BITFIELD_H
#define BITFIELD_H
#include <limits.h>
@OrangeTide
OrangeTide / pipe-example.c
Last active May 14, 2018 17:44
demonstration of using pipe() and fork() to redirect
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
int main() {
int s, fd[2];
pipe(fd);
if (!fork()) { /* child */
dup2(fd[0], STDIN_FILENO);