Skip to content

Instantly share code, notes, and snippets.

View mickvangelderen's full-sized avatar

Mick van Gelderen mickvangelderen

  • The Netherlands
  • 17:25 (UTC +02:00)
View GitHub Profile
@mickvangelderen
mickvangelderen / continue-do-while.c
Created January 18, 2019 11:10
Obtain a screen pointer by the screen number with xcb.
// Find XCB screen.
int default_screen_number = XDefaultScreen(display);
xcb_screen_t *screen = NULL;
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(connection));
int index = 0;
do {
if (index == default_screen_number) {
screen = iter.data;
break;
@mickvangelderen
mickvangelderen / gl-texture-binding-mental-model.rs
Last active February 22, 2018 11:56
My understanding of what happens when you activate texture units and bind textures in OpenGL.
#![feature(nonzero)]
extern crate core;
mod gl {
use core::nonzero::NonZero;
#[derive(Debug)]
pub struct Context {
texture_id_counter: u32,
active_texture_unit_index: TextureUnitIndex,
@mickvangelderen
mickvangelderen / remove-unreachable-code.rs
Last active February 22, 2018 09:58
Does the compiler remove unreachable code? Compile with `rustc -C opt-level=2 --emit asm remove-unreachable-code.rs`.
#[no_mangle]
#[inline(never)]
pub fn on_change(x: u32) {
println!("x is now {}", x);
}
struct X(u32);
impl X {
fn change(&mut self, y: u32) {
@mickvangelderen
mickvangelderen / pi.rs
Created February 9, 2018 22:14
How to create and use constants in Rust.
trait Pi {
fn give_me_pi() -> f32;
}
struct PI();
impl Pi for PI {
fn give_me_pi() -> f32 {
3.14
}
@mickvangelderen
mickvangelderen / lerp.rs
Last active December 6, 2017 11:34
Super convoluted linear map/interpolate implementation
#![feature(fn_must_use)]
#[cfg(test)]
mod tests {
use std::ops::{Sub, Add, Mul, Div};
trait Unit {
type Value;
fn value(&self) -> Self::Value;
@mickvangelderen
mickvangelderen / accessors.rs
Created November 17, 2017 23:30
An exploration of implementing accessors in rust so that changes in struct layout/composition do not require changes in the code using them.
extern crate gl;
extern crate cgmath;
use gl::types::*;
use cgmath::*;
macro_rules! def_accessor_trait {
(($Trait:ident, $method:ident), ($TraitMut:ident, $method_mut:ident), $Field:ty) => {
pub trait $Trait {
fn $method(&self) -> &$Field;
// Consider the following calls:
// PrintAngle((Radians) 0.10, "radians");
// PrintAngle((Degrees) 30, "degrees");
// Obviously no conversion are required, but without the IAngularUnit
// interface, we would have to provide copies of this method for each
// Unit manually.
public static void PrintAngle<TAngularUnit>(TAngularUnit angle, string unit) where TAngularUnit : IAngularUnit {
switch (unit) {
case "radians": System.Console.WriteLine("{0:F2} rad", (float) angle.ToRadians); break;
case "degrees": System.Console.WriteLine("{0:F2} deg", (float) angle.ToDegrees); break;
// Some constants, may get them from another place in production code.
internal abstract class Constants {
public const float RadiansPerCircle = 2.0f*(float) System.Math.PI;
public const float DegreesPerCircle = 360.0f;
public const float RadiansPerDegree = RadiansPerCircle/DegreesPerCircle;
public const float DegreesPerRadian = DegreesPerCircle/RadiansPerCircle;
}
public interface IAngularUnit {
float AsRadians { get; }
#[derive(Clone,Copy,Debug)]
enum State {
A,
B,
Trap,
}
impl State {
fn is_accepting(self) -> bool {
use State::*;
# http://editorconfig.org/
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 72