Skip to content

Instantly share code, notes, and snippets.

View axefrog's full-sized avatar

Nathan Ridley axefrog

  • Brisbane, Australia
View GitHub Profile
@axefrog
axefrog / scope.js
Last active May 6, 2024 05:20
An idea for scoping Cycle.js child components/dialogues in the context of their parent. Any driver can optionally supply a scope() function, which returns a scoped instance of itself. A driver can also supply an unscope() function which returns a transformed instance of its associated sink. Anything that is scope-unaware is preserved.
function runInScope(main, sources, context, ...args) {
if(!main) {
throw new Error('A "main" function must be supplied, which will be called in scope and from which a (sinks) object will be returned');
}
if(!sources) {
throw new Error('A source drivers object must be supplied, to which scoping can be applied');
}
if(!context) {
throw new Error('A scope context object must be supplied, either as a string, or as an object of key/value pairs');
}
@axefrog
axefrog / router.js
Last active May 6, 2024 05:17
Simple router driver for Cycle.js utilising Router5 for routing functionality and adapting some of the code from VisionMedia's Page.js for automatic link click intercepting
'use strict';
import {Router5, RouteNode} from 'router5';
import logger from '../logger';
// The set of valid sink functions includes synchronous state-affecting router functions that do not require a callback
// and which do not have a significant return value other than the router object itself.
const validSinkFuncs = ['add','addNode','canActivate','deregisterComponent','navigate','registerComponent','setOption','start','stop'];
function validateAndRemapSinkArgument(arg) {
@axefrog
axefrog / example.js
Last active September 8, 2015 14:14
Simple logger for my Cycle.js apps. Writes pretty console output and exposes an observable message stream if further processing is desired.
import logger from './logger';
let log = logger('Category');
log.trace('Just tracing stuff');
log.debug('Here is a debug message');
log.success('The successful thing happened that we wanted to happen');
log.info('Information makes the world go around, and here is that string:', str);
log('An info message can be logged using short form');
log.warn('You better be careful about this kind of thing');
@axefrog
axefrog / example.js
Last active October 4, 2016 04:01
Logging driver for cycle.js
import {Rx} from '@cycle/core';
export default function (responses, strings) {
var log = responses.log.create('Sample');
var str$ = Rx.Observable
.from(strings)
.do(str => {
log.trace('Just tracing stuff');
log.debug('Here is a debug message');
log.success('The successful thing happened that we wanted to happen');
@axefrog
axefrog / linked-data.html
Created April 13, 2015 23:57
Aurelia self-referencing view causing stack overflow error
<template>
<require from="./linked-data"></require>
<table>
<tbody>
<tr repeat.for="pair of pairs()">
<td>${pair.predicate}</td>
<td>
<linked-data subject.bind="this" predicate.bind="pair.predicate" value.bind="pair.value"></linked-data>
</td>
</tr>
@axefrog
axefrog / font-atlas-plan.cpp
Created February 22, 2015 01:16
Planning out a font atlas
std::shared_ptr<FontAtlas> generateFontAtlas(int fontId, int fontSizeInPixels, int sdfPadding)
{
/* ACTION PLAN:
create a new list (vector) in which to stage a set of character glyphs, each prepared in isolation
for each character:
load the character and its metrics from the font engine, using a very large font size (10 times the requested font size)
create a new image expanded to include padding for the distance field
copy the character bitmap into the centre of the image
render the distance field into the image
downsize the image
@axefrog
axefrog / build.ps1
Last active August 29, 2015 14:14
A simple MSVC++ build script for my game development. Supports an arbitrary source folder structure and command arguments for running the built file and optionally starting the debugger.
$postbuild = ""
foreach ($arg in $args) {
switch ($arg) {
"run" { $postbuild = "run" }
"debug" { $postbuild = "debug" }
}
}
if (!(test-path build)) { mkdir build }
if (!(test-path build/obj)) { mkdir build/obj }
@axefrog
axefrog / cubeapp.cs
Created October 16, 2014 08:07
Rendering an animated texture
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
@axefrog
axefrog / cube.cs
Last active June 20, 2018 16:04
A textured cube using SharpDX
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Windows;
@axefrog
axefrog / 01-setup.cs
Last active April 13, 2021 20:49
SharpDX - Drawing pixels on a Direct2D surface - Written for this blog post - http://nathanridley.com/2014/07/30/drawing-pixels-directly-onto-a-direct2d-surface-using-c-and-sharpdx/
// Here we are creating our device, swapchain, etc. A fairly basic
// example of setting up our form for Direct2D drawing. Note the
// pixel format - we need to remember this when drawing pixels to
// our raw byte array.
var swapChainDesc = new SwapChainDescription
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,