essaye ce code :
Code : Tout sélectionner
;
; ------------------------------------------------------------
;
; PureBasic - cSFML Binding
;
; 2011 - Cpl.Bator
;
; ------------------------------------------------------------
;
XIncludeFile "../../include/cSFML.pbi"
Dim TextString.s(5)
index = 0
TextString(0) = "cSFML - Binding For PureBasic 4.5x"
TextString(1) = "SFML By Laurent Gomilla"
TextString(2) = "Binding for PureBasic By Cpl.Bator"
TextString(3) = "[F1 to F3] For Change shader"
TextString(4) = "this is a powerfull api !"
TextString(5) = ":)"
;-Embeded shader
; Blur
Blur_ShaderCode$ = "uniform sampler2D texture;" + Chr(10)
Blur_ShaderCode$ + "uniform float offset;"+ Chr(10)
Blur_ShaderCode$ + "void main()"+ Chr(10)
Blur_ShaderCode$ +"{"+ Chr(10)
Blur_ShaderCode$ +" vec2 offx = vec2(offset, 0.0);"+ Chr(10)
Blur_ShaderCode$ +" vec2 offy = vec2(0.0, offset);"+ Chr(10)
Blur_ShaderCode$ +" vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * 1 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy - offx) * 2 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy + offx) * 2 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy - offy) * 2 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy + offy) * 2 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1 +"+ Chr(10)
Blur_ShaderCode$ +" texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1;"+ Chr(10)
Blur_ShaderCode$ +" gl_FragColor = gl_Color * (pixel / 13.0);"+ Chr(10)
Blur_ShaderCode$ +"}"+ Chr(10)
; Stereo
Stereo_ShaderCode$ = "#define textureW 1024.0"+ Chr(10)
Stereo_ShaderCode$ + "uniform sampler2D colorMap;"+ Chr(10)
Stereo_ShaderCode$ + "void main (void)"+ Chr(10)
Stereo_ShaderCode$ + "{"+ Chr(10)
Stereo_ShaderCode$ + " float dec = fract(10 / textureW);"+ Chr(10)
Stereo_ShaderCode$ + " vec4 ColorLeft = texture2D( colorMap, gl_TexCoord[0].st + vec2(dec,0) );"+ Chr(10)
Stereo_ShaderCode$ + " vec4 ColorRight = texture2D( colorMap, gl_TexCoord[0].st - vec2(dec,0) );"+ Chr(10)
Stereo_ShaderCode$ + " float red = ColorLeft.x;"+ Chr(10)
Stereo_ShaderCode$ + " float green = (ColorRight.y + ColorRight.z)/2;"+ Chr(10)
Stereo_ShaderCode$ + " float blue = (ColorRight.z + ColorLeft.z)/2;"+ Chr(10)
Stereo_ShaderCode$ + " gl_FragColor = vec4(red, green, blue, 1);"+ Chr(10)
Stereo_ShaderCode$ + "}"+ Chr(10)
; Sepia
Sepia_ShaderCode$ = "uniform sampler2D texture;"+ Chr(10)
Sepia_ShaderCode$ + "uniform float alpha;"+ Chr(10)
Sepia_ShaderCode$ + "void main()"+ Chr(10)
Sepia_ShaderCode$ + "{"+ Chr(10)
Sepia_ShaderCode$ + " vec4 FragColor = texture2D(texture, gl_TexCoord[0].xy);"+ Chr(10)
Sepia_ShaderCode$ + " vec4 Texel = max(FragColor,max(FragColor.g,FragColor.b)); "+ Chr(10)
Sepia_ShaderCode$ + " Texel[0] = dot(vec3(FragColor.x,FragColor.y,FragColor.z), vec3(0.393, 0.769, 0.189));"+ Chr(10)
Sepia_ShaderCode$ + " Texel[1] = dot(vec3(FragColor.x,FragColor.y,FragColor.z), vec3(0.349, 0.686, 0.168));"+ Chr(10)
Sepia_ShaderCode$ + " Texel[2] = dot(vec3(FragColor.x,FragColor.y,FragColor.z), vec3(0.272, 0.534, 0.131));"+ Chr(10)
Sepia_ShaderCode$ + " gl_FragColor = vec4(Texel[0],Texel[1],Texel[2],alpha);"+ Chr(10)
Sepia_ShaderCode$ + "}"+ Chr(10)
Settings.sfContextSettings
Settings\DepthBits = 24 ; ZBuffer de 24 bits
Settings\AntialiasingLevel = 2 ; Niveau d'antialiasing
SettingsMajorVersion = 1 ; Version OpenGL (2.1) pour la 3 , mettre 3.0
Settings\MinorVersion = 0
*Mode.sfVideoMode = sfVideoMode_GetDesktopMode()
*window = sfRenderWindow_Create(*Mode,"cSFML - PureBasic",#sfFullscreen,@Settings)
ClearColor = sfBlack
Font = sfFont_CreateFromFile("./data/sensation.ttf")
BackGroundImg = sfImage_CreateFromFile("./data/BackGround.jpg")
BackGroundSprite = sfSprite_Create()
sfSprite_SetImage(BackGroundSprite,BackGroundImg,#True)
sfSprite_Resize(BackGroundSprite,*Mode\Width, *Mode\Height)
; Creation d'une surface de rendu "OffScreen" sans z-buffer ( inutile pour de la 2D )
; Elle servira pour appliquer les shaders sur tout l'écran.
If sfRenderImage_IsAvailable()
OffScreenSurface = sfRenderImage_Create(*Mode\Width, *Mode\Height , #False) ; On crée l'image
ImageOffScreen = sfRenderImage_GetImage(OffScreenSurface) ; on va la lié à un sprite
SpriteOffScreen = sfSprite_Create() ; on créer un sprite , qui sera le resultats des opérations
sfSprite_SetImage(SpriteOffScreen,ImageOffScreen,#True)
Else
OffScreenSurface = #Null
TextString(0) = "OFFSCREEN RENDERING NOT SUPPORTED !"
EndIf
If Not sfShader_IsAvailable()
ShaderBlur = sfShader_CreateFromMemory( @Blur_ShaderCode$)
ShaderStereo = sfShader_CreateFromMemory( @Stereo_ShaderCode$)
ShaderSepia = sfShader_CreateFromMemory( @Sepia_ShaderCode$)
sfShader_SetParameter1(ShaderSepia,"alpha", 1.0)
Shader.a = #True
Else
ShaderBlur = #Null
ShaderStereo = #Null
ShaderSepia = #Null
Shader = #False
TextString(3) = "Pas de shader :/"
EndIf
Text = sfText_Create()
; Size 78 for an screen height of 1050
CharSize = *Mode\Height * 78 / 1050
sfText_SetCharacterSize(Text,CharSize)
cShader = ShaderBlur
MsgTimer = 0
Repeat
sfRenderWindow_GetEvent(*window,@event) ; On récupère les evenement lié à la fenetre
input = sfRenderWindow_GetInput(*window)
If sfInput_IsKeyDown(input,#sf_key_Escape)
sfWindow_Close(*window)
EndIf
If sfInput_IsKeyDown(input,#sf_key_F1)
cShader = ShaderBlur
EndIf
If sfInput_IsKeyDown(input,#sf_key_F2)
cShader = ShaderStereo
EndIf
If sfInput_IsKeyDown(input,#sf_key_F3)
cShader = ShaderSepia
EndIf
; On joue avec les shader en temps réel ;)
If shader = #True
sfShader_SetParameter1(ShaderBlur,"offset", (5 * Cos(ElapsedMilliseconds()/500))/1000 )
EndIf
Angle.f = 10 + 5 * Cos(ElapsedMilliseconds()/250)-10
sfText_SetRotation(Text, Angle)
If MsgTimer < ElapsedMilliseconds()
MsgTimer = ElapsedMilliseconds() + 3000
sfText_SetString(Text,TextString(index))
; Center Text on the screen
*Rect.sfFloatRect = sfText_GetRect(Text)
sfText_SetOrigin(Text,*Rect\Width / 2, *Rect\Height / 2)
sfText_SetX(Text, *Mode\Width/2)
sfText_SetY(Text, *Mode\Height/2)
index + 1
index % 6
EndIf
;Rendu OffScreen (si disponible)
If OffScreenSurface<>#Null
sfRenderImage_Clear(OffScreenSurface,ClearColor) ; Efface la surface offscreen
sfRenderImage_DrawSprite(OffScreenSurface,BackGroundSprite) ; Affiche le fond
sfRenderImage_DrawText(OffScreenSurface,Text) ; Affiche le texte
sfRenderImage_Display(OffScreenSurface) ; met à jour la surface offscreen
sfRenderWindow_Clear(*window,ClearColor)
If Shader = #True
sfRenderWindow_DrawSpriteWithShader(*window,SpriteOffScreen,cShader)
Else
sfRenderWindow_DrawSprite(*window,SpriteOffScreen)
EndIf
sfRenderWindow_Display(*window)
Else
sfRenderWindow_Clear(*window,ClearColor)
sfRenderWindow_DrawSprite(*window,BackGroundSprite)
sfRenderWindow_DrawText(*window,Text)
sfRenderWindow_Display(*window)
EndIf
Until sfRenderWindow_IsOpened(*window) <> #True