sfml 0.10.1

Rust binding for sfml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Rust-SFML - Copyright (c) 2013 Letang Jeremy.
*
* The original software, SFML library, is provided by Laurent Gomila.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim
*    that you wrote the original software. If you use this software in a product,
*    an acknowledgment in the product documentation would be appreciated but is
*    not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
*    misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/


//! Shader class (vertex and fragment)
//!
//! Shaders are programs written using a specific language, executed directly by
//! the graphics card and allowing to apply real-time operations to the
//!  rendered entities.

use std::ptr;
use std::ffi::CString;

use traits::Wrappable;
use graphics::{Texture, Color};
use sfml_types::Vector2f;
use sfml_types::Vector3f;

use csfml_graphics_sys as ffi;
use csfml_system_sys as sys_ffi;

use std::io::{Read, Seek};
use system::inputstream::InputStream;

// pub mod rc;

/// Shader class (vertex and fragment)
///
/// Shaders are programs written using a specific language,
/// executed directly by the graphics card and allowing to apply
/// real-time operations to the rendered entities.
pub struct Shader<'s> {
    shader: *mut ffi::sfShader,
    texture: Option<&'s Texture>
}

impl<'s> Shader<'s> {
    /// Load both the vertex and fragment shaders from files
    ///
    /// This function can load both the vertex and the fragment
    /// shaders, or only one of them: pass None if you don't want to load
    /// either the vertex shader or the fragment shader.
    /// The sources must be text files containing valid shaders
    /// in GLSL language. GLSL is a C-like language dedicated to
    /// OpenGL shaders; you'll probably need to read a good documentation
    /// for it before writing your own shaders.
    ///
    /// # Arguments
    /// * vertexShaderFilename - Some(Path) of the vertex shader file to load, or None to skip this shader
    /// * fragmentShaderFilename - Some(Path) of the fragment shader file to load, or None to skip this shader
    ///
    /// Return Some(Shader) or None
    pub fn new_from_file(vertex_shader_filename: Option<&str>,
                         fragment_shader_filename: Option<&str>)
                         -> Option<Shader<'s>> {
        let shader = unsafe {
            let c_vertex_shader_filename = if vertex_shader_filename.is_none() {
                ptr::null()
            } else {
                CString::new(vertex_shader_filename.unwrap().as_bytes()).unwrap().as_ptr()
            };
            let c_fragment_shader_filename = if fragment_shader_filename.is_none() {
                ptr::null()
            } else {
                CString::new(fragment_shader_filename.unwrap().as_bytes()).unwrap().as_ptr()
            };
            ffi::sfShader_createFromFile(c_vertex_shader_filename,
                                         c_fragment_shader_filename)
        };
        if shader.is_null() {
            None
        } else {
            Some(Shader {
                    shader: shader,
                    texture: None
                })
        }
    }

    /// Load both the vertex and fragment shaders from streams
    ///
    /// This function can load both the vertex and the fragment
    /// shaders, or only one of them: pass None if you don't want to load
    /// either the vertex shader or the fragment shader.
    /// The sources must be text files containing valid shaders
    /// in GLSL language. GLSL is a C-like language dedicated to
    /// OpenGL shaders; you'll probably need to read a good documentation
    /// for it before writing your own shaders.
    ///
    /// # Arguments
    /// * vertexShaderStream - Some(T: Read + Seek) of the vertex shader stream to load, or None to skip this shader
    /// * fragmentShaderStream - Some(T: Read + Seek) of the fragment shader stream to load, or None to skip this shader
    ///
    /// Return Some(Shader) or None
    pub fn new_from_stream<T: Read + Seek>(vertex_shader_stream: Option<&mut T>,
                                           fragment_shader_stream: Option<&mut T>)
                                           -> Option<Shader<'s>> {
        let shader = unsafe {
            let c_vertex_shader_stream = match vertex_shader_stream {
                Some(stream) => &mut InputStream::new(stream) as *mut InputStream,
                None => ptr::null_mut()
            };
            let c_fragment_shader_stream = match fragment_shader_stream {
                Some(stream) => &mut InputStream::new(stream) as *mut InputStream,
                None => ptr::null_mut()
            };
            ffi::sfShader_createFromStream(&mut (*c_vertex_shader_stream).0 as *mut sys_ffi::sfInputStream,
                                           &mut (*c_fragment_shader_stream).0 as *mut sys_ffi::sfInputStream)
        };
        if shader.is_null() {
            None
        } else {
            Some(Shader {
                    shader: shader,
                    texture: None
                })
        }
    }

    /// Load both the vertex and fragment shaders from source codes in memory
    ///
    /// This function can load both the vertex and the fragment
    /// shaders, or only one of them: pass None if you don't want to load
    /// either the vertex shader or the fragment shader.
    /// The sources must be valid shaders in GLSL language. GLSL is
    /// a C-like language dedicated to OpenGL shaders; you'll
    /// probably need to read a good documentation for it before
    /// writing your own shaders.
    ///
    /// # Arguments
    /// * vertexShader - Some(String) containing the source code of the vertex shader, or None to skip this shader
    /// * fragmentShader - Some(String) containing the source code of the fragment shader, or None to skip this shader
    ///
    /// Return Some(Shader) or None
    pub fn new_from_memory(vertex_shader: Option<&str>,
                           fragment_shader: Option<&str>)
                           -> Option<Shader<'s>> {
        let shader = unsafe {
            let c_vertex_shader = if vertex_shader.is_none() {
                ptr::null()
            } else {
                CString::new(vertex_shader.unwrap().as_bytes()).unwrap().as_ptr()
            };
            let c_fragment_shader = if fragment_shader.is_none() {
                ptr::null()
            } else {
                CString::new(fragment_shader.unwrap().as_bytes()).unwrap().as_ptr()
            };
            ffi::sfShader_createFromFile(c_vertex_shader, c_fragment_shader)
        };
        if shader.is_null() {
            None
        } else {
            Some(Shader {
                    shader: shader,
                    texture: None
                })
        }
    }

    /// Change a f32 parameter of a shader
    ///
    /// # Arguments
    /// * name - Name of the parameter in the shader
    /// * x - Value to assign
    pub fn set_float_parameter(&mut self, name: &str, x: f32) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setFloatParameter(self.shader, c_str, x)
        }
    }

    /// Change a 2-components vector parameter of a shader
    ///
    /// name is the name of the variable to change in the shader.
    /// The corresponding parameter in the shader must be a 2x1 vector
    /// (vec2 GLSL type).
    ///
    /// # Arguments
    /// * name - Name of the parameter in the shader
    /// * x - First component of the value to assign
    /// * y - Second component of the value to assign
    pub fn set_float_2_parameter(&mut self, name: &str, x: f32, y: f32) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
                    ffi::sfShader_setFloat2Parameter(self.shader, c_str, x, y)
        }
    }

    /// Change a 3-components vector parameter of a shader
    ///
    /// name is the name of the variable to change in the shader.
    /// The corresponding parameter in the shader must be a 3x1 vector
    /// (vec3 GLSL type).
    ///
    /// # Arguments
    /// * name - Name of the parameter in the shader
    /// * x - First component of the value to assign
    /// * y - Second component of the value to assign
    /// * z - Third component of the value to assign
    pub fn set_float_3_parameter(&mut self,
                                 name: &str,
                                 x: f32,
                                 y: f32,
                                 z: f32) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setFloat3Parameter(self.shader,
                                             c_str,
                                             x,
                                             y,
                                             z)
        }
    }

    /// Change a 4-components vector parameter of a shader
    ///
    /// name is the name of the variable to change in the shader.
    /// The corresponding parameter in the shader must be a 4x1 vector
    /// (vec4 GLSL type).
    ///
    /// # Arguments
    /// * name - Name of the parameter in the shader
    /// * x - First component of the value to assign
    /// * y - Second component of the value to assign
    /// * z - Third component of the value to assign
    /// * w - Fourth component of the value to assign
    pub fn set_float_4_parameter(&mut self,
                                 name: &str,
                                 x: f32,
                                 y: f32,
                                 z: f32,
                                 w: f32) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setFloat4Parameter(self.shader,
                                             c_str,
                                             x,
                                             y,
                                             z,
                                             w)
        }
    }

    /// Change a texture parameter of a shader
    ///
    /// name is the name of the variable to change in the shader.
    /// The corresponding parameter in the shader must be a 2D texture
    /// (sampler2D GLSL type).
    ///
    /// # Arguments
    /// * name - Name of the texture in the shader
    /// * texture - Texture to assign
    pub fn set_texture_parameter(&mut self,
                                 name: &str,
                                 texture: &'s Texture) {
        self.texture = Some(texture);
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setTextureParameter(self.shader,
                                              c_str,
                                              texture.unwrap())
        }
    }

    /// Change a texture parameter of a shader
    ///
    /// This function maps a shader texture variable to the
    /// texture of the object being drawn, which cannot be
    /// known in advance.
    /// The corresponding parameter in the shader must be a 2D texture
    /// (sampler2D GLSL type).
    ///
    /// # Arguments
    /// * name - Name of the texture in the shader
    pub fn set_current_texture_parameter(&self, name: &str) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setCurrentTextureParameter(self.shader, c_str)
        }
    }

    /// Bind a shader for rendering (activate it)
    ///
    /// This function is not part of the graphics API, it mustn't be
    /// used when drawing SFML entities. It must be used only if you
    /// mix sfShader with OpenGL code.
    pub fn bind(&mut self) {
        unsafe {
            ffi::sfShader_bind(self.shader)
        }
    }

    /// Tell whether or not the system supports shaders
    ///
    /// This function should always be called before using
    /// the shader features. If it returns false, then
    /// any attempt to use sfShader will fail.
    ///
    /// Return true if the system can use shaders, false otherwise
    pub fn is_available() -> bool {
        unsafe { ffi::sfShader_isAvailable() }.to_bool()
    }

    /// Change a 2-components vector parameter of a shader
    ///
    /// name is the name of the variable to change in the shader.
    /// The corresponding parameter in the shader must be a 2x1 vector
    /// (vec2 GLSL type).
    ///
    /// # Arguments
    /// * name - Name of the parameter in the shader
    /// * vector - Vector to assign
    pub fn set_vector2_parameter(&mut self,
                                 name: &str,
                                 vector: &Vector2f) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setVector2Parameter(self.shader,
                                              c_str,
                                              *vector)
        }
    }

    /// Change a 3-components vector parameter of a shader
    ///
    /// name is the name of the variable to change in the shader.
    /// The corresponding parameter in the shader must be a 2x1 vector
    /// (vec2 GLSL type).
    ///
    /// # Arguments
    /// * name - Name of the parameter in the shader
    /// * vector - Vector to assign
    pub fn set_vector3_parameter(&mut self,
                                 name: &str,
                                 vector: &Vector3f) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setVector3Parameter(self.shader,
                                              c_str,
                                              *vector)
        }
    }

    /// Change a color parameter of a shader
    ///
    /// name is the name of the variable to change in the shader.
    /// The corresponding parameter in the shader must be a 4x1 vector
    /// (vec4 GLSL type).
    ///
    /// It is important to note that the components of the color are
    /// normalized before being passed to the shader. Therefore,
    /// they are converted from range [0 .. 255] to range [0 .. 1].
    /// For example, a sf::Color(255, 125, 0, 255) will be transformed
    /// to a vec4(1.0, 0.5, 0.0, 1.0) in the shader.
    ///
    /// # Arguments
    /// * name - Name of the parameter in the shader
    /// * color - Color to assign
    pub fn set_color_parameter(&mut self,
                               name: &str,
                               color: &Color) {
        let c_str = CString::new(name.as_bytes()).unwrap().as_ptr();
        unsafe {
            ffi::sfShader_setColorParameter(self.shader, c_str, color.0)
        }
    }
}

impl<'s> Wrappable<*mut ffi::sfShader> for Shader<'s> {
    fn wrap(shader: *mut ffi::sfShader) -> Shader<'s> {
        Shader {
            shader: shader,
            texture: None
        }
    }

    fn unwrap(&self) -> *mut ffi::sfShader {
        self.shader
    }
}

impl<'s> Drop for Shader<'s> {
    /// Destroy an existing shader
    fn drop(&mut self) {
        unsafe {
            ffi::sfShader_destroy(self.shader)
        }
    }
}