diff --git a/examples/shader.nim b/examples/shader.nim index 8f7bdc3..c61d1ae 100644 --- a/examples/shader.nim +++ b/examples/shader.nim @@ -1,5 +1,5 @@ import httpclient, math -import csfml +import csfml, csfml_ext type Scene = enum @@ -15,7 +15,7 @@ proc cycle[T](x: var T) = let texture = newTexture("resources/background.jpg") var sprite = newSprite(texture) var pxShader = newShader(nil, fragmentShaderFilename="resources/pixelate.frag") -pxShader.setParameter "texture", CurrentTexture +pxShader["texture"] = CurrentTexture let ipsum = getContent("http://loripsum.net/api/12/short/plaintext") @@ -33,8 +33,7 @@ window.verticalSyncEnabled = true var clock = newClock() while window.open: - var event: Event - while window.pollEvent(event): + for event in window.events: if event.kind == EventType.Closed: window.close() elif event.kind in {EventType.MouseButtonPressed, EventType.KeyPressed}: @@ -47,14 +46,14 @@ while window.open: case scene of scPixelate: - pxShader.setParameter "pixel_threshold", (x+y)/30 + pxShader["pixel_threshold"] = (x+y)/30 window.draw sprite, renderStates(shader=pxShader) of scWaveBlur: - wbShader.setParameter "wave_phase", time - wbShader.setParameter "wave_amplitude", x*40, y*40 - wbShader.setParameter "blur_radius", (x+y)*0.008 + wbShader["wave_phase"] = time + wbShader["wave_amplitude"] = (x*40, y*40) + wbShader["blur_radius"] = (x+y)*0.008 window.clear White window.draw text, renderStates(shader=wbShader) diff --git a/src/csfml_ext.nim b/src/csfml_ext.nim index 1fa3068..c447a38 100644 --- a/src/csfml_ext.nim +++ b/src/csfml_ext.nim @@ -33,6 +33,28 @@ iterator events*[T: Window|RenderWindow] (window: T): Event = yield event +proc `[]=`*[T: cfloat|float](shader: Shader, name: string, x: T) = + shader.setParameter(name, cfloat(x)) +proc `[]=`*[T: cfloat|float](shader: Shader, name: string, p: tuple[x, y: T]) = + shader.setParameter(name, cfloat(p.x), cfloat(p.y)) +proc `[]=`*[T: cfloat|float](shader: Shader, name: string, p: tuple[x, y, z: T]) = + shader.setParameter(name, cfloat(p.x), cfloat(p.y), cfloat(p.z)) +proc `[]=`*[T: cfloat|float](shader: Shader, name: string, p: tuple[x, y, z, w: T]) = + shader.setParameter(name, cfloat(p.x), cfloat(p.y), cfloat(p.z), cfloat(p.w)) +proc `[]=`*(shader: Shader, name: string, vector: Vector2f) = + shader.setParameter(name, vector) +proc `[]=`*(shader: Shader, name: string, vector: Vector3f) = + shader.setParameter(name, vector) +proc `[]=`*(shader: Shader, name: string, color: Color) = + shader.setParameter(name, color) +proc `[]=`*(shader: Shader, name: string, transform: Transform) = + shader.setParameter(name, transform) +proc `[]=`*(shader: Shader, name: string, texture: Texture) = + shader.setParameter(name, texture) +proc `[]=`*(shader: Shader, name: string, currentTexture: type(CurrentTexture)) = + shader.setParameter(name, currentTexture) + + proc red*(color: Color): int = ## *Returns*: red component of a Color diff --git a/src/csfml_graphics.nim b/src/csfml_graphics.nim index 2516c24..e029aff 100644 --- a/src/csfml_graphics.nim +++ b/src/csfml_graphics.nim @@ -161,9 +161,9 @@ proc renderStates*(blendMode = BlendMode.Alpha, transform = Identity, type CurrentTextureType = object -let CurrentTexture* = CurrentTextureType() +let CurrentTexture* = CurrentTextureType() ## Placeholder argument for setParameter -proc setParameter*(shader: Shader, name: cstring, currentTexture: CurrentTextureType) = +proc setParameter*(shader: Shader, name: cstring, currentTexture: type(CurrentTexture)) = ## Change a texture parameter of a shader ## ## This function maps a shader texture variable to the