Skip to content

Instantly share code, notes, and snippets.

@Costava
Costava / fread_gotcha.c
Last active January 18, 2020 20:58
In C, the `fread` function can move the file position indicator forward but not read any objects if object size is greater than 1 and less than <object size> bytes remain in the file. This makes it possible to reach the end of the file, but 'miss' the last 1-3 bytes (1-3 if reading 4-byte objects).
// If you try to read a 3-byte file for a 4-byte object,
// then you will read 0 objects and the file position indicator will be moved
// to the end of the file
//
// so you can reach end-of-file and possibly miss 1-3 bytes
// if you've been reading 4-byte objects
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@Costava
Costava / uniquelines.c
Last active September 3, 2020 05:22
Prints a list of the lines in file1 that are not in file2 and a list of the lines in file2 that are not in file1
// COMPILING:
// gcc -std=c99 -Wall --output uniquelines uniquelines.c
// USAGE:
// ./uniquelines path/to/file1 path/to/file2
// DESCRIPTION:
// Prints a list of the lines in file1 that are not in file2
// and a list of the lines in file2 that are not in file1
// Within a file, lines are separated by a single-character `DELIMITER`
@Costava
Costava / ray-demo.rkt
Created June 14, 2020 21:12
Small, interactive racket/gui program that uses a ray shot from a camera position to color each pixel of the window.
#lang racket/gui
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; File: ray-demo.rkt
; Author: Costava
; License: zlib license (see end of file)
; Written for Racket 7.7
; Opens a window
@Costava
Costava / racket-gui-slider.rkt
Created July 3, 2020 21:09
Window with a slider labeled "Volume"
#lang racket/gui
(define frame (new frame% [label "Slider"]))
(new slider%
[parent frame]
[label "Volume"]
[min-value 0]
[max-value 100])
@Costava
Costava / solidcolorppm.c
Created August 20, 2020 01:20
Writes a solid color P3 .ppm file to stdout using the given width, height, and RGB color arguments
// Writes a solid color P3 .ppm file to stdout
// using the given width, height, and RGB color arguments
// Info on the .ppm file format:
// https://en.wikipedia.org/wiki/Netpbm#File_formats
// Compile:
// clang solidcolorppm.c --output solidcolorppm
// Run:
// ./solidcolorppm <width> <height> <r> <g> <b>
// gcc -std=c99 -Wall --output cwrap cwrap.c && ./cwrap
#include <stdint.h>
#include <stdio.h>
int main(void)
{
uint8_t val = 255;
val += 1;
printf("val: %d\n", val);// val: 0
@Costava
Costava / countnonwhitespace.c
Created September 3, 2020 04:32
Read stdin, count non-whitespace characters, and print the count to stdout
// The program reads stdin, counts the number of non-whitespace characters, and
// then prints the count to stdout
// UTF-8 encoding is expected
// If a non-ASCII character is found, the program prints to stderr and exits,
// but the program should be easy to extend to properly support UTF-8
// COMPILING:
// gcc -std=c99 -Wall --output countnonwhitespace countnonwhitespace.c
// USAGE:
@Costava
Costava / fltk_radio_button_groups.cpp
Created September 20, 2020 19:54
FLTK GUI window with two sets of radio buttons
// FLTK GUI window with two sets of radio buttons
//
// COMPILE AND RUN:
/* g++ --output fltk_radio_button_groups -std=c++98 -Wall -g -lfltk \
fltk_radio_button_groups.cpp && ./fltk_radio_button_groups */
//
// Install FLTK through your package manager e.g. pamac install fltk
//
// Unfortunately, this compiles with 4 ugly warnings that
// the button variables are unused (using g++ v10.2.0)
@Costava
Costava / rand_rgb_window.c
Created September 23, 2020 23:48
Benchmark touching every pixel of an SDL_Surface every frame
// Sort of a benchmark --- if touching every pixel every frame,
// a real program should not hope to get a higher frames per second
// than this simple demo.
//
// Single process and thread
// Uses SDL_Surface (none of SDL_Texture)
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
@Costava
Costava / sdl2_tex_component_order.c
Created September 28, 2020 23:20
SDL_Texture color component order in pixels array (SDL2)
// If a texture has format SDL_PIXELFORMAT_ABGR8888,
// then the red component is the first byte in the pixels array of the texture
// Tested on a little-endian (x86) system
// Would the red component also come first in the array on a big-endian system?
//
// Tested with SDL2 version 2.0.12
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>