Created
December 9, 2016 00:04
-
-
Save kylewlacy/7b2c9ccb28dc45177db11986cd7de4cc to your computer and use it in GitHub Desktop.
Rust gfx triangle example using SDL2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2014 The Gfx-rs Developers. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
extern crate sdl2; // 0.18.0 | |
#[macro_use] | |
extern crate gfx; // 0.12.2 | |
extern crate gfx_window_sdl; // 0.3.0 | |
use sdl2::event::Event; | |
use gfx::traits::FactoryExt; | |
use gfx::Device; | |
gfx_defines!{ | |
vertex Vertex { | |
pos: [f32; 2] = "a_Pos", | |
color: [f32; 3] = "a_Color", | |
} | |
pipeline pipe { | |
vbuf: gfx::VertexBuffer<Vertex> = (), | |
out: gfx::RenderTarget<gfx::format::Srgba8> = "Target0", | |
} | |
} | |
const VERTEX_SHADER: &'static [u8] = br##"#version 150 core | |
in vec2 a_Pos; | |
in vec3 a_Color; | |
out vec4 v_Color; | |
void main() { | |
v_Color = vec4(a_Color, 1.0); | |
gl_Position = vec4(a_Pos, 0.0, 1.0); | |
} | |
"##; | |
const FRAGMENT_SHADER: &'static [u8] = br##"#version 150 core | |
in vec4 v_Color; | |
out vec4 Target0; | |
void main() { | |
Target0 = v_Color; | |
} | |
"##; | |
const TRIANGLE: [Vertex; 3] = [ | |
Vertex { pos: [ -0.5, -0.5 ], color: [1.0, 0.0, 0.0] }, | |
Vertex { pos: [ 0.5, -0.5 ], color: [0.0, 1.0, 0.0] }, | |
Vertex { pos: [ 0.0, 0.5 ], color: [0.0, 0.0, 1.0] } | |
]; | |
const CLEAR_COLOR: [f32; 4] = [0.1, 0.2, 0.3, 1.0]; | |
pub fn main() { | |
let sdl = sdl2::init().unwrap(); | |
let mut builder = sdl.video().unwrap().window("Triangle", 800, 600); | |
let (window, _gl, mut device, mut factory, color_view, _depth_view) = | |
gfx_window_sdl::init(&mut builder); | |
let command_buffer = factory.create_command_buffer(); | |
let mut encoder: gfx::Encoder<_, _> = command_buffer.into(); | |
let pso = factory.create_pipeline_simple( | |
VERTEX_SHADER, | |
FRAGMENT_SHADER, | |
pipe::new() | |
).unwrap(); | |
let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&TRIANGLE, ()); | |
let data = pipe::Data { | |
vbuf: vertex_buffer, | |
out: color_view | |
}; | |
let mut events = sdl.event_pump().unwrap(); | |
'main: loop { | |
for event in events.poll_iter() { | |
match event { | |
Event::Quit { .. } => { | |
break 'main; | |
}, | |
_ => { } | |
} | |
} | |
encoder.clear(&data.out, CLEAR_COLOR); | |
encoder.draw(&slice, &pso, &data); | |
encoder.flush(&mut device); | |
window.gl_swap_window(); | |
device.cleanup(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment