Skip to content

Instantly share code, notes, and snippets.

View grapefrukt's full-sized avatar
💭
🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭

martin jonasson grapefrukt

💭
🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭🌭
View GitHub Profile
@grapefrukt
grapefrukt / arcade_joy.ino
Last active August 29, 2015 14:15
This is the code I use for my Teensy 2.0++ Joystick interface in my Arcade machine.
#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]
#define CHECK_SIZE(a) (sizeof(a)/sizeof(a[0]))
#define NUM_DIR 4
#define NUM_BUT 6
#define NUM_INPUT (NUM_DIR + NUM_BUT)
#define LEFT 0
#define RIGHT 1
#define UP 2
package com.grapefrukt.games.slicer.utils;
/**
* ...
* @author Martin Jonasson, m@grapefrukt.com
*/
class ConvexHull {
public static function get<T:{x:Float, y:Float}>(vertices:Array<T>):Array<T> {
@grapefrukt
grapefrukt / setconfig.hx
Created October 13, 2014 13:35
Recursively applies the values from one object to another using reflection
static function setconfig(target:Dynamic, config:Dynamic) {
// iterate over the fields of the config object
for (key in Reflect.fields(config)) {
// make sure our target has this field
if (!Reflect.hasField(target, key)) continue;
// fetch the value of the first field
var configvalue:Dynamic = Reflect.field(config, key);
// check if its and object, and if so, make sure it actually has multiple values inside
// (strings are for some reason considered objects)
@grapefrukt
grapefrukt / RTextField.hx
Created May 30, 2014 11:13
Hack to fix offset/cropped text when scaling a TextField in OpenFL
package flash.text;
import openfl.text.TextField;
import openfl.text.TextFormat;
/**
* ...
* @author Martin Jonasson, m@grapefrukt.com
*/
#if flash
typedef RTextField = TextField;
@grapefrukt
grapefrukt / Main.hx
Last active August 29, 2015 13:57
Compiler bug? "Variable initialization must be a constant value" despite being just that.
class Main {
// this particular setup gives me the error:
// Main.hx:12: characters 22-85 : Variable initialization must be a constant value
// set either GRID_SIZE, CELL_SIZE or OUTLINE_RATIO to anything else and it's fine
public static inline var GRID_SIZE :Int = 20;
public static inline var CELL_SIZE :Int = GRID_SIZE - 2;
public static inline var OUTLINE_RATIO :Float = 1.2;
@grapefrukt
grapefrukt / IFilter.hx
Created February 4, 2014 23:04
Here's a few bits and pieces I once used to pitch/filter a sound in OpenFL
/*
The MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
package flash.text;
import flash.display.BitmapData;
import flash.display.Sprite;
/**
* Wraps a regular TextField and renders it to a Bitmap without it ever being on the DisplayList.
* Used to work around this bug: https://github.com/openfl/lime/issues/20
* @author Martin Jonasson, m@grapefrukt.com
*/
class RTextField extends Sprite {
@grapefrukt
grapefrukt / Main.hx
Created January 13, 2014 22:47
Causes a crash on Windows XP systems once it goes above 489 rectangles
package ;
import flash.display.Sprite;
import flash.events.Event;
import flash.Lib;
import flash.text.TextField;
class Main extends Sprite {
@grapefrukt
grapefrukt / WindowsVersion.hx
Created January 13, 2014 22:09
Due to incompatibilities I needed a way to detect the version of Windows my game was running on. Sys.systemName() helpfully returns "Windows" and nothing more, so I had to get creative.
package com.grapefrukt.utils;
/**
* Retrieves the version of Windows the application is running under
* @author Martin Jonasson, m@grapefrukt.com
*/
class WindowsVersion {
private static var inited:Bool = false;
private static var _major:Int = 0;
package ;
import flash.errors.Error;
import flash.net.SharedObject;
/**
* Completely unnecessary wrapper for SharedObject
* @author Martin Jonasson, m@grapefrukt.com
*/
class SaveData {