Skip to content

Commit

Permalink
Add []= shorthand for setParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Feb 7, 2015
1 parent ba8f25c commit 7b4ce97
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
15 changes: 7 additions & 8 deletions examples/shader.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import httpclient, math
import csfml
import csfml, csfml_ext


type Scene = enum
Expand All @@ -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")
Expand All @@ -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}:
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions src/csfml_ext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/csfml_graphics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7b4ce97

Please sign in to comment.