Skip to content

Instantly share code, notes, and snippets.

View fowlmouth's full-sized avatar
🦃

Tim E fowlmouth

🦃
  • United States of America
View GitHub Profile
## Bare-bones SDL2 example
import sdl2, sdl2/gfx
discard sdl2.init(INIT_EVERYTHING)
var
window: WindowPtr
render: RendererPtr
window = createWindow("SDL Skeleton", 100, 100, 640,480, SDL_WINDOW_SHOWN)
template mkcolor (a,b,c,d): uint32 =
uint32(SDL_FOURCC(a.int, b.int, c.int, d.int))
converter toUint32 (some:color):uint32 =
mkColor(some.r, some.g, some.b, some.a)
import macros
macro super (x:typed): untyped =
## returns `x` with a type conv to its parent type,
## keeping ptr/ref info. `x` may be be an object,
## ptr or ref to an object, or a typedesc
var ty = getType(x)
var isTypedesc = ty.typekind == ntyTypedesc
if isTypedesc: ty = ty[1]
C:\Users\phowl_000\src\crystal>crystal build test.cr
can't find file 'prelude'
C:\Users\phowl_000\src\crystal>set CRYSTAL_PATH
CRYSTAL_PATH="C:\users\phowl_000\src\crystal\src;libs"
C:\Users\phowl_000\src\crystal>set CRYSTAL_PATH=
C:\Users\phowl_000\src\crystal>crystal build test.cr
unexpected token: DELIMITER_START
type Unchecked* {.unchecked.}[T] = array[1,T]
var x = [1,2,3]
let y = cast[ptr Unchecked[int]](x[0].addr)
echo y[0]
echo y[1]
echo y[2]
echo y[3] # stack garbage
import macros
proc concreteType* (n:NimNode): NimNode{.compileTime.}=
return case n.typeKind
of ntyTypeDesc:
n[1].concreteType
@fowlmouth
fowlmouth / n.nim
Last active August 29, 2015 14:17
nim variadic templates
import macros
macro first (va_expr: expr): expr =
if va_expr.kind == nnkBracket:
return va_expr[0]
else:
return va_expr
macro rest (va_expr: expr): expr =
echo treerepr(va_expr)
type
jsobj {.importc.} = object
update*: proc(){.nimcall.}
d3_obj {.importc.} = object
linear* : proc(): jsobj{.nimcall.}
# this might work, to make it callable, i doubt it though
proc `()` (obj:jsobj; scale:float): float {.importc.}
{.emit:"""
struct FooBase {
int x;
};
template <typename T>
struct Foo: FooBase {
T val;
};
type
App* = ref object of RootObj
vt*: AppVT
AppVT = object
draw*: proc(my:App)
update*: proc(my:App, dt:float)